Serving the Quantitative Finance Community

 
User avatar
European81
Topic Author
Posts: 0
Joined: April 6th, 2004, 11:41 am

Simulating a simple geometric brownian motion

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
 
User avatar
European81
Topic Author
Posts: 0
Joined: April 6th, 2004, 11:41 am

Simulating a simple geometric brownian motion

April 9th, 2006, 11:44 am

I tried with a different random number simulator and now get much better results. I guess the quality of random numbers mean a lot.
 
User avatar
mutley
Posts: 20
Joined: February 9th, 2005, 3:51 pm

Simulating a simple geometric brownian motion

April 11th, 2006, 9:56 am

most people use the different formulation of S_t = S_0 * exp[ (mu-0.5*sig^2)*dt + sig*rand*sqrt(dt) ] where rand is your random [0,1] increment. it's mathematically equivalent to the GBM process, just yields better numerical results.