Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6K views

Random Variable Generation

The document describes four common methods for generating random variables: the linear congruential method, inversion method, acceptance-rejection method, and polar transformation method. Code examples are provided for each method to generate random variables from different distributions like uniform and normal.

Uploaded by

api-285777244
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6K views

Random Variable Generation

The document describes four common methods for generating random variables: the linear congruential method, inversion method, acceptance-rejection method, and polar transformation method. Code examples are provided for each method to generate random variables from different distributions like uniform and normal.

Uploaded by

api-285777244
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Random Variable Generation

YIK LUN, KEI


allen29@ucla.edu

1.Linear Congruential Method


Xt+1 = aXt + b (mod M)
lcg <- function(a,b,m,run.length,seed){
x <- rep(0,run.length)
x[1]<- seed
for (i in 1:(run.length-1)){x[i+1] = (a*x[i] + b) %% m}
U<- x/m #scale all of x to be between 0 and 1
return(list(x=x,U=U))
}
z = lcg(7^5,0,2^31-1,10000,1)
par(mfrow=c(1,2))
hist(z$U,xlab ="U",prob=T,main="Histogram of U",col="purple")
plot(z$x[seq(2,1000,by=2)], z$x[seq(3,1001,by=2)] ,
xlab = expression('x'[2*t]), ylab = expression('x'[2*t+1]), main="scatterplot")

scatterplot

1.0e+09

x2t+1

0.6
0.4

0.0e+00

0.2
0.0

Density

0.8

1.0

2.0e+09

Histogram of U

0.0 0.2 0.4 0.6 0.8 1.0

0.0e+00

1.0e+09
x2t

2.0e+09

2. Inversion Method
The idea is to set

f (x) = F (x) = U

Exponential Distribution: f (x) = ex and F (x) =


Uniform Distribution: U =

Rx
0

ex dx = 1 ex

1
ba

Set 1 ex = U . Using uniform distribution to generate exponential distribution


)
by plotting histogram of x = log(1U
for given

inversion <- function(n,lambda){


u = runif(n)
x<- -log(1-u)/lambda
return(x)}
par(mfrow = c(2, 2))
lambda4 = c(0.5, 1, 2, 1.5)
n = 10000
for (lambda in lambda4){
hist(inversion(n,lambda), breaks=30,
xlab = "x", prob= T, main = paste("Hist of Exp(",lambda, ")"))
}

Hist of Exp( 1 )

0.0
5

10

15

20

10

Hist of Exp( 2 )

Hist of Exp( 1.5 )

1.0

3
x

12

0.0 0.6 1.2

0.0
0

Density

Density

0.4

Density

0.3
0.0

Density

0.8

Hist of Exp( 0.5 )

3. Acceptance-Rejection Method
Using Mg(x) to envelop f(x) which is the targeted distribution
P(accept x) =

f (x)
M g(x)

and acceptance rate =

Because Mg(x) f(x), Min M = Max

1
M

f (x)
g(x)

The algorithm of acceptance-rejection method is:


(1) Generate X ~ g(x)
(2) Generate U ~ Unif[0, 1], return X if U <
Given g(x) = ex where = 1 and f (x) =
So

f (x)
g(x)

2
=

ex

Maximum of
Thus M =

x2
2

f (x)
g(x)

2 12
e

2
exp( 12 ((x

f (x)
.
M g(x)

2 x
e 2

Otherwise, go back to (1).

1)2 1))

is acheived at x = 1.

and

f (x)
M g(x)

= exp( (x1)
)
2

num = 0
iter = 10000
result =rep(0,iter)
for (i in 1:iter){
rejected = 0
repeat{
X = -log(runif(1)) #Using uniform to generate exponential distribution g(x)
U = runif(1,min=0,max=1)
if( U< exp(-(X-1)^2/2)){ #accept x
break}
rejected = rejected + 1}
num = num + rejected # number of rejected
#accept X as a realization of truncated normal
#Now need to flip a coin to generate the whole normal distribution
if (runif(1)<= 0.5) {Z = X
}else {Z =-X}
result[i] = Z
}
rejection_rate = num/(iter+num)
acceptance_rate = 1-rejection_rate
hist(result, breaks = 30, prob=T, xlab = "Z",
main = paste("Histogram of Z, Acceptance rate= ", round(acceptance_rate,2)))
curve(dnorm(x,mean=0,sd=1), lwd=2, add=T, col="blue")

0.2
0.0

0.1

Density

0.3

0.4

Histogram of Z, Acceptance rate= 0.76

Polar Transformation
Using polar transformation to generate N(0, 1) random variable$
~Unif[0, 2], So = 2u1
normsamp = function(n) {
u1 = runif(n)
u2 = runif(n)
theta = 2*pi*u1
t = -log(u2)
r = sqrt(2*t)
x = r*cos(theta)
y = r*sin(theta)
res = list("X"=x, "Y"=y)
return(res)
}
sample = normsamp(1e4)
par(mfrow =c(1,2))
hist(sample$X , breaks =30, prob =T, xlim =c(-4,4) ,
xlab ="X", main = "Histogram of X")
curve(dnorm(x,mean =0, sd =1) ,lwd =2, add =T, col="blue")
hist(sample$Y , breaks =30, prob =T, xlim =c(-4,4) ,
4

xlab ="Y", main = "Histogram of Y")


curve(dnorm(x,mean =0, sd =1) ,lwd =2, add =T, col="blue")

Histogram of Y

0.3
0.0

0.1

0.2

Density

0.2
0.1
0.0

Density

0.3

0.4

0.4

Histogram of X

0
Y

You might also like