Skip to content

ENH: analytic version of arma_acovf - #5324

Merged
ChadFulton merged 6 commits into
statsmodels:masterfrom
ChadFulton:arma_acovf
Oct 17, 2018
Merged

ChadFulton merged 6 commits into
statsmodels:masterfrom
ChadFulton:arma_acovf

Conversation

@ChadFulton

@ChadFulton ChadFulton commented Oct 16, 2018 •

Copy link
Copy Markdown
Member

Updates arima_process.arma_acovf to compute the autocovariances using the analytical approach of Brockwell and Davis (1991).

  • Somewhat faster in most some cases, much faster in cases with high persistence
  • Improves precision
  • Adds additional unit tests

@ChadFulton ChadFulton changed the title ENH: theoretical version of arma_acovf ENH: analytic version of arma_acovf Oct 16, 2018
@codecov

codecov Bot commented Oct 16, 2018 •

Copy link
Copy Markdown

Codecov Report

Merging #5324 into master will increase coverage by <.01%.
The diff coverage is 97.43%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #5324      +/-   ##
==========================================
+ Coverage   81.72%   81.72%   +<.01%     
==========================================
  Files         569      569              
  Lines       89712    89735      +23     
  Branches    10019    10017       -2     
==========================================
+ Hits        73317    73339      +22     
  Misses      14126    14126              
- Partials     2269     2270       +1
Impacted Files Coverage Δ
statsmodels/tsa/tests/test_arima_process.py 100% <100%> (ø) ⬆️
statsmodels/tsa/arima_process.py 78.35% <94.44%> (+0.13%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update bcb0312...96c35e2. Read the comment docs.

@ChadFulton

Copy link
Copy Markdown
Member Author

Green, but I will push up one more doc change w/ squashing, and then wait until tomorrow to merge in case anyone has thoughts / improvements / concerns.

@jbrockmendel

Copy link
Copy Markdown
Contributor

I’ll give this a review in the next day or so.

@jbrockmendel

Copy link
Copy Markdown
Contributor

Do you have an example where the new implementation outperforms (in speed or accuracy or ...) the old one?

@josef-pkt

Copy link
Copy Markdown
Member

My old version makes a detour via a infinite series that needs truncation, so there is a truncation error if the truncation happens too soon, or wasteful computation if truncation leaves a large number of terms.
The old version might have a speed advantage for large m (convolution versus lstsq), but in all other cases the new version should be faster, besides being more accurate.

@josef-pkt

Copy link
Copy Markdown
Member

I'm not sure anymore whether there is a truncation error for acovf at small lags

ir = arma_impulse_response(ar, ma, leads=nobs_ir)
acovf = np.correlate(ir, ir, 'full')[len(ir) - 1:]

This looks similar to the forecast variance on the mailing list (except for the missing cumsum?), and it should be possible to compute the forecast covariance at different horizons/steps to get the joint distribution and a simultaneous confidence interval.

Computationally the new version is better and LGTM,
For the theoretical interpretation, I liked the old version.

assert_almost_equal(rep1, rep2, 8) # 8 is max precision here
assert_allclose(rep1, rep2)

# Test other cases, against R's ARMAacf

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
# Test other cases, against R's ARMAacf
def test_arma_acf_compare_R_ARMAacf():
# Test other cases, against R's ARMAacf; GH#5324

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.

Why the reference to the github PR?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Habit. Avoids the need to look up git history.

assert_almost_equal(ac1, ac2, decimal=7,
err_msg='acovf not equal for %s, %s' % (ar, ma))
assert_allclose(ac1, ac2, atol=1e-15,
err_msg='acovf not equal for %s, %s' % (ar, ma))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Are the actual values of ac1 and ac2 big enough that atol=1e-15 is decently tight?

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.

Without setting atol we have:

E               AssertionError: 
E               Not equal to tolerance rtol=1e-07, atol=0
E               acovf not equal for [1.0], [1, 0.9]
E               (mismatch 80.0%)
E                x: array([ 1.810000e+00,  9.000000e-01, -1.581012e-18,  1.073360e-17,
E                       6.474231e-18,  2.394510e-18,  1.061885e-17, -2.302974e-18,
E                       3.987144e-19, -1.813615e-18])
E                y: array([1.81, 0.9 , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  ])

so I think it's fine - basically we just need to drop numerical noise for comparison to exact zero.

@@ -155,8 +173,8 @@ def test_armafft():
arma = ArmaFft(ar, ma, 20)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This test should be parametrized

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.

Noted :)

assert_equal(res.ndim, 1)
assert_allclose(res, expected, atol=1e-6)
# atol=7 breaks at .999, worked at .995
assert_allclose(res, expected)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does the change in this PR improve the match? (i.e. the resolve the comment above)

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.

Yes, all of the precision improvements are due to the analytic compuation.

nobs : int
number of terms (lags plus zero lag) to include in returned acovf
sigma2 : float
Variance of the innovation term.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

require strictly positive?

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.

sure

# Custom example 7
ar_params = c(1, -0.25, 0.1, -0.05, 0.02)
ma_params = c(1, 0.2)
print(ARMAacf(ar_params, ma_params, lag.max=9))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is there a manual step to create results/arma_acov_results.py from the output of this script? Can that step be automated?

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.

It could surely be automated, but I'm not sure if it's worth it?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Don’t bother; only if there were an obvious way to do it.

@@ -0,0 +1,43 @@
bd_example_3_3_2 = [

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Comment describing where these numbers come from, how to reproduce

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.

Good idea

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.01%) to 84.255% when pulling 78e375b on ChadFulton:arma_acovf into bcb0312 on statsmodels:master.

@ChadFulton

Copy link
Copy Markdown
Member Author

I also forgot to mention that an alternative to this PR is to keep both approaches around in some form, so if that's preferable let me know.

@josef-pkt

Copy link
Copy Markdown
Member

I thought about adding a method argument, but I don't think it is worth it.
AFAIK, the new code is computationally much better than the old, and doesn't need the guessing for the truncation.

(If there is interest, then parking the simplified old version in an example or test case could be useful just for documenting the theoretical relationship. Sometimes I like to keep an in most cases computationally inferior version around just as documentation or for quick experiments, but this is not really my area anymore.)

@ChadFulton

Copy link
Copy Markdown
Member Author

Thanks for the review @jbrockmendel and the comments @josef-pkt.

@ChadFulton

Copy link
Copy Markdown
Member Author

(If there is interest, then parking the simplified old version in an example or test case could be useful just for documenting the theoretical relationship. Sometimes I like to keep an in most cases computationally inferior version around just as documentation or for quick experiments, but this is not really my area anymore.)

This is a good idea, I will make a test case that uses it. Thanks!

@ChadFulton ChadFulton added this to the 0.10 milestone Oct 17, 2018
@ChadFulton

Copy link
Copy Markdown
Member Author

Green as it gets right now (it looks like some deprecation warnings about np.matrix from Numpy are flooding the Travis log, and causing it to error out before finishing on three of the runs).

@ChadFulton
ChadFulton merged commit 362f107 into statsmodels:master Oct 17, 2018
@ChadFulton
ChadFulton deleted the arma_acovf branch October 17, 2018 11:59
@jbrockmendel

Copy link
Copy Markdown
Contributor

If you're interested in clearing warnings out from the logs: #5260, #5204 are ready


References
----------
Brockwell, Peter J., and Richard A. Davis. 2009.

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.

Too late now, but ideally would be in rst reference style .. [*] Brockwell...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants