General Question
If someone handed you a direct sampling algorithm and a density function, and they told you that the two corresponded to the same random variate, how would you check this?
Motivation
Some use cases:
- Let $\mathbf{X}\in E\subseteq\mathbb{R}^n$ be a random vector whose density is $f_{\mathbf{X}}$ and for which a direct sampler is known. Let $\mathbf{g}:E\to\mathbb{R}^n$ be a "nice" transformation ($C^1$ diffeomorphism), and define a new random vector $\mathbf{Y}=\mathbf{g}(\mathbf{X})$. Since we can sample $\mathbf{X}$, it's easy to sample $\mathbf{Y}$; draw an $\mathbf{X}$ and apply $\mathbf{g}$ to it. Furthermore, you can derive the density of $\mathbf{Y}$ by computing $$f_{\mathbf{Y}}(\mathbf{y}) = f_{\mathbf{X}}(\,\mathbf{g}^{-1}(\mathbf{y})\,)\cdot|\det\mathrm{J}_{\mathbf{g}^{-1}}(\mathbf{y})|.$$ So say you grind through that computation, and you want to check that you did it right. How can you use the fact that you know how to sample $\mathbf{Y}$ to check that you correctly computed the density of $\mathbf{Y}$?
- Say you are writing some software to implement a probability distribution (perhaps the one you derived above). So you want to write two functions:
mydist_rand(...)
for generating random draws, andmydist_pdf(...)
for evaluating the pdf at a point. To test your software for errors, you want to check that the outputs of these two functions "agree." How can you do it?
(Update: Here is a related question with a great answer, but the proposed solutions seem applicable only to univariate random variables, and several assume that the CDF is easy to access, which I don't want to assume here.)