# Solved – How to generate stationary and non-stationary time series data in python

I need custom simulated stationary and non-stationary(trending) time series data for one of my python projects. I searched all the web for it but didn't find anything for python. Then I came across this post Is a model with a sine wave time-series stationary? where a stationary series is explained as
`yt=yt−1+ϵ` where `μ(ϵ)=0` in the comment section. I coded a sample code for generating stationary series

``import matplotlib.pyplot as plt import numpy as np  n=100 x=np.arange(n) y_=np.random.uniform(-1,1,[n])  mu=0 sigma=0.01 e= np.random.normal(mu, sigma, n) #stationary series y=y_+e  plt.plot(x,y) plt.show() ``

the output looks like a stationary time series but I am not sure of it.

But the most difficult part is finding a way to generate non-stationary(ie. trending) time series data. I can't find anything releated to it. How can we generate stationary and non-stationary time series data in python?

Contents

You need to come up with a data generating process (DGP), because simply saying "stationary" is too broad, there's too many processes that fall into this bucket. For instance, this is stationary $$y_t=varepsilon_t$$, where $$varepsilon_tsimmathcal N(0,1)$$ as well as this too $$x_t=varepsilon_t/2+varepsilon_{t-1}/4$$ etc.
The first one is easy to generate in any language, it's just a bunch of independent random numbers: `y= np.random.normal(0, 1, n)` Obviously, you can change the mean and the variance, or pick any other distribution, e.g. Poisson
The nonstationary is also a very broad category: anything with changing mean or variance. You can change mean and variance deterministically or stochastically. The simplest process could be: $$y_t=y_{t-1}varepsilon_t$$ or even $$y_t=y_{t-1}+varepsilon_t$$, which you can generate by simple recursion or a loop