Last active
February 11, 2021 22:33
-
-
Save misTrasteos/26ec2eedc59553a353c6e5a4ade16291 to your computer and use it in GitHub Desktop.
generate a random walk time series
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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