Search code examples
pythontime-series

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?


Solution

  • 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