Introducing Monte Carlo Methods With R
Introducing Monte Carlo Methods With R
net/publication/41222616
CITATIONS READS
310 13,193
All content following this page was uploaded by Christian P. Robert on 14 March 2014.
Based on
This Chapter
◮ We introduce the programming language R
◮ Input and output, data structures, and basic programming commands
◮ The material is both crucial and unavoidably sketchy
Monte Carlo Methods with R: Basic R Programming [3]
Basic R Programming
Introduction
Basic R Programming
Why R ?
◮ There exist other languages, most (all?) of them faster than R, like Matlab, and
even free, like C or Python.
Basic R Programming
Why R ?
Basic R Programming
Getting started
Basic R Programming
R objects
Basic R Programming
Interpreted
Basic R Programming
More vector class
Basic R Programming
Even more vector class
Basic R Programming
Comments on the vector class
Basic R Programming
The matrix, array, and factor classes
Basic R Programming
Some matrix commands
Basic R Programming
The list and data.frame classes
The Last One
◮ R code
Monte Carlo Methods with R: Basic R Programming [16]
Probability distributions in R
data: x
t = -0.8168, df = 24, p-value = 0.4220
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
-0.4915103 0.2127705
sample estimates:
mean of x
-0.1393699
Monte Carlo Methods with R: Basic R Programming [18]
◮ Correlation
> attach(faithful) #resident dataset
> cor.test(faithful[,1],faithful[,2])
◮ Fitting a binomial (logistic) glm to the probability of suffering from diabetes for
a woman within the Pima Indian population
> glm(formula = type ~ bmi + age, family = "binomial", data = Pima.tr)
Deviance Residuals:
Min 1Q Median 3Q Max
-1.7935 -0.8368 -0.5033 1.0211 2.2531
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -6.49870 1.17459 -5.533 3.15e-08 ***
bmi 0.10519 0.02956 3.558 0.000373 ***
age 0.07104 0.01538 4.620 3.84e-06 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
◮ Concluding with the significance both of the body mass index bmi and the age
◮ Other generalized linear models can be defined by using a different family value
> glm(y ~x, family=quasi(var="mu^2", link="log"))
⊲ Quasi-Likelihood also
◮ Many many other procedures
⊲ Time series, anova,...
◮ One last one
Monte Carlo Methods with R: Basic R Programming [22]
◮ The bootstrap procedure uses the empirical distribution as a substitute for the
true distribution to construct variance estimates and confidence intervals.
⊲ A sample X1, . . . , Xn is resampled with replacement
⊲ The empirical distribution has a finite but large support made of nn points
◮ For example, with data y, we can create a bootstrap sample y ∗ using the code
> ystar=sample(y,replace=T)
⊲ For each resample, we can calculate a mean, variance, etc
Monte Carlo Methods with R: Basic R Programming [23]
◮ R code
0.0
Bootstrap
x Means
Monte Carlo Methods with R: Basic R Programming [24]
◮ Linear regression
Yij = α + βxi + εij ,
α and β are the unknown intercept and slope, εij are the iid normal errors
◮ The residuals from the least squares fit are given by
ε̂ij = yij − α̂ − β̂xi,
⊲ We bootstrap the residuals
⊲ Produce a new sample (ε̂∗ij )ij by resampling from the ε̂ij ’s
⊲ The bootstrap samples are then yij∗ = yij + ε̂∗ij
Monte Carlo Methods with R: Basic R Programming [25]
250
200
200
150
Frequency
Frequency
150
◮ Histogram of 2000 bootstrap samples
100
100 ◮ R code
50
50
0
Intercept Slope
Monte Carlo Methods with R: Basic R Programming [26]
Basic R Programming
Some Other Stuff
◮ Graphical facilities
⊲ Can do a lot; see plot and par
◮ Writing new R functions
⊲ h=function(x)(sin(x)^2+cos(x)^3)^(3/2)
⊲ We will do this a lot
◮ Input and output in R
⊲ write.table, read.table, scan
◮ Don’t forget the mcsm package
Monte Carlo Methods with R: Random Variable Generation [27]
This Chapter
◮ We present practical techniques that can produce random variables
◮ From both standard and nonstandard distributions
◮ First: Transformation methods
◮ Next: Indirect Methods - Accept–Reject
Monte Carlo Methods with R: Random Variable Generation [28]
Introduction
◮ We are not concerned with the details of producing uniform random variables
Introduction
Using the R Generators
R has a large number of functions that will generate the standard random variables
> rgamma(3,2.5,4.5)
produces three independent generations from a G(5/2, 9/2) distribution
◮ It is therefore,
⊲ Counter-productive
⊲ Inefficient
⊲ And even dangerous,
◮ To generate from those standard distributions
◮ If it is built into R , use it
◮ But....we will practice on these.
◮ The principles are essential to deal with distributions that are not built into R.
Monte Carlo Methods with R: Random Variable Generation [30]
Uniform Simulation
◮ The other optional parameters are min and max, with R code
Uniform Simulation
Checking the Generator
Uniform Simulation
Plots from the Generator
Histogram of x
120
1.0
100
0.8
80
0.6
Frequency
60
x2
0.4
40
0.2
20
0.0
0
x x1
Uniform Simulation
Some Comments
◮ For example, if X has density f and cdf F , then we have the relation
Z x
F (x) = f (t) dt,
−∞
and we set U = F (X) and solve for X
◮ Example 2.1
⊲ If X ∼ Exp(1), then F (x) = 1 − e−x
⊲ Solving for x in u = 1 − e−x gives x = − log(1 − u)
Monte Carlo Methods with R: Random Variable Generation [35]
Generating Exponentials
1 e−(x−µ)/β 1
◮ Logistic pdf: f (x) = β [1+e−(x−µ)/β ]2 , cdf: F (x) = 1+e−(x−µ)/β
.
1 1
◮ Cauchy pdf: f (x) = πσ 1+ x−µ 2 , cdf: F (x) = 21 + π1 arctan((x − µ)/σ).
( σ )
Monte Carlo Methods with R: Random Variable Generation [37]
◮ For example, to generate χ26 random variables, we could use the R code
> U=runif(3*10^4)
> U=matrix(data=U,nrow=3) #matrix for sums
> X=-log(U) #uniform to exponential
> X=2* apply(X,2,sum) #sum up to get chi squares
◮ Not nearly as efficient as calling rchisq, as can be checked by the R code
> system.time(test1());system.time(test2())
user system elapsed
0.104 0.000 0.107
user system elapsed
0.004 0.000 0.004
◮ test1 corresponds to the R code above
◮ test2 corresponds to X=rchisq(10^4,df=6)
Monte Carlo Methods with R: Random Variable Generation [39]
◮ These transformations are quite simple and will be used in our illustrations.
Discrete Distributions
Discrete Distributions
Binomial
Discrete Distributions
Comments
Discrete Distributions
Poisson R Code
◮ R code that can be used to generate Poisson random variables for large values
of lambda.
◮ The sequence t contains the integer values in the range around the mean.
> Nsim=10^4; lambda=100
> spread=3*sqrt(lambda)
> t=round(seq(max(0,lambda-spread),lambda+spread,1))
> prob=ppois(t, lambda)
> X=rep(0,Nsim)
> for (i in 1:Nsim){
+ u=runif(1)
+ X[i]=t[1]+sum(prob<u)-1 }
◮ The last line of the program checks to see what interval the uniform random
variable fell in and assigns the correct Poisson value to X.
Monte Carlo Methods with R: Random Variable Generation [46]
Discrete Distributions
Comments
◮ Another remedy is to start the cumulative probabilities at the mode of the dis-
crete distribution
◮ Then explore neighboring values until the cumulative probability is almost 1.
◮ Specific algorithms exist for almost any distribution and are often quite fast.
◮ So, if R has it, use it.
◮ But R does not handle every distribution that we will need,
Monte Carlo Methods with R: Random Variable Generation [47]
Mixture Representations
Mixture Representations
Generating the Mixture
◮ Continuous
Z
f (x) = g(x|y)p(y) dy ⇒ y ∼ p(y) and X ∼ f (x|y), then X ∼ f (x)
Y
◮ Discrete
X
f (x) = pi fi(x) ⇒ i ∼ pi and X ∼ fi(x), then X ∼ f (x)
i∈Y
Mixture Representations
Continuous Mixtures
0.06
0.05
0.04
◮ If X is negative binomial X ∼ N eg(n, p)
Density
0.03
⊲ X|y ∼ P(y) and Y ∼ G(n, β),
0.02
⊲ R code generates from this mixture
0.01
0.00
0 10 20 30 40 50
x
Monte Carlo Methods with R: Random Variable Generation [50]
Accept–Reject Methods
Introduction
◮ Accept–Reject Methods
⊲ Only require the functional form of the density f of interest
⊲ f = target, g=candidate
◮ Where it is simpler to simulate random variables from g
Monte Carlo Methods with R: Random Variable Generation [51]
Accept–Reject Methods
Accept–Reject Algorithm
1
◮ P ( Accept ) = M
, Expected Waiting Time = M
Monte Carlo Methods with R: Random Variable Generation [52]
Accept–Reject Algorithm
R Implementation
Accept–Reject Algorithm
Normals from Double Exponentials
◮ Candidate Y ∼ 21 exp(−|y|)
◮ Target X ∼ √1 exp(−x2/2)
2π
√1 exp(−y 2 /2)
2π 2
1 ≤ √ exp(1/2)
2 exp(−|y|) 2π
⊲ Maximum at y = 1
◮ Look at R code
Monte Carlo Methods with R: Random Variable Generation [54]
Accept–Reject Algorithm
Theory
Accept–Reject Algorithm
Betas from Uniforms
2.5
250
2.0
200
Frequency
1.5
Density
150
1.0
100
0.5
50
0.0
0
0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.2 0.4 0.6 0.8 1.0
v Y
Accept–Reject Algorithm
Betas from Betas
3.0
2.5
2.5
2.0
2.0
Density
Density
1.5
1.5
1.0
1.0
0.5
0.5
0.0
0.0
0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.2 0.4 0.6 0.8 1.0
v Y
Accept–Reject Algorithm
Betas from Betas-Details
y a(1 − y)b a − a1
maximized at y =
y a1 (1 − y)b1 a − a 1 + b − b1
Accept–Reject Algorithm
Comments
This Chapter
◮ This chapter introduces the major concepts of Monte Carlo methods
◮ The validity of Monte Carlo approximations relies on the Law of Large Numbers
◮ The versatility of the representation of an integral as an expectation
Monte Carlo Methods with R: Monte Carlo Integration [60]
◮ The Convergence
Xn Z
1
hn = h(xj ) → h(x) f (x) dx = Ef [h(X)]
n j=1 X
◮ Monitoring Convergence
◮ R code
Monte Carlo Methods with R: Monte Carlo Integration [64]
◮ They are Confidence Intervals were you to stop at a chosen number of iterations
Monte Carlo Methods with R: Monte Carlo Integration [65]
◮ If vn does not converge, converges too slowly, a CLT may not apply
Monte Carlo Methods with R: Monte Carlo Integration [66]
◮ Normal Probability
n Z
1X t
1 2
Φ̂(t) = Ixi≤t → Φ(t) = √ e−y /2dy
n i=1 −∞ 2π
Importance Sampling
Introduction
⊲ Sound Familiar?
Monte Carlo Methods with R: Monte Carlo Integration [68]
Importance Sampling
Introduction
◮ So n
1 X f (Xj )
h(Xj ) → Ef [h(X)]
n j=1 g(Xj )
◮ As long as
⊲ Var (h(X)f (X)/g(X)) < ∞
⊲ supp(g) ⊃ supp(h × f )
Monte Carlo Methods with R: Monte Carlo Integration [69]
Importance Sampling
Revisiting Normal Tail Probabilities
Importance Sampling
Normal Tail Variables
◮ The Importance sampler does not give us a sample ⇒ Can use Accept–Reject
◮ Sample Z ∼ N (0, 1), Z > a ⇒ Use Exponential Candidate
√1 exp(−.5x2)
2π 1 1
= √ exp(−.5x2 + x + a) ≤ √ exp(−.5a∗2 + a∗ + a)
exp(−(x − a)) 2π 2π
⊲ Where a∗ = max{a, 1}
◮ Normals > 20
◮ The Twilight Zone
◮ R code
Monte Carlo Methods with R: Monte Carlo Integration [71]
Importance Sampling
Comments
Importance Sampling
Easy Model - Difficult Distribution
Importance Sampling
Easy Model - Difficult Distribution -2
◮ Contour Plot
◮ Suggest a candidate?
◮ R code
Monte Carlo Methods with R: Monte Carlo Integration [74]
Importance Sampling
Easy Model - Difficult Distribution – 3
Importance Sampling
Easy Model - Difficult Distribution – Posterior Means
where
n oλ+1
Γ(α+β)
⊲ π(α, β|x) ∝ Γ(α)Γ(β)
[xx0]α [(1 − x)y0]β
⊲ g(α, β) = T (3, µ, Σ)
Importance Sampling
Probit Analysis
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -0.44957 0.09497 -4.734 2.20e-06 ***
x 0.06479 0.01615 4.011 6.05e-05 ***
---
Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
So BMI has a significant impact on the possible presence of diabetes.
Monte Carlo Methods with R: Monte Carlo Integration [77]
Importance Sampling
Bayesian Probit Analysis
◮ From a Bayesian perspective, we use a vague prior
⊲ β = (β1, β2) , each having a N (0, 100) distribution
◮ With Φ the normal cdf, the posterior is proportional to
n
Y β 2 +β 2
1 2
yi 1−yi − 2×100
[Φ(β1 + (xi − x̄)β2] [Φ(−β1 − (xi − x̄)β2] ×e
i=1
Importance Sampling
Probit Analysis Importance Weights
This Chapter
◮ Two uses of computer-generated random variables to solve optimization problems.
◮ The first use is to produce stochastic search techniques
⊲ To reach the maximum (or minimum) of a function
⊲ Avoid being trapped in local maxima (or minima)
⊲ Are sufficiently attracted by the global maximum (or minimum).
◮ The second use of simulation is to approximate the function to be optimized.