Why is the Optimal Discriminator $D^{*}_G(x) = \frac{p_\text{data}(x)}{p_\text{data}(x) + p_g(x)}$ in Generative Adversarial Networks? Proposition 1, 
The optimal discriminator is 
$$
D^{*}_G(x) = \frac{p_\text{data}(x)}{p_\text{data}(x) + p_g(x)}
$$
At the proof, I couldn't understand about change of variables with integral.
Why the first line is changed to second line?!
$$
V(G,D) = \int_x p_\text{data}(x)\log(D(x))\,dx + \int_z p_Z(z)\log(1-D(g(z)))\,dz \\ 
= \int_x p_\text{data}(x)\log(D(x)) + p_g(x)\log(1-D(x))\,dx
$$
I tried to calculate it myself.
But a below condition is needed to change the first line of $V(G,D)$ to second line of $V(G,D)$ $$ p_z(z) \frac{1}{g'(z)}=p_g(x)$$ 
In summary.. My question is that.. 


*

*Why the first line of V(G,D) can be changed to second line of V(G,D) 

*In my own trial to change the V(G,D), the above condition was needed. Is it appropriate condition?!  

 A: Hi~ To understand the change of the variables, we can first take a look at the Figure.1 in Generative Adversarial Networks, Goodfellow et al (2014), eprint arXiv:1406.2661.
According to the paper. 

The lower horizontal line is the domain from which $z$ is sampled and the above horizontal line is part of the domain of $x$. The upward arrows show the transformation $x = g(z)$.

Back to the equation it's clear that:
$$\int_z p_Z(z)\log(1-D(g(z))\,dz=E_{p_z}[\log(1-D(g(z))]$$
Since $x = g(z)$, we can replace $g(z)$ with variable $x$. Also notice that, in this case, $p_g$ is the distribution of $x$. As a result, we have this:
$$E_{p_Z}[\log(1-D(g(z))] = E_{p_g}[\log(1-D(x))]$$
Then we expand the expection to an integral form:
$$E_{p_g}[\log(1-D(x))] = \int_x p_g(x)\log(1-D(x))\,dx$$
A: Q1: Why the first line of $V(G,D)$ can be changed to second line of $V(G,D)$?
The task is to find the maximum value of $V(G,D)$ so perhaps better notation for the first line would be
$$\max[V(G,D)] = \max\left[\int_x p_\text{data}(x)\log(D(x))\,dx + \int_z p_Z(z) \log(1-D(g(z)))\,dz\right]$$
Then the second line is as follows
$$\max[V(G,D)]= \max \left[ \int_x p_\text{data}(x)\log (D(x)) + p_g(x) \log(1-D(g(x))) \, dx\right]$$
has the form $y → a \log(y) + b \log(1 − y)$ inside the integral, which achieves its maximum in $[0, 1]$ at $\frac a {a+b }$. That implies that $z=x$ allows for the maximum sum of the integrals, which allowed the first line to lead to the second line.
Q2: Is this appropriate?
If $g'(z)=1$ at $\max[p_g(x)]$. I think the problem suggests that $\max[V(G,D)] \neq V(G,D)$ except when $D^{*}_G(x) = \frac{p_\text{data}(x)}{p_\text{data}(x) + p_g(x)}$. The answer has the form $\frac a {a+b }$, where $p_\text{data}(x)=a$ and $P_g(x)=b$.
A: You've basically gotten it. So the definition of $p_g$ (see first paragraph of section 4 Theoretical Results) is the distribution of samples $G(z)$ obtained when $z$ comes from distribution $p_z$. Thus 
$$\int_z p_Z(z)\log(1-D(g(z))dz=E_{p_Z}[\log(1-D(g(z))]=E_{p_x}[\log(1-D(x))]$$
A: Since $z \mapsto G(z)$ is a deterministic mapping from $\mathcal{Z}$ to $\mathcal{X}$, let $y = G(z)$, then $p(y|z) = \delta(y - G(z))$. Therefore
$$\begin{split}
    \int_{\mathcal{X}} p_g(y)\log(1 - D(y)) dy & = \int_{\mathcal{X}} \left[\int_{\mathcal{Z}}p(z,y)dz\right]\log(1-D(y))dy \\
    & = \int_{\mathcal{X}} \left[\int_{\mathcal{Z}}p(z)p(y|z)dz\right]\log(1-D(y))dy \\
    & = \int_{\mathcal{X}} \left[\int_{\mathcal{Z}}p(z)dz\right]p(y|z)\log(1-D(y))dy \\
    & = \int_{\mathcal{Z}}p(z)\left[\int_{\mathcal{X}}\delta(y - G(z))\log(1 - D(y))dy\right]dz \\
    & = \int_{\mathcal{Z}}p(z)\left[\delta(y-G(z)) * \log(1-D(y))\right]dz \\
    & = \int_{\mathcal{Z}}p(z)\log(1 - D(G(z)))dz.
  \end{split}$$
The 2nd last row to the last row is the convolution property of the Dirac delta function.
A: The only thing you seem to be missing is the change of variable formula for probabilities, which states that the distribution of a random variable, $X$, that is transformed to $Y = f(X)$ by the function $f$ is given by
$$p_Y(y) = p_X(f^{-1}(y)) \left|f'(f^{-1}(y))\right|^{-1}.$$
Therefore, if we write out the substitution $x = g(z)$ in the integral, this change of variables formula magically appears:
$$\int_z p_Z(z)\log(1-D(g(z)))\,dz = \int_x \underbrace{\frac{1}{g'(g^{-1}(x))} p_Z(g^{-1}(x))}_{=p_{g(z)}(x)} \log(1 - D(x))\,dx$$
Note that this ignores the absolute value. I am not quite sure whether/how much it matters in this case (since the gradient of the generator is definitely not guaranteed to be positive).
A: Q1: Why the first line of V(G,D) can be changed to second line of V(G,D)
The change of random variable is a standard result from probability theory (see e.g. Papoulis' book Probability, Random Variables, and Stochastic Processes). In the 1-D case, for some arbitrary function $f(z)$ the expectation is given by:
${\rm E}_z[f(z)] = \int_{-\infty}^{\infty} f(z){\rm p}_z(z) dz$
if we apply an invertible change of variable by letting $x=g(z)$ where $x$ is a scalar random variable, where the PDFs satisfy ${\rm p}_x(x) dx={\rm p}_z(z) dz$, we obtain:
${\rm E}_z[f(z)]=\int_{-\infty}^{\infty} f(g^{-1}(x)) {\rm p}_x(x) dx={\rm E}_x[f(g^{-1}(x))]$
The result generalises to (i) many-to-one scalar transformations (ii) invertible vector transformations $G(\cdot)$ with ${\rm dim}(x)={\rm dim}(z)$; and (iii) vector transformations where ${\rm dim}(x)<{\rm dim}(z)$. In case (i), the PDF is composed of a sum of terms each of which corresponds to a root of the equation $g(z)=0$. In case (ii), the two PDFs are linked via the determinant of the Jacobian.
In the case where ${\rm dim}(x)>{\rm dim}(z)$, the PDF ${\rm p}_x(x)$ is non-unique and degenerate (it contains contains delta functions). This situation is illustrated by Example 2.1 in the paper (IEEE Access, Dec. 2021)
https://www.researchgate.net/publication/356815736_Convergence_and_Optimality_Analysis_of_Low-Dimensional_Generative_Adversarial_Networks_using_Error_Function_Integrals
Q2: In my own trial to change the V(G,D), the above condition was needed. Is it appropriate condition?!
You need to pay attention to the dimensions of $x$ and $z$. For a scalar change of variables $x=g(z)$, clearly $dx/dz=g'(z)$ so
${\rm p}_x(x)={\rm p}_z(z) dz/dx$,
which is the same as your equation. This requires $x$ and $z$ to be scalar random variables. For vectors of the same dimension, you can use the Jacobian.
However, as explained above, when ${\rm dim}(x)>{\rm dim}(z)$, the PDF of $x$ is degenerate. Although the expectation result still holds, the method used to obtain the optimal discriminator does not. This is because the latter uses calculus of variations, which requires continuously differentiable integrands. The integrand as a function of $x$ and $D$ is not continuously differentiable when ${\rm dim}(x)>{\rm dim}(z)$, so the optimal discriminator does not exist in this case.
This case is actually the one of practical interest and the counter-examples provided in the reference (based on the arguments in this post) invalidate the theoretical results in Goodfellow et al's 2014 GAN paper where they are based on Proposition 1 [Optimal Discriminator]. This is not to say the algorithms don't work: they clearly do in many cases and are extremely useful for machine learning; however the theory does not stand up to scrutiny when the data dimension ${\rm dim}(x)$ exceeds the latent variable dimension ${\rm dim}(z)$.
These points are explained further in the paper "Convergence and Optimality Analysis of Low-Dimensional Generative Adversarial Networks using Error Function Integrals" for which the link was given above. A practical demonstration appeared in a paper by Qin et al. (NIPS 2020) "Training Generative Adversarial Networks by Solving Ordinary Differential Equations." They showed on CIFAR-10 with ${\rm dim}(x)=3072>{\rm dim}(z)=256$ that neither the discriminator nor the generator losses converged to the predicted "Nash equilibrium" values, whereas the Nash equilibrium values were obtained on a ${\rm dim}(x)=2\leq {\rm dim}(z)=32$ Gaussian mixture simulation.
