```{r}
set.seed(5172013)
```
Day 4 Lab Solutions
Monte Carlo integration
Example 1: integral of \(e^{x^4 - x^2}\) over [0,1]
Define an integrand
```{r}
<- function(x){
integrand_function return(exp(x^4 - x^2))
}```
Plot the integrand on the interval [0,1]
```{r}
<- c(0:100) / 100
x_values
plot(x_values, integrand_function(x_values), type = "l", lwd = 2, xlab = "x", ylab = "y", cex.lab = 1.3, col = "red")
text(0.6, 0.95, expression(y == e^{x^4 - x^2}), cex = 2)
```
See ?plotmath
for more info on plotting math in R
Deterministic numerical integration from 0 to 1
Notice that we are passing function integrand_function
as an argument Also the integral can be seen as \(E(e^{U^4 - U^2})\), where \(U \sim Uniform(0, 1)\)
```{r}
<- integrate(integrand_function, lower = 0, upper = 1)
det_int ```
The unction integrate()
returns an object, not a value to find out what is in the object, type names(det_int)
```{r}
names(det_int)
$value
det_int$message
det_int```
[1] "value" "abs.error" "subdivisions" "message" "call"
[1] 0.8785237
[1] "OK"
Monte Carlo integration
By default runif()
defines uniform distribution on [0,1]
```{r}
<- 1000
mcarlo_iterations
<- runif(mcarlo_iterations)
unif_sample ```
artihmetic average
```{r}
mcarlo_int = mean(integrand_function(unif_sample)))
(```
[1] 0.8794199
Monte Carlo error
```{r}
mcarlo_error = sd(integrand_function(unif_sample))/sqrt(mcarlo_iterations))
(```
[1] 0.002431959
Monte Carlo 95% confidence interval
```{r}
c(mcarlo_int - 1.96*mcarlo_error, mcarlo_int + 1.96*mcarlo_error))
(```
[1] 0.8746533 0.8841866
Compare to the deterministic numerical integration
```{r}
(det_int)```
0.8785237 with absolute error < 1e-14
Example 2: second moment of the Beta distribution \(X \sim Beta(\alpha, \beta)\)
```{r}
<- 2
alpha <- 2
beta ```
- theory tells us that \(E(X^2) = \frac{\alpha(\alpha + 1)}{(\alpha + \beta + 1)(\alpha + \beta)}\)
```{r}
<- alpha * (alpha + 1) / ((alpha + beta + 1) * (alpha + beta)))
(beta_2nd_moment_theory ```
[1] 0.3
- deterministic integration
```{r}
<- function(x){
beta_integrand return(x^2 * dbeta(x, shape1 = alpha, shape2 = beta))
}
<- integrate(beta_integrand, lower = 0, upper = 1))
(beta_2nd_moment_det ```
0.3 with absolute error < 3.3e-15
- Monte Carlo integration
```{r}
<- 1000
beta_mcarlo_iterations
<- rbeta(beta_mcarlo_iterations, shape1=alpha, shape2=beta)
beta_sample
<- mean(beta_sample^2))
(beta_2nd_moment_mc ```
[1] 0.298036
Monte Carlo errors
```{r}
<- sd(beta_sample^2) / sqrt(beta_mcarlo_iterations))
(beta_mcarlo_error ```
[1] 0.007272444
Monte Carlo 95% confidence intervals
```{r}
c(
- 1.96 * beta_mcarlo_error,
beta_2nd_moment_mc + 1.96 * beta_mcarlo_error
beta_2nd_moment_mc
)```
[1] 0.2837821 0.3122900
Compare the three answers
```{r}
beta_2nd_moment_theory
beta_2nd_moment_det
beta_2nd_moment_mc
c(
- 1.96 * beta_mcarlo_error,
beta_2nd_moment_mc + 1.96 * beta_mcarlo_error
beta_2nd_moment_mc
)```
[1] 0.3
0.3 with absolute error < 3.3e-15
[1] 0.298036
[1] 0.2837821 0.3122900
Exercise 1
Use:
deterministic
Monte Carlo integration to find the 4th moment of a beta distribution with parameters alpha = 2, beta = 5
Compute Monte Carlo error and confidence intervals of your estimate(s) from part b.
Exercise 1 solution
```{r}
<- 2
alpha <- 5
beta ```
- deterministic integration
```{r}
<- function(x){
beta_integrand return(x^4 * dbeta(x, shape1 = alpha, shape2 = beta))
}
<- integrate(beta_integrand, lower = 0, upper = 1))
(beta_4th_moment_det ```
0.02380952 with absolute error < 2.6e-16
- Monte Carlo integration
```{r}
<- 1000
beta_mcarlo_iterations
<- rbeta(beta_mcarlo_iterations, shape1 = alpha, shape2 = beta)
beta_sample
<- mean(beta_sample^4))
(beta_4th_moment_mc ```
[1] 0.02468678
- Monte Carlo error and 95% confidence intervals
```{r}
<- sd(beta_sample^4) / sqrt(beta_mcarlo_iterations))
(beta_mcarlo_error
c(
- 1.96*beta_mcarlo_error,
beta_4th_moment_mc + 1.96*beta_mcarlo_error
beta_4th_moment_mc
)```
[1] 0.001560208
[1] 0.02162877 0.02774478
Compare the two answers
```{r}
beta_4th_moment_det
beta_4th_moment_mc
c(
- 1.96*beta_mcarlo_error,
beta_4th_moment_mc + 1.96*beta_mcarlo_error
beta_4th_moment_mc
)```
0.02380952 with absolute error < 2.6e-16
[1] 0.02468678
[1] 0.02162877 0.02774478
Exercise 2
Use:
deterministic integration
Monte Carlo integration to approximate integral of \(\exp(-x^5)\) on \([0, \infty]\). Hint: Uniform sampling won’t work, because uniform distribution is not defined on \([0, \infty]\), use exponential distribution samples instead – think about how to write the desired integral in terms of expectation
compute Monte Carlo error and confidence intervals of your estimate(s) from part b
compare Monte Carlo error of the procedure using two different rates of the exponential distribution
Exercise 2 Solutions
Define function
```{r}
<- function(x){
integrand_function2 return(exp(-x^5))
}```
Deterministic integration
```{r}
<- integrate(integrand_function2, lower = 0, upper = Inf))
(det_int2 ```
0.9181687 with absolute error < 1.3e-07
Monte Carlo
```{r}
<- 1000000
mcarlo_iterations2 <- 0.6
exp_rate <- rexp(mcarlo_iterations2, rate = exp_rate)
exp_sample
#artihmetic average
<- mean(integrand_function2(exp_sample) / dexp(exp_sample, rate = exp_rate)))
(mcarlo_int2 ```
[1] 0.916192
Monte Carlo error
```{r}
mcarlo_error2 = sd(integrand_function2(exp_sample) / dexp(exp_sample, rate = exp_rate)) / sqrt(mcarlo_iterations2))
(```
[1] 0.0009449927
Monte Carlo 95% confidence interval
```{r}
c(mcarlo_int2 - 1.96 * mcarlo_error2, mcarlo_int2 + 1.96 * mcarlo_error2)
```
[1] 0.9143398 0.9180442
Compare to the deterministic numerical integration
```{r}
det_int2```
0.9181687 with absolute error < 1.3e-07
Importance sampling
Define a threshold value and number of Monte Carlo samples
```{r}
<- 4.5
my_const <- 1000000
sim_size ```
true probability of interest
```{r}
<- pnorm(my_const, lower.tail = FALSE))
(true_prob ```
[1] 3.397673e-06
naive Monte Carlo estimate
```{r}
<- ifelse(rnorm(sim_size) > my_const, yes = 1, no = 0)
naive_samples <- mean(naive_samples))
(naive_est ```
[1] 0
importance sampling estimate
```{r}
<- rexp(sim_size, rate = 1) + my_const
shift_exp <- dnorm(shift_exp) / exp(-(shift_exp - my_const))
is_samples <- mean(is_samples))
(is_est ```
[1] 3.402526e-06
```{r}
print(true_prob)
print(naive_est)
print(is_est)
```
[1] 3.397673e-06
[1] 0
[1] 3.402526e-06
Computing Monte Carlo variances
```{r}
<- var(naive_samples) / sim_size)
(naive_var_mc <- var(is_samples) / sim_size)
(is_var_mc ```
[1] 0
[1] 1.949041e-17
Computing Monte Carlo confidence intervals
```{r}
# Naive (not very useful, usually variance estimate is degenerate):
c(naive_est - 1.96 * sqrt(naive_var_mc), naive_est + 1.96 * sqrt(naive_var_mc))
# Importance Sampling:
c(is_est - 1.96 * sqrt(is_var_mc), is_est + 1.96 * sqrt(is_var_mc))
```
[1] 0 0
[1] 3.393873e-06 3.411179e-06