## Introduction

Often in probability we are interested in finding the probability that a possibly complicated event occurs. For the most part, we’ll spend our time in this class computing such probabilities by analytical (ie. by hand) techniques. However, there is a deep, equally interesting side of probability which is interested in numerical (ie. by computer) estimation of probabilities or related quantities, known generally as Monte Carlo simulation.

## Examples

### Rolling a die

Suppose we roll a fair die. Here is a simulation of one trial of this experiment:

trial <- sample(1:6, size = 1)
print(trial)
## [1] 3

Next, we can check whether our event (getting a sum of 6) occurred. We print 1 if it did and 0 otherwise:

if (trial == 6)
print(1) else
print(0)
## [1] 0

Finally, we can repeat or replicate the trial and check steps many times and take an average:

n <- 1e6   ### number of iterations
simlist <- numeric(n)

# Replicate
for (i in 1:n)
{
# Simulate
trial <- sample(1:6, size = 1)
# Check for success
success <- if (trial == 6) 1 else 0
simlist[i] <- success
}

### Estimated result
mean(simlist)
## [1] 0.166981

### Summing two dice

Suppose we’re interested in finding the probability of getting a sum of 6 after rolling a pair of dice. Here is a simulation of one trial of the experiment:

trial <- sample(1:6, size = 2, replace = TRUE)
print(trial)
## [1] 2 5
sum(trial)
## [1] 7

Next, we can check whether our event (getting a sum of 6) occurred in this trial:

if (sum(trial) == 6)
print(1) else
print(0)
## [1] 0

Finally, we can repeat or replicate the trial and check steps many times and take an average:

n <- 1e4   ### number of iterations
simlist <- numeric(n)

# Replicate
for (i in 1:n)
{
# Simulate
trial <- sample(1:6, size = 2, replace = TRUE)
# Check for success
success <- if (sum(trial) == 6) 1 else 0
simlist[i] <- success
}

### Estimated result
mean(simlist)
## [1] 0.138
### Exact result
5/36
## [1] 0.1388889

## Summary

Here is a breakdown of the Monte Carlo simulation approach of numerically approximating $$P(A)$$ the probability that event $$A$$ occurs:

1. Simulate a trial of the experiment.
2. Check whether $$A$$ occurred in the trial.
3. Replicate the trial and check steps many times.

## Questions

For the following questions, use commands similar to those above to investigate the following random experiment: toss a fair coin 4 times.

1. Simulate one trial of the experiment.
2. Check whether the following event, which we’ll call $$A$$, occurred: a total of 2 heads came up in the 4 tosses.
3. Replicate the trial and check steps to estimate the probability of $$A$$.