Hey, I would like to know how to implement Monte Carlo three methods in c++? Also where could I read about three different Monte Carlo sampling methods? Currently, I have written a pretty bad code, so I don't want to post anything until I write something that makes a bit of sense. Unfortunately, I don't understand Monte Carlo methods used in integration as well as I would. Need to read more. Anyhow, the problem I have is this:
Evaluate integral: I = Int(x(1 + 0.3x(1-x)))dx between 1 and 0, and find its divergence from 0.5.
The idea is that an integral is an area under a curve. That area can be estimated as a fraction of a known rectangular area with the same base by generating a random set of points in that rectangle and finding the fraction of them that lie below the curve.
Suppose you used a random-number generator to generate a set of points (x,y) with x uniformly distributed between 0 and 1 and y uniformly distributed between 0 and 2 (say - anything that spans the range of your integrand 1 + 0.3x(1-x) between those values of x will do). Find the fraction of points that lie below your integrand (f) and multiply it by the area of the whole rectangle (2 in this instance). If you have a large number of points, that will be the Monte-Carlo randomly-approximated area under your curve ... and hence an approximation to your integral.
It's similar in principle to a well-known Monte-Carlo method of approximating pi from the area of a quadrant compared with the area of a square.
BTW, this doesn't seem like"lounge" material - you could move the topic to "General C++ programming".
I need to find the integral - 0.5 for function x(1 + 0.3x(1-x)). So far I have successfully managed to find integral using first Monte Carlo method. Now I have two Monte Carlo methods left. I have written code but instead of getting the approximate integral value, I get -0.5 (II and III column in the result below). I believe there might be a problem somewhere for Monte_Carlo_2 and Monte_Carlo_3 functions. Basically, I would like to know what I need to change to get approximate values for integral - 0.5 for second and third option. The code and result are shown below:
Two points:
(1) Monte_Carlo_1 is the method that I would expect; I can't see what you are actually trying to do (mathematically) with your other two methods; please explain.
EDIT: one other method is not to randomise x - just take equally-spaced values; it is whether y lies below or above the function that matters.
(2) Why don't you just return the counts as the value of the functions? You also have no need to use pointers. Something like (untested):
1 2 3 4 5 6 7 8
long Monte_Carlo_1 ()
{
long s = 0;
double x = rnd();
double y = rnd();
if (y < function(x) ) s++;
return s;
}
Actually, given the number of simulations you will need I should use unsigned long long rather than long.
Other things:
- you are presuming that your integrand lies between 0 and 1 when you set y - it happens to be true, but you can't guarantee it;
- there are better random number generators, including one which will give you a continuous uniform distribution (which is what you want).
From the book I read apparently there are three Monte Carlo sampling methods. First is calculating the area behind the curve where you divide area in small squares of sizes i and j. The second method (depicted Monte_Carlo_2) to get more accurate results is where you divide the area behind the curve into rectangles of size i. The third method involves non homogenous distributed numbers ONLY under the curve whereas we randomly generate numbers ALSO above the curve in the first and second methods, which isn't the case in the third MC method. In third MC we generate numbers according to function (p(x)) = 2x) in my case. However, I solved the problem anyhow. After some hard work I managed to solve it.
I could return, but there is little difference whether I use void or long function and return a value. Why would you suggest to return a value?
little difference whether I use void or long function and return a value. Why would you suggest to return a value?
It doesn't really matter, I guess; however, if a function produces a single output it feels slightly more natural (to me) to use that as the return value of the function than doing so through a function parameter ...
... which sort of leads to another suggestion: if you do want to return a value as a function parameter then make that a reference; then you won't need the unnecessary pointers.