Simulate a smooth timeseries in Python

Question:

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?

Asked By: user1551817

||

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

Answered By: Alexander
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.