# Why doesn't this function TransformedDistribution work?

According to the information in this post, we know that $${\displaystyle z={\sqrt {-2\ln U_{1}}}\cos(2\pi U_{2})}$$ follows a normal distribution, and the datas generated supports this view.

data = Table[Sqrt[-2 Log[RandomReal[]]] Cos[2 π RandomReal[]],
10000];
ListPlot[data // BinCounts[#, {Min[data], Max[data], 0.05}] &,
PlotRange -> All]


But the following code can't plot the probability density function of $${\displaystyle z={\sqrt {-2\ln U_{1}}}\cos(2\pi U_{2})}$$, and I want to know how to solve it, even if the numerical approximation is used.

    dist = TransformedDistribution[
Sqrt[-2 Log[U1]] Cos[2 π U2], {U1 \[Distributed]
UniformDistribution[{0, 1}],
U2 \[Distributed] UniformDistribution[{0, 1}]}]
Plot[PDF[dist, z], {z, 0, 1}, Filling -> Axis]

• MomentGeneratingFunction[dist, z] is identical to MomentGeneratingFunction[NormalDistribution[0, 1], z], and Mean[dist] is 0 and Variance[dist] is 1. So I don't know why it cannot figure out it's a normal distribution. Show[ Histogram[RandomVariate[dist, 100000], 2000, "PDF"], Plot[PDF[NormalDistribution[0, 1], x], {x, -4, 4}] ] Commented Jul 24, 2020 at 11:04

Form the TransformedDistribution in steps.

Clear["Global*"]

dist1 = TransformedDistribution[Sqrt[-2 Log[U1]],
U1 \[Distributed] UniformDistribution[]];

dist2 = TransformedDistribution[Cos[2 π U2],
U2 \[Distributed] UniformDistribution[]];

dist = TransformedDistribution[x*y,
{x \[Distributed] dist1, y \[Distributed] dist2}];


EDIT: Evaluating the PDF

PDF[dist, x]

(* Piecewise[{{1/(E^(x^2/2)*Sqrt[2*Pi]), x != 0}}, 0] *)


Except for a discontinuity at x == 0 this is the PDF for a standard normal distribution

PDF[NormalDistribution[], x]

(* E^(-(x^2/2))/Sqrt[2 π] *)


For a continuous distribution any specific value has measure zero so the difference makes no difference.

END EDIT

Show[
Plot[Evaluate@PDF[dist, z], {z, -4, 4},
PlotStyle -> Thin,
Filling -> Axis],
Plot[PDF[NormalDistribution[], z], {z, -4, 4},
PlotStyle -> Red]]


Drawing data from the transformed distribution,

data = RandomVariate[dist, 10000];

Show[
Histogram[data, Automatic, "PDF"],
Plot[Evaluate@PDF[dist, z], {z, -4, 4}]]
`