Skip to content

ENH: Statespace - add OIM and OPG covariance matrices - #2356

Merged
josef-pkt merged 22 commits into
statsmodels:masterfrom
ChadFulton:ss-info-mat
Jul 3, 2015
Merged

josef-pkt merged 22 commits into
statsmodels:masterfrom
ChadFulton:ss-info-mat

Conversation

@ChadFulton

Copy link
Copy Markdown
Member

Greatly improves covariance matrix and standard error calculation in statespace models.

Primary changes:

  • Adds an observed information matrix calculation based on Harvey (1989), which only uses the numerical gradient rather than the numerical Hessian
  • Adds an outer product of gradients information matrix
  • Adds the ability to specify information_matrix_type in fit and in the results class to govern whether the complex step Hessian was used (cs), observed information matrix (oim), outer product of gradients (opg), or the delta method on the complex step Hessian (delta). Default is opg, which is guaranteed to yield a covariance matrix with non-negative diagonal elements, and which is the default in Stata.

Other changes

  • Adds loglikeobs function (used in OPG calculation)
  • Adds the ability to specify what Hessian you want to use during optimization, via the optional hessian_method parameter to fit(). Default is as before, the complex step Hessian.
  • Add notation to Summary showing which type of information matrix was used
  • Add unit tests for OPG, OIM covariance matrices.

Questions:

  • I called the parameter in the results class information_matrix_type rather than cov_type because I believe we want to reserve the latter for specifying e.g. HC#, which could be calculated regardless of which information matrix was used.

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.

Three levels of loops, but I'm not sure there's anything to be done about it. It's probably only run once though, so it shouldn't be a problem.

@ChadFulton ChadFulton changed the title ENH: Add OIM and OPG covariance matrices ENH: Statespace - add OIM and OPG covariance matrices Apr 9, 2015
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.02%) to 83.93% when pulling 716cc88 on ChadFulton:ss-info-mat into a1f53b0 on statsmodels:master.

@ChadFulton

Copy link
Copy Markdown
Member Author

The way I added the notation to summary is as follows:

                           Statespace Model Results                           
==============================================================================
Dep. Variable:                    wpi   No. Observations:                  124
Model:               SARIMAX(3, 1, 2)   Log Likelihood                -136.030
Date:                Wed, 08 Apr 2015   AIC                            284.060
Time:                        20:52:29   BIC                            300.981
Sample:                    01-01-1960   HQIC                           290.934
                         - 10-01-1990                                         
==============================================================================
                             OPG                                              
                 coef    std err          z      P>|z|      [95.0% Conf. Int.]
------------------------------------------------------------------------------
ar.L1          0.7304      0.472      1.548      0.122        -0.195     1.655
ar.L2         -0.0582      0.552     -0.105      0.916        -1.140     1.024
ar.L3          0.2347      0.165      1.427      0.154        -0.088     0.557
ma.L1         -0.2141      0.467     -0.458      0.647        -1.130     0.702
ma.L2          0.0348      0.351      0.099      0.921        -0.652     0.722
sigma2         0.5291      0.055      9.657      0.000         0.422     0.636
==============================================================================

@josef-pkt

Copy link
Copy Markdown
Member

As preliminary comment:
There needs to be a clear distinction between an optimization option and a covariance for inference option.
for example as HC sandwich needs the "real" hessian, using OPG for the hessian would just cancel and result in non-sandwich OPG, IIUC.
Also, we need to watch out for the state of the attributes when changing the optimization option, and/or make it different than the cov_type

I did only a quick skimming of the changes, very useful

@josef-pkt

Copy link
Copy Markdown
Member

I'll go through the interface later, could be after pycon.
I added cov_type in summary in the top columns, IIRC, in OLS also in the text at the end of the summary table. But that's cov_type, not necessarily what was used for the optimization

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.02%) to 83.93% when pulling 716cc88 on ChadFulton:ss-info-mat into a1f53b0 on statsmodels:master.

