2
$\begingroup$

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)
$\endgroup$
5
  • $\begingroup$ Hi, some questions: When you say that "half the time fits the data pretty", do you mean that you simulate a new path until the path you have just simulated resembles the historically observed price path in some way? If so, do you assess this by inspection? And then you use that price path (and its 'future realizations') for some time until the quality of the resemblance deteriorates? Could you please elaborate a bit more on these aspects? If that is truly the case, and without delving into the many conceptual shortcomings of this, the answer from ir7 is indeed the way to go. $\endgroup$ – Kermittfrog Feb 3 at 8:59
  • $\begingroup$ ...You simply play with the random number seed until the resemblance is "good". You then store this number and re-use it whenver you want to produce the exact same path again. Alternatively, you could simply store the path (and its future realizations) to file and load from that file; but that does not answer your specific question. $\endgroup$ – Kermittfrog Feb 3 at 9:00
  • $\begingroup$ Yes, that is correct. I run the simulations until I get a path that fits historical values. I'm assuming that, with the seed value that fits that data, I can run future simulations and they will also fit the actual data. Yes I asses this by inspection, I plot the historical prices together with the simulation. Ir7, has answered my question nicely. But feel free to add some comments. $\endgroup$ – PlatinumMaths Feb 3 at 18:45
  • $\begingroup$ What you are effectively suggesting is to a series of (identical) coins for as long as the history of the last $N$ flips look like something you look for. You then assume that the next flip should be representative of what you are looking for... That sounds dangerously foolish! $\endgroup$ – Kermittfrog Feb 4 at 8:58
  • $\begingroup$ What would you suggest then? If the simulation that got what I looked for worked well on the not too distant past, say week before. Then why should another simulation with the same parameters not perform equally well? (obviously a different mu and sigma value) I'm not looking for precise forecast. I'm after a general direction of the price path and I think that a good GBM simulation can accomodate that. That way I can trade accordingly. $\endgroup$ – PlatinumMaths Feb 4 at 20:13
1
$\begingroup$

Fix the RNG (random number generator)'s seed. This article from sharpsightlabs.com is accessible and illuminating.

$\endgroup$
4
  • 1
    $\begingroup$ I kind of do not know why you got a downvote. Let me balance that. $\endgroup$ – Kermittfrog Feb 3 at 8:55
  • $\begingroup$ @Kermittfrog Thank you. I'm assuming that it is not a matter of substance, but more one of taste/form. In that case, 'De gustibus non est disputandum'. :) $\endgroup$ – ir7 Feb 3 at 15:23
  • $\begingroup$ Thanks! Will have a read! $\endgroup$ – PlatinumMaths Feb 3 at 18:47
  • $\begingroup$ @PlatinumMaths I should note that you are using the native Python random. The article clarifies the pseudo-random number generator meaning and uses, but exemplifies them on numpy package (which has its own random implementation and seed control). $\endgroup$ – ir7 Feb 3 at 19:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.