Simulation of Continuous Random Variables

MATH 262 Probability Theory

Problem 1 from class

Here is the module that simulates the value of random variable X with the distribution from class:

simX[]:=Module[{},
u=RandomReal[];
If[u≤1/2,x=-1+Sqrt[2u],x=1-Sqrt[2-2u]];
Return[x]
]

simX[]

Let's simulate 10,000 values of X:

xvals=Table[simX[],10000]

Make a histogram of the simulated values. It has the same shape as our desired density function!

Histogram[xvals,20,"PDF",AxesLabel->{"x value","frequency"}]

Problem 2: Simulating Brownian Motion

We can use values of X as “steps” in a simulation of a particle moving randomly left and right on the number line.

positions = {0};
Do[
AppendTo[positions, simX[] + positions[[-1]]],
500]

ListLinePlot[positions, AxesLabel->{"step","position"}]

Animate[NumberLinePlot[{positions[[i]]},{x,-10,10},PlotStyle->PointSize[0.03]],{i,1,Length[positions],1},AnimationRate->20]

Find time until the particle moves a net distance of 10 units from the origin

Here is a module that simulates the Brownian motion until the particle is 10 units from the origin.

timeToTen[]:=Module[{pos=0, time = 0},
While[Abs[pos]<10,
pos+=simX[];
time+=1
];
Return[time]
]

timeToTen[]

To estimate the average time until the particle reaches 10 units from the origin, we will simulate 1000 times and take the average.

tenVals=Table[timeToTen[],1000]

Mean[tenVals]//N