@ChadFulton

Copy link
Copy Markdown
Member Author

for example as HC sandwich needs the "real" hessian, using OPG for the hessian would just cancel and result in non-sandwich OPG, IIUC.

As I understand it, we have the following options for inference purposes:

  • CS (using the complex step Hessian approximation)
  • OPG
  • OIM
  • HCE as inv(CS * OPG * CS) / nobs
  • [or] HCE as inv(OIM * OPG * OIM) / nobs

So it is clear that the we couldn't have cov_type='robust' in combination with information_matrix_type='opg', but I believe we could have cov_type='robust' in combination with either information_matrix_type='oim' or information_matrix_type='cs'.

There needs to be a clear distinction between an optimization option and a covariance for inference option.

From e.g. the BHHH paper, my understanding is that any of the first three are acceptable for use in optimization, i.e. dictating the direction/stepsize of the quasi-Newton method.

Currently there are two different parameters governing these issues:

  • hessian_method governs which is used for optimization
  • information_matrix_type (or maybe we will switch it to cov_type) governs which is used for inference

Let me know if I have any of this wrong.

@ChadFulton

Copy link
Copy Markdown
Member Author

I added cov_type in summary in the top columns, IIRC, in OLS also in the text at the end of the summary table.

Ah, I see. Well I'm happy to do either, I took the cue here from Stata's output.

But that's cov_type, not necessarily what was used for the optimization

Hopefully my above comment clarified that what is reported in the table is the information matrix approximation used for inference.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.02%) to 83.93% when pulling 0f1b448 on ChadFulton:ss-info-mat into a1f53b0 on statsmodels:master.

@josef-pkt

Copy link
Copy Markdown
Member

(without looking at the code)

Your list of cov_type sounds right. If we allow for pure OLS/WLS cases without lagged endogenous, then we could also use HAC, AFAIU.

[or] HCE as inv(OIM * OPG * OIM) / nobs

