Skip to content

Commit

Permalink
BUG: Fix ARMA cov_params
Browse files Browse the repository at this point in the history
Fix cov_params so that it works with standard methods

closes statsmodels#947
  • Loading branch information
bashtage committed Jul 25, 2019
1 parent 75045f0 commit 495bfcc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
4 changes: 3 additions & 1 deletion statsmodels/tsa/ar_model.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-
import copy

import numpy as np
from numpy.linalg import inv, slogdet
from scipy.stats import norm
Expand Down Expand Up @@ -593,7 +595,7 @@ def fit(self, maxlag=None, method='cmle', ic=None, trend='c',

pinv_exog = np.linalg.pinv(X)
normalized_cov_params = np.dot(pinv_exog, pinv_exog.T)
arfit = ARResults(self, params, normalized_cov_params)
arfit = ARResults(copy.copy(self), params, normalized_cov_params)
if method == 'mle' and full_output:
arfit.mle_retvals = mlefit.mle_retvals
arfit.mle_settings = mlefit.mle_settings
Expand Down
17 changes: 6 additions & 11 deletions statsmodels/tsa/arima_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# This code does not allow this, but it adds consistency with other
# packages such as gretl and X12-ARIMA

import copy
from datetime import datetime

import numpy as np
Expand All @@ -12,7 +13,7 @@
from numpy.linalg import inv
from scipy import optimize
from scipy.signal import lfilter
from scipy.stats import t, norm
from scipy.stats import norm

import statsmodels.base.wrapper as wrap
from statsmodels.regression.linear_model import yule_walker, OLS
Expand Down Expand Up @@ -1008,7 +1009,7 @@ def fit(self, start_params=None, trend='c', method="css-mle",
self.transparams = False # so methods do not expect transf.

normalized_cov_params = None # TODO: fix this
armafit = ARMAResults(self, params, normalized_cov_params)
armafit = ARMAResults(copy.copy(self), params, normalized_cov_params)
armafit.mle_retvals = mlefit.mle_retvals
armafit.mle_settings = mlefit.mle_settings
# Save core fit parameters for future checks
Expand Down Expand Up @@ -1494,9 +1495,9 @@ def bse(self):
return np.sqrt(-1. / hess[0])
return np.sqrt(np.diag(-inv(hess)))

def cov_params(self): # add scale argument?
params = self.params
hess = self.model.hessian(params)
@cache_readonly
def cov_params_default(self):
hess = self.model.hessian(self.params)
return -inv(hess)

@cache_readonly
Expand Down Expand Up @@ -1532,12 +1533,6 @@ def fittedvalues(self):
def resid(self):
return self.model.geterrors(self.params)

@cache_readonly
def pvalues(self):
# TODO: same for conditional and unconditional?
df_resid = self.df_resid
return t.sf(np.abs(self.tvalues), df_resid) * 2

def predict(self, start=None, end=None, exog=None, dynamic=False,
**kwargs):
return self.model.predict(self.params, start, end, exog, dynamic,
Expand Down
19 changes: 19 additions & 0 deletions statsmodels/tsa/tests/test_arima.py
Original file line number Diff line number Diff line change
Expand Up @@ -2699,3 +2699,22 @@ def test_arima_repeated_fit():
assert_allclose(res.params, repeat.params)
assert isinstance(res.summary().as_text(), str)
assert isinstance(repeat.summary().as_text(), str)


def test_arma_ttest_ftest():
# GH947
ar, ma = [1, -0.5], [1., 0.4]
x = 2 + fa.ArmaFft(ar, ma, 40).generate_sample(nsample=1000, burnin=100)
arma = ARMA(x, (1, 1))
res = arma.fit(disp=-1)
r = np.array([0, 1, 1])
t_test = res.t_test(r)
c = res.cov_params()
v = r @ c @ r
numerator = res.params @ r
denom = np.sqrt(v)
stat = numerator / denom
assert_allclose(stat, t_test.statistic)

f_test = res.f_test(r)
assert_allclose(stat**2, f_test.statistic)

0 comments on commit 495bfcc

Please sign in to comment.