Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulate a smooth timeseries in Python

I want to use Python to generate some data that will simulate a fairly smooth wandering timeseries - similar to the following plot.

enter image description here

I originally started with a random walk, but if I made my standard deviation small, the data did not wander enough, and if I made the standard deviation too large, the plot is not smooth at all.

Is there a better way to approach this?

like image 719
user1551817 Avatar asked Apr 20 '26 12:04

user1551817


1 Answers

Just apply a rolling moving average to your results:

from numpy import sqrt

annualized_vol = .30  # 30%
lag = 30
random_normals = np.random.randn(1000)  # 1,000 trading days.
daily_vol = sqrt(annualized_vol) * sqrt(1 / 252.)  # 252 trading days in a year.
random_daily_log_returns = random_normals * daily_vol
df = pd.DataFrame(random_daily_log_returns).cumsum()
df.rolling(lag).mean().plot()

The bigger the lag and the smaller the vol, the smoother the series

like image 110
Alexander Avatar answered Apr 22 '26 03:04

Alexander



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!