I haven't seen this form yet , OPG is an inverse. I have seen the form inv(OIM) * S'S * inv(OIM) (I don't remember the nobs division.

From what I understand of your description, information_matrix_type should be renamed to cov_type since it specifies the cov_params_default

about summary: it's mainly a taste question. I don't like the line with only a single entry OPG (which is a two line column header if all other headers are only one line).
Also p-values and confidence intervals are based on the same cov_type, which implicitly would have the same OPG or cov_type in the description.

@ChadFulton

Copy link
Copy Markdown
Member Author

I haven't seen this form yet , OPG is an inverse. I have seen the form inv(OIM) * S'S * inv(OIM) (I don't remember the nobs division.

You're absolutely right, this is just my fault for not being clear what I was referring to, and for having a typo. For my own future reference on this PR, let me lay out things in a way that is, I think, corrent. Here is how Hamilton (1994) defines things (pg 143-145):

  • theta ~ N(theta_0, inv(T * information_matrix) )

where information_matrix can be defined as:

  • information_matrix_cs = (1/T) * (evaluated hessian)
  • information_matrix_opg = (1/T) * sum_{t=1}^T ( gradient_t * gradient_t')

and then the asymptotic approximation to the covariance matrix can be defined as:

  • cov_matrix_2d = inv(T * information_matrix_cs)

    = inv(T * (1/T) * (evaluated hessian))

    = inv(evaluated hessian)

  • cov_matrix_opg = inv(T * information_matrix_opg)

    = inv(T * (1/T) * sum_{t=1}^T ( gradient_t * gradient_t'))

    = inv( sum_{t=1}^T ( gradient_t * gradient_t') )

and then, the quasi-maximum likelihood standard errors for approximate use when errors may not be IID Gaussian is given by:

  • cov_matrix_robust = inv(T * information_matrix_cs * inv(information_matrix_opg * information_matrix_cs)

    = inv( T * (1/T) * (evaluated hessian) * inv((1/T) * sum_{t=1}^T ( gradient_t * gradient_t')) * (1/T) * (evaluated hessian))

    = inv((evaluated hessian) * inv(sum_{t=1}^T ( gradient_t * gradient_t')) * (evaluated hessian) )

The final note in this class of models is that information_matrix_cs is an approximation to the information matrix by directly numerically approximating the second derivative. Harvey (1989) shows how in the asymptotic case we can find an analytic form of the information matrix based on the Hessian that only relies on the numerically evaluated gradient, so that we have an alternative measure:

  • information_matrix_oim is performing the same approximation (evaluating the Hessian) as information_matrix_cs, but in a slightly different way.

@ChadFulton

Copy link
Copy Markdown
Member Author

With that in mind, I guess my question is how to integrate the robust covariance matrices.

It looks like, to maintain consistency, I should define a get_robustcov_results method in the results class to replace the class with one where robust standard errors are reported by default.

Then for cov_type (previously information_matrix_type), we will have the following options:

  • opg
  • oim
  • cs
  • robust_oim
  • robust_cs
  • Maybe it would be good to have a robust option which defaults to robust_oim in case people don't want to specify?

I do not know if there is a better name for the robust option. I believe the "robustness" appeals to the work in White (1982) on quasi-maximum likelihood with misspecified densities (Hamilton and Stata cite this work). As I understand it, is distinct from the MacKinnon and White HCE and the Newey and West HAC. But I am not familiar with this area, so I may be incorrect.

So perhaps robust or qmle are candidates?

@ChadFulton

Copy link
Copy Markdown
Member Author

(And there's no urgency here, I'm just recording thoughts; so things can just sit until after PyCon or whenever.)

@ChadFulton

Copy link
Copy Markdown
Member Author

about summary: it's mainly a taste question. I don't like the line with only a single entry OPG (which is a two line column header if all other headers are only one line).
Also p-values and confidence intervals are based on the same cov_type, which implicitly would have the same OPG or cov_type in the description.

Good points, I will change the behavior to put the covariance type in the top table.

@ChadFulton

Copy link
Copy Markdown
Member Author

It's now open season for GSoC and this is the first thing on my list. I still need to change the behavior of the covariance matrix printing in summary, but still to decide is the naming for the cov_type argument values. It looks like we want:

  • opg (default)
  • oim
  • cs
  • robust_oim or qmle_oim
  • robust_cs or qmle_cs
  • robust or qmle (alternate name for robust_oim)

Whether or not to use robust vs qmle is up to you. The former is probably more in line with Statsmodels lingo, but I don't know if it has a specific meaning that would not be satisfied in this case?

@ChadFulton

Copy link
Copy Markdown
Member Author

This commit fixes the two outstanding issues:

  • Rename information_matrix_type to cov_type
  • Add robust (QMLE) covariance calculation: adds robust_oim, robust_cs, and robust (alias for robust_oim)

Looking back over the comments, I think this PR still needs some code review, but I don't have anything else planned to do to it right now.

Comment thread statsmodels/tsa/statespace/mlemodel.py Outdated

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.

oim Harvey is an approximation, is it?
cs is real hessian.

If I remember this correctly, then it might be better to use oim for cs, and oim_harvey or something like this for current oim

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.

i don't see why users should set hessian method. We should have cov_type as explicit keyword, following my pattern in other models.

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.

oim Harvey is real (analytic) Hessian but it requires the gradient, which we only have numerically. So it it sort of a hybrid.

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.

cov_type is an allowed argument, but following the pattern of other models (ARIMAModel, LikelihoodModel, I left it in the kwargs. I can make it explicit though.

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.

Users can set the hessian method because there are three approximations to the Hessian available, and from BHHH (e.g. p655) we need to use a positive definite matrix that approximates the Hessian. In some cases, I've found the cs Hessian approximation may not be positive definite.

So to my understanding, there are two uses for the Hessian here:

  1. In optimization (i.e. choosing the direction and stepsize in the optimizer), and that can be affected via hessian_method.
  2. In inference, and that can be affected via cov_type.

We don't have to have the first one, and I can just eliminate hessian_method.

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.

about cov_type: LikelihoodModel is a base class and not all subclasses have the cov_type argument. I thought ARIMA doesn't have a covtype choice.
The final models for the user OLS, WLS, Logit, Poisson, ... and SARIMAX should have and explicit cov_type argument in fit. We can put it explicitly in the fit of a class if it is implemented for the subclasses. I don't know where in the class hierarchy this would be the case for the statespace models.

hessian_method: AFAIU it's irrelevant or superseded by cov_type for inference
I guess it can be useful for the optimization, but if it's only for that, then I would make that more explicit and call it optim_hessian. (which I guess is only relevant if the optimizer uses the hessian. So maybe we should hide hessian_method in the kwargs with the other optimization options.

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.

about cov_type: LikelihoodModel is a base class and not all subclasses have the cov_type argument. I thought ARIMA doesn't have a covtype choice.
The final models for the user OLS, WLS, Logit, Poisson, ... and SARIMAX should have and explicit cov_type argument in fit. We can put it explicitly in the fit of a class if it is implemented for the subclasses. I don't know where in the class hierarchy this would be the case for the statespace models.

I will make this explicit in the MLEModel, since it should be appropriate for all state space models.

hessian_method: AFAIU it's irrelevant or superseded by cov_type for inference
I guess it can be useful for the optimization, but if it's only for that, then I would make that more explicit and call it optim_hessian. (which I guess is only relevant if the optimizer uses the hessian. So maybe we should hide hessian_method in the kwargs with the other optimization options.

I changed the name to optim_hessian and added a note that it's only used if the optimizer uses the Hessian. I didn't subsume it in kwargs because the base class only checks if a hessian method is defined in the class, and optim_hessian is only used to tell the state space class which method to use. So it's not a generic optimization option. But I can hide it if you prefer.

@ChadFulton

Copy link
Copy Markdown
Member Author

Rebased. Good to merge when green.

Comment thread statsmodels/tsa/statespace/mlemodel.py Outdated

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.

I had forgotten, privatize this with leading underscore. This does not work for creating a correct results instance if some of the cached attributes are already set.
The use_self option only works if none of the cached properties like bse have been set.

@josef-pkt

Copy link
Copy Markdown
Member

privatizing get_robustcov_results is important, because otherwise users might be mislead.
Docstring should mention that it only works for changing the existing results instance if no properties have been cache.

We really need to switch to making this internal use only.
(linear model was easier because it's not difficult to recreate a new results instance, but it's very messy or impossible in almost all other models.)

@josef-pkt

Copy link
Copy Markdown
Member

I will merge when TravisCI comes back green.

I think this is a very important PR, both in the direction of OPG for numerical problems and to robust for unspecified heteroscedasticity (and some deviations from normality)
The only extension I can think of is adding HAC, which might not be valid in all cases. I don't know the relevant literature but inference for OLS with ARMA errors can be made robust with HAC w.r.t. misspecified ARMA, I guess.

josef-pkt added a commit that referenced this pull request Jul 3, 2015
ENH: Statespace - add OIM and OPG covariance matrices
@josef-pkt
josef-pkt merged commit 0ddfba9 into statsmodels:master Jul 3, 2015
@josef-pkt josef-pkt modified the milestones: 0.7, 0.8 Jul 3, 2015
@ChadFulton

Copy link
Copy Markdown
Member Author

The only extension I can think of is adding HAC, which might not be valid in all cases. I don't know the relevant literature but inference for OLS with ARMA errors can be made robust with HAC w.r.t. misspecified ARMA, I guess.

I will add an issue.

Thanks!

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.

3 participants