Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions statsmodels/tsa/stattools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1005,9 +1005,9 @@ def innovations_algo(acov, nobs=None, rtol=None):
rtol = 0.0 if rtol is None else rtol
if not isinstance(rtol, float):
raise ValueError('rtol must be a non-negative float or None.')
n = acov.shape[0] if nobs is None else int(nobs)
if n != nobs or nobs < 1:

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to change this, since n != nobs is always true if nobs is not explicitly set.

if nobs is not None and (nobs != int(nobs) or nobs < 1):
raise ValueError('nobs must be a positive integer')
n = acov.shape[0] if nobs is None else int(nobs)
max_lag = int(np.max(np.argwhere(acov != 0)))

v = np.zeros(n + 1)
Expand All @@ -1020,7 +1020,7 @@ def innovations_algo(acov, nobs=None, rtol=None):
for j in range(max(i - max_lag, 0), k):
sub += theta[k, k - j] * theta[i, i - j] * v[j]
theta[i, i - k] = 1. / v[k] * (acov[i - k] - sub)
v[i] = acov[0]
v[i] = acov[0]
for j in range(max(i - max_lag, 0), i):
v[i] -= theta[i, i - j] ** 2 * v[j]
# Break if v has converged
Expand Down Expand Up @@ -1093,7 +1093,7 @@ def innovations_filter(endog, theta):
hat = (theta[i, :i] * u[:i][::-1]).sum()
else:
hat = (theta[i] * u[i - k:i][::-1]).sum()
u[i] = endog[i] + hat
u[i] = endog[i] - hat

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These look right. Surprised I missed that

if is_pandas:
u = pd.Series(u, index=orig_endog.index.copy())
return u
Expand Down
34 changes: 33 additions & 1 deletion statsmodels/tsa/tests/test_stattools.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
arma_order_select_ic, levinson_durbin,
levinson_durbin_pacf, pacf_burg,
innovations_algo, innovations_filter)
from statsmodels.tsa.arima_process import arma_acovf
from statsmodels.tsa.statespace.sarimax import SARIMAX

DECIMAL_8 = 8
DECIMAL_6 = 6
Expand Down Expand Up @@ -759,7 +761,7 @@ def test_innovations_filter_brockwell_davis():
resid = innovations_filter(endog, theta)
expected = [endog[0]]
for i in range(1, 4):
expected.append(endog[i] + theta[i, 0] * expected[-1])
expected.append(endog[i] - theta[i, 0] * expected[-1])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess repeating the same typo is how

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:)

expected = np.array(expected)
assert_allclose(resid, expected)

Expand Down Expand Up @@ -787,3 +789,33 @@ def test_innovations_filter_errors():
innovations_filter(np.empty(4), theta[:-1])
with pytest.raises(ValueError):
innovations_filter(pd.DataFrame(np.empty((1, 4))), theta)


def test_innovations_algo_filter_kalman_filter():
# Test the innovations algorithm and filter against the Kalman filter
# for exact likelihood evaluation of an ARMA process
ar_params = np.array([0.5])
ma_params = np.array([0.2])
# TODO could generalize to sigma2 != 1, if desired, after #5324 is merged
# and there is a sigma2 argument to arma_acovf
# (but maybe this is not really necessary for the point of this test)
sigma2 = 1

endog = np.random.normal(size=10)

# Innovations algorithm approach
acovf = arma_acovf(np.r_[1, -ar_params], np.r_[1, ma_params],
nobs=len(endog))

theta, v = innovations_algo(acovf)
u = innovations_filter(endog, theta)
llf_obs = -0.5 * u**2 / (sigma2 * v) - 0.5 * np.log(2 * np.pi * v)

# Kalman filter apparoach
mod = SARIMAX(endog, order=(len(ar_params), 0, len(ma_params)))
res = mod.filter(np.r_[ar_params, ma_params, sigma2])

# Test that the two approaches are identical
assert_allclose(u, res.forecasts_error[0])
assert_allclose(theta[1:, 0], res.filter_results.kalman_gain[0, 0, :-1])
assert_allclose(llf_obs, res.llf_obs)