# How to do MC integration from gibbs sampling of posterior

I'm a beginner of MCMC. Two questions confused me.

If I know the posterior distribution, and from the Gibbs sampling, I got the sampled parameter, so

1. How to draw the histogram with y axis as marginal density in R ?
2. How to further apply Monte Carlo integration on the resulting Markov Chain, which are sampled from this posterior. It seems I couldn't get a connection between these two.

Could anyone give an example to explain this?

Thank you

First I presume that

If I know the posterior distribution, and from the Gibbs sampling, I got the sampled parameter

simply means that you can compute $\pi(\theta|x)$ up to a constant and derive a Markov chain $\theta¹,\theta²,\ldots,\theta^T$ that converges to $\pi(\theta|x)$.

1. How to draw the histogram with y axis as marginal density in R ?

is unclear. You can draw the histogram of your simulations, one component at a time, but you do not know the marginal density $\pi_1(\theta_1|x)$ in most cases. You can always produce and display an estimate of this marginal density, e.g., by Rao-Blackwellisation:$$\hat{\pi}_1(\theta_1|x)=\frac{1}{T}\sum_{t=1}^T \pi(\theta_1|\theta_{-1}^t,x)$$where the conditional density on the right represents the full conditional of $\theta_1$ given all other components that you used in Gibbs sampling. But this display cannot be used as a proof of convergence as the histogram _and_ the alternative density estimate are based on the same simulation.

In Example 7.2 of our book Introducing Monte Carlo methods with R, we consider the pair of distributions $$X\vert\theta \sim \mathcal{B}\text{in}(n,\theta)\,,\quad \theta \sim {\mathcal{B}}e(a,b)$$ (i.e., the joint distribution) as our target (this is a toy example where there is no observation, no parameter, no posterior, despite the possibly confusing notations of $X$ and $\theta$: both are random variables to be simulated there) and this leads to the joint distribution $$f(x,\theta) = {n \choose x} \frac{\Gamma(a+b)}{\Gamma(a)\Gamma(b)}\, \theta^{x+a-1} (1-\theta)^{n-x+b-1}$$ that we can simulate by Gibbs sampling since the corresponding conditional distribution of $X \vert \theta$ is given above, while $\theta \vert x \sim {\cal B}e(x+a,n-x+b)$. The histograms in the associated figure (Fig. 7.1) are constructed by Gibbs sampling, while the overlaid curves are the true marginal densities, because this is a toy problem where the exact marginals are known to be a Beta-Binomial for $X$ and a Beta for $\theta$. This hardly ever happens in practical situations.

[Reproduced from Introducing Monte Carlo methods with R]

And

1. How to further apply Monte Carlo integration on the resulting Markov Chain, which are sampled from this posterior. It seems I couldn't get a connection between these two.

is also unclear. If you mean producing an approximation of a posterior expectation $\mathbb{E}[\mathfrak{h}(\theta)|x]$, you simply apply the regular Monte Carlo formula: $$\mathbb{E}[\mathfrak{h}(\theta)|x]\approx\frac{1}{T}\sum_{t=1}^T \mathfrak{h}(\theta^t)$$

• Thank you @Xi'an . For the first questions, I'm referring the Example of 7.2 in the book of Introducing Monte Carlo Methods with R by Robert and Casella. In that example, they simply using Gibbs Sampler to draw sample from a beta-binomial distribution, and plot a marginal density histogram. Quite confusing about that. Commented Nov 3, 2016 at 2:28
• @Xi'an I'm a little puzzled by "you can at best produce an estimate of this marginal density by Rao-Blackwellisation" right after discussing the histogram -- wouldn't the histogram of the sampled parameter itself be an estimate of the marginal density? [It's not as efficient as a Rao-Blackwellized estimate, of course.] Possibly I misunderstood, however -- were you saying that the histogram of the sampled values isn't such an estimate or were you saying that it may not be a good estimate? In either case are there some conditions that hold when it works (or perhaps when it doesn't)? Commented Nov 3, 2016 at 5:45
• @Glen_b: sorry to confuse everyone with the book example behind this question and with this answer!!! The example utilises the true marginal distributions to show that Gibbs converges to the right limiting distributions. In realistic situations, this is not possible. Hence, neither is plotting the true target. When drawing the marginal density based on the MCMC sample, the checking aspect disappears, whether one uses histogram, kernel, or RB estimates. Commented Nov 3, 2016 at 7:03
• @Xi'an Thank you. Actually, I have one more confusion about the the full conditional equation of Example 7.3 just below the marginal density figure. For the example 7.3, when you are generating the full conditional Eqn. 7.4 from the posterior Eqn.7.3, I'm curious about it seems you have modified the prior distribution of theta in Eqn. 7.3 by multiplying some form of sigma in it. My understanding for full conditional of , e.g. theta, should drop out the second exponential in Eqn. 7.4. Commented Nov 5, 2016 at 22:50
• First there is a typo in Eqn. (7.3), the last term should be $e^{\mathbf{-}1/b\sigma^2}$. Second, there is indeed a mistake in the model: we should have used the prior $\theta\sim\mathcal{N}(\theta_0,\tau^2\sigma^2)$ instead of the one we propose at the bottom of page 202, in order to make the example easier [and Eqn. (7.4)]. Apologies for the confusion! If you put this as another question, I can provide the complete correction to Example 7.3. Commented Nov 6, 2016 at 11:23