crflynn / stochastic

Generate realizations of stochastic processes in python.
http://stochastic.readthedocs.io/en/stable/
MIT License
451 stars 80 forks source link

Geometric Brownian Motion process seemingly can't be seeded / results can't be reproduced #69

Closed BenSchZA closed 2 years ago

BenSchZA commented 2 years ago

Hi there,

I created an MVP of a piece of code that has been causing issues, I don't think it's intended behaviour unless I'm misunderstanding some difference between GBM and BM processes. Whereas the BrownianMotion process will always return the same result when seeded, the GeometricBrownianMotion process does not. I have tried a couple of variations of the below code to test that it's not some bug in the order of code execution or seeding of the RNG. I did dig into the stochastic code a bit, but didn't find any obvious errors - maybe a maintainer of this codebase has a better idea?

MVP:

from stochastic import processes
import numpy as np

rng = np.random.default_rng(1)

gbm_process = processes.continuous.GeometricBrownianMotion(rng=rng)
gbm_sample = gbm_process.sample(1)

bm_process = processes.continuous.BrownianMotion(rng=rng)
bm_sample = bm_process.sample(1)

print(f"Geometric Brownian Motion sample: {gbm_sample}")
print(f"Brownian Motion sample: {bm_sample}")

Output:

Geometric Brownian Motion sample: [1.         0.62310484]
Brownian Motion sample: [0.         0.34558419]

Thanks!

crflynn commented 2 years ago

That should be fixed by https://github.com/crflynn/stochastic/pull/67 but I have yet to make a new release.

BenSchZA commented 2 years ago

Thanks for the quick response! Should have dug deeper into existing issues. I'll pull that down.