Statistical Computing and Lab - Note2
Statistical Computing and Lab - Note2
Gyuhyeong Goh
Department of Statistics
Kyungpook National University
1
Introduction
2
Sampling from a finite population
3
Random Generators of Common Probability Distributions
1 > n<-3
2 > rbeta(n,shape1=2,shape2=2)
3 [1] 0.9840114 0.4127489 0.2268932
4 > rbinom(n,size=5,prob=0.5)
5 [1] 1 1 3
6 > rchisq(n,df=2)
7 [1] 0.1987044 1.1532773 0.3874872
8 > rgamma(n,shape=3,rate=1)
9 [1] 2.7883450 0.6982289 2.3801886
0 > rnorm(n,mean=1,sd=2)
1 [1] 0.3269119 4.1151027 -0.1809909
2 > rpois(n,lambda=5)
3 [1] 3 8 3
4 > runif(n,min=0,max=2)
5 [1] 0.4537955 0.9852103 1.3223165
4
The Inverse Transform Method
This implies that if U ∼ Uniform(0, 1), then FX−1 (U) has the same
distribution as X .
5
Inverse Transform Method, Continuous Case
6
Example
Inverse transform method, continuous case
Our goal is to generate a random sample with pdf fX (x) = 3x 2 , 0 < x < 1.
Rx
Note that FX (x) = 0 3t 2 dt = x 3 and FX−1 (u) = u 1/3 .
Then, the R code can be written as follows:
7
Example (cont.)
Inverse transform method, continuous case
f(x) = 3x2
2.5
2.0
1.5
Density
1.0
0.5
0.0
8
Inverse Transform Method, Discrete Case
x1 < x2 < · · · .
9
Example: Bernoulli distribution
Inverse Transform Method, Discrete Case
10
Example: Geometric distribution
Inverse Transform Method, Discrete Case
log(1 − u)
1 − q xi < u ≤ 1 − q xi +1 ⇔ xi < ≤ xi + 1.
log(q)
11
The Acceptance-Rejection (AR) Method
Let Y be a random variable with pdf (or pmf) g (y ) such that f (t)/g (t) ≤ c
for some constant c.
The AR method proceeds as follows:
1. Generate y from the distribution with g .
2. Generate u from from Uniform(0, 1).
3. If u < f (y )/{cg (y )}, then accept y . i.e., x = y ;
otherwise reject y go to Step 1.
12
Example: Beta(2,2)
AR method
Let X ∼ Beta(2, 2). Then, the pdf is f (x) = 6x(1 − x), 0 < x < 1.
Note that
f (x) 6x(1 − x)
= ≤ 6 ≡ c.
g (x) 1
f (y )
Hence, we accept if u < cg (y ) = y (1 − y ).
1 n <- 1000
2 x <- rep(0,n) #storage for generated x-values
3 k <- 0 #count of accepted samples
4 while (k < n){
5 u <- runif(1)
6 y <- runif(1) #random variate from g
7 if (y * (1-y) > u) { #we accept y if the condition is true
8 k <- k + 1
9 x[k] <- y
0 }
1 }
13
Example: Beta(2,2) (cont.)
AR method
1.0
●
●●●
●●●
●
●
●
●●
●●●●●
●
●●
●
●
●
●●
●●
●
●●
●
●●
●
●
●●
●
●
●●
●
●
●●
●
●●
●
●●
●
●
●●
●
●●
●
●●
●
●●
●
●●
●●
●
●
●●
●
●●
●
●
●●
●
●●
●
●
●●
0.8
●
●
●●
●
●
●●
●
●
●●
●
●
●
●●
●
●
●
●
●●
●
●
●
●●
●
●
●
●
●●
●
●
●●
●
●
●●
●
●
●
●
●
●●
●
●
●
●●
●
●
●
●
●
●
●●
●
●●
●●
●●
●
●
●●
●
●●
●
●
●●
●
●
●
●●
●
●
●
●●
●
●●
●●
●
●●
●
●●
●●
●
●
●
●●
●
●●
●
●
●
●●
●
●●
●
●
●
●●
●●
●
●
●
●
●●
●
●●
●
●
0.6
●
●●
●
●
●
●●
●●
●
●●
●
●●
●
●●
●
●●
●
●
●
●●
●
●
●
●●
●
●●
●
●
●●
●
●●
●
●
●
Sample
●●
●
●●
●
●●
●
●●
●
●
●
●●
●
●
●
●
●
●●
●
●
●
●
●●
●
●
●●
●
●
●
●●
●
●●
●
●
●●
●
●●
●
●
●
●●
●
●
●●
●
●●
●
●
●●
●
●
●●
●
●●
●
●
●
●
●●
●
●
●
●●
●
●
●
●
●
●
●●
●
●
●
●●
●
●●
●
●
●●
●
●●
●
●●
0.4
●
●
●●
●
●●
●
●●
●
●
●●
●
●●
●
●
●●
●
●●
●
●●
●
●
●●
●●
●
●●
●●
●
●
●●
●
●●
●
●
●●
●
●
●
●●
●
●
●●
●
●●
●
●
●
●●
●
●●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●●
●
●●
●
●
●
●●
●●
●
●●
●
●●
●
●●
●
●●
●●
●
●●
●●
●●
●
●●
●
●
●
●●
●●
●
●
●
●●
●
●
●
●
●
●●
0.2
●●
●
●
●●
●
●
●
●
●●
●
●
●
●
●
●
●
●●
●
●
●
●●
●
●
●●
●●
●
●
●●
●
●●
●
●
●
●
●●
●
●●
●
●
●
●
●
●●
●
●●
●
●
●
●●
●
●●
●
●●
●
●
●●
●
●
●
●●
●●
●
●
●
●●
●
●●
●
●
●●
●
●
●
●
●
●
●●
●●
●
●●
●●
●
●●●
●●●
●●
●●
●
0.0
Beta(2, 2)
14
Remark on AR
15
Exercise 1
16
Exercise 2
17
Exercise 3
18