Lec26 RandomVariableGeneration
Lec26 RandomVariableGeneration
Generation
Prof. Nicholas Zabaras
Email: [email protected]
URL: https://www.zabaras.com/
October 8, 2020
Following closely:
C. Robert, G. Casella, Monte Carlo Statistical Methods (Ch.. 1, 2, 3.1, & 3.2) (google books, slides, video)
J. S. Liu, MC Strategies in Scientific Computing (Chapters 1 & 2)
J-M Marin and C. P. Robert, Bayesian Core (Chapter 2)
Statistical Computing & Monte Carlo Methods, A. Doucet (course notes, 2007)
Statistical Computing and Machine Learning, Fall 2020, N. Zabaras 2
Goals
The goals for today’s lecture include:
f ( x ) f ( x ) ( x )dx
N
1
f ( x )
N
f (x ), x
i 1
i i i.i.d .
or more precisely:
𝑉𝑎𝑟𝜋 𝑓
𝔼𝑿 𝔼𝜋ෝ 𝑁 𝑓 = 𝔼𝜋 𝑓 𝑎𝑛𝑑 𝑉𝑎𝑟𝑿 𝔼𝜋ෝ 𝑁 𝑓 =
𝑁
( X 1) 1/ 6, ( X 2) 2 / 6, ( X 3) 1/ 2
Define the cdf of 𝑋 for 𝑥 ∈ [0, 3] as
3
FX ( x) ( X i ) (i x)
i 1
0.2
k
Pk p j [0,1], P0 0, P1 p1 , P 1
0.15
0.1
j 1
0.05
Obviously 0
1 2 3 4 5
Pk Pr X k
𝑃2 = 𝑝1 + 𝑝2 = Pr 𝑋 ≤ 2
Set 𝑥 = 𝑘, where 𝑃1 ≤ 𝑢 ≤ 𝑃2
𝑃2
MatLab Implementation
Statistical Computing and Machine Learning, Fall 2020, N. Zabaras 10
Sampling from a Discrete Distribution
Consider a discrete random variable 𝑋 taking values ሼ1,2,3, … , ∞} with
probability Pr X k p , with p 1 k i 1
i
Draw 𝑢~𝒰[0,1].
Pk
Note : Pr x k Pr Pk 1 u Pk 1du 𝑃2
Pk 1 𝑢 𝑃1 𝑝2
k k 1 𝑝1
Pk Pk 1 p j p j pk , i.e. the desired distribution
j 1 j 1
Indeed : Pr x y Pr u FX ( y )
FX ( y )
0
1du FX ( y )
Pr X x Pr log U l x Pr U e l x 1 e l x X ~ Exp (l )
2.5
1.5
0.5
MatLab Implementation
0
1 2 3 4
Statistical Computing and Machine Learning, Fall 2020, N. Zabaras 16
Inverse Method: The Max Function
Consider 𝑁 i.i.d. random variables 𝑋𝑖 ~𝑓𝑋 (with CDF 𝐹𝑋). We are interested to
sample from the following distribution:
Z max X 1 ,..., X N
You can use an inverse approach as follows:
FZ ( z ) Pr X 1 z ,..., X N z
N
Pr X i z FX ( z )
N
i 1
Z FZ1 (U ) FX1 (U 1/ N )
Limited to cases where the inverse cdf has an analytical form that can be
tabulated.
Xi
Y i 1
~ Be ( , )
i 1
Xi
log U i
Y i 1
~ Be ( , )
i 1
log U i
With this approach, we cannot generate Gamma random variables with a non-
integer shape parameter 𝑎.
z ~ N 0, 2 1
X 1 1 1 z1
X 2 2 2 z1 z2 1 2
3) 𝑋1 and 𝑋2 will have means 𝜇1 and 𝜇2, standard deviations 𝜎1 and 𝜎2, and correlation 𝜌
Example 2. data are sampled from the bivariate normal distribution with
Sample 𝒖~𝒰[0,1]𝑑
1
Set x FX (u)
For the multivariate N ( , ) , we can apply the following technique:
Draw 𝑧𝑖~𝒩(0,1ሻ i.i.d.
Introduce the Cholesky decomposition of : SS T
Set x = + Sz
Indeed : x - x - = Szz T S T
T
S zz T S T SS T
Exact pdf PDF with bivariate samples PDF with univariate samples
y
Set 𝑖 = 1 Accept
Repeat until 𝑖 = 𝑁
Sample y ~ q( y ) and u ~ U(0,1)
If u M' *( y)
q *( y )
then accept (set 𝑥 (𝑖ሻ = 𝑦) and increment the
counter 𝑖
Otherwise, reject
X *( y)dy
Note : From Pr[Y accepted ] large M ' leads to very low acceptance
M ' q *( y )dy
X
Statistical Computing and Machine Learning, Fall 2020, N. Zabaras 32
Rejection Sampling
We can alternatively show that Pr Y x | Y accepted Pr X x x X
xπ *( y )
q ( y )dy =
π *( y )dy
M'q *( y )
M' q *( y )dy
X
*( y)dy
Using Bayes rule and Pr[Y accepted ] X
M ' q *( y )dy
X
x
π *( y )dy
M' q *( y )dy
Pr(Y ≤ x and Y accepted) x
Pr(Y ≤ x | Y accepted) = X
( y ) dy
X
Pr(Y accepted)
π *( y )dy
M' q *( y )dy
X
Statistical Computing and Machine Learning, Fall 2020, N. Zabaras 33
Accept/Reject Efficiency
The acceptance probability Pr(𝑌 𝑎𝑐𝑐𝑒𝑝𝑡𝑒𝑑ሻ is a measure of efficiency.
This is important as we will see later when discussing the Metropolis Hastings
algorithm.
X *( y)dy
Pr(Y is accepted )
M ' q *( y )dy
X
The mean of the geometric distribution is 1/𝛾, thus the number of trials before
success is thus an unbiased estimate of 1/Pr(𝑌 𝑖𝑠 𝑎𝑐𝑐𝑒𝑝𝑡𝑒𝑑ሻ.
x 0,1 1
1.5 2.5
1 2
0 1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
0.5
For a C++ implementation of
this example see here. 0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
also available.
Statistical Computing and Machine Learning, Fall 2020, N. Zabaras 36
Example: Accept-Reject Sampling
In this example, the target and proposal distributions are:
* ( x) exp x 2 / 2 sin 2 (6 x) 3cos 2 ( x) sin 2 (4 x) 1
q* ( x) exp x 2 / 2
q ( x) exp x / 2
* 2