Skip to content

ENH: State space: allow concentrating scale out of log-likelihood - #3480

Merged
ChadFulton merged 32 commits into
statsmodels:masterfrom
ChadFulton:ss-conc-lik
Jun 21, 2018
Merged

ChadFulton merged 32 commits into
statsmodels:masterfrom
ChadFulton:ss-conc-lik

Conversation

@ChadFulton

Copy link
Copy Markdown
Member

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_scale to 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.

@coveralls

coveralls commented Feb 13, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.01%) to 90.312% when pulling 34dffc4 on ChadFulton:ss-conc-lik into e757e8b on statsmodels:master.

@ChadFulton

Copy link
Copy Markdown
Member Author

In case anyone's interested in the details: http://nbviewer.jupyter.org/gist/ChadFulton/b0479cc3faecf29503b37412fdcbe6c6
I'll probably flesh this out a bit more with some examples. I'm not sure if it's useful in the documentation example notebooks or if it's too detailed for that.

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 set_filter_method or mod.ssm.filter_concentrated = True).

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 KalmanFilter class as a method: fixed_scale. This allows temporarily setting a fixed scale, which can be handy when forecasting or simulating, etc.

Finally, as I mentioned above, I added a concentrate_scale option to the SARIMAX class, which allows estimating the model with one less parameter.

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.

@ChadFulton

Copy link
Copy Markdown
Member Author

I think most of the PR is actually pretty straightforward.

Over 50% is just the new unit tests.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.06%) to 90.354% when pulling 91e0339 on ChadFulton:ss-conc-lik into 18983ea on statsmodels:master.

@coveralls

coveralls commented Feb 22, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.05%) to 90.347% when pulling 91e0339 on ChadFulton:ss-conc-lik into 18983ea on statsmodels:master.

@ChadFulton

Copy link
Copy Markdown
Member Author

Rebased, also one of the test precisions reduced to make the Appveyor happy.

I should note that some of the test precisions in test_sarimax.py:test_concentrated_scale may look a little low, but there are a couple of things to note:

  1. The low precision is only needed for some of the most complicated models in the test (which runs over I think 64 combinations of models for coverage).
  2. These are comparing output from (1) running the usual Kalman filter recursions versus (2) running the Kalman filter recursions with observation and state covariance first rescaled, then the scale computed as the MLE estimate, and then the output rescaled. So there's a lot of room for a little rounding / numerical error to creep in.
  3. I have tests in the test_concentrated.py file that are higher precision.

@coveralls

coveralls commented Feb 22, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.05%) to 90.355% when pulling 5eb67c1 on ChadFulton:ss-conc-lik into 898ddfc on statsmodels:master.

@ChadFulton

Copy link
Copy Markdown
Member Author

I will probably squash this into a single commit, since some of these were mistakes / corrections.

@ChadFulton

ChadFulton commented Feb 26, 2017 •

Copy link
Copy Markdown
Member Author

The RecursiveLS model previously did an "ad-hoc" scale concenatration, in the most recent commit, I replace that with the more general approach added in this PR.

This is all I have planned for this PR (except for squashing).

Edit: although some of the work in this PR (in mlemodel.py) is duplicated in #3514, so I would probably wait for that to be merged first, and then rebase this.

@coveralls

coveralls commented Feb 26, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.03%) to 90.34% when pulling 0ec4e2d on ChadFulton:ss-conc-lik into 898ddfc on statsmodels:master.

@ChadFulton

Copy link
Copy Markdown
Member Author

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.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.03%) to 90.34% when pulling 0ec4e2d on ChadFulton:ss-conc-lik into 898ddfc on statsmodels:master.

@coveralls

coveralls commented Feb 26, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.07%) to 83.542% when pulling 3da6a36 on ChadFulton:ss-conc-lik into 9f8d3c1 on statsmodels:master.

@ChadFulton

Copy link
Copy Markdown
Member Author

This is now rebased and also I added support for concentrating the scale out in the case of exact diffuse initialization.

@ChadFulton

Copy link
Copy Markdown
Member Author

Now rebased again, fixed a couple of bugs.

@codecov-io

codecov-io commented Jun 14, 2018 •

Copy link
Copy Markdown

Codecov Report

Merging #3480 into master will increase coverage by 0.07%.
The diff coverage is 96.67%.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
statsmodels/tsa/statespace/tests/test_options.py 100% <ø> (ø) ⬆️
...models/tsa/statespace/tests/test_dynamic_factor.py 96.6% <100%> (+0.04%) ⬆️
statsmodels/tsa/statespace/sarimax.py 94.17% <100%> (+0.76%) ⬆️
...tsmodels/tsa/statespace/tests/test_concentrated.py 100% <100%> (ø)
statsmodels/regression/recursive_ls.py 94.42% <100%> (+0.02%) ⬆️
statsmodels/tsa/statespace/kalman_smoother.py 87.91% <100%> (+2.27%) ⬆️
statsmodels/tsa/statespace/mlemodel.py 86.19% <86.66%> (+0.18%) ⬆️
statsmodels/tsa/statespace/kalman_filter.py 87.75% <88.88%> (+0.12%) ⬆️
statsmodels/tsa/statespace/tests/test_sarimax.py 99.33% <98%> (-0.15%) ⬇️
statsmodels/stats/descriptivestats.py 24.13% <0%> (ø) ⬆️
... and 5 more

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 9f8d3c1...3da6a36. Read the comment docs.

@ChadFulton

Copy link
Copy Markdown
Member Author

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!

@ChadFulton

Copy link
Copy Markdown
Member Author

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.

@ChadFulton

Copy link
Copy Markdown
Member Author

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

Of course I put up the version with no cell output...updated now, but nbviewer will take a little time to re-render it.

@josef-pkt

Copy link
Copy Markdown
Member

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:
When we take a numerical derivative of the concentrated loglike, then we get the indirect effect through a change in scale. That is, the numerical derivative of the concentrated loglike is not the partial derivative with respect to the params keeping the scale fixed.
This is not a problem in OLS because loglike is essentially stuck on at the end. parameter estimates and importantly cov_params are independent of the loglike. We get the scale essentially as post-estimation auxiliary statistic or parameter.

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.

@ChadFulton

Copy link
Copy Markdown
Member Author

Is it possible to fix the scale when evaluating the loglike and derivatives.

Yes, I have a fixed_scale context manager, so that you can e.g. do:

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 simulate and impulse_responses but I can't remember if there was a reason I wouldn't do it in other places...)

@josef-pkt

Copy link
Copy Markdown
Member

The main difference in GLM was that we possibly need different derivatives during optimization from the derivatives when computing the cov_params.
In GLM the solution was to fix the scale during the optimization because the parameter estimates are independent of the scale estimate.

In your case you might have to use a hessian with fixed scale when computing cov_params.

@ChadFulton

Copy link
Copy Markdown
Member Author

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:

  • The cov_params estimates are pretty close between the concentrated model and the non-concentrated model, and this is true regardless of whether I employ a fixed scale in computing the hessian and/or score_obs (for the OPG estimator)
  • In general, the oim and approx methods are very close (e.g. to about 1e-6 or 1e-7), while the opg method is somewhat farther apart (1e-4 or in some cases even 1e-2).
  • Employing the fixed scale sometimes improves the agreement between the concentrated and non-concentrated models for some elements of the cov_params matrix but often (more often?) it makes it worse for some elements.

So I am thinking that it is best not to employ the fixed scale in computing the hessian or score_obs, what do you think?

@josef-pkt

Copy link
Copy Markdown
Member

My guess is that it is fine in the linear gaussian case, i.e. the practical difference is small, if it exists at all.
For the optimization the main point is to have consistent definitions of loglike and derivative. But AFAIK you are using numerical derivatives.

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.
My failing cases where highly non-normal with nonlinear mean functions. In those cases the indirect effect through changes in the scale are much larger.
The rtol in comparing analytical and numerical derivatives is only 5% which I think comes from the large nonlinearities.

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.
(proof by numerical example)

@ChadFulton

Copy link
Copy Markdown
Member Author

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.

@ChadFulton
ChadFulton merged commit 01ce369 into statsmodels:master Jun 21, 2018
@ChadFulton

Copy link
Copy Markdown
Member Author

@josef-pkt thanks for your thoughts / help!

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.

4 participants