Hey guys, in this video I want to go over how to do naive Monte Carlo integration. This is
probably the first type of Monte Carlo random sampling method that you're going to go over
in a Monte Carlo class and let's go ahead and get started with the video. Naive Monte Carlo is used
to estimate an integral where it's difficult to get a closed-form solution for the integral
or it's difficult to get an analytical solution. For me every time I had to do naive Monte Carlo,
it was always for calculating the integral of an expectation. For me, it was always I had to
estimate a mean, which could also be considered the expectation of some function h of X where X
is a random variable that's distributed pi of X and so this is equal to the integral of h of X
pi of X dx. And so what we want to solve is this integral, however in the examples where you use
naive Monte Carlo, finding the exact solution for this integral will be difficult to do
analytically, so what you're going to do is you're going to estimate it using naive Monte Carlo. The
algorithm for naive Monte Carlo is as follows. It has two steps. First you're going to draw IID
samples, which I will call X1 to Xm. These are m samples from your target distribution pi of X.
Then you're going to estimate mu by taking the sample mean. The sample mean is going to look
like one over m of h of X1 plus h of X2 plus ... h of Xm. This is basically just the
sample mean and then that will give you your final solution. In this video, I'm going to go
over this example problem, which says to use the naive Monte Carlo method to estimate the expected value
of e to the X squared where X is a random variable that has a uniform 0, 1 distribution. It says to
describe the algorithm using naive Monte Carlo based on 1000 samples and then to estimate
the standard error of that estimate as well. I will go over the algorithm and then I will
show how to implement that algorithm in R in this video. For this problem, we want to estimate the
expected value of e to the X squared. Our h of X, the function of the expected value, is e to the
X squared and we're given in the problem to use 1,000 samples. So m, our sample size, is equal to 1000.
The algorithm is just a simple two steps. For the first step, we want to draw 1,000 IID samples
from the target distribution, which we're given is uniform 0, 1. I'm going to call my samples U1
to U1000 from uniform 0,1. Then in the second step, the naive Monte Carlo estimate of the mean
or expected value is basically just going to be a sample mean. So that's going to be 1 over 1000 and
then it's going to be e to the U1 squared plus e to the U2 squared plus ... e to the U1000
squared. This is how you verbally write out the algorithm and that's going to give you
the estimate of the expected value of e to the X squared and the problem also asked to calculate
or estimate the standard error of the estimate as well. The standard error is just going to
be equal to the estimated standard deviation divided by the square root of the sample size,
which is m. This is going to be equal to the square root of the estimated variance of e to
the X squared divided by 1000. And so next I will show how you can calculate the exact values in R.
Finally in the last part of the video, I'm going to go over a short R demo of how you can calculate
the naive Monte Carlo estimate of an integral. For this problem, the first line of code is
I'm just setting the seed. Because we have to do random sampling, if you want to get the same exact
solution as I'm getting in this video, you just want to set seed. I just set that to a random
number. Then in the first line of code, I'm going to perform the first step of naive Monte Carlo,
which is calculating the random samples from the target distribution. For this problem,
the target distribution was uniform 0,1 and I'm also calculating 1,000 random samples. So here I say
to calculate 1,000 random samples from the uniform 0,1 distribution and I save that as a vector
named a. So I get a vector with a thousand uniform 0,1 results after I run that line of code
and then the second line of code is what's going to actually give me the numeric solution. So the
naive Monte Carlo estimate of the expected value of e to the X squared is going to be calculated by
taking the mean of exponential a squared, where a is the vector of uniform 0,1 values. And so
the actual solution is going to be 1.46. This is our expected solution for the integral using naive
Monte Carlo. And then the final part of the problem also asked for the standard error. So we said that
we can calculate that as the square root of the estimated variance divided by the square root
of the sample size. That's the same as calculating the standard deviation of e to the a
squared divided by the square root of 1,000. When you calculate that, our standard error is
.015. These are the numeric solutions using naive Monte Carlo and that's it for this video.
*chiptune music*