Numerical vs. Monte-Carlo Simulations

  • #1
1,367
61
I have an integration that doesn't have a solution in the table of integrals. So, I evaluated it using Mathematica using the command NIntegrate. However, when I compare the result with Monte-Carlo simulations, there is a very small constant gap between the two curves. Is it because of the numerical integration accuracy?
 

Answers and Replies

  • #2
1,367
61
How can I upload an image from my computer?
 
  • #3
Dale
Mentor
Insights Author
2020 Award
30,839
7,439
Usually NIntegrate is pretty reliable. The algorithm selection mechanism is very good at choosing the right algorithm for the particular integrand. I would be much more skeptical of the Monte Carlo algorithm. I would look for errors there first.
 
  • #4
1,367
61
Error like what? In Monte-Carlo simulations, I basically generate random variables, insert them in an expression, and then average the expression over the number of samples to get the average value of the expression.
 
  • #6
Dale
Mentor
Insights Author
2020 Award
30,839
7,439
Could be anything. You could have a bad random number generator, insufficient number of samples, an error in the arithmetic, numerical precision problems , or any number of other mistakes.

The point is that it is hard to find a more well tested numerical integrator than NIntegrate. If your problem can be expressed as an integral and evaluated with NIntegrate then that is your answer, the Monte Carlo is just an approximation with a much less sophisticated process for controlling the errors.
 
  • #7
FactChecker
Science Advisor
Gold Member
6,052
2,336
Your link goes to an integral from 0 to infinity. I would expect the random number generator to never get close to infinity. Could that cause the consistent bias that you are seeing?
 
  • #8
1,367
61
Your link goes to an integral from 0 to infinity. I would expect the random number generator to never get close to infinity. Could that cause the consistent bias that you are seeing?
Is there any relationship between the two? I mean, the integral is supposed to find the expected value of a random variable numerically. So, in Monte-Carlo simulations, I generated a sufficient number of random samples and then averaged them to find the same quantity.
 
  • #9
FactChecker
Science Advisor
Gold Member
6,052
2,336
Is there any relationship between the two? I mean, the integral is supposed to find the expected value of a random variable numerically. So, in Monte-Carlo simulations, I generated a sufficient number of random samples and then averaged them to find the same quantity.
I guess that depends on how you generate random numbers that can go from 0 to infinity. Are you sure that there is not an upper limit to the numbers that you are generating?
 
  • #10
1,367
61
I guess that depends on how you generate random numbers that can go from 0 to infinity. Are you sure that there is not an upper limit to the numbers that you are generating?
The expected value by definition requires an infinity number of samples. But I generated ##10^6## samples, which I think gives close result to infinity, i.e., when I increase from ##10^6## to ##10^7##, the improvement in results is nothing noticeable.
 
  • #11
nrqed
Science Advisor
Homework Helper
Gold Member
3,737
279
The expected value by definition requires an infinity number of samples. But I generated ##10^6## samples, which I think gives close result to infinity, i.e., when I increase from ##10^6## to ##10^7##, the improvement in results is nothing noticeable.
I think that what FactChecker is talking about is the not the number of samples but the range of values sampled by your random generator, i.e. how does one sample fairly all values from 0 to infinity?
 
  • #12
1,367
61
I think that what FactChecker is talking about is the not the number of samples but the range of values sampled by your random generator, i.e. how does one sample fairly all values from 0 to infinity?
How can I know that? I use MATLAB for the random sample generator. Since the random variables are exponential random variables with unity mean I use exprnd(1). I'm not sure how it works, and what range of values it generates. But I used it before for other formulas, and it gave accurate results compared to the numerical ones.
 
  • #13
Dale
Mentor
Insights Author
2020 Award
30,839
7,439
The expected value by definition requires an infinity number of samples. But I generated ##10^6## samples, which I think gives close result to infinity, i.e., when I increase from ##10^6## to ##10^7##, the improvement in results is nothing noticeable.
This is a good thing to check, but it is a check of convergence, not accuracy. Numerical methods are much more subtle than that and require more care if you want high precision.

It is fundamentally impossible to represent a continuous range of real numbers numerically, due to finite precision. It is further impossible to represent an infinite range.

Both of these facts will unavoidably lead to a loss of accuracy and precision in any numerical method. So you need to quantify those and determine how to control them. This is well done in NIntegrate, but not in the Monte Carlo simulation. It appears that the Monte Carlo method converges to an inaccurate number.
 
  • Like
Likes EngWiPy
  • #14
FactChecker
Science Advisor
Gold Member
6,052
2,336
How can I know that? I use MATLAB for the random sample generator. Since the random variables are exponential random variables with unity mean I use exprnd(1). I'm not sure how it works, and what range of values it generates. But I used it before for other formulas, and it gave accurate results compared to the numerical ones.
That's a pretty good answer. I would expect the MATLAB implementation to be good and you have gotten good results from prior use of it.
 
  • Like
