Repository navigation
ENH: State space: allow concentrating scale out of log-likelihood - #3480
Conversation
18db9ae to
f3610e1
Compare
|
In case anyone's interested in the details: http://nbviewer.jupyter.org/gist/ChadFulton/b0479cc3faecf29503b37412fdcbe6c6 The basic idea here is that we can concentrate the scale out of the log-likelihood function pretty generically in state space models. This PR sets up the Cython filters to separate the parts of the log-likelihood required for computation of scale versus the remainder of the log-likelihood, if activated. The default is as before, where the non-concentrated log-likelihood is computed. To activate the separated computation, the FILTER_CONCENTRATED flag should be set (e.g. via When the concentrated log-likelihood is used, the filter output needs to be rescaled after the scale is computed (see the notebook where I show what the scaling factor is - not surprisingly, it's the estimated scale parameter), and the log-likelihood needs to be adjusted after the fact. I've also added a context manager in the Finally, as I mentioned above, I added a I think most of the PR is actually pretty straightforward. The only part which might seem a bit dubious is the rescaling of the output in the concentrated case, but I show why that works in the notebook above. |
Over 50% is just the new unit tests. |
91e0339 to
5eb67c1
Compare
|
Rebased, also one of the test precisions reduced to make the Appveyor happy. I should note that some of the test precisions in
|
|
I will probably squash this into a single commit, since some of these were mistakes / corrections. |
|
The This is all I have planned for this PR (except for squashing). Edit: although some of the work in this PR (in |
|
Whoops, forgot that concentrated scale is only available in scipy>=0.16 version of state space, so I'll have to roll back the RecursiveLS model to how it was before. |
|
This is now rebased and also I added support for concentrating the scale out in the case of exact diffuse initialization. |
|
Now rebased again, fixed a couple of bugs. |
Codecov Report
@@ Coverage Diff @@
## master #3480 +/- ##
=========================================
+ Coverage 80.92% 81% +0.07%
=========================================
Files 577 578 +1
Lines 88700 89069 +369
Branches 9889 9937 +48
=========================================
+ Hits 71782 72149 +367
- Misses 14655 14657 +2
Partials 2263 2263
Continue to review full report at Codecov.
|
|
I have no more planned to do on this PR and it could be merged whenever. But if anyone has any concerns or questions, that would be great! |
|
A notebook with some examples on how to concentrate the scale out of the likelihood function in state space models: http://nbviewer.jupyter.org/gist/ChadFulton/b5e2bb487cd838be6fcb0c00950813c4 Since this is more detail than most users probably want, I was thinking that I might add a new section to the notebooks page title "State space models: Technical Notes" to distinguish this kind of notebook from the ones that are aimed at showing how to use the models. Or if it seems like it's not useful, we don't need to post this notebook at all. |
Of course I put up the version with no cell output...updated now, but nbviewer will take a little time to re-render it. |
|
Is it possible to fix the scale when evaluating the loglike and derivatives. I ran into some problem with GLM with concentrated out scale. I don't have a clear picture yet, about all the details and what it will affect and how we need to handle this. The main point: However, when we use the score_obs (OPG) or hessian method for cov_params, then it differs whether that derivatives are only the partial derivatives or are derivatives that include an indirect effect changes in the concentrated out scale. I ran into this initially in unit tests when the analytical hessian didn't match up with a numerical derivative of loglike, because loglike adjusted internally also the scale. I have no idea (I didn't look) how this applies in your case. |
Yes, I have a with self.model.ssm.fixed_scale(scale):
self.model.loglike(self.params)although now that you mention it, I should double-check that I am actually using it in all of the appropriate circumstances (a quick looks suggests I'm only using it in |
|
The main difference in GLM was that we possibly need different derivatives during optimization from the derivatives when computing the cov_params. In your case you might have to use a hessian with fixed scale when computing cov_params. |
I've tried to do some testing of this issue:
So I am thinking that it is best not to employ the fixed scale in computing the |
|
My guess is that it is fine in the linear gaussian case, i.e. the practical difference is small, if it exists at all. In GLM the derivatives are analytical expressions that don't take a changing, concentrated out effect of the scale into account, so they were inconsistent with the derivatives of the concentrated likelihood. After fixing the scale, the 'newton' method worked much better in the optimization. Checking #4616, https://github.com/statsmodels/statsmodels/pull/4620/files#diff-bacf4e7dec296273aba74b04be3c42f8R993 again. Another point: linear gaussian with concentrated out scale is just least squares (at least for OLS), so the scale might drop out of the derivatives (I guess). I never worked enough with MLE for ARMA/SARIMAX to have any specific intuition for the scale effect in it. To conclude, I guess my comments are mostly false alarm in the linear gaussian case, but you might have to look again for nonlinear or non-gaussian models. |
|
Thanks for that explanation - that makes sense to me. To be honest I'm surprised at how little variation there is in the information matrix / Hessian when I use different constructions (e.g. fixed scale vs not fixed, complex step vs finite difference, Harvey's score iterations vs complex step, etc.). It looks like things are at least okay the way they are, and so I guess I plan to merge this tonight if it remains green with the latest commit, unless of course there are concerns. |
|
@josef-pkt thanks for your thoughts / help! |
Adds the ability to perform filtering where the scale is concentrated out of the log-likelihood function, but is computed along with the filtering recursions.
Also adds an argument
concentrate_scaleto SARIMAX to allow estimating the model with the scale concentrated out. From very quick tests, it appears that we can get a higher optimized log-likelihood if there are a lot of parameters.The filter output between the concentrated and non-concentrated models is the same, since they just need to be rescaled (and I do the relevant rescaling when constructing the filter results).
Not really an "essential" feature, but may be useful.