5C RV Simulation New
5C RV Simulation New
Vineet Sahula
Estimating E[g(X)]
• Let X = (X1 , X2 , . . . , Xn ) denote a random vector
• having density function f (x1 , x2 . . . , xn )
• Objective is to find the expected value of g (X )
• E [g (X )] = . . . g (x1 , x2 , . . . xn ) f (x1 , x2 , . . . , xn ) dx1 dx2 , . . . , dxn
• Analytical OR numerical integration is tedious
• Alternately, approximate E [g (X)] by means of SIMULATION
Estimating E[g(X)]
• Let X = (X1 , X2 , . . . , Xn ) denote a random vector
• having density function f (x1 , x2 . . . , xn )
• Objective is to find the expected value of g (X )
• E [g (X )] = . . . g (x1 , x2 , . . . xn ) f (x1 , x2 , . . . , xn ) dx1 dx2 , . . . , dxn
• Analytical OR numerical integration is tedious
• Alternately, approximate E [g (X)] by means of SIMULATION
Estimating E[g(X)]
• Let X = (X1 , X2 , . . . , Xn ) denote a random vector
• having density function f (x1 , x2 . . . , xn )
• Objective is to find the expected value of g (X )
• E [g (X )] = . . . g (x1 , x2 , . . . xn ) f (x1 , x2 , . . . , xn ) dx1 dx2 , . . . , dxn
• Analytical OR numerical integration is tedious
• Alternately, approximate E [g (X)] by means of SIMULATION
Estimating E[g(X)]
• Let X = (X1 , X2 , . . . , Xn ) denote a random vector
• having density function f (x1 , x2 . . . , xn )
• Objective is to find the expected value of g (X )
• E [g (X )] = . . . g (x1 , x2 , . . . xn ) f (x1 , x2 , . . . , xn ) dx1 dx2 , . . . , dxn
• Analytical OR numerical integration is tedious
• Alternately, approximate E [g (X)] by means of SIMULATION
Estimating E[g(X)]
• Let X = (X1 , X2 , . . . , Xn ) denote a random vector
• having density function f (x1 , x2 . . . , xn )
• Objective is to find the expected value of g (X )
• E [g (X )] = . . . g (x1 , x2 , . . . xn ) f (x1 , x2 , . . . , xn ) dx1 dx2 , . . . , dxn
• Analytical OR numerical integration is tedious
• Alternately, approximate E [g (X)] by means of SIMULATION
Estimating E[g(X)]
• Let X = (X1 , X2 , . . . , Xn ) denote a random vector
• having density function f (x1 , x2 . . . , xn )
• Objective is to find the expected value of g (X )
• E [g (X )] = . . . g (x1 , x2 , . . . xn ) f (x1 , x2 , . . . , xn ) dx1 dx2 , . . . , dxn
• Analytical OR numerical integration is tedious
• Alternately, approximate E [g (X)] by means of SIMULATION
Estimating E[g(X)]
• Let X = (X1 , X2 , . . . , Xn ) denote a random vector
• having density function f (x1 , x2 . . . , xn )
• Objective is to find the expected value of g (X )
• E [g (X )] = . . . g (x1 , x2 , . . . xn ) f (x1 , x2 , . . . , xn ) dx1 dx2 , . . . , dxn
• Analytical OR numerical integration is tedious
• Alternately, approximate E [g (X)] by means of SIMULATION
# of distinct entries
• Consider list of n entries, where n is very large
• Objective is to compute d, # of distinct entries
• Let, mi be number of times element in position i appears on the list
Xn
• Then, d = 1/mi
i =1
# of distinct entries
• Consider list of n entries, where n is very large
• Objective is to compute d, # of distinct entries
• Let, mi be number of times element in position i appears on the list
Xn
• Then, d = 1/mi
i =1
# of distinct entries
• Consider list of n entries, where n is very large
• Objective is to compute d, # of distinct entries
• Let, mi be number of times element in position i appears on the list
Xn
• Then, d = 1/mi
i =1
# of distinct entries
• Consider list of n entries, where n is very large
• Objective is to compute d, # of distinct entries
• Let, mi be number of times element in position i appears on the list
Xn
• Then, d = 1/mi
i =1
# of distinct entries
• To estimate d, lets generate a random value
X = {1, 2, . . . n} taking X = ⌊nU ⌋ + 1
• Let m(X ) denote the number of times, the element in position X
appears in the list
X
n
1 1
• E [1/m(X )] =
mi n
i =1
• If we generate k such random variables X1 , X2 , . . . Xk , the estimate is
X
k
1/m(X )
i
• d= i =1
k
# of distinct entries
• To estimate d, lets generate a random value
X = {1, 2, . . . n} taking X = ⌊nU ⌋ + 1
• Let m(X ) denote the number of times, the element in position X
appears in the list
X
n
1 1
• E [1/m(X )] =
mi n
i =1
• If we generate k such random variables X1 , X2 , . . . Xk , the estimate is
X
k
1/m(X )
i
• d= i =1
k
# of distinct entries
• To estimate d, lets generate a random value
X = {1, 2, . . . n} taking X = ⌊nU ⌋ + 1
• Let m(X ) denote the number of times, the element in position X
appears in the list
X
n
1 1
• E [1/m(X )] =
mi n
i =1
• If we generate k such random variables X1 , X2 , . . . Xk , the estimate is
X
k
1/m(X )
i
• d= i =1
k
# of distinct entries
• To estimate d, lets generate a random value
X = {1, 2, . . . n} taking X = ⌊nU ⌋ + 1
• Let m(X ) denote the number of times, the element in position X
appears in the list
X
n
1 1
• E [1/m(X )] =
mi n
i =1
• If we generate k such random variables X1 , X2 , . . . Xk , the estimate is
X
k
1/m(X )
i
• d= i =1
k
FX (a) = P {X ⩽ a} = P F −1 (U ) ⩽ a
FX (a) = P {X ⩽ a} = P F −1 (U ) ⩽ a
FX (a) = P {X ⩽ a} = P F −1 (U ) ⩽ a
F(x)
F (x ) = 1 − e −x
• For F (x ) = 1 − e −x ,
• then F −1 (u ) is that value of x such that F (x ) = u , i.e.
1 − e −x = u
x = − log (1 − u )
F(x)
• Hence, if U is UNIFORM (0,1) variable, then following is a random
variable EXPONENTIALLY distributed
F −1 (U ) = − log(1 − U )
F(x)
• Hence, if U is UNIFORM (0,1) variable, then following is a random
variable EXPONENTIALLY distributed
F −1 (U ) = − log(1 − U )
Stopping Time
Definition
An integer RV N, is said to be stopping time for the sequence X1 , X2 , . . .,
if the event {N = n} is independent of Xn+1 , Xn+2 , . . . for all n = 1, 2, ...
Stopping Time
Definition
An integer RV N, is said to be stopping time for the sequence X1 , X2 , . . .,
if the event {N = n} is independent of Xn+1 , Xn+2 , . . . for all n = 1, 2, ...
Stopping Time
Definition
An integer RV N, is said to be stopping time for the sequence X1 , X2 , . . .,
if the event {N = n} is independent of Xn+1 , Xn+2 , . . . for all n = 1, 2, ...
Stopping Time
Definition
An integer RV N, is said to be stopping time for the sequence X1 , X2 , . . .,
if the event {N = n} is independent of Xn+1 , Xn+2 , . . . for all n = 1, 2, ...
2 2
• Normal distribution, f (x , y ) = 21π e −(x +y )/2
• Cauchy distribution, f (x ) = 1
π(1+x 2 )
−∞<x <∞
• Gamma distribution ?
• Chi-squared distribution, χ2n = Z12 + Z22 + · · · + Zn2 (n-dimensional
Chi-squared distribution)
• here, Zi , i − 1, 2, ..., n are independent unit Normals
• Beta (n, m) distribution,
f (x ) = (n(−n1+)m −1)!
!(m−1)! x
n−1 (1 − x )m−1 with 0 < x < 1
2 2
• Normal distribution, f (x , y ) = 21π e −(x +y )/2
• Cauchy distribution, f (x ) = 1
π(1+x 2 )
−∞<x <∞
• Gamma distribution ?
• Chi-squared distribution, χ2n = Z12 + Z22 + · · · + Zn2 (n-dimensional
Chi-squared distribution)
• here, Zi , i − 1, 2, ..., n are independent unit Normals
• Beta (n, m) distribution,
f (x ) = (n(−n1+)m −1)!
!(m−1)! x
n−1 (1 − x )m−1 with 0 < x < 1
2 2
• Normal distribution, f (x , y ) = 21π e −(x +y )/2
• Cauchy distribution, f (x ) = 1
π(1+x 2 )
−∞<x <∞
• Gamma distribution ?
• Chi-squared distribution, χ2n = Z12 + Z22 + · · · + Zn2 (n-dimensional
Chi-squared distribution)
• here, Zi , i − 1, 2, ..., n are independent unit Normals
• Beta (n, m) distribution,
f (x ) = (n(−n1+)m −1)!
!(m−1)! x
n−1 (1 − x )m−1 with 0 < x < 1
2 2
• Normal distribution, f (x , y ) = 21π e −(x +y )/2
• Cauchy distribution, f (x ) = 1
π(1+x 2 )
−∞<x <∞
• Gamma distribution ?
• Chi-squared distribution, χ2n = Z12 + Z22 + · · · + Zn2 (n-dimensional
Chi-squared distribution)
• here, Zi , i − 1, 2, ..., n are independent unit Normals
• Beta (n, m) distribution,
f (x ) = (n(−n1+)m −1)!
!(m−1)! x
n−1 (1 − x )m−1 with 0 < x < 1
2 2
• Normal distribution, f (x , y ) = 21π e −(x +y )/2
• Cauchy distribution, f (x ) = 1
π(1+x 2 )
−∞<x <∞
• Gamma distribution ?
• Chi-squared distribution, χ2n = Z12 + Z22 + · · · + Zn2 (n-dimensional
Chi-squared distribution)
• here, Zi , i − 1, 2, ..., n are independent unit Normals
• Beta (n, m) distribution,
f (x ) = (n(−n1+)m −1)!
!(m−1)! x
n−1 (1 − x )m−1 with 0 < x < 1
2 2
• Normal distribution, f (x , y ) = 21π e −(x +y )/2
• Cauchy distribution, f (x ) = 1
π(1+x 2 )
−∞<x <∞
• Gamma distribution ?
• Chi-squared distribution, χ2n = Z12 + Z22 + · · · + Zn2 (n-dimensional
Chi-squared distribution)
• here, Zi , i − 1, 2, ..., n are independent unit Normals
• Beta (n, m) distribution,
f (x ) = (n(−n1+)m −1)!
!(m−1)! x
n−1 (1 − x )m−1 with 0 < x < 1
2 2
• Normal distribution, f (x , y ) = 21π e −(x +y )/2
• Cauchy distribution, f (x ) = 1
π(1+x 2 )
−∞<x <∞
• Gamma distribution ?
• Chi-squared distribution, χ2n = Z12 + Z22 + · · · + Zn2 (n-dimensional
Chi-squared distribution)
• here, Zi , i − 1, 2, ..., n are independent unit Normals
• Beta (n, m) distribution,
f (x ) = (n(−n1+)m −1)!
!(m−1)! x
n−1 (1 − x )m−1 with 0 < x < 1
• As
• j −1
X X
j
P {X = xj } = P Pi < U < Pi = Pj
1 1
• As
• j −1
X X
j
P {X = xj } = P Pi < U < Pi = Pj
1 1
• As
• j −1
X X
j
P {X = xj } = P Pi < U < Pi = Pj
1 1
• As
• j −1
X X
j
P {X = xj } = P Pi < U < Pi = Pj
1 1
• As
• j −1
X X
j
P {X = xj } = P Pi < U < Pi = Pj
1 1
1 − (1 − p )i −1 < U < 1 − (1 − p )i
(1 − p )j −1 > 1 − U > (1 − p )i
1 − (1 − p )i −1 < U < 1 − (1 − p )i
(1 − p )j −1 > 1 − U > (1 − p )i
1 − (1 − p )i −1 < U < 1 − (1 − p )i
(1 − p )j −1 > 1 − U > (1 − p )i
log U
X = min j : (1 − p )j < U = min j : j >
log (1 − p )
log U
=1+
log (1 − p )
Binomial discrete RV
• A binomial RV (n, p ) can be most easily simulated considering that
• it can be expressed as the sum of n independent Bernoulli RV
• e.g., if U1 , U2 , . . . Un are independent Uniform (0,1) RVs then letting
1 if Ui < p
Xi =
0 otherwise
X
n
• then X = Xi is a binomial RV with (n, p )
i =1
Binomial discrete RV
• A binomial RV (n, p ) can be most easily simulated considering that
• it can be expressed as the sum of n independent Bernoulli RV
• e.g., if U1 , U2 , . . . Un are independent Uniform (0,1) RVs then letting
1 if Ui < p
Xi =
0 otherwise
X
n
• then X = Xi is a binomial RV with (n, p )
i =1
Binomial discrete RV
• A binomial RV (n, p ) can be most easily simulated considering that
• it can be expressed as the sum of n independent Bernoulli RV
• e.g., if U1 , U2 , . . . Un are independent Uniform (0,1) RVs then letting
1 if Ui < p
Xi =
0 otherwise
X
n
• then X = Xi is a binomial RV with (n, p )
i =1
Procedure
Improvements Step_1 Let α = 1/p and
• The previous method requires β = 1/(1 − p )
generation of n random numbers Step_2 Set k = 0
• Instead of using value of U, the Step_3 Generate a uniform RV U
previous algorithm uses the fact Step_4 If k = n stop, else RESET
Ui < p or not k =k +1
• Conditional distribution of U given Step_5 If U ⩽ p set Xk = 1 and
that U < p is uniform in (0, p ) & RESET U to αU. If U > p
conditional distribution of U given set Xk = 0 and RESET U
that U > p is uniform in (p, 1) to β(U − p ). RETURN to
Step_4
Alias Method
• Let quantities, P, P(k ) , Q(k ) , k ⩽ n − 1 represent pmf on 1, 2, . . . , n
i.e. they are n-vectors of non-negative numbers summing to 1
• Additionally, each of P(k ) will have at most k non-zero components
• Each of Q(k ) will have at most 2 non-zero components
Lemma
Let P = {Pi , i = 1, 2, . . . , n} denote pmf , then
1
(a) there exists and i, 1 ⩽ i ⩽ n, such that Pi < n− 1 , and
1
(b) for this i, there exists a j, j ̸= i, such that Pi + Pj ⩾ n− 1
Alias Method
• Let quantities, P, P(k ) , Q(k ) , k ⩽ n − 1 represent pmf on 1, 2, . . . , n
i.e. they are n-vectors of non-negative numbers summing to 1
• Additionally, each of P(k ) will have at most k non-zero components
• Each of Q(k ) will have at most 2 non-zero components
Lemma
Let P = {Pi , i = 1, 2, . . . , n} denote pmf , then
1
(a) there exists and i, 1 ⩽ i ⩽ n, such that Pi < n− 1 , and
1
(b) for this i, there exists a j, j ̸= i, such that Pi + Pj ⩾ n− 1
Alias Method
• Let quantities, P, P(k ) , Q(k ) , k ⩽ n − 1 represent pmf on 1, 2, . . . , n
i.e. they are n-vectors of non-negative numbers summing to 1
• Additionally, each of P(k ) will have at most k non-zero components
• Each of Q(k ) will have at most 2 non-zero components
Lemma
Let P = {Pi , i = 1, 2, . . . , n} denote pmf , then
1
(a) there exists and i, 1 ⩽ i ⩽ n, such that Pi < n− 1 , and
1
(b) for this i, there exists a j, j ̸= i, such that Pi + Pj ⩾ n− 1
Alias Method
• Let quantities, P, P(k ) , Q(k ) , k ⩽ n − 1 represent pmf on 1, 2, . . . , n
i.e. they are n-vectors of non-negative numbers summing to 1
• Additionally, each of P(k ) will have at most k non-zero components
• Each of Q(k ) will have at most 2 non-zero components
Lemma
Let P = {Pi , i = 1, 2, . . . , n} denote pmf , then
1
(a) there exists and i, 1 ⩽ i ⩽ n, such that Pi < n− 1 , and
1
(b) for this i, there exists a j, j ̸= i, such that Pi + Pj ⩾ n− 1
1 X (k )
n−1
P= Q
n−1
k =1
Lemma
Let P = {Pi , i = 1, 2, . . . n} denote a probability mass function, then
1 There exists an i, 1 ⩽ i ⩽ n, such that Pi < 1/(n − 1), and
2 for this i there exists a j, j ̸= i, such that Pi + Pj ⩾ 1/(n − 1)
1 X (k )
n−1
P= Q
n − 1 k =1
Pi(n−1) = 0
n−1 1 n−1 1
Pj(n−1) = Pj − Q (1)
= Pi + Pj −
n−2 n−1 j n−2 n−1
n−1
Pk(n−1) = P , k ̸= i or j
n−2 k
1 n − 3 (n−2)
P(n−1) = Q (2) + P
n−2 n−2
• Hence, full expansion,
1 1 n − 3 (n−2)
P= Q (1) + Q (2) + P
n−1 n−1 n−2
1
P= Q (1) + Q (2) + . . . + Q (n−1)
n−1
Algorithm
The P can now be simulated as follows
• Generating random integer N equally likely to be either 1, 2, . . . (n − 1)
• If N is such that Q (N ) puts positive weight only on point iN and jN ,
• then we can set X equal to iN , if second random number is less that Qi(N ) ;
N
equal to jN , otherwise,
Step_1 Generate U1 and set N = 1 + [(n − 1)U1 ]
Step_2 Generate U2 and set
iN if U2 < Qi(NN )
X=
jN Otherwise
Example-1
From a set of n elements– numbered 1,2,.....,n– we wish to simulate the
choice of a random SUBSET of size k which can be chosen such that each
of n Ck subsets is equally likely to be chaosen.
Aliter-1 Generate U1 , U2 , ....Un , and then
select the indices of the k smallest (or largest) of the ngenerated values
Example-2
Simulate discrete random variables, which are equally likely to take on
values–
1,2,....n (such a random variable is[nU ] + 1)
Stopping when kdiscrete values have been obtained
This is convenient for small values of k
Example-3
Simulating a random subset of size k is equivalent to simulating a random vector I1 , I2 , .....In
Such that, P {Iii , = I, .... = Iik = 1, Ij = 0 otherwise} = n C1
k
After simulating random vector, we chose our subset of all i for which Ii = 1, as following
k
P {I1 = 1} =
n
k
n−1 if I1 = 0
P {I2 = 1|I1 } = = kn−I1
k −1
−1
n−1 if I1 = 1
X
j −1
k− Ii
i =1
P {Ij = 1|I1 , I2 , .....Ij −1 } = j = 2, ....n
n−j +1
1 if U1 < kn
I1 =
0 otherwise
1 if U2 < kn−
−1
I1
I2 =
0 otherwise
.. ..
. .
k −I1 −I2 −I3 −....Ij −1
1 if Uj < n−j +1
Ij =
0 otherwise
X
k h i 2
Ψ=
• Y Yi /k and E Y = θ, E Y −θ = Var Y
i =1
2
• Y is an estimator of θ; we wish to minimize E Y −θ
X
k h i 2
Ψ=
• Y Yi /k and E Y = θ, E Y −θ = Var Y
i =1
2
• Y is an estimator of θ; we wish to minimize E Y −θ
X
k h i 2
Ψ=
• Y Yi /k and E Y = θ, E Y −θ = Var Y
i =1
2
• Y is an estimator of θ; we wish to minimize E Y −θ
Number of RUNs
• Let use simulation to generate i.i.d. (independent idetically
distributed) Y1 , Y2 , . . . , Yr having mean µ and variance σ2
• Yr = Y (1) +Y (2) ++Y (r )
r , we use Y r as an estimate of µ
h i
• The precision of this estimate is Var (Y r ) = E (Y r − µ)2 = σ2
r
Number of RUNs
• Let use simulation to generate i.i.d. (independent idetically
distributed) Y1 , Y2 , . . . , Yr having mean µ and variance σ2
• Yr = Y (1) +Y (2) ++Y (r )
r , we use Y r as an estimate of µ
h i
• The precision of this estimate is Var (Y r ) = E (Y r − µ)2 = σ2
r
Number of RUNs
• Let use simulation to generate i.i.d. (independent idetically
distributed) Y1 , Y2 , . . . , Yr having mean µ and variance σ2
• Yr = Y (1) +Y (2) ++Y (r )
r , we use Y r as an estimate of µ
h i
• The precision of this estimate is Var (Y r ) = E (Y r − µ)2 = σ2
r
Number of RUNs
2
• We wish to choose r sufficiently large so that σr is acceptably small
• BUT σ2 is not known in advance
• To get around this, we initially simulate k times to evalaute σ2 , and
use simulated Y (1) , Y (2) , . . . Y (k ) to estimate σ2 by sample variance
2
X Y (k ) − Y k
•
k −1
• Based on this estimate, rest of the r − k values can be simulated
Number of RUNs
2
• We wish to choose r sufficiently large so that σr is acceptably small
• BUT σ2 is not known in advance
• To get around this, we initially simulate k times to evalaute σ2 , and
use simulated Y (1) , Y (2) , . . . Y (k ) to estimate σ2 by sample variance
2
X Y (k ) − Y k
•
k −1
• Based on this estimate, rest of the r − k values can be simulated
Number of RUNs
2
• We wish to choose r sufficiently large so that σr is acceptably small
• BUT σ2 is not known in advance
• To get around this, we initially simulate k times to evalaute σ2 , and
use simulated Y (1) , Y (2) , . . . Y (k ) to estimate σ2 by sample variance
2
X Y (k ) − Y k
•
k −1
• Based on this estimate, rest of the r − k values can be simulated
Number of RUNs
2
• We wish to choose r sufficiently large so that σr is acceptably small
• BUT σ2 is not known in advance
• To get around this, we initially simulate k times to evalaute σ2 , and
use simulated Y (1) , Y (2) , . . . Y (k ) to estimate σ2 by sample variance
2
X Y (k ) − Y k
•
k −1
• Based on this estimate, rest of the r − k values can be simulated
Number of RUNs
2
• We wish to choose r sufficiently large so that σr is acceptably small
• BUT σ2 is not known in advance
• To get around this, we initially simulate k times to evalaute σ2 , and
use simulated Y (1) , Y (2) , . . . Y (k ) to estimate σ2 by sample variance
2
X Y (k ) − Y k
•
k −1
• Based on this estimate, rest of the r − k values can be simulated
Number of RUNs
2
• We wish to choose r sufficiently large so that σr is acceptably small
• BUT σ2 is not known in advance
• To get around this, we initially simulate k times to evalaute σ2 , and
use simulated Y (1) , Y (2) , . . . Y (k ) to estimate σ2 by sample variance
2
X Y (k ) − Y k
•
k −1
• Based on this estimate, rest of the r − k values can be simulated