Skip to content

Commit

Permalink
Merge pull request #1 from EC-AI/main
Browse files Browse the repository at this point in the history
Option of using (neqs,) and other changes and cleaning
  • Loading branch information
EC-AI committed Dec 7, 2021
2 parents 2f005fa + 47c7e28 commit b5ea779
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
23 changes: 15 additions & 8 deletions statsmodels/tsa/vector_ar/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""
from statsmodels.compat.pandas import frequencies
from statsmodels.compat.python import asbytes
from statsmodels.tools.validation import array_like

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -190,7 +191,7 @@ def acf_to_acorr(acf):
return acf / np.sqrt(np.outer(diag, diag))


def varsim(coefs, intercept, sig_u, steps=100, initvalues=None, seed=None):
def varsim(coefs, intercept, sig_u, steps=100, initial_values=None, seed=None):
"""
Simulate VAR(p) process, given coefficients and assuming Gaussian noise
Expand All @@ -215,11 +216,16 @@ def varsim(coefs, intercept, sig_u, steps=100, initvalues=None, seed=None):
observations to start the autoregressive process.
If offset is not None, then exog of the model are used if they were
provided in the model
initvalues : None or ndarray (nlags, neqs)
Initial values for use in the simulation.
initial_values : array_like, optional
Initial values for use in the simulation. Shape should be
(nlags, neqs) or (neqs,). Values should be ordered from less to
most recent. Note that this values will be returned by the
simulation as the first values of `endog_simulated` and they
will count for the total number of steps.
seed : {None, int}
If seed is not None, then it will be used with for the random
variables generated by numpy.random.
Returns
-------
endog_simulated : nd_array
Expand All @@ -242,11 +248,12 @@ def varsim(coefs, intercept, sig_u, steps=100, initvalues=None, seed=None):
result[p:] += ugen[p:]
else:
result[p:] = ugen[p:]

if initvalues is not None:
if not initvalues.shape == (p, k):
raise ValueError("Initial values should have shape (p, k) where p is the number of lags and k is the number of equations.")
result[:p] = initvalues

initial_values = array_like(initial_values, "initial_values", optional=True, maxdim=2)
if initial_values is not None:
if not (initial_values.shape == (p, k) or initial_values.shape == (k,)):
raise ValueError("initial_values should have shape (p, k) or (k,) where p is the number of lags and k is the number of equations.")
result[:p] = initial_values

# add in AR terms
for t in range(p, steps):
Expand Down
12 changes: 8 additions & 4 deletions statsmodels/tsa/vector_ar/var_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,7 @@ def is_stable(self, verbose=False):
"""
return is_stable(self.coefs, verbose=verbose)

def simulate_var(self, steps=None, offset=None, initial_state=None, seed=None):
def simulate_var(self, steps=None, offset=None, seed=None, initial_values=None):
"""
simulate the VAR(p) process for the desired number of steps
Expand All @@ -941,11 +941,15 @@ def simulate_var(self, steps=None, offset=None, initial_state=None, seed=None):
the linear predictor of those components will be used as offset.
This should have the same number of rows as steps, and the same
number of columns as endogenous variables (neqs).
initial_state : None or ndarray (nlags, neqs)
Initial values for use in the simulation.
seed : {None, int}
If seed is not None, then it will be used with for the random
variables generated by numpy.random.
initial_values : array_like, optional
Initial values for use in the simulation. Shape should be
(nlags, neqs) or (neqs,). Values should be ordered from less to
most recent. Note that this values will be returned by the
simulation as the first values of `endog_simulated` and they
will count for the total number of steps.
Returns
-------
Expand Down Expand Up @@ -981,7 +985,7 @@ def simulate_var(self, steps=None, offset=None, initial_state=None, seed=None):
)

y = util.varsim(
self.coefs, offset, self.sigma_u, steps=steps, initvalues=initial_state, seed=seed
self.coefs, offset, self.sigma_u, steps=steps, seed=seed, initial_values=initial_values,
)
return y

Expand Down

0 comments on commit b5ea779

Please sign in to comment.