Monte Carlo Method

Jimeshia

New Member
Joined
Feb 25, 2018
Messages
12
Can anyone help? I am needing to write a code in monte carlo method to solve the integral from 0 to 1 for the function e^-xdx. I am struggling with this project. This code should start with an amount of 10,000 and increase by 5,000 until the difference between the answers is less than 0.001. I am VERY NEW to VBA and need help. Thanks.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
You don't need to calculate the integral of the function, just the function itself.

You are picking random points in a rectangle. If the point is below the function, it is inside the area that comprises the integral. When you're done, the integral is the area of the rectangle times the fraction of points below the function.
 
Last edited:
Upvote 0
Yes, if you define e

There is also an Exp function in VBA.
 
Upvote 0
ok so like my previous post if f <=.7 then count+1

No, 0.6 or 0.7 would just be my first guess of the end result, based on the graph. By making a first guess, I'll know whether my code is producing reasonable results.

Have a a look at the graph:

https://app.box.com/s/jgysb68w8gyl8wew9p614p0j2tr0j1jc

You're being asked to generate 10,000 random points and test whether they lie above or below the function. Let's say your code counts exactly 6,300 point on or below f(x). Then your first estimate of the integral (given that the area of the boundary rectangle is 1) will be 0.63.

Now increase the number of iterations to 15,000 and do it again. Perhaps this time the count is 9,495, which is 63.3% of 15,000, so you can refine your estimate to 0.633. This is 0.003 different to the previous time, so increase the iterations to 20,000 and do it again etc etc, until the difference between the two successive results changes by <0.001.

That should help you to structure your code?
 
Upvote 0
We need to keep the discussion in-Forum.

The code you've got is good so far.

Think of your code as having paused temporarily at the point shown. The graph illustrates the single random point (x,y) that the code has just generated. This particular point is below the f(x) line, therefore you need to count it. If it was above the line, you wouldn't count it.

Code:
Dim numtries As Integer
Dim count As Integer

numtries = 10000
count = 0

For Index = 1 To numtries
    x = Rnd()
    y = Rnd()
    'We've temporarily stopped here at Index =1
    'The graph illustrates our one random point (x,y)
    'Do we count this point?  Yes, it's below the f(x) line
    'What code line here will do this?
End If

'The end of our first try with 10000 iterations
'What calculation do you need here?

Once this is done, you can code the loop that increases the number of iterations by 5,000 until the results converge to the 0.001 tolerance specified.
 
Last edited:
Upvote 0
Dim numtries As Integer
Dim count As Integer

numtries = 10000
count = 0

For Index = 1 To numtries
x = Rnd()
y = Rnd()
'We've temporarily stopped here at Index =1
'The graph illustrates our one random point (x,y)
'Do we count this point? Yes, it's below the f(x) line
'What code line here will do this?
End If

'The end of our first try with 10000 iterations
'What calculation do you need here?


Would I need to say this to have my range included
xmin= 0
xmax= 1
x = xmin + (xmax - xmin) *RND()
y = ymin + (ymax - ymin) * RND()
if f(x) > 0 and y > 0 and y <= f(x):
count + 1
if f(x) < 0 and y < 0 and y >= f(x):
count + 1

Area = AreaUnderCurve * count / numtries

If this is correct how would I do the Do While for the increase in numtries
 
Upvote 0
Would I need to say this to have my range included
xmin= 0
xmax= 1
x = xmin + (xmax - xmin) *RND()
y = ymin + (ymax - ymin) * RND()

if f(x) > 0 and y > 0 and y <= f(x):
count + 1
if f(x) < 0 and y < 0 and y >= f(x):
count + 1

Area = AreaUnderCurve * count / numtries

Let's keep it simple and consider only x>0. Then you'll need just: If y <= Exp(-x) Then count = count + 1

It's great that you've generalised to use xmin and xmax, ymin and ymax. This means we can check your code against other integrals, e.g. 0 to 2, or 1 to 2. (In the simple case where xmin = 0 and xmax =0, x = xmin + (xmax - xmin) * Rnd() will simplify to x=Rnd() as you had before).

Putting this together:

Code:
numtries = 10000
count = 0
Randomize

xmin = 0
xmax = 1
ymax = Exp(-xmin)
ymin = 0

For Index = 1 To numtries
    x = xmin + (xmax - xmin) * Rnd()
    y = ymin + (ymax - ymin) * Rnd()
If y <= Exp(-x) Then count = count + 1
Next Index
Area = [COLOR=#ff0000][B]AreaOfRectangle[/B][/COLOR] * count / numtries

In this specific case, AreaOfRectangle is 1. But in the general case using xmin, xmax, ymin, ymax it will be ... ?

Have a go at coding the Do While loop. Review the VBA help material and try a simple example first.

In this case, you'll need to keep track of PreviousArea, and compare Area to PreviousArea to see whether you can stop looping.
 
Upvote 0

Forum statistics

Threads
1,216,456
Messages
6,130,743
Members
449,588
Latest member
accountant606

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top