1

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?

Eka
  • 1,921
  • 2
  • 22
  • 28

2 Answers2

2

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_t\sim\mathcal 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

Aksakal
  • 55,939
  • 5
  • 90
  • 176
1

For stationary signal, you can also simply generate a white Gaussian process (e.g. your $\epsilon$ is stationary). Your signal is also stationary because the noise process is stationary, and LTI filtering of stationary processes create stationary processes. To create a non-stationary signal, it is enough to have a trend in mean. For example, $y(t)=t+\epsilon$, where $\epsilon$ is normal.

And, your $y$ generation seems quite wrong. You write, $y(t)=y(t-1)+\epsilon$, but y_ is a random sequence.

gunes
  • 49,700
  • 3
  • 39
  • 75
  • If I understood you correct for generting stationary we can simply use `np.random.normal(mu, sigma, 1000)`. what you meant by `it is enough to have a trend in mean`, can you explain it a little bit more on this? – Eka Jan 14 '19 at 14:18
  • Correct. And, if your mean process, $\mu(t)$ depends on $t$, then the process is not stationary. – gunes Jan 14 '19 at 14:20
  • So $\mu(t)$ depends on `t` means that as time moves mean also changes? How do we programmetically code a trend. will `y=2x+e` have trend? In my stationary code i coded y as `y=y_+e` is my assumption right? Sorry for bothering you but my math skills are abysmal – Eka Jan 14 '19 at 14:29
  • 1
    Yes. Create a vector t = np.array(range(n)), and set y = t + e. – gunes Jan 14 '19 at 14:35