1. What is Monte Carlo Integration?
Monte Carlo Integration is a tools that can be used by researchers to compute integration of complex function. For instance, the Normal ( ) distribution (or called as the cumulative probability distribution of Normal) which is written as the probability of the variable to be less than a certain value (or can be written as ) is hard to be solved analytically as the integrals result to a 'non-elementary' integrals. This can be seen through the following equation
2. When to apply Monte Carlo Integration?
We can use this technique when computation on the value of a complex integrals is needed. For instance, in my master's research, I need to calculate a double integrals which is in the following form.
where variable represents biomarker and variable represents time-to-event. The double integration cannot be solved analytically and other numerical techniques results to a tedious procedure of trial-and-error when setting the initial value. Thus, Monte Carlo Integration techniques came to the rescue.
3. Why it works?
This technique works because it uses random numbers to perform the estimation. In probability theory, if we know that a variable (let's say ) is distributed randomly in a certain fashion such as distribution (i.e: Normal, Weibull, Exponential, Poisson, etc.), then we can calculate what is the expected value of a function by solving the following equation
if is a continuous distribution and
if is a discrete distribution.
In statistics, the can be estimated using its unbiased estimator which is
or in laymen terms is called the average of our 's.
4. How to perform Monte Carlo Integration?
- Need to assume our observation
's is coming from a specific distribution. (Typically a Uniform(0,1) distribution. However, in the following examples, I will use the Normal and Exponential distribution). - Generate a lot of
's. - For each
, if , then compute . If , then assign 0. - Compute the average value.
a. Find the for if is following an Exponential(0.3) distribution.
Solution:
Here, we want to compute the following integral.
If we use Monte Carlo Integration, our answer can be approximated by
where is randomly generated through Exponential distribution.
Using the R script below, we get the estimated answer as 77.13
x_i <- rexp(10000, rate=0.3) # generate x randomly # create indicator function f<- function(x){ if(x <= 10){ return(x^3) } else{ return(0) } } f_xi <- lapply(x_i, f) mean(unlist(f_xi)) # compute the average
## [1] 77.1272
When solving analytically, the question above is solvable with integration by parts (IBP). We will obtain the following result when use the IBP.
b. Find the where is coming from a Normal( ) distribution.
Solution:
Here, we are trying to solve the following equation
Our Monte Carlo estimator can be calculated as follows
where is our indicator function with the following rule
Using the R script below, the estimated result is
x_i <- rnorm(10000, mean=3, sd=0.8) # generate x randomly
# create indicator function
f<- function(x){
if(x >= 5){
return(1)
}
else{
return(0)
}
}
f_xi <- sapply(x_i, f)
mean(unlist(f_xi)) # compute the average
## [1] 0.0061
# comparing with the true value
1 - pnorm(5, mean=3, sd=0.8)
## [1] 0.006209665
c. For 2-dimension problem, it is a little bit complex to show the R script but the following are the steps that we can use to estimate the solution.
Let's say that we need to find the solution to the following integrals,
The steps to use are:
- Assume both
variables are following a Uniform(a,b) and Uniform(c,d) respectively. - Randomly generate a lot of
's and 's. - The answer is approximate as
d. Find area of a circle with radius 2.
Solution:
Since a circle can be divided into four quadrant, it is sufficient for us to just compute the area of a single quadrant before multiply the answer by four.
Recalled that with centre (0,0), a circle can be expressed as
thus we only need to solve the following integrals
To apply our Monte Carlo Integration, we just need to manipulate the above integrals to be multiplied with any desired probability distribution. For this case, we choose Uniform(0,3) & Uniform(0,3) for our and . Let and denote the chosen Uniform distributions, the integrals above is extended into
The final expression simply means that we just need to find the expected value of where & is randomly generated from Uniform distributions. Denote the Uniform(0,3) density function is written as follows
hence the desired integrals is approximated as
We can further reduced the Monte Carlo Integration to be expressed into
The following is the R script used.
x_i <- runif(10000, 0, 3) # generate x randomly
y_i <- runif(10000, 0, 3) # generate y randomly
# create indicator function
f<- function(x,y){
if(x^2 + y^2 <= 2^2){
return(1)
}
else{
return(0)
}
}
result <- matrix(NA,ncol=1, nrow=10000)
for (i in 1:10000){
result[i] <- f(x_i[i],y_i[i])
}
4*(3-0)*(3-0)*mean(result) # 4 * average in a quadrant
## [1] 12.5352
# comparing with the true value
pi*2^2
## [1] 12.56637
5. Conclusion.
We have seen how Monte Carlo Integration works. In particular, I have shown how we can use this numerical technique to solve integration problems which revolved around computation of a complex function. We also have seen how this method can be extended into multiple dimension and how we can use any known distribution to sample our observation before calculating its average (refer example a,b,c). Moreover, if our integrals is not involving any probability function, we still can use this technique by manipulating our integrals to contain a probability function (refer example d).
With the improvement on the speed and efficiency of our computer nowadays, this technique has become more practical to be applied by researchers and professionals in solving their daily problem.
😄😄😄
No comments:
Post a Comment