Generating Random Variables
Generating Random Variables
RANDOM
NUMBER
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.
SEED
f f f f f
s1 s2 s3 ··· sd s1
g g g g g
u1 u2 u3 ··· ud u1
X = F −1 (U)
has distribution F .
1 2
P(X ≤ x ) = 1 − −
3(x + 1) 3(x + 1)2
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)
## [1] 2.749474
quantile(totalexpense,0.9)
## 90%
## 3.63548
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
## [1] 0 0 1 0 0 0 1 0 0 0
sum(x)/n #should be close to p
## [1] 0.2851
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
∞
P
with pi = 1; and
i=0
k
qk = P(X ≤ k) =
P
pi
i=0
## [1] 0.165746
2
Calculate E(e X + e X ).
α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 ).