April 9th, 2006, 10:23 am
Just for the fun of it I have written a little vba program to simulate a GBM. My simulation has some unfortunate properties. When I increase the number of time steps in each year the mean drops to a level way below what it should be. I have looked at it again and again and I cannot find any problems with the code. I have included the code below together with a gaussian random number generator that I found online.Function gbm(s As Double, years As Double, my As Double, vol As Double, intperyear As Double) As Double Dim x As Long Dim price As Double price = s Dim dt As Double dt = 1 / intperyear For x = 1 To intperyear * years price = price + price * my * dt + price * vol * Gauss(1, 0, dt) Next x gbm = priceEnd FunctionPublic Function Gauss(HowMany As Long, Optional mean As Double = 0, Optional variance As Double = 1) As Double'Returns a normally distributed random variate with mean 0 and variance 1, a.k.a. "White Noise".' Uses the Box-Muller method Dim Value1 As Single, Value2 As Single, Fac As Single, Rsq As Single, SD As Double, i As Long Dim dblResArr() As Double: ReDim dblResArr(HowMany - 1, 0) SD = Sqr(variance) For i = 1 To HowMany Do Randomize Value1 = 2 * Rnd - 1 Value2 = 2 * Rnd - 1 Rsq = Value1 ^ 2 + Value2 ^ 2 Loop Until Rsq > 0 And Rsq < 1 Fac = (-2 * Log(Rsq) / Rsq) ^ 0.5 If Rnd < 0.5 Then dblResArr(i - 1, 0) = mean + SD * Value1 * Fac Else dblResArr(i - 1, 0) = mean + SD * Value2 * Fac End If Next i Gauss = dblResArr(0, 0) End Function