- #1

- 1,368

- 61

You are using an out of date browser. It may not display this or other websites correctly.

You should upgrade or use an alternative browser.

You should upgrade or use an alternative browser.

- Mathematica
- Thread starter EngWiPy
- Start date

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.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.f

- #1

- 1,368

- 61

MATLAB, Maple, Mathematica, LaTeX News on Phys.org

- #2

- 1,368

- 61

How can I upload an image from my computer?

- #3

Mentor

- 34,504

- 12,490

- #4

- 1,368

- 61

- #5

- #6

Mentor

- 34,504

- 12,490

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

Science Advisor

Homework Helper

Gold Member

- 8,091

- 3,726

- #8

- 1,368

- 61

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

Science Advisor

Homework Helper

Gold Member

- 8,091

- 3,726

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?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.

- #10

- 1,368

- 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

Science Advisor

Homework Helper

Gold Member

- 3,766

- 295

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?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.

- #12

- 1,368

- 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

Mentor

- 34,504

- 12,490

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.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.

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.

- #14

Science Advisor

Homework Helper

Gold Member

- 8,091

- 3,726

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.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.

- #15

- 1,368

- 61

- #16

Gold Member

- 3,381

- 463

how far away [in standard deviations] are your values?

- #17

- 1,368

- 61

how far away [in standard deviations] are your values?

What do you mean? (Excuse me if my question sounded naive)

- #18

Mentor

- 34,504

- 12,490

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.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.

- #19

- 1,368

- 61

- #20

Gold Member

- 3,381

- 463

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...What do you mean? (Excuse me if my question sounded naive)

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:

- #21

- 117

- 43

- #22

Gold Member

- 463

- 39

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,368

- 61

I'm sorry. I didn't understand what you mean. Again, why my Monte Carlo simulations is screwed up?

- #24

- 1,368

- 61

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,368

- 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?

- #26

Gold Member

- 3,381

- 463

- #27

- 1,368

- 61

What would the range be in my case? and how will this verify the exprnd generator which generates exponential random variables I use in my simulations? I don't generate random variables and pass them to an exponential function.

- #28

Mentor

- 34,504

- 12,490

You will want to plot the random data using either a histogram or a qq plot.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?

The histogram will probably look pretty good, but the qq plot will be more sensitive to deviations.

- #29

- 1,368

- 61

You will want to plot the random data using either a histogram or a qq plot.

The histogram will probably look pretty good, but the qq plot will be more sensitive to deviations.

How can I do it with the histogram? I have the exponential random variables in ySim.

- #30

Mentor

- 34,504

- 12,490

- #31

- 1,368

- 61

- #32

Mentor

- 34,504

- 12,490

Yes, that seems right. Notice how it does not go out to infinity and how it is not strictly monotonically decreasing. The variations are small, but they will make a difference in achieving a given level of numerical precision.It's histogram(x). When I used with the data I have, I got the attached figure. Does that make sense?

- #33

- 1,368

- 61

Yes, that seems right. Notice how it does not go out to infinity and how it is not strictly monotonically decreasing. The variations are small, but they will make a difference in achieving a given level of numerical precision.

Can I do anything about it? I'm writing a scientific journal, do you think such justification is acceptable for why there is a difference between the two curves?

- #34

Mentor

- 34,504

- 12,490

First, instead of generating 1,000,000 numbers that are exponentially distributed and plotting the histogram of the distribution, let's investigate the sampling distribution. Instead of 1,000,000 numbers, generate 1,000 sets of 1,000 numbers each. For each set calculate the mean and the standard deviation (1,000 means and 1,000 standard deviations) and then plot the histograms of those. Let's see if there is any bias.

Second, see if you can easily get a qq plot in Matlab.

- #35

- 1,368

- 61

Share:

Mathematica
Numerical integration over a Green's function

- Replies
- 13

- Views
- 1K

- Replies
- 5

- Views
- 412

- Replies
- 2

- Views
- 1K

Mathematica
Numerical Integration error

- Replies
- 16

- Views
- 1K

- Replies
- 3

- Views
- 1K

- Replies
- 27

- Views
- 2K

MATLAB
Double Numerical Integration

- Replies
- 3

- Views
- 2K

- Replies
- 7

- Views
- 1K

- Replies
- 8

- Views
- 3K

Mathematica
Conversion from Mathematica to Matlab

- Replies
- 1

- Views
- 962