I have the following question about a random simulation that I am doing.
I want to compute \begin{align} M_1=E[|Z|] + E[|Z+a|] \end{align} where $a$ is some fixed constant and $Z$ is a standard normal.
Next, observe that
\begin{align} E[|Z+a|]&= \int |z+a| \frac{1}{\sqrt{2\pi}}\exp(-z^2/2) dz \\ &= \int |z| \frac{1}{\sqrt{2\pi}}\exp(-(z-a)^2/2) dz \\ &= \int |z| \exp( az -a^2/2) \frac{1}{\sqrt{2\pi}}\exp(-z^2/2) dz \\ &= E \left[|Z| \exp( aZ -a^2/2)\right] \end{align} Therefore, \begin{align} E[|Z|] + E[|Z+a|] &= E[|Z|] + E \left[|Z| \exp( aZ -a^2/2)\right]\\ &=E \left[|Z| \left(1+\exp( aZ -a^2/2) \right)\right]\\ &=M_2 \end{align}
I don't see a mistake in the above steps and I think $M_1=M_2$.
However, if I do a numerical simulation for example for a value of $a=3$ I get different values for $M_1$ and $M_2$. How can this be? Clearly, I am making a mistake somewhere. However, I am not sure where?
Here is the MatLab code that I am using
a=3;
Z=randn(1,2000);
M1=mean( abs(Z))+ mean( abs(Z+a))
M2= mean(abs(Z).*(1+exp( a*Z-a^2/2)))
and the outputs are
M1 =
3.7768
M3 =
0.8341
2000 is not enough to sample from the second one. I did 5 million and have consistently observed values close to M1. (The reason there is simply the second way egregiously shifts values around because of that exponential term; it is like both the random variable that is 0 and 2 with probability 1/2 each, and a random variable that takes 1000000 with probability 1/1000000 both have mean 1, but if you don't sample enough, you will not be able to see the difference.)