How can Metropolis-Hastings use the function it is trying to approximate? The MH algoithm is used to obtain samples from a probability distribution $f$ that is difficult to sample from directly. The process as described in this answer is:


*

*Pick a initial random state $x_0$.

*Generate a candidate $x^{\star}$ from $Q(\cdot\vert x_0)$.

*Calculate the ratio $\alpha=f(x^{\star})/f(x_0)$.

*Accept $x^{\star}$ as a realisation of $f$ with probability $\alpha$.

*Take $x^{\star}$ as the new initial state and continue sampling until you get the desired sample size.


But how can one compute $\alpha=f(x^{\star})/f(x_0)$ if we are trying to estimate $f$ in the first place? If we are able to calculate this ratio, why do we need to use Metropolis-Hastings?
A similar question was asked here and it linked to this tutorial where the author shows an example of sampling from an exponential distribution, but they use that exact exponential distribution in the sampling process, hence my confusion. Why can't we sample from the distribution directly if it is exactly known?

You misunderstood it, $f$ is known, just we don't know how to sample from it. For some distributions, we have dedicated algorithms to sample from them. There are also generic algorithms like the inverse transform sampling, but to use it you need to know the inverse cumulative probability function (or quantile function), but we don't always know it. Algorithms like Metropolis-Hastings are even broader, you can use them to sample from any distribution. More than this, you only need to know the probability density function $f$ up to a constant.
The caveat is that algorithms like Metropolis-Hastings are usually more costly computationally than the dedicated algorithms, so there's no free lunch.
