Alastair Heggie
2018-08-08 18:17:25 UTC
I would like to create a model in which the regression coefficients for
multiple exogenous covariates evolve with a seasonal component and a random
walk component.
For a single exogenous covariate I can create a local level model with a
seasonal component and modify the design matrix to use the exogenous
covariate like this:
dynamic_mod=sm.tsa.UnobservedComponents(endog,"local level",
freq_seasonal=
[{"period":24,"harmonics":nharmonics}])
exog_seasonal_array=np.vstack(tuple(np.array([exog,np.repeat(0,nobs)])
for i in range(0,nharmonics)))
dynamic_mod.ssm['design'] = np.asfortranarray(
np.array([np.vstack((exog,exog_seasonal_array))]))
dynamic_mod.ssm._time_invariant=False
I can use a similar approach to give the coefficients of multiple
covariates a seasonal component by just adding more components to
freq_seasonal. However, since the UnobservedComponents class only supports
a single local level component I can't use this method to have multiple
regression coefficients evolve according to a random walk.
In order to achieve what I want I think I will either need to create a new
class extending MLEModel or modify UnobservedComponents to allow stochastic
regression coefficients.
So far I have attempted the former: writing my own class that extends
MLEModel which I call SDR (Seasonal Dynamic Regerssion). However I am
having problems with convergence.
At the moment I set the starting parameters all as zeros, with the
exception of the variance of the irregular term which I set to the variance
of the data. However, I don't think the starting values are the root of the
problem. I can check if the starting values matter in the case with only 1
exogenous covariate because I can model this using the UnobservedComponents
class (as shown in the code block above). I use the optimised parameter
values for the starting parameters in my SDR class. When I do this the max
likelihood optimisation still fails to converge. However, the smoothed
state estimates from these starting parameters are the same as from the
UnobservedComponents model, which gives me more confidence that I have at
least defined the state space matrices correctly.
I wonder if part of the problem could be that I do not constrain the
parameters as in UnobservedComponents using a transform_params method.
I am hoping someone can help with these questions:
1) Is there is any fundamental problem with the kind of model I am trying
to form?
2) Is there is an easier way to implement this which I have not thought of
that does not involve writing a new class?
3) Are better starting parameters likely to help?
4) What might be responsible for the convergence problems?
5) Assuming I am able to implement this, would a pull request to add
stochastic regression components to the UnobservedComponents class be
welcomed?
I've attached my SDR class in case anyone is able to take a look.
Thanks!
multiple exogenous covariates evolve with a seasonal component and a random
walk component.
For a single exogenous covariate I can create a local level model with a
seasonal component and modify the design matrix to use the exogenous
covariate like this:
dynamic_mod=sm.tsa.UnobservedComponents(endog,"local level",
freq_seasonal=
[{"period":24,"harmonics":nharmonics}])
exog_seasonal_array=np.vstack(tuple(np.array([exog,np.repeat(0,nobs)])
for i in range(0,nharmonics)))
dynamic_mod.ssm['design'] = np.asfortranarray(
np.array([np.vstack((exog,exog_seasonal_array))]))
dynamic_mod.ssm._time_invariant=False
I can use a similar approach to give the coefficients of multiple
covariates a seasonal component by just adding more components to
freq_seasonal. However, since the UnobservedComponents class only supports
a single local level component I can't use this method to have multiple
regression coefficients evolve according to a random walk.
In order to achieve what I want I think I will either need to create a new
class extending MLEModel or modify UnobservedComponents to allow stochastic
regression coefficients.
So far I have attempted the former: writing my own class that extends
MLEModel which I call SDR (Seasonal Dynamic Regerssion). However I am
having problems with convergence.
At the moment I set the starting parameters all as zeros, with the
exception of the variance of the irregular term which I set to the variance
of the data. However, I don't think the starting values are the root of the
problem. I can check if the starting values matter in the case with only 1
exogenous covariate because I can model this using the UnobservedComponents
class (as shown in the code block above). I use the optimised parameter
values for the starting parameters in my SDR class. When I do this the max
likelihood optimisation still fails to converge. However, the smoothed
state estimates from these starting parameters are the same as from the
UnobservedComponents model, which gives me more confidence that I have at
least defined the state space matrices correctly.
I wonder if part of the problem could be that I do not constrain the
parameters as in UnobservedComponents using a transform_params method.
I am hoping someone can help with these questions:
1) Is there is any fundamental problem with the kind of model I am trying
to form?
2) Is there is an easier way to implement this which I have not thought of
that does not involve writing a new class?
3) Are better starting parameters likely to help?
4) What might be responsible for the convergence problems?
5) Assuming I am able to implement this, would a pull request to add
stochastic regression components to the UnobservedComponents class be
welcomed?
I've attached my SDR class in case anyone is able to take a look.
Thanks!