Skip to content

Instantly share code, notes, and snippets.

@misTrasteos
Last active February 11, 2021 22:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save misTrasteos/26ec2eedc59553a353c6e5a4ade16291 to your computer and use it in GitHub Desktop.
Save misTrasteos/26ec2eedc59553a353c6e5a4ade16291 to your computer and use it in GitHub Desktop.
generate a random walk time series
import matplotlib.pyplot as plt
import numpy as np
numberOfDays = 7
minutesPerDay = 24 * 60
def addNoise(array, noise=5):
# some random noise
return array + np.random.rand( len(array) ) * noise
steps = np.random.standard_normal(minutesPerDay)
# first step is zero
steps[0]=0
oneDayRandomWalk = np.cumsum(steps)
# generate the complete walk concatenating all days
# each day some white noise is added
completeRandomWalk = np.concatenate( [addNoise(oneDayRandomWalk) for _ in range(numberOfDays)] )
## add an upward trend
completeRandomWalk = completeRandomWalk + np.arange(len(completeRandomWalk)) * 0.0015
# I would like all of them to be positive
completeRandomWalk += min(completeRandomWalk)
plt.plot(completeRandomWalk)
plt.show()
#based on this great article, https://towardsdatascience.com/time-series-analysis-creating-synthetic-datasets-cf008208e014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment