Skip to content

Commit

Permalink
Merge pull request #5501 from bashtage/var-acf-bug
Browse files Browse the repository at this point in the history
BUG: Correct error in VAR ACF
  • Loading branch information
ChadFulton committed Feb 26, 2019
2 parents feaace7 + 47ebb6a commit 7a33ef0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
22 changes: 21 additions & 1 deletion statsmodels/tsa/vector_ar/tests/test_var.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import statsmodels.api as sm
import statsmodels.tsa.vector_ar.util as util
import statsmodels.tools.data as data_util
from statsmodels.tsa.vector_ar.var_model import VAR
from statsmodels.tsa.vector_ar.var_model import VAR, var_acf
from statsmodels.tools.sm_exceptions import ValueWarning


Expand Down Expand Up @@ -374,6 +374,26 @@ def test_acf(self):
acfs = self.res.acf()
assert(len(acfs) == self.p + 1)

def test_acf_2_lags(self):
c = np.zeros((2, 2, 2))
c[0] = np.array([[.2, .1], [.15, .15]])
c[1] = np.array([[.1, .9], [0, .1]])

acf = var_acf(c, np.eye(2), 3)

gamma = np.zeros((6, 6))
gamma[:2, :2] = acf[0]
gamma[2:4, 2:4] = acf[0]
gamma[4:6, 4:6] = acf[0]
gamma[2:4, :2] = acf[1].T
gamma[4:, :2] = acf[2].T
gamma[:2, 2:4] = acf[1]
gamma[:2, 4:] = acf[2]
recovered = np.dot(gamma[:2, 2:], np.linalg.inv(gamma[:4, :4]))
recovered = [recovered[:, 2 * i:2 * (i + 1)] for i in range(2)]
recovered = np.array(recovered)
assert_allclose(recovered, c, atol=1e-7)

def test_acorr(self):
acorrs = self.res.acorr(10)

Expand Down
3 changes: 2 additions & 1 deletion statsmodels/tsa/vector_ar/var_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ def _var_acf(coefs, sig_u):
vecACF = scipy.linalg.solve(np.eye((k*p)**2) - np.kron(A, A), vec(SigU))

acf = unvec(vecACF)
acf = acf[:k].T.reshape((p, k, k))
acf = [acf[:k, k * i:k * (i + 1)] for i in range(p)]
acf = np.array(acf)

return acf

Expand Down

0 comments on commit 7a33ef0

Please sign in to comment.