Skip to content

ENH: Statespace: Add Kalman smoother - #2434

Merged
josef-pkt merged 6 commits into
statsmodels:masterfrom
ChadFulton:kalman-smoother
Sep 9, 2015
Merged

josef-pkt merged 6 commits into
statsmodels:masterfrom
ChadFulton:kalman-smoother

Conversation

@ChadFulton

Copy link
Copy Markdown
Member

Adds pure python Kalman state and disturbance smoothers. Probably wouldn't have enough performance for use real use in estimation (e.g. EM or MCMC), but will be useful for people wanting to get smoothed output when parameters are estimated via MLE.

Also does not currently support smoothing in the case of missing data.

@ChadFulton

Copy link
Copy Markdown
Member Author

@josef-pkt

Copy link
Copy Markdown
Member

just a quick clarification to the description:

"pure python" means it doesn't use the cython extension?
It looks to me you still import it.

@ChadFulton

Copy link
Copy Markdown
Member Author

To get the filtered output (or for parameter estimation, etc.), it uses the usual Cython extension.

It's just that the smoothing itself is done in pure python (see the _kalman_smooth function at the top of the kalman_smoother.py file).

@ChadFulton

Copy link
Copy Markdown
Member Author

Probably wouldn't have enough performance for use real use in estimation (e.g. EM or MCMC), but will be useful for people wanting to get smoothed output when parameters are estimated via MLE.

Whereas MLE estimation only calls the Kalman filter repeatedly, EM and MCMC call both the Kalman filter and smoother repeatedly. So while the Kalman filtering would still be fast, the Kalman smoothing would be slow here. Eventually I'll add a Cython Kalman smoother, but for now I thought something was better than nothing (and since we don't focus on EM or MCMC, the slower python implementation won't be noticeable).

@josef-pkt

Copy link
Copy Markdown
Member

Ok, thanks for clarifying

brief look at the notebook

res_ll = mod_ll.fit()
res_ll = mod_ll.smooth()

looks strange because in python this looks like fit() would be thrown away, the first res_ll will be overwritten.

should this be

res_ll = mod_ll.fit()
res_ll = mod_ll.smooth(result=res_ll)

or is there a "state" of the model so it is equivalent to

mod_ll.fit()
res_ll = mod_ll.smooth()

@ChadFulton

Copy link
Copy Markdown
Member Author

Right now there is a "state" of the model, so that you're right, it's equivalent to:

mod_ll.fit()
res_ll = mod_ll.smooth()

I know we'd like to move away from holding state like that. I plan (and I think we discussed before) to do that by separating the Statsmodels component (MLEModel, Model classes) from the Kalman filtering component (KalmanSmoother, KalmanFilter, Representation classes) and using delegation. Then the Statsmodels component won't hold state, but the Kalman filtering component will (it has to, since it holds the state space matrices which by definition hold state).

After that's done, I suppose that it will look something like your second example. Either

res_ll = mod_ll.fit()
smoothed_ll = mod_ll.smooth(res_ll)

or:

res_ll = mod_ll.fit()
smoothed_ll = mod_ll.smooth(res_ll.params)

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.06%) to 83.97% when pulling 38b9234 on ChadFulton:kalman-smoother into 4b55fa4 on statsmodels:master.

@ChadFulton

Copy link
Copy Markdown
Member Author

Test failure related to old Numpy version (same function failing as in #2431)

@josef-pkt josef-pkt modified the milestone: 0.8 Jun 23, 2015
@josef-pkt josef-pkt mentioned this pull request Jun 24, 2015
3 of 7 tasks
@ChadFulton

Copy link
Copy Markdown
Member Author

Rebased so tests pass.

@ChadFulton

Copy link
Copy Markdown
Member Author

Rebased after decoupling, etc. PRs.

@ChadFulton

Copy link
Copy Markdown
Member Author

Rebased again.

@ChadFulton

Copy link
Copy Markdown
Member Author

Rebased. I think this should be good to merge, the interface issues we discussed above have been fixed (I think) since the rebase after decoupling. See e.g. this notebook for the interface to get smoothed results: http://nbviewer.ipython.org/gist/ChadFulton/06557357b2be654b13a1

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.

blank line before

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.

Thanks.

@josef-pkt

Copy link
Copy Markdown
Member

similar here

looks good from a superficial review, example notebook looks nice.
But we will need feedback after playing with it for a while.

I started to read the Stata documentation for the models related to these PRs, but I would need to read again for a few days to "understand" this.

I'm fine with merging it as is.

@ChadFulton

Copy link
Copy Markdown
Member Author

Rebased on master.

@josef-pkt

Copy link
Copy Markdown
Member

travis the first py2.7 timed out, otherwise green

merging

josef-pkt added a commit that referenced this pull request Sep 9, 2015
ENH: Statespace: Add Kalman smoother
@josef-pkt
josef-pkt merged commit 88e67f2 into statsmodels:master Sep 9, 2015
@ChadFulton
ChadFulton deleted the kalman-smoother branch January 16, 2016 08:43
@josef-pkt josef-pkt mentioned this pull request Feb 9, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants