# Deriving the Reparameterization Trick

I am reading about the reparameterization trick from here.

It states

$$\boldsymbol{\epsilon}\sim p(\boldsymbol{\epsilon})$$, $$\textbf{z}=g_\theta(\boldsymbol{\epsilon},\textbf{x})$$, and $$\mathbb{E}_{p_\theta(\textbf{z})}[f(\textbf{z}^{(i)})] = \mathbb{E}_{p(\boldsymbol{\epsilon})}[f(g_\theta(\boldsymbol{\epsilon},\textbf{x}^{(i)}))]$$

I am able to understand the steps that follow. How to derive the above equation? I understand it has to do with the change-of-variables, but I am not able to proceed beyond

$$\int_\textbf{z} f(\textbf{z}^{(i)}) p_\theta(\textbf{z}) d\textbf{z} = \int_\boldsymbol{\epsilon} f(g_\theta(\boldsymbol{\epsilon},\textbf{x}^{(i)})) \ p_\theta(g_\theta(\boldsymbol{\epsilon},\textbf{x})) \ |\det(J)| \ d\boldsymbol{\epsilon}$$

In fact, the change of variable formula directly gives (see wikipedia article):

$$\int_z f(z^{(i)}) p_\theta(z) dz = \int_\epsilon f(g_\theta({\epsilon},{x}^{(i)})) \ p(\epsilon)d{\epsilon},$$

or in other terms,

$$\mathbb{E}_{p_\theta({z})}[f({z}^{(i)})] = \mathbb{E}_{p({\epsilon})}[f(g_\theta({\epsilon},{x}^{(i)}))].$$

The Jacobian relates to the way you would compute one density from the other, e.g., $$p_\theta(z)=p(\epsilon)\left|\frac{dg_{\theta}^{-1}(z)}{dz}\right|$$, however this is not what we are trying to achieve here.

The reparameterization trick involves a change of variables to compute the expectation under the transformed density $$p(\boldsymbol\epsilon)$$. The expression $$\int_{\boldsymbol\epsilon}f(g_{\theta}(\boldsymbol\epsilon, \mathbf{x}^{(i)})p_{\theta}(g_{\theta}(\boldsymbol\epsilon, \mathbf{x})) |\mathrm{det}(J)| d\boldsymbol\epsilon$$ in your question will involve another change of variables to calculate the expectation under the transformed density $$p(\boldsymbol\epsilon)$$ instead of $$p_{\theta}(\mathbf{z})$$.

We know that $$\mathbf{z} = g_{\theta}(\boldsymbol\epsilon, \mathbf{x})$$. Hence, $$\boldsymbol\epsilon = g_{\theta}^{-1}(\mathbf{z})$$. Based on the expression in the question, I am assuming the expression for the Jacobian as shown in the question.

\begin{aligned} & \mathrm{Using} \quad J = \frac{d \mathbf{z}}{d \boldsymbol\epsilon } \\ & p(\boldsymbol\epsilon) = \left\| \mathrm{det} \left( \frac{d \mathbf{z}}{d \boldsymbol\epsilon} \right) \right\| p_{\theta}(\mathbf{z}) \\ & \mathrm{Hence,} \quad p_{\theta}(\mathbf{z}) = \frac{1}{|\mathrm{det}(J)|} p(\boldsymbol\epsilon) \end{aligned} \label{eq:density}

The determinant of the Jacobian from the two variable transformations will then cancel out leading to the following expression for the expectation.

\begin{aligned} & \int_\textbf{z} f(\textbf{z}^{(i)}) p_\theta(\textbf{z}) d\textbf{z} = \int_\boldsymbol\epsilon f(g_{\theta}(\boldsymbol\epsilon, \mathbf{x})) p(\boldsymbol\epsilon) \frac{|\mathrm{det}(J)|}{|\mathrm{det}(J)|} d\boldsymbol\epsilon = E_{p{(\boldsymbol\epsilon)}} (f(g_{\theta}(\boldsymbol\epsilon, \mathbf{x})) ) \end{aligned}

Within the above derivation, I have attempted to use a simplified version of the LOTUS theorem. More details related to this theorem can be found in https://en.wikipedia.org/wiki/Law_of_the_unconscious_statistician. The article performs two change of variables to arrive at the final expression for the expectation in the continous case.