When I simulate a stock's price path using geometric brownian motion I am sometimes able to get a pretty good forecast that fits the real values very well. But if I run the simulation again, the results are different. This is probably due to the random process of brownian motion.
Is there a way to run the same simulation (the one that fit the actual values well) over and over again without getting different results?
This is good when you are actually trading. Say for example you can run a test on some past data and then compare it with the real values to see if the model performed well or not. If it did, then you can use the same model for a future forecast in which you are going to buy and/or sell. But if the results are different on every simulation, then if you wish to do future forecast, you won't be able to test it with real data, since you are forecasting the future for which there is no data.
Here is my code. Half the time fits the data pretty, the other half not so.
#from __future__ import division from random import gauss from math import exp, sqrt from matplotlib import pyplot as plt import pandas as pd import numpy as np def generate_asset_price(S,v,r,T): return S * exp((mu - 0.5 * v**2) * T + v * sqrt(T) * gauss(0,1.0)) #or dt instead of T # return S * exp((mu - 0.5 * v**2) * T + v * sqrt(T) * gauss(0,1.0)) S0 = 12.2 # underlying price v = 0.114764067 mu = -0.002773523 dt = 0.01 # 1 day T = 20 n = int(20) # number of steps S_path= S=S0 # starting price for i in xrange(1,n+1): S_t = generate_asset_price(S,v,mu,dt) S= S_t S_path.append(S_t)