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

Part-4-R-and-random-variables

This document discusses random variables and their generation using R programming. It covers true random generators, pseudo-random generators, and various algorithms like Wichmann-Hill and Mersenne-Twister for generating random numbers. Additionally, it explains how to generate continuous and discrete random variables with examples and R code snippets.

Uploaded by

phuongmn0802
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Part-4-R-and-random-variables

This document discusses random variables and their generation using R programming. It covers true random generators, pseudo-random generators, and various algorithms like Wichmann-Hill and Mersenne-Twister for generating random numbers. Additionally, it explains how to generate continuous and discrete random variables with examples and R code snippets.

Uploaded by

phuongmn0802
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

Part 4 R and random variables

Dr. Nguyen Quang Huy

March 06, 2020

Dr. Nguyen Quang Huy Part 4 R and random variables March 06, 2020 1 / 43
Where does “randomness” come from ?

Why using “sample(0:1,1)” can generate a random number X where


P (X = 0) = P (X = 1) = 0.5

Dr. Nguyen Quang Huy Part 4 R and random variables March 06, 2020 2 / 43
Where does “randomness” come from ?

Why using “sample(0:1,1)” can generate a random number X where


P (X = 0) = P (X = 1) = 0.5

RANDOM
NUMBER
GENERATOR

True random Pseudo-random


generator generator

Human sources Natural sources

Dr. Nguyen Quang Huy Part 4 R and random variables March 06, 2020 2 / 43
True random generator - Human sources

Dr. Nguyen Quang Huy Part 4 R and random variables March 06, 2020 3 / 43
True random generator - Natural source
Ratioactive decay:

Dr. Nguyen Quang Huy Part 4 R and random variables March 06, 2020 4 / 43
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.

Dr. Nguyen Quang Huy Part 4 R and random variables March 06, 2020 5 / 43
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 state universe (all possible states).
s1 ∈ S is the seed.
f : S → S.
g : S → [0, 1].
Uniform random variable u1 , u2 , · · · , ud are simulated as follows:
u1 = g(s1 ).
sk = f (sk−1 ).
uk = g(sk ).
*Note: For any PRNG, the number
Dr. Nguyen Quang Huy
of state d, d = number March
Part 4 R and random variables
of elements
06, 2020
in
6 / 43
Pseudo-random generator

SEED

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

g g g g g

u1 u2 u3 ··· ud u1

Dr. Nguyen Quang Huy Part 4 R and random variables March 06, 2020 7 / 43
Pseudo-random generator - linear congruence
s1 is an integer from 1 to M
f (s) = (A × s + C ) mod M i.e. sk+1 = (A × sk + C ) mod M.
s sk
g(s) = i.e. uk =
M M
Let M = 231 − 1; A = 75 ; C = 0 and s1 = 10.
Generate u1 , u2 , · · · , uN from above algorithm (N = 106 ) ?
Check the correlation between uk−1 and uk ?
Calculate from the sample the probability pi = P(U ≤ vi ) with
!
1 2 k
v= , ,··· ,
k +1 k +1 k +1
and plot pi against v ?
Calculate d - smallest integer number such that ud+1 = u1 (from 10 to
Dr. Nguyen Quang Huy Part 4 R and random variables March 06, 2020 8 / 43
Wichmann Hill algorithm
Let p1 = 30269; p2 = 30307 and p3 = 30323.
Seed s1 = (s1,1 , s1,2 , s1,3 ) where s1,j are integer numbers from 1 to
30,000.
Function f :
sk,1 = 171 × s(k−1),1 mod p1
sk,2 = 172 × s(k−1),2 mod p2
sk,3 = 170 × s(k−1),3 mod p3

Function g: uk = vk − [vk ] where [vk ] is integer part of vk and


sk,1 sk,2 sk,3
vk = + +
p1 p2 p3

*Note: Wichmann Hill algorithm’s cycle is ∼ 6.95 × 1012 .


Dr. Nguyen Quang Huy Part 4 R and random variables March 06, 2020 9 / 43
Wichmann Hill algorithm

With s1 = (100, 200, 300), generate u1 , u2 , · · · , uN from above


algorithm (N = 106 ) ?
Check the correlation between uk−1 and uk ?
Calculate from the sample the probability pi = P(U ≤ vi ) with
!
1 2 k
v= , ,··· ,
k +1 k +1 k +1

and plot pi against v ?

Dr. Nguyen Quang Huy Part 4 R and random variables March 06, 2020 10 / 43
Mersenne-Twister algorithm

Default PRNG in almost all softwares: R, Excel, MATLAB, Python, C++,


Mathematica . . .
Developed in 1997 by Makoto Matsumoto and Takuji Nishimura
Period length is chosen to be a Mersenne prime
Maximum period is 219937 − 1 ≈ 105981
"Mersenne twister: a 623-dimensionally equidistributed uniform
pseudo-random number generator"
Generate an uniform vector of length N in [0, 1] with R: runif (N, 0, 1)

Dr. Nguyen Quang Huy Part 4 R and random variables March 06, 2020 11 / 43
Generate continuous random variables

If X is continuous random variable with distribution function


F (x ) = P(X ≤ x ), the random variable U defined by U = F (X) is uniformly
distributed on [0, 1].
→ to simulate X with distribution F :
1. Simulate a random number U in [0, 1].
2. Return X = F ← (U) where F ← is the inverse function of F (Using
"uniroot" function in R to solve it if there is no analytical form of F ← )
3. Repeat above steps n times to has n realization of X.

Dr. Nguyen Quang Huy Part 4 R and random variables March 06, 2020 12 / 43
Generate continuous random variables

Dr. Nguyen Quang Huy Part 4 R and random variables March 06, 2020 13 / 43
Generate continuous random variables

Dr. Nguyen Quang Huy Part 4 R and random variables March 06, 2020 14 / 43
Generate continuous random variables

Dr. Nguyen Quang Huy Part 4 R and random variables March 06, 2020 15 / 43
Generate continuous random variables

Dr. Nguyen Quang Huy Part 4 R and random variables March 06, 2020 16 / 43
Generate continuous random variables

Dr. Nguyen Quang Huy Part 4 R and random variables March 06, 2020 17 / 43
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 F ← (X ) = U. ?


Simulate a sample of n = 106 of value 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 ← we can use uniroot
function in R. "Unifoot(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).

Dr. Nguyen Quang Huy Part 4 R and random variables March 06, 2020 18 / 43
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)

