Hi,
I am very new to visual basic and am using the code to create a monte carlo simulation. I have created a function, but do not know how to make the function repeat itself a specific number of times (which will be inputted by the user), and how to make these iterations output themselves onto the spreadsheet. The code i have created is:
Option Explicit
Public Function gBmProcess(FTSEinitial As Double, RiskFreeRate As Double, Dividend As Double, _
Volatility As Double, TimeLength As Double) As Double
'This function will allow the simulation of FTSE100 values.
Application.Volatile (True)
gBmProcess = FTSEinitial * Exp((RiskFreeRate - Dividend - 0.5 * (Volatility * Volatility)) _
* TimeLength + Volatility * Sqr(TimeLength) * StdNorm())
End Function
Public Function StdNorm() As Double
'Uses the Box-Muller method to generate normally distributed random variables
Application.Volatile (True)
Static s As Boolean
Static w As Double
Dim X1 As Double
Dim X2 As Double
'Box-Muller Algorithm.
If s Then
s = False
StdNorm = w
Else
s = True
Do
X1 = 2 * Rnd() - 1
X2 = 2 * Rnd() - 1
w = X1 ^ 2 + X2 ^ 2
Loop Until w <= 1
w = Sqr(-2 * Log(w) / w)
StdNorm = X1 * w
w = X2 * w
End If
End Function
Please help me find a way to repeat this function a number of times specified within, e.g. cell C10, and output the solutions into cells, e.g.d45 - d45000. Many thanks
I am very new to visual basic and am using the code to create a monte carlo simulation. I have created a function, but do not know how to make the function repeat itself a specific number of times (which will be inputted by the user), and how to make these iterations output themselves onto the spreadsheet. The code i have created is:
Option Explicit
Public Function gBmProcess(FTSEinitial As Double, RiskFreeRate As Double, Dividend As Double, _
Volatility As Double, TimeLength As Double) As Double
'This function will allow the simulation of FTSE100 values.
Application.Volatile (True)
gBmProcess = FTSEinitial * Exp((RiskFreeRate - Dividend - 0.5 * (Volatility * Volatility)) _
* TimeLength + Volatility * Sqr(TimeLength) * StdNorm())
End Function
Public Function StdNorm() As Double
'Uses the Box-Muller method to generate normally distributed random variables
Application.Volatile (True)
Static s As Boolean
Static w As Double
Dim X1 As Double
Dim X2 As Double
'Box-Muller Algorithm.
If s Then
s = False
StdNorm = w
Else
s = True
Do
X1 = 2 * Rnd() - 1
X2 = 2 * Rnd() - 1
w = X1 ^ 2 + X2 ^ 2
Loop Until w <= 1
w = Sqr(-2 * Log(w) / w)
StdNorm = X1 * w
w = X2 * w
End If
End Function
Please help me find a way to repeat this function a number of times specified within, e.g. cell C10, and output the solutions into cells, e.g.d45 - d45000. Many thanks