Lecture 2
Lecture 2
Ahmed Kebaier
[email protected]
HEC, Paris
Simulation of Random variables
Outline
Algorithms
Linear Congruential Generators The simplest are the linear
congruential generators. Starting with the seed x0 , these generate
a sequence of integers by
xk = (axk−1 + c) mod M.
Algorithms
Linear Congruential Generators The simplest are the linear
congruential generators. Starting with the seed x0 , these generate
a sequence of integers by
xk = (axk−1 + c) mod M.
Uniform distribution
If x is a uniform [0, 1] distribution, then the change of variable
y = (b − a)x + a
-->x =rand(1,10000);
-->y = 10*x - 5;
-->histplot(100,y)
Simulation of Random variables
Normal distribution
If x has a normal µ = 0, σ = 1 distribution, then the change of
variable
y = µ + σx
Here is an example generating a normal µ = 100, σ = 10
distribution:
-->x = rand(1,10000,’normal’);
-->y = 10*x + 100;
-->histplot(100,y)
Simulation of Random variables
Theorem 1
Let Y be a random variable with distribution function Fy . We
define for all x ∈ [0, 1]
Remark:
1 Note that by construction we always have
∀y ∈ R y ≤ FY FY−1 (y ) .
Exercise
Solution
-->y = rand(1,10000);
-->x = - log(1-y)/2;
-->histplot(100,x)
Solution
-->y = rand(1,10000);
-->x = - log(1-y)/2;
-->histplot(100,x)
Exercise
Generate uniformly distributed random integers in the range 1 to
10 (inclusive).
Simulation of Random variables
Exercise
Generate uniformly distributed random integers in the range 1 to
10 (inclusive).
Solution
-->x = rand(1,30);
-->y = floor(10*x+1)
Simulation of Random variables
Exercise
Simulate a Binomial distribution with parameter N and P. (N = 5
and p = 0.25).
Simulation of Random variables
Exercise
Simulate a Binomial distribution with parameter N and P. (N = 5
and p = 0.25). Solution
function [x]=s binomial(s,N,p)
// simulation binomial
// s = Sample size
// N,p = parameters
//-------------------------------
x=0*ones(1:s);
y=rand(s,N);
for i=1:s do ;
for j=1:N do ;
if y(i,j)< p then x(i)=x(i)+1;
elseif y(i,j) >= p then x(i)=x(i);
end;
end;
end;
enfunction -->exec(’s binomial.sci’,-1)
-->x=s binomial(1000,5,0.25)
Simulation of Random variables
Acceptance/Rejection Method
Suppose that ρ(x) is zero outside of the interval [a, b] and
furthermore that ρ(x) is bounded above by c.
Exercise
Consider the hat-shaped probability density defined on [−1, 1] by
x +1 −1 ≤ x ≤ 0
ρ(x) =
1−x 0≤x ≤1
Exercise
Consider the hat-shaped probability density defined on [−1, 1] by
x +1 −1 ≤ x ≤ 0
ρ(x) =
1−x 0≤x ≤1
Solution
function [y] = rhohat(x)// First we need the density
function
if (x < 0) then
y = x + 1
else
y = 1 - x end
endfunction
Simulation of Random variables
Solution
function [y] = rhohat(x)// First we need the density
function
if (x < 0) then
y = x + 1
else
y = 1 - x end
endfunction
function [x] = randhat(n)// Now the random number
generator
x = zeros(1,n)
k = 0 // keep count of numbers generated
while (k < n)
xx = -1 + 2*rand(1,1) // uniform on [-1,1]
yy = rand(1,1)
Simulation of Random variables
Solution
function [y] = rhohat(x)// First we need the density
function
if (x < 0) then
y = x + 1
else
y = 1 - x end
endfunction
function [x] = randhat(n)// Now the random number
generator
x = zeros(1,n)
k = 0 // keep count of numbers generated
while (k < n)
xx = -1 + 2*rand(1,1) // uniform on [-1,1]
yy = rand(1,1)
if (yy <= rhohat(xx))// accept xx
k = k + 1
x(k) = xx
end
end
endfunction