Monte Carlo method is sufficient for conjugate prior distribution which means that we can derive the explicit form of posterior distribution, and Gibbs sampler also can handle semiconjugate prior distribution, but when the conjugate or semiconjugate distribution is unavailable, we need the Metropolis-Hastings algorithm which is a generic method of approximating the posterior distribution.
Procedure
sample θ∗∼J(θ∣θ(s));
Compute the acceptance ratio
r=p(θ(s)∣y)p(θ∗∣y)=p(y∣θ(s))p(θ(s))p(y∣θ∗)p(θ∗)
θ(s+1)=θ∗w.p.min(r,1), and θ(s+1)=θ(s)w.p.1−min(r,1)
In practice, the first B iterations are called "burn-in" period, which should be discarded.
Example: Normal distribution with known variance
Let θ∼N(μ,τ2),{y1,…,yn∣θ}∼i.i.d.N(θ,σ2), then the posterior distribution of θ is N(μn,τn2).