Dr. Nguyen Quang Huy Part 4 R and random variables March 06, 2020 19 / 43
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

Dr. Nguyen Quang Huy Part 4 R and random variables March 06, 2020 20 / 43
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.735214
quantile(totalexpense,0.9)

## 90%
## 3.610101
Dr. Nguyen Quang Huy Part 4 R and random variables March 06, 2020 21 / 43
Generate continuous random variables

Variable Density Generate n obsers


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

Pareto(α, β) α β α (x + β)−α−1 NA

Dr. Nguyen Quang Huy Part 4 R and random variables March 06, 2020 22 / 43
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] 0
Dr. Nguyen Quang Huy Part 4 R and random variables March 06, 2020 23 / 43
Generate discrete random variables

Dr. Nguyen Quang Huy Part 4 R and random variables March 06, 2020 24 / 43
Generate discrete random variables

Dr. Nguyen Quang Huy Part 4 R and random variables March 06, 2020 25 / 43
Generate discrete random variables

Dr. Nguyen Quang Huy Part 4 R and random variables March 06, 2020 26 / 43
Generate discrete random variables

Dr. Nguyen Quang Huy Part 4 R and random variables March 06, 2020 27 / 43
Generate discrete random variables

Dr. Nguyen Quang Huy Part 4 R and random variables March 06, 2020 28 / 43
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 0 0 0 0 0 0 1 1
sum(x)/n #should be close to p

## [1] 0.2949

Dr. Nguyen Quang Huy Part 4 R and random variables March 06, 2020 29 / 43
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.

Dr. Nguyen Quang Huy Part 4 R and random variables March 06, 2020 30 / 43
Generate discrete random variables


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

Dr. Nguyen Quang Huy Part 4 R and random variables March 06, 2020 31 / 43
Generate discrete random variables

Dr. Nguyen Quang Huy Part 4 R and random variables March 06, 2020 32 / 43
Generate discrete random variables

Dr. Nguyen Quang Huy Part 4 R and random variables March 06, 2020 33 / 43
Generate discrete random variables

Dr. Nguyen Quang Huy Part 4 R and random variables March 06, 2020 34 / 43
Generate discrete random variables

Dr. Nguyen Quang Huy Part 4 R and random variables March 06, 2020 35 / 43
Generate discrete random variables

Dr. Nguyen Quang Huy Part 4 R and random variables March 06, 2020 36 / 43
Generate discrete random variables

Dr. Nguyen Quang Huy Part 4 R and random variables March 06, 2020 37 / 43
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

Dr. Nguyen Quang Huy Part 4 R and random variables March 06, 2020 38 / 43
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.165482

Dr. Nguyen Quang Huy Part 4 R and random variables March 06, 2020 39 / 43
Generate discrete random variables

Variables P(X = k) Generate N obsers


Bernoulli(p) p k (1 − p)(1−k) 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 rnbinom(N, 1, p)
r +k−1  r
Negative binomial(r , p) k
p (1 − p)k rnbinom(N, r , p)
λk
Poisson (λ) e −λ rpois(N, λ)
k!

Dr. Nguyen Quang Huy Part 4 R and random variables March 06, 2020 40 / 43
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 ).

Dr. Nguyen Quang Huy Part 4 R and random variables March 06, 2020 41 / 43
Simulation exercises

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


variables of parameter:

α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
P(S5 > 10); P(S5 > 103 ); P(S5 > 105 ).
VaR99% (S5 ) and CVaR99% (S5 ).

Dr. Nguyen Quang Huy Part 4 R and random variables March 06, 2020 42 / 43
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 of parameter λ = 2.
Xi are i.i.d Pareto random variables of parameter α = 1.5 and β = 2.5.
Calculate P(SN > 20) and VaR99% (SN )

Dr. Nguyen Quang Huy Part 4 R and random variables March 06, 2020 43 / 43

You might also like