Skip to content

ENH: State space: dynamic regression class - #5299

Open
AliHeggie wants to merge 14 commits into
statsmodels:mainfrom
AliHeggie:dynamic-regression
Open

AliHeggie wants to merge 14 commits into
statsmodels:mainfrom
AliHeggie:dynamic-regression

Conversation

@AliHeggie

@AliHeggie AliHeggie commented Oct 4, 2018 •

Copy link
Copy Markdown

Implements a dynamic regression model of the form

$y_t = x_t \beta_t + \epsilon_t$

x and \beta can be vectors to support multiple exogenous regressors.

Each exogenous regressor in $\beta$ evolves according to a univariate unobserved components model.

At present $\beta$ may have an irregular, local level and linear trend component. The local level and trend can be deterministic or stochastic.

It is intended to extend the modelling options for $\beta$ to include the full suite of terms routinely found in unobserved components models including dummy seasonal terms, harmonic seasonal terms and autoregressive terms.

This is my fist contribution to an open source project so I don't expect the code to be acceptable in it's current state. In particular tests and documentation are required.

I'd appreciate any and all pointers for how to get this to a state where it is a valuable addition to Statsmodels.

Discussion of this feature can be found here:
https://groups.google.com/forum/?hl=en#!topic/pystatsmodels/rEbbZJPxzIo

Added numpy style docstrings and created new methods/properties to
create system matrices
Base version of a dynamic regression model with regression coefficients
evolving according to a unobserved components model. At present it is
possible for the UC model to have a local leve, trend and irregular
component.
@coveralls

coveralls commented Oct 4, 2018 •

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.03%) to 84.174% when pulling cfbb863 on AliHeggie:dynamic-regression into 1883f8a on statsmodels:master.

import warnings

# Construct the model
class dynamic_regression(sm.tsa.statespace.MLEModel):

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.

Needs to follow standard Python name rules, e.g. DynamicRegression

@bashtage bashtage left a comment

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.

Also needs tests.

it's value is not False, then it is included in the model. If the
corresponding value is "deterministic" then the state
corresponding to that component will not have a disturbance term.
dynamic_regression : bool

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.

Should be just dynamic or possibly update


Parameters
----------
unconstrained : array_like

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.

array_like is for inputs that could be various classes, e.g. ndarray, Series, DataFrame. Should be ndarray or (I don't like) array (since array is a python type, and confuses some introspection)

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.

In this case, I think array_like is okay since we might actually have pd.Series (e.g. if you were to pass in the params from a previously estimated results object).

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 think I was looking at the output, which is always a ndarray. I take your point on the input.

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.

Good point - I should change that in the base class too.

return constrained

def untransform_params(self, constrained):
"""

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.

Are these useful publically or should they be private?

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.

transform_params and untransform_params are part of the typical state space class structure, so they're public (for better or worse)

unconstrained[start:] = np.sqrt(constrained[start:])
return unconstrained

def plot_dynamic_regression(results,which="smoothed",figsize=None,

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.

Need spaces. Should run

flake8 --isolated statsmodels/tsa/statespace/dynamic_regression.py

and fix any issues.

unconstrained[start:] = np.sqrt(constrained[start:])
return unconstrained

def plot_dynamic_regression(results,which="smoothed",figsize=None,

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.

And maybe this function should be included as a results method?

A model in which the coefficients of the exogonous covariates are
described by a univariate unobserved components model. At present
only local level and trend models are available for the coefficinets.
Ulitmately it is intended to add the full suite of components that

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.

typo in "Ultimately"

only local level and trend models are available for the coefficinets.
Ulitmately it is intended to add the full suite of components that
are standard in unobserved components models:

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.

Can you include the latex definition of the model here?

exog_models : dict or list of dicts, optional
Either a dict, a list containing a single dict, or a list of dict
s, one for each exogonous regressor.
Admisble keys are the supported component types: 'irregular',

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.

typo in "Admissible"

self.ssm['obs_cov'] = self.obs_cov
self.state_cov = np.zeros((self.k_state_cov,self.k_state_cov))
self.ssm['state_cov'] = self.state_cov

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.

Would be great if you could implement param_names

@ChadFulton

Copy link
Copy Markdown
Member

I've added some specific comments, but there are some more general ones:

  • We need to find a good reference for at least the basic TVP class of models
  • As Kevin said, implementing unit tests are important - at the most basic level, these can be testing that the state space model matrices are set up correctly, but there should be a package somewhere (e.g. in R) that we can test results against too.

Either a dict, a list containing a single dict, or a list of dict
s, one for each exogonous regressor.
Admisble keys are the supported component types: 'irregular',
'local_level', and 'trend'. If a key is included in the dict and

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 think it should be level and trend, to match what UnobservedComponents has.

@ChadFulton ChadFulton changed the title Dynamic regression ENH: State space: dynamic regression class Oct 5, 2018
@ChadFulton ChadFulton added this to the 0.11 milestone Oct 5, 2018
Many fixes in accordance with flake8. Rename base class to
DynamicRegression. Rename parameter, dynamic_regression to dynamic.
@codecov

codecov Bot commented Oct 7, 2018 •

Copy link
Copy Markdown

Codecov Report

Merging #5299 into master will decrease coverage by 0.04%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #5299      +/-   ##
==========================================
- Coverage   81.71%   81.67%   -0.05%     
==========================================
  Files         569      569              
  Lines       89710    89754      +44     
  Branches    10028     9304     -724     
==========================================
- Hits        73306    73305       -1     
- Misses      14132    14171      +39     
- Partials     2272     2278       +6
Impacted Files Coverage Δ
statsmodels/iolib/openfile.py 66.66% <0%> (-9.1%) ⬇️
statsmodels/datasets/utils.py 78.39% <0%> (-4.94%) ⬇️
statsmodels/iolib/foreign.py 70.63% <0%> (-1.99%) ⬇️
statsmodels/base/wrapper.py 89.85% <0%> (-1.45%) ⬇️
statsmodels/tsa/tests/test_arima_process.py 98.93% <0%> (-1.07%) ⬇️
...ar/tests/JMulTi_results/parse_jmulti_var_output.py 90.16% <0%> (-1.02%) ⬇️
statsmodels/iolib/tests/test_foreign.py 97.63% <0%> (-0.79%) ⬇️
statsmodels/tsa/tsatools.py 89.16% <0%> (-0.62%) ⬇️
statsmodels/discrete/tests/test_discrete.py 98.66% <0%> (-0.6%) ⬇️
...r/tests/JMulTi_results/parse_jmulti_vecm_output.py 88.25% <0%> (-0.58%) ⬇️
... and 14 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 1883f8a...cfbb863. Read the comment docs.

@AliHeggie

Copy link
Copy Markdown
Author

Just pushed a bunch of commits to address some of the above.

The major outstanding issue is tests and documentation improvement I think.

According to the Codacy report there is a syntax problem on line 186 but I'm not sure what this is - seems like a perfectly valid f-string to me.

Previously param_names and set_param_indices did not correctly handle
names and indices of freq_seasonal and stochastic_freq_seasonal
components
@josef-pkt josef-pkt modified the milestones: 0.11, josef Oct 5, 2019
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.

6 participants