HiveBrain v1.2.0
Get Started
← Back to all entries
snippetpythonMinor

Numerical Python code to generate artificial data from a time series process

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
processnumericalartificialtimegeneratepythoncodeseriesfromdata

Problem

I'm writing code to generate artificial data from a bivariate time series process, i.e. a vector autoregression. This is my first foray into numerical Python, and it seemed like a good place to start.

The specification is of this form:

\begin{align}
\begin{bmatrix}
y_{1,t} \\
y_{2,t}
\end{bmatrix}
&=
\begin{bmatrix}
0.02 \\
0.03
\end{bmatrix}
+
\begin{bmatrix}
0.5 & 0.1 \\
0.4 & 0.5
\end{bmatrix}
\begin{bmatrix}
y_{1,t-1} \\
y_{2,t-1}
\end{bmatrix}
+
\begin{bmatrix}
0 & 0 \\
0.25 & 0
\end{bmatrix}
\begin{bmatrix}
y_{1,t-2} \\
y_{2,t-2}
\end{bmatrix}
+
\begin{bmatrix}
u_{1,t} \\
u_{2,t}
\end{bmatrix}
\end{align}

where \$u\$ has a multivariate normal distribution, i.e.
\begin{align}
\begin{bmatrix}
u_{1,t} \\
u_{2,t}
\end{bmatrix}
\sim N \left( \begin{bmatrix} \mu_1 \\ \mu_2 \end{bmatrix}, \Sigma \right)
\end{align}

My basic algorithm generates enough observations to get past the effects of the (arbitrary) initial conditions, and only returns the number of observations asked for. In this case, it generates ten times the number of observations asked for and discards the first 90%.

```
import scipy

def generate_data(y0, A0, A1, A2, mu, sigma, T):
""" Generate sample data from VAR(2) process with multivariate
normal errors

:param y0: Vector of initial conditions
:param A0: Vector of constant terms
:param A1: Array of coefficients on first lags
:param A2: Array of coefficients on second lags
:param mu: Vector of means for error terms
:param sigma: Covariance matrix for error terms
:param T: Number of observations to generate
"""

if y0.ndim != 1:
raise ValueError("Vector of initial conditions must be 1 dimensional")

K = y0.size

if A0.ndim != 1 or A0.shape != (K,):
raise ValueError("Vector of constant coefficients must be 1 dimensional and comformable")

if A1.shape != (K, K) or A2.shape != (K, K):
raise ValueError("Coefficient matrices must be conformable")

if mu.shape

Solution

-
Is

scipy.dot(A1, data[i-1, :]) + scipy.dot(A2, data[i-1, :])


a bug or a copy-paste error? According to the spec, the second term should have i - 2.

-
A 90% lead-in seems arbitrary. In fact, any lead-in would be arbitrary, as long as you didn't quantify how much the initial condition should fade. In any case, I recommend to pass it as a parameter (either percentage to discard, or - preferably - the desired decay of the initial condition).

Code Snippets

scipy.dot(A1, data[i-1, :]) + scipy.dot(A2, data[i-1, :])

Context

StackExchange Code Review Q#100961, answer score: 4

Revisions (0)

No revisions yet.