Skip to content

ENH: state space: more flexible return options in MLEModel.fit - #3513

Closed
ChadFulton wants to merge 2 commits into
statsmodels:masterfrom
ChadFulton:ss-fit-ret
Closed

ChadFulton wants to merge 2 commits into
statsmodels:masterfrom
ChadFulton:ss-fit-ret

Conversation

@ChadFulton

Copy link
Copy Markdown
Member

Deprecates the return_params argument to MLEModel.fit and adds a new argument results which can be params, mlefit, filter, or smooth. Previously, it always returned smoothed results unless return_params=True in which is just returned a vector of parameters. Sometimes we may want a middle ground.

@ChadFulton

Copy link
Copy Markdown
Member Author

I also plan to merge this PR quickly if green, seems pretty innocuous. But as always, I can wait if there are concerns.

@josef-pkt

Copy link
Copy Markdown
Member

The main question is consistency of the keyword across models. We will need consistency across models when we start to add bootstrap or similar generic methods that should apply to hopefully all models.

so the API change is not completely innocuous

@ChadFulton

Copy link
Copy Markdown
Member Author

The main question is consistency of the keyword across models. We will need consistency across models when we start to add bootstrap or similar generic methods that should apply to hopefully all models.

so the API change is not completely innocuous

Sure, not completely innocuous. Something to be discussed?

Are you concerned about the keyword name, or the argument type (e.g. strings here)?

From a quick search across all the fit methods in Statsmodels, it looks like most (all?) do not give a choice of results, except Markov switching and State space, which both currently use return_params.

@josef-pkt

Copy link
Copy Markdown
Member

I haven't thought much about it yet. I just wanted to signal that this needs some discussion or thought.
It will set a precedence for other models. AFAIR, we have skip_hessian internally as the only related keyword.

I think switching to a string makes sense.
I think results as the name of the keyword is a bit confusing because it usually/always refers to a results instance. Adding a verb might be better, e.g. limit_results or something better than that.

@josef-pkt

Copy link
Copy Markdown
Member

In some of the hypothesis test we use return_results as a boolean to choose between a tuple of numbers and some kind of results instance.

All this is still a bit of a grey zone, but we should work on establishing a consistent API for these extra choices for the returns.

@ChadFulton

Copy link
Copy Markdown
Member Author

Okay, sounds good, I'll let this PR rest for a while. I'm happy with limit_results or results_type or other things.

I think the reason for this PR is aligned with your concern, in that return_params was too inflexible and we needed some mechanism for selecting just the results that are required.

(This came up for me in designing a subclass, where I wanted the mlefit object so that return_params was too restrictive, but I did not need the rest of the output of smooth, so that it was a waste to do the smoothing and then just extract the mlefit object from the smoothed results. But in the short term there's no problem with me continuing to do that)

@coveralls

coveralls commented Feb 26, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.004%) to 90.278% when pulling 71b9e5e on ChadFulton:ss-fit-ret into ab03a63 on statsmodels:master.

@ChadFulton

Copy link
Copy Markdown
Member Author

I was hoping we could revisit this PR.

To summarize, the main idea is that there may be different types of output possible from a fit call, e.g. maybe you only want parameters back, or maybe you want a full results object, or something in between.

In state space models, we would have the following options:

  • params: just an array of parameters
  • mlefit: the output from the base MLEModel statsmodels fit method
  • filtered: output from running the Kalman filter but not the Kalman smoother
  • smoothed: full output

Code-wise the change is pretty trivial, but the issue is that this may set a precedent for future API.

I like @josef-pkt's idea of a verb, like limit_results.

@josef-pkt

Copy link
Copy Markdown
Member

Mainly as general considerations

We need a generic keyword across models that should have at least the default full Results instance and as second option to return just params. Other keywords could be model specific.

Beside bootstrapping params we also had a use case of just requiring params, when calling an auxiliary model as a preliminary estimator, start_params = aux_model.fit(..).params where I thought this would be useful.
However, I extended the start_params computation for the main models so now it also needs resid and predict. (in count models)

Something like results_type or results_xxx for some xxx would work in a more than boolean way which it looks like you need or want here. Other models need only a boolean keyword, but that sounds too restrictive. results alone as keyword is too ambiguous, IMO.

Another design alternative, if you return a results instance is to trigger the extra computation in a lazy way from the results instance. That's our main pattern, but I don't know if that would work here. It also doesn't work well for some other Results options like cov_type.

@josef-pkt

Copy link
Copy Markdown
Member

I'm now thinking also in hypothesis test about 3 cases for return_results instead of binary.
The extra case is to return just the (pivotal) test statistic, which would be all that we need for bootstrap or similar. In some previous cases I had written an extra function that just computes the mean.

@bashtage

bashtage commented Aug 3, 2020

Copy link
Copy Markdown
Member

@ChadFulton Does this PR still have any value? statespace has moved so far I suspect no.

@ChadFulton

Copy link
Copy Markdown
Member Author

I agree, thanks for the nudge to get this closed @bashtage

@ChadFulton ChadFulton closed this Aug 3, 2020
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