Monte Carlo Simulation Help

michaelsmith559

Well-known Member
Joined
Oct 6, 2013
Messages
881
Office Version
  1. 2013
  2. 2007
Here is a screenshot of some data that I have. My goal is to run simulations given a margin of error. Range("H11") contains the number of simulations/iterations, I will be doing. The problem I am having is if I change the margin of error from say 0.1 to 0.01 or 0.001 the number of simulations increases (this is to be expected) and the macro gives an error type mismatch when I go over a certain number of simulations. The line highlighted in the macro is the output=worksheetfunction.average(u). Is there a way to correct this. Here is the screenshot and macro:


Excel 2007
ABCDEFGHIJ
1ValuesNumbersProbabilityCountAvgNext AvgNext StdevStdevMin:1
21117.33%6114.5524082.652520832.652520807Max:9
33210.23%3624.5552412.65252081Count:352
43313.35%472.3333334.5580742.65252079Z-Score
54411.36%402.754.5609072.652520783.890591886
65511.36%403.24.5637392.65252078Results:
7167.95%282.8333334.5665722.65252079Margin Error4.562254
8178.52%302.5714294.5694052.65252080.1
94810.23%362.754.5722382.65252083
10399.66%342.7777784.5750712.65252086n
1183.310650
1283.727273AvgAvg Stdev
1394.1666674.5637392.65252081
1494.538462
1524.357143
One
Cell Formulas
RangeFormula
F3=AVERAGE(INDIRECT("One!A2:A" &$J$3+1),B3)
F5=AVERAGE(INDIRECT("One!A2:A" &$J$3+1),B5)
F6=AVERAGE(INDIRECT("One!A2:A" &$J$3+1),B6)
F13=AVERAGE(F2:F10)
F2=AVERAGE(INDIRECT("One!A2:A" &$J$3+1),B2)
G3=STDEV(INDIRECT("One!A2:A" &$J$3+1),F3)
G5=STDEV(INDIRECT("One!A2:A" &$J$3+1),F5)
G6=STDEV(INDIRECT("One!A2:A" &$J$3+1),F6)
G13=AVERAGE(G2:G10)
G2=STDEV(INDIRECT("One!A2:A" &$J$3+1),F2)
J1=MIN(A:A)
H5='Z-Score'!D120
H11=ROUND(((H2*H5)/H8)^2,0)
H2=G13
C2=D2/$J$3
D2=COUNTIF(A:A,B2)
E2=AVERAGE($A$2:A2)


Here is the macro:
Code:
Sub Monte_Carlo_Simulation()
Dim datapoints As Long, iterations As Long
Dim c As Long, j As Long, k As Long, l As Long, x As Double, s As Double
Dim a, u() As Long
Dim output As Double

With Sheets("One")

iterations = Range("H11").Value

ReDim u(1 To iterations, 1 To 1)

a = Range("B2:C10")

Application.ScreenUpdating = False
    
Randomize

    For c = 1 To iterations
        s = 0: x = Rnd
            For j = 1 To UBound(a)
                s = s + a(j, 2)
                    If x <= s Then
                        u(c, 1) = a(j, 1)
                    Exit For
                    End If
            Next j
    Next c

output = WorksheetFunction.Average(u)
Range("J7").Value = output

Application.ScreenUpdating = True
End With
End Sub
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
WorksheetFunction.Average(u) is limited by 65536 values in array maximum. But you may use the following VBA function Avarege():
Rich (BB code):
Function Average(Arr) As Double
  Dim i As Long, v As Double, x
  For Each x In Arr
    i = i + 1
    v = v + x
  Next
  Average = v / i
End Function
then replace this code line: output = WorksheetFunction.Average(u)
by that: output = Average(u)
 
Upvote 0
Hi Mike,
The suggested code has a memory limit for the array u() storing.
In 32 bit version of Excel it works with H8 = 0.002 but faults with "Out of Memory" error at H8 = 0.001.
The below version has no such a limitation.
It calculate sum on a fly and then divides it by the iterations count.
Rich (BB code):
Sub Monte_Carlo_Simulation()
 
  Dim a()
  Dim i As Long
  Dim c As Double, n As Double, r As Double, s As Double
  Dim Total As Double
 
  With Sheets("One")
    n = Range("H11").Value
    a() = Range("B2:C10").Value
    Randomize
    For c = 1 To n
      s = 0
      r = Rnd
      For i = 1 To UBound(a)
        s = s + a(i, 2)
        If r <= s Then
          Total = Total + a(i, 1)
          Exit For
        End If
      Next
    Next
    Range("J7").Value = Total / n
  End With
 
  MsgBox "Ok!"
 
End Sub
Best Regards
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

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