Likes EngWiPy
  • #15
1,367
61
Then the question still remains: if NIntegrate is accurate, and the implementation of the random number generator is good, what else could be the problem?!
 
  • #16
ChrisVer
Gold Member
3,373
459
how far away [in standard deviations] are your values?
 
  • #17
1,367
61
how far away [in standard deviations] are your values?
What do you mean? (Excuse me if my question sounded naive)
 
  • #18
Dale
Mentor
Insights Author
2020 Award
30,839
7,439
Then the question still remains: if NIntegrate is accurate, and the implementation of the random number generator is good, what else could be the problem?!
The fact that you have gotten good results from it in other, less demanding, applications does not imply that it is well suited to this application. In particular the numerical errors are not clearly controlled.



How can I know that? I use MATLAB for the random sample generator. Since the random variables are exponential random variables with unity mean I use exprnd(1). I'm not sure how it works, and what range of values it generates. But I used it before for other formulas, and it gave accurate results compared to the numerical ones.
First, you should check the documentation for any known weaknesses. Then generate a large sample and test how much it deviates from an exponential distribution. Then generate a large number of smaller samples to determine if the sampling distribution of the mean is unbiased.
 
  • #19
1,367
61
Thanks for clarifying. I will test it tomorrow when I get to my office. But if the samples generated represent the exponential distribution fairly well, can we rule out the possibility that the error is due to the random samples generator?
 
  • #20
ChrisVer
Gold Member
3,373
459
What do you mean? (Excuse me if my question sounded naive)
I meant when you calculate an integral with numerical or stochastic [MC] methods, the value is not the exact solution [which may not be known], as a result it comes with an error [itex]\int_a^b f(x) dx = I \pm \delta I[/itex]... I don't know how NIntegrate works and stuff, but for MC you get a statistical error for sure to your integral estimate...
Also afterall, MC can still give results off since it's a random method... However what I've seen in some cases is that if you take several results out of the MC [itex]\mu_i[/itex] then their average is pretty close to the expected value [within 1 standard deviation]
 
Last edited:
  • Like
Likes EngWiPy
  • #21
117
43
I think your monte carlo equation is screwed up. You cant get the solution you are seeking unless the exponential equals 0, which cant happen without an imaginary number.
 
  • #22
Hepth
Gold Member
448
39
Test NIntegrate with different methods

NIntegrate[___, Method-> "MonteCarlo"]
"GlobalAdaptive" [default]
"DuffyCoordinates"
"MonteCarlo"
"QuasiMonteCarlo"
"AdaptiveMonteCarlo"
"AdaptiveQuasiMonteCarlo"
"DoubleExponential"

you might have to add MaxPoints-> 10^7

Plain montecarlo seems to do the worst.
 
  • #23
1,367
61
I think your monte carlo equation is screwed up. You cant get the solution you are seeking unless the exponential equals 0, which cant happen without an imaginary number.
I'm sorry. I didn't understand what you mean. Again, why my Monte Carlo simulations is screwed up?
 
  • #24
1,367
61
Test NIntegrate with different methods

NIntegrate[___, Method-> "MonteCarlo"]
"GlobalAdaptive" [default]
"DuffyCoordinates"
"MonteCarlo"
"QuasiMonteCarlo"
"AdaptiveMonteCarlo"
"AdaptiveQuasiMonteCarlo"
"DoubleExponential"

you might have to add MaxPoints-> 10^7

Plain montecarlo seems to do the worst.
I'm doing Monte Carlo simulations on MATLAB. What does the above code do?
 
  • #25
1,367
61
The fact that you have gotten good results from it in other, less demanding, applications does not imply that it is well suited to this application. In particular the numerical errors are not clearly controlled.



First, you should check the documentation for any known weaknesses. Then generate a large sample and test how much it deviates from an exponential distribution. Then generate a large number of smaller samples to determine if the sampling distribution of the mean is unbiased.
I tried to check the generator. But after I draw the exponential distribution for an exponential random variable with mean 1, I didn't know how to generate the exponential random variables and match them to the numerical result. I wrote the following:

Code:
x=0:0.1:5;
y=exp(-x);

ySim=exprnd(1,1,length(x));%This generates an 1-by-length(x) array of exponential random variables of mean 1

plot(x,y,x,ySim);
I got the results attached. I think I didn't do it right, did I?
 

Attachments

Related Threads on Numerical vs. Monte-Carlo Simulations

Replies
8
Views
2K
  • Last Post
Replies
3
Views
924
  • Last Post
Replies
3
Views
27K
Replies
1
Views
6K
Replies
4
Views
61K
Replies
1
Views
977
Replies
3
Views
9K
Replies
4
Views
2K
Replies
3
Views
2K
Top