TransWikia.com

Why doesn't this function TransformedDistribution work?

Mathematica Asked on August 30, 2021

According to the information in this post, we know that ${displaystyle z={sqrt {-2ln U_{1}}}cos(2pi 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 {-2ln U_{1}}}cos(2pi 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]

One Answer

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]]

enter image description here

Drawing data from the transformed distribution,

data = RandomVariate[dist, 10000];

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

enter image description here

Correct answer by Bob Hanlon on August 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP