0% found this document useful (0 votes)
34 views

Generating Random Variables

The document discusses methods for generating random variables. It describes true random generators that use natural or human sources, as well as pseudo-random generators that use computer algorithms. The inverse transform method is presented for generating continuous random variables from their distribution functions. Discrete random variables can be generated by comparing uniform random numbers to the cumulative distribution function.

Uploaded by

Bảo Dương
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Generating Random Variables

The document discusses methods for generating random variables. It describes true random generators that use natural or human sources, as well as pseudo-random generators that use computer algorithms. The inverse transform method is presented for generating continuous random variables from their distribution functions. Discrete random variables can be generated by comparing uniform random numbers to the cumulative distribution function.

Uploaded by

Bảo Dương
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Generating random variables

Generating random variables 1 / 25


Where does “randomness” come from ?

RANDOM
NUMBER
GENERATOR

True random Pseudo-random


generator generator

Human sources Natural sources

Generating random variables 2 / 25


True random generator - Human sources

Generating random variables 3 / 25


True random generator - Natural source
Ratioactive decay:

Generating random variables 4 / 25


True random generator

Pros:
They are truly random numbers.

Cons:
Random numbers are generated slowly (compared to Pseudo-radom
generator)
Need a huge storage to record.
Systematic error.

Generating random variables 5 / 25


Pseudo-random generator (Computer algorithms)

Pseudo-random number generator (PRNG) are computer algorithms to


generate sequences of Uniform[0,1] random variables u1 , u2 , · · ·, ud .
PRNG is defined by {S, s1 , f , g} where
S is the state space (set of all possible values during the process).
s1 ∈ S is the seed.
f : S → S.
g : S → [0, 1].
Uniform random variables u1 , u2 , · · · , ud are simulated as follows:
sk = f (sk−1 ). The common algo: sk = a ∗ sk−1 + b (mod m)
uk = g(sk ) = sk /(m − 1) ∈ [0, 1]

Generating random variables 6 / 25


Pseudo-random generator

SEED

f f f f f
s1 s2 s3 ··· sd s1

g g g g g

u1 u2 u3 ··· ud u1

If seed is not specified then R initialises it using a value taken from


the system clock.

Generating random variables 7 / 25


Pseudo-random generator

Since sk ∈ {0, . . . , m − 1} it follows that after some finite number (of at


most m), a value must repeat itself and once this happens the whole
sequence will begin to repeat. In general m should be chosen to satisfy
three criteria:
1 For any initial seed, the resultant sequence has the "appearance" of
being a sequence of independent Uni[0, 1] random variables.
2 For any initial seed, the number of variables that can be generated
before repetition begins is large.
3 The values can be computed efficiently on a digital computer.
One can choose m to be a sufficiently large prime number that can be fitted
to the computer word size. For example, the ideal choice for a 32-bit word
machine is m = 231 − 1.

Generating random variables 8 / 25


Generate continuous random variables

Proposition. Let U be a uniform [0, 1] random variable. For any


continuous distribution function F , the random variable X defined by

X = F −1 (U)

has distribution F .

The inverse transform algorithm


To simulate X with distribution F :
1 Simulate a random number U in [0, 1].
2 Return X = F −1 (U) where F −1 is the inverse function of F . (Using
uniroot function in R to solve it if there is no analytical form of F −1 ).
3 Repeat the above steps n times to has n realization of X.

Generating random variables 9 / 25


Generate continuous random variables

Example 1: A continuous random variable X ≥ 0, has distribution function:

1 2
P(X ≤ x ) = 1 − −
3(x + 1) 3(x + 1)2

Is there a close form solution for X = F −1 (U) ?


Simulate a sample of n = 106 values of X.
Calculate from the sample: P(X ≤ x ) and compare them with the true
probabilities.
(If there is not a closed-form for inverse function F −1 we can use uniroot
function in R. "uniroot(f,c(a,b))$root" returns the root r of equation
f (x ) = 0 where a < r < b. Note that f (a) and f (b) must have opposite
sign).

Generating random variables 10 / 25


Generate continuous random variables

1 t 2t 2
Example 1: Closed-form solution: Let = t then 1 − − = u.
x +1 3 3
p
2 1 + 24(1 − u) − 1
→ 2t + t − 3(1 − u) = 0 → t =
4
4
→ X=p −1
1 + 24(1 − U) − 1

F.inverse<-function(u){
F.inverse<-4/(sqrt(1+24*(1-u))-1)-1}
U<-runif(10ˆ6,0,1)
X<-F.inverse(U)

Generating random variables 11 / 25


Generate continuous random variables

Example 2: The total expenses of company ABC is calculated by the sales


amount X as follow:

Total expense = x 0.1 + x 0.2 + x 0.3

where X is a random variable with distribution function

P(X ≤ x ) = 1 − 0.3e −x − 0.7e −1.5x

What is the expectation of total expense?


What is VaR90% of total expense?

Generating random variables 12 / 25


Generate continuous random variables
Example 2:
n<-10ˆ4
u<-runif(n,0,1)
x<-rep(0,n)
for (i in 1:n){
F1<-function(x){F1<-1-0.3*exp(-0.5*x)-0.7*exp(-1.5*x)-u[i]}
x[i]<-uniroot(F1,c(0,1000))$root}
totalexpense<-xˆ0.1+xˆ0.2+xˆ0.3
mean(totalexpense)

## [1] 2.749474
quantile(totalexpense,0.9)

## 90%
## 3.63548

Generating random variables 13 / 25


Generate continuous random variables
Variable Density Generate n obsers
Uniform(a, b) 1/(b − a) runif (n, a, b)

Exponential(λ) λe −λx rexp(n, λ)


Γ(a + b) a−1
Beta(a, b) x (1 − x )b−1 rbeta(n, a, b)
Γ(a) Γ(b)
β α α−1 −βx
Gamma(α, β) x e rgamma(n, α, β)
Γ(α)
(x − µ)2
!
1
Normal (µ, σ) √ exp − rnorm(n, µ, σ)
2πσ 2σ 2
Log-normal (µ, σ) Ln(X ) ∼ Normal(µ, σ) rlnorm(n, µ, σ)
α βα Install Pareto package
Pareto(α, β)
x α+1 rPareto(n, β, α)

Generating random variables 14 / 25


Generate discrete random variables
Example 1: Let X be a Bernoulli random variable where

P(X = 1) = p
P(X = 0) = 1 − p

To simulate X
1. Generate U ∼ Uniform(0,1).
2. If U ≤ (1 − p) return 0; return 1 otherwise.
p<-0.3
u<-runif(1,0,1)
x<-ifelse(u<=(1-p),0,1)
print(x)

## [1] 1

Generating random variables 15 / 25


Generate discrete random variables

Example 1: Generate a vector contains n Bernoulli random variables


p<-0.3
n<-10ˆ4
u<-runif(n,0,1)
x<-ifelse(u<=(1-p),0,1)
print(x[1:10])

## [1] 0 0 1 0 0 0 1 0 0 0
sum(x)/n #should be close to p

## [1] 0.2851

Generating random variables 16 / 25


Generate discrete random variables

Discrete random variable X has probability mass function

p0 = P(X = 0)
p1 = P(X = 1)
···
pk = P(X = k)
···
k
Let qk = P(X ≤ k) = pi with k = 0, 1, 2, · · ·; to simulate X :
P
i=0

1. Generate an uniform random variable U in [0, 1].


2. Find k such that qk−1 ≤ U < qk and return k.

Generating random variables 17 / 25


Generate discrete random variables


P
with pi = 1; and
i=0
k
qk = P(X ≤ k) =
P
pi
i=0

Generating random variables 18 / 25


Generate discrete random variables

Generating random variables 19 / 25


Generate discrete random variables

Example 2: A discrete random variable X , X ≥ 0, has probability mass


function:
1 2 1
P(X = k) = p (1 − p)k + e −1
3 3 k!
where p = 0.2
Simulate a sample of N = 106 of value of X .
Calculate from the sample: P(X = 0), P(X = 1), · · · , and compare
them with the true probabilities.

WARNING: vector indices in R start with 1

Generating random variables 20 / 25


Generate discrete random variables
n<-100
p<-0.2
P0<-1/3*p+2/3*exp(-1)
P<-rep(0,n)
for (i in 1:n){P[i]<-1/3*p*(1-p)ˆi+2/3*exp(-1)/factorial(i)}
N<-10ˆ6
X<-rep(0,N)
U<-runif(N,0,1)
for (i in 1:N){
if (U[i]<P0){k<-0} else {
for (k in 1:n){if (P0+sum(P[1:k])>U[i]) {break}}}
X[i]<-k}
sum(X==2)/N

## [1] 0.165746

Generating random variables 21 / 25


Generate discrete random variables

Variables P(X = k) Generate N obsers


Bernoulli(p) pk (1 − p)(1−k) , k ∈ {0, 1} rbinom(N, 1, p)
n k
Binomial(n, p) k
p (1 − p)(n−k) rbinom(N, n, p)
Geometric(p) p (1 − p)k rgeom(N, p)
r +k−1  r
Negbinomial(r , p) k
p (1 − p)k rnbinom(N, r , p)
λk
Poisson (λ) e −λ rpois(N, λ)
k!

Generating random variables 22 / 25


Simulation exercises

Exercise 1: A random variable X has cumulative distribution function:





 0 x <0
 x2

0≤x <1

F (x ) = 4
x +1


 4 1≤x <2

1 2≤x

2
Calculate E(e X + e X ).

Generating random variables 23 / 25


Simulation exercises

Exercise 2: Given that X1 , X2 , · · · , X5 are 5 independent Pareto random


variables with parameters:

α1 = 1.2; β1 = 2.0
α2 = 1.3; β2 = 1.8
α3 = 1.4; β3 = 1.6
α4 = 1.5; β4 = 1.4
α5 = 1.6; β5 = 1.2

Let S5 = X1 + X2 + X3 + X4 + X5 . Calculate
1 P(S5 > 10); P(S5 > 103 ); P(S5 > 105 ).
2 VaR99% (S5 ) and CVaR99% (S5 ).

Generating random variables 24 / 25


Simulation exercises

Exercise 3 (Collective model): Total claim amount of a car driver in a


year is modeled by 
0
 N=0
SN = N
Xi N ≥ 1
P


i=1
where N is number of claims in the year and Xi is the claim amount of the
i th accident. N and Xi are independent variables. The following assumptions
are given about the number of claim N and the claim amounts Xi :
N is a Poisson random variable with parameter λ = 2.
Xi are i.i.d Pareto random variables with parameters α = 1.5 and
β = 2.5.
Calculate P(SN > 20) and VaR99% (SN )

Generating random variables 25 / 25

You might also like