Monte Carlo Analysis in R

Monte Carlo analysis in R, If there’s one probability trick you should know, it’s how to write a Monte Carlo simulation.

You can write a Monte Carlo simulation if you know how to program, even if only a little.

The basic idea behind a Monte Carlo simulation is to represent your problem and then replicate it randomly until you find an answer.

Let’s get started! We will showcase a few cases in this article.

Principal Component Analysis in R » finnstats

Example 1

We can simply run a simulation that samples 100,000 times from this distribution to observe how many values are between 5 and 10.

runs <- 100000
sims <- rnorm(runs,mean=1,sd=8)
mc <- sum(sims >= 5 & sims <= 10)/runs

The result we get is: 0.17955

Example 2

We flip a coin six times and want to know how likely it is that we will obtain more than three heads.

This is a simple problem for the Binomial distribution, but let’s pretend we forgot or never learned it in the first place.

With a Monte Carlo Simulation, we can easily address this problem.

We’ll replicate 6 coin tosses 100,000 times and observe how often it happens using the typical approach of representing tails with 0 and heads with 1.

Linear optimization using R » Optimal Solution » finnstats

runs <- 100000
trial <- function(){
  sum(sample(c(0,1),6,replace=T)) > 3
}
mc<- sum(replicate(runs,trial()))/runs

mc= 0.34735

Which we may compare to the Binomial distribution function integrated into R.

pbinom(3,6,0.5,lower.tail=FALSE)
0. 34735

Example 3

Consider the following scenario: we’re comparing two web pages to see which one converts more in “newsletter” at a higher rate (this is known as an A/B Test).

On page A, 30 people converted and 100 did not, while on page B, 58 people converted and 125 did not. As seen below, we’ll model this as two Beta distributions.

Although it appears that B is the winner, we’d like to know how likely this is.

Of course, we could use a single-tailed t-test, but it would imply that these are Normal distributions.

Regression Analysis » Aim » Assumptions » Coefficients » finnstats

We can also use a Monte Carlo simulation to solve this!

We’ll take 100,000 samples from A and 100,000 samples from B to see how frequently A is greater than B.

runs <- 100000
a.samples <- rbeta(runs,30,100)
b.samples <- rbeta(runs,58,125)
mc<- sum(a.samples > b.samples)/runs
mc

probability is 0.04601.

You may also like...

Leave a Reply

Your email address will not be published.