Repository navigation
ENH: Add linear restrictions to RecursiveLS - #4133
Conversation
Codecov Report
@@ Coverage Diff @@
## master #4133 +/- ##
==========================================
+ Coverage 80.74% 80.93% +0.18%
==========================================
Files 573 577 +4
Lines 87893 88696 +803
Branches 9147 9889 +742
==========================================
+ Hits 70970 71782 +812
+ Misses 14678 14651 -27
- Partials 2245 2263 +18
Continue to review full report at Codecov.
|
a5f75e3 to
92f77f8
Compare
|
A couple of notes:
|
|
Other than those notes, if this comes back green, it is ready to merge. |
|
I will look at it briefly tomorrow (i.e. today) I am using singular cov_params for the fit_constrained and similar results. |
| return numer / denom | ||
|
|
||
| @cache_readonly | ||
| def llf_obs(self): |
There was a problem hiding this comment.
maybe better llf_recursive, IIUC
those are sequential loglike_obs values, and not the loglike evaluated at the final params, is it?
There was a problem hiding this comment.
Ah, this is a good idea, thank you.
There was a problem hiding this comment.
If I do this, I guess I should leave llf as the Kalman filter output rather than the one computed from the recursive residuals, since it might be confusing to have np.sum(res.llf_obs) != res.llf?
|
|
||
| @cache_readonly | ||
| def llf(self): | ||
| return np.sum(self.llf_obs) |
There was a problem hiding this comment.
Does this have a standard MLE interpretation?
e.g. would be use this for comparing models, or for aic, bic
There was a problem hiding this comment.
Yes, this one (computed from the recursive residuals) is the same as the OLS profile log-likelihood function (i.e after concentrating out the scale), the same as you would get from sm.OLS.loglike with scale=None.
|
The constraints look fine to me. I never looked at the meaning of other results statistics like llf if they are defined via recursive residuals. So I don't have much of an idea what's appropriate there. |
|
aside: a lag selection test failed now at least for the second time in master. AFAIR it's one of the failures that Matthew reported for 0.9. |
I think that in the case of recursive least squares, either the Kalman filter loglikelihood or the OLS loglikelihood should be "valid" MLE likelihoods, even if they're not computing exactly the same object. |
|
llf and llf_obs should be consistent, i.e. sum up. |
|
We don't actually have llf_obs in any other model explicitly |
|
That makes sense. I think I'll just leave |
|
Basically the recent commits fix some numerical issues, make some definitions more consistent, provide some more specific documentation about what is being computed (e.g. in Also adds a sort-of test for |
|
If you want to use it for comparison or unit tests, GLM-Gaussian allows linear restriction with fit_constrained |
That's amazing, I should get out of Even better, the |
|
This pull request introduces 1 alert when merging 1ebf8e6 into d8cb151 - view on LGTM.com new alerts:
Comment posted by LGTM.com |
|
Actually now that I think about it. (aside: I didn't look closely yet, but I would like to have |
|
This pull request introduces 1 alert when merging d9005f8 into d8cb151 - view on LGTM.com new alerts:
Comment posted by LGTM.com |
| # used in OLS. Compute new ic based on llf_alternative to compare. | ||
| # TODO: Why do I need a -1 here but not in the unconstrained case? It must | ||
| # have something to do with the fact that GLM does not report the intercept | ||
| # as a degree of freedom so we need to subtract it here, but then why don't |
There was a problem hiding this comment.
I check this tomorrow (which is today)
There was a problem hiding this comment.
Hope you didn't spend too much time on this, turns out that what I wrote there is not true, and the test passes with actual_aic = aic(llf_alternative, res.nobs_effective, res.df_model). Sorry
There was a problem hiding this comment.
Ignore what I just said. The comment in the code is correct.
There was a problem hiding this comment.
I didn't have much time today, so I haven't looked at it yet.
Either it's a bug in my fit_constrained code, or the constant detection in the transformed model fails, or there is an ambiguity in definition of df_model if eg. the constant is part of a constraint, or all of the above.
I don't like it that df_model doesn't include the constant, I just creates pain everywhere and anytime I look at it. :(
There was a problem hiding this comment.
It doesn't look like a bug in fit_constrained,
I'm adjusting both df_model and df_resid for the constraints.
Implicitly this assumes that the constraints are on the slope coefficient.
k_constr = len(q)
res._results.df_resid += k_constr
res._results.df_model -= k_constr
GLM initialize always subtracts 1 independent of whether there is a constant or not, i.e. we assume there always is a constant.
self.df_model = np_matrix_rank(self.exog) - 1
so I don't see anything specific to the constrained case.
| # we need to do that it the unconstrained case? | ||
| actual_aic = aic(llf_alternative, res.nobs_effective, res.df_model - 1) | ||
| assert_allclose(actual_aic, res_glm.aic) | ||
| # TODO: Why does AIC match but BIC does not match? |
There was a problem hiding this comment.
#1733 BIC uses a different definition specific to GLM.
And because of my inertia/reluctance with breaking backwards compatibility it still hasn't been changed.
|
This pull request introduces 3 alerts when merging 9d0920e into d8cb151 - view on LGTM.com new alerts:
Comment posted by LGTM.com |
| # does not report the intercept as a degree of freedom so we need to | ||
| # subtract it here, but then why don't we need to do that it the | ||
| # unconstrained case? | ||
| df_model = res.df_model - 1 if constraints is not None else res.df_model |
There was a problem hiding this comment.
you can also directly check df_model for the example, i.e df_model == 1 in the constrained and ==2 in the unconstrained case
There was a problem hiding this comment.
Interesting. Maybe the df should be different in the RecursiveLS case with constraints; it's set here by the number of diffuse observations, which almost always turns out to be equal to the number of states with diffuse initialization, and so is same here regardless of the constraints.
Since the constraint (in this example) implies that there are is really only independent 1 state, I guess the best way to handle that would be with some kind of modification of the diffuse initialization to also enforce the constraint, but I don't know of a way to do that in the current structure.
| # given `test_ols`, but this is mostly to complement the tests in | ||
| # `test_glm_constrained`) | ||
| endog = dta.infl | ||
| exog = add_constant(dta[['unemp', 'm1']]) |
There was a problem hiding this comment.
unrelated aside
Isn't 'm1' integrated with drift/trend, while infl and unemp don't have a trend/drift?
i.e. it would imply that the params for m1 is zero
e.g. use differenced m1 or m1 deviation from trend ?
???
There was a problem hiding this comment.
Yes, very true. Just a sloppy test :)
|
This pull request introduces 3 alerts when merging f06cdca into d8cb151 - view on LGTM.com new alerts:
Comment posted by LGTM.com |
|
Will merge tonight unless there are concerns or suggestions for improvements. |
| self.df_resid = np.inf # attribute required for wald tests | ||
| # Since we are overriding params with things that aren't MLE params, | ||
| # need to adjust df's | ||
| self.df_model = self.k_diffuse_states |
There was a problem hiding this comment.
if you know that self.k_diffuse_states does not take constraints into account (or as long as it doesn't), then you could explicitly correct it for the number of constraints, i.e. subtract k_constraints if constraints are not None.
There was a problem hiding this comment.
I was considering that approach, but what I was concerned about is that the initialization still has k_diffuse_states states. So, for example, the first few log-likelihood values will still be computed as under the diffuse filter, and the recommendation for that case is that there be a df correction for those observations. Since I can't fix the diffuse initialization itself to take into account the constraints, I wasn't sure if it was appropriate to use a smaller model df.
On the other hand, it would match better what is going in on e.g. GLM, and it is the case that recursive LS should be pretty much just like the usual OLS case, including if there are linear restrictions.
There was a problem hiding this comment.
Assuming the effect of the (diffuse) initialization goes away as more observations are used, I would go for the later outcome, i.e. use the constraints corrected df_model and df_resid.
I don't really understand how diffuse initialization works, but maybe a comment in the Notes section of the docstring mentioning this would be enough.
There was a problem hiding this comment.
Assuming the effect of the (diffuse) initialization goes away as more observations are used, I would go for the later outcome, i.e. use the constraints corrected df_model and df_resid...maybe a comment in the Notes section of the docstring mentioning this would be enough.
Sounds good to me.
I don't really understand how diffuse initialization works
Basically just algebraics of a fully diffuse Gaussian prior for at least some components of the state vector; as the filter processes more observations, eventually the conditional distribution becomes proper.
|
This pull request introduces 3 alerts when merging 160accf into d8cb151 - view on LGTM.com new alerts:
Comment posted by LGTM.com |
|
Thanks @josef-pkt for all the useful comments! |
Allow linear restrictions on the (recursively estimated) least squares parameter estimates.
Needs unit tests.