Repository navigation
ENH: Fast ARMA innovation algorithm and loglike computation - #5360
Conversation
|
@ChadFulton I'll give this a look over the weekend. Not just as a thank-you for handling obvious fixes elsewhere, but it helps. |
|
Thanks! FYI some functions in the _arma_innovations.pyx.in file have two versions, one with the suffix I sort of assume that we'll drop the slower versions before merging (maybe keeping them in Git history for reference though), since they have no advantage. |
| import numpy as np | ||
| import warnings | ||
| from statsmodels.tsa import arima_process | ||
| from statsmodels.src.math cimport * |
There was a problem hiding this comment.
can you make these explicit. tracking down what is actually imported/used can be a PITA.
| import warnings | ||
| from statsmodels.tsa import arima_process | ||
| from statsmodels.src.math cimport * | ||
| cimport numpy as np |
There was a problem hiding this comment.
Can you make this cimport numpy as cnp? I find it helps to keep the python-numpy and cython-numpy namespaces explicitly distinct
| if prefix == 's': | ||
| combined_prefix = 'd' | ||
| combined_cython_type = 'np.float64_t' | ||
| }} |
There was a problem hiding this comment.
looks like a good use case for fused types!
There was a problem hiding this comment.
Agree. Fused types + pyx allows for coverage checks.
There was a problem hiding this comment.
Good idea - not for initial commit, but I do like the idea of moving to fused types in the future.
|
|
||
| cdef {{prefix}}toeplitz(int n, int o0, int o1, | ||
| {{cython_type}} [:] in_column, | ||
| {{cython_type}} [:, :] out_matrix): |
There was a problem hiding this comment.
The syntax I see elsewhere usually doesn't ptu a space between the "{{cython_type}}" and the "[:"
There was a problem hiding this comment.
I'd prefer to leave this as-is. I know that Pandas doesn't usually include a space, but e.g. the Cython docs do (https://cython.readthedocs.io/en/latest/src/userguide/memoryviews.html).
| any output. | ||
|
|
||
| """ | ||
| cdef int i, j |
| if i != j: | ||
| # Note: scipy by default does complex conjugate, but not | ||
| # necessary here since we're only dealing with covariances, | ||
| # which will be real |
| o0 : int | ||
| The row offset for `out_matrix` at which to begin writing the Toeplitz | ||
| block. | ||
| o1 : int |
There was a problem hiding this comment.
can you think of any more informative names than n, o0, o1?
There was a problem hiding this comment.
I think n is okay, but I'll go to offset0 and offset1
| convention assumes the coefficients are part of the lag polynomial on | ||
| the left-hand-side of the ARMA definition (i.e. they have the opposite | ||
| sign from the usual econometrics convention in which the coefficients | ||
| are on the right-hand-side of the ARMA definition). |
There was a problem hiding this comment.
The lack of clear naming conventions has caused trouble elsewhere. Maybe arpoly or something otherwise difficult to confuse with -ar[1:]? (ditto for ma)
There was a problem hiding this comment.
I think this is a good idea. Have we already started to do this anywhere, or do we need to establish a new convention?
@josef-pkt ? @bashtage ?
There was a problem hiding this comment.
so what about one of (listed in order of my preference):
ar_polynomialarpolypolynomial_ar
There was a problem hiding this comment.
Very much +1 for establishing a standard with unambiguous names and using it consistently to clear up (ar|ma)(params|coefs|poly). Slight preference for arpoly over ar_polynomial.
There was a problem hiding this comment.
on the other hand, we already have ar_params, so what about ar_poly?
There was a problem hiding this comment.
(mainly checking if my new internet connection works)
I thought we are reasonably consistent
params, ar_coeffs are econometrics estimation version
ar is lag polynomial coefficients
ar with poly in the name should be a numpy Polynomial or similar instance which can be used to directly compute with the polynomials, e.g. multiplication, inverse and so on.
There was a problem hiding this comment.
I thought we are reasonably consistent
I'd speculate that past-me would may have found ar and ma ambiguous, possibly been confused why there are both arparams and arcoeffs if they mean the same thing. I also wouldn't be surprised if there are test/example/sandbox/pseudo-sandbox files that don't follow the same conventions.
Quick look grepping for (ar|ma)_?(params|coef) suggests that code looks appreciably better than the tests consistency/clarity-wise:
ArmaProcess.__init__ takes ar and ma
ARMA.geterrors defines b, a = np.r_[1, -arparams], np.r_[1, maparams]
arma_mle does its own thing
varma_process.VarmaPoly.__init__ takes ar and ma, looks vaguely like it follows the convention.
test_arima.test_long_ar_start_params defines arparams and maparams where it should be ar and ma.
test_arima.test_arima_predict_exog has commented-out code defining arparams and maparams with leading 1s, looks inconsistent, but who knows with commented-out code.
test_arima.test_arima_1123 passes no-leading-1s arparams/maparams to arma_generate_sample, which AFAICT doesn't match the intended usage.
test_stattools.test_arma_order_select_ic starts out strong before taking its eyes off the prize:
arparams = np.array([.75, -.25])
maparams = np.array([.65, .35])
arparams = np.r_[1, -arparams]
maparam = np.r_[1, maparams]
Not going to bother looking at sandbox.
In no particular order, I'd suggest:
- fixing (or clarifying if my reading is incorrect) the apparently-inconsistent usages in the test files (side-note: delete-or-document commented-out code)
- adding validation in
arma_generate_sampleiftest_arima_1123is in fact passing invalid params (side-note: give test_arima_1123 a meaningful name) - using something more explicit than
ar/ma, e.g.arpoly/mapoly- could consider going even further with this and only allow np.polynomial.Polynomial objects for these
- settling on either
(ar|ma)paramsor(ar|ma)coeffs, but not using both synonymously. - have ArmaProcess and ARMAResults share code to decrease the potential for future inconsistencies
There was a problem hiding this comment.
tl;dr - it sounds like a precedent already exists to use ar for the polynomial and ar_params for the econometric versions, so that's what I'll do here too.
I thought we are reasonably consistent
params, ar_coeffs are econometrics estimation version
ar is lag polynomial coefficients
ar with poly in the name should be a numpy Polynomial or similar instance which can be used to directly compute with the polynomials, e.g. multiplication, inverse and so on.
Thanks, I had forgotten about the ar vs ar_params distinction. I think you're right, that this is reasonably consistent in statsmodels as of now.
Quick look grepping for (ar|ma)_?(params|coef) suggests that code looks appreciably better than the tests consistency/clarity-wise:
Thanks for taking a look into the consistency in the existing code.
In no particular order, I'd suggest:
I support those goals, but I guess out of scope for this PR.
| cdef np.npy_intp dim1[1] | ||
| cdef np.npy_intp dim2[2] | ||
| cdef {{cython_type}} [:, :] acovf | ||
| cdef {{cython_type}} [:] tmp |
There was a problem hiding this comment.
personal preference, feel free to ignore: i like to have one cdef: and then an indented block
| m2 = 2 * m | ||
| n = max(m2, nobs) | ||
|
|
||
| dim2[0] = n; dim2[1] = n; |
There was a problem hiding this comment.
separate lines; no semicolons
|
needs rebase there are also problems with |
6741f65 to
d0abf26
Compare
Codecov Report
@@ Coverage Diff @@
## master #5360 +/- ##
==========================================
- Coverage 81.75% 81.72% -0.03%
==========================================
Files 569 573 +4
Lines 89851 90068 +217
Branches 10022 9311 -711
==========================================
+ Hits 73455 73611 +156
- Misses 14126 14176 +50
- Partials 2270 2281 +11
Continue to review full report at Codecov.
|
Codecov Report
@@ Coverage Diff @@
## master #5360 +/- ##
=========================================
+ Coverage 81.75% 81.8% +0.04%
=========================================
Files 569 574 +5
Lines 89824 90028 +204
Branches 10022 10027 +5
=========================================
+ Hits 73440 73645 +205
+ Misses 14117 14116 -1
Partials 2267 2267
Continue to review full report at Codecov.
|
ea32256 to
19b7fbd
Compare
|
Thanks @jbrockmendel and @josef-pkt for review + comments.
This is now fixed,
This was a bit of a puzzle, but appears to somehow be related to the |
|
I think the failures are now unrelated, so we can think about what form this should take if it is to be merged into Statsmodels. I guess my vote is to merge it basically as-is. It is not a complete model class, but it does provide: One question is whether it should be exposed as part of the api somewhere (e.g. If included as part of the API, then I need to add a documentation section for it. Any other thoughts on what to do, or pre-merge improvements, would be welcome. |
b408862 to
1f73340
Compare
ebca1d5 to
ab07597
Compare
|
Okay, I have:
Everything was green before, and these changes shouldn't affect that, so if all is well I will plan to merge tomorrow, unless there are comments or concerns. |
af4d277 to
ebc283c
Compare
ebc283c to
b4678b8
Compare



This adds a fast Cython innovations algorithm specific to ARMA processes along with a fast exact loglikelihood computation method. (this isn't exactly #5326, since this is only for ARMA processes)
In most cases, it is just about as fast as the state space models, but it scales to large AR and MA orders much better because we can exploit a lot of zeros that appear in the innovations algorithm once we make the transformation suggested by Brockwell and Davis (section 3.3).
For example, it should make it feasible to estimate the parameters of an ARMA model with 50000 observations and a seasonal periods of a year, which would be extremely slow when using the state space models.
It's not a full model class right now, of course, but I think the plan would be to eventually embed it as an alternative
fitmethod inSARIMAX.And of course all of the details are still up in the air: where to put it, what to call it, etc...