# Extracting Marginal Distribution by Monte Carlo [duplicate]

Suppose we know the distribution of a variable $x$ is $g(X)$, and we also know the conditional distribution of a variable $y$ given $x$ is $f(y|x)$. It has been shown that (such as in this book, p. 52 ) by

1. Draw $x^*$~$g(x)$

2. Draw $y^*$~$f(y|x)$

After $n$ draws, the quantities $y_1,...,y_n$ are an iid sample from the marginal density $J(y)$. I understand that the pairs $(x_1,y_1),...,(x_n,y_n)$ are an iid sample from the joint distribution $Q(x,y)$, but I still try to figure out the intuition/theory about why $y_1,...,y_n$ are an iid sample from the marginal density $J(y)$. Can anyone help? Thanks.

p.s. I know the answer to this thread explains it but I still don't get why ignoring $x$'s equals to integrating out them.

• This question has been discussed many times on X validated: here and here and here. – Xi'an Apr 18 '18 at 8:25