1

I am trying to identify a state space model from discrete time series data in Python using statsmodels library: statsmodel.tsa.statespace.sarimax.SARIMAX.

I need the matrices of the state space general form (here the statsmodel reference): from the statsmodel page these matrices are explained but it is not clear how to extrapolate them.

For example if I want to apply a kalman filter to the identified model (by means of a sarimax) I need the matrices described in this picture state space matrices needed

Is it possible to obtain the matrices coefficients with statsmodel?

giulia
  • 13
  • 3
  • To me, this question is not very clear. What exactly are you looking for? What have you already tried? Can you post some example code that makes clear what part you can't figure out? – cfulton Aug 25 '21 at 19:38
  • I am creating a SARIMAX model fitted on data `model = SARIMAX(Y_tr, exog = X_tr, order = (p,d,q), enforce_invertibility = False) best_model = model.fit() best_model.summary()` Then I would like to know the state space matrices of the so called **best_model** in order to build a [Kalman Filter](https://en.wikipedia.org/wiki/Kalman_filter). The kalman filter can be applied to a model represented by means of state space equation, so I would like to extrapolate from the SARIMAX model (**best_model**) the state space equation. Did I explained my self? – giulia Aug 26 '21 at 10:06

1 Answers1

1

All of the state space system matrices are saved in the filter_results attribute of the fitted model. The names of the matrices are given in the links that you included in your answer (e.g. "design", etc.)

For example:

model = SARIMAX(Y_tr, exog = X_tr, order = (p,d,q), enforce_invertibility = False)
best_model = model.fit()

print(best_model.filter_results.design)
print(best_model.filter_results.obs_cov)
# ...
cfulton
  • 2,855
  • 2
  • 14
  • 13
  • Thank you for the answer. Do you know where I can find some documentation about model.filter_results? In [statsmodel API] (https://www.statsmodels.org/dev/generated/statsmodels.tsa.statespace.sarimax.SARIMAX.html) I can find only `Kalman_filter.filter_results`, that is not related to the SARIMAX model. Moreover, where can I find the matrix linking the exogenous input (`X_tr`) and the output (`Y_tr`) ? I was expecting matrix `Z : design (k_endog×k_states×nobs)` being the size of `(k_exog×nobs)`. – giulia Aug 27 '21 at 13:34
  • For SARIMAX, the `filter_results` attribute is an instance of the [`FilterResults`](https://www.statsmodels.org/stable/generated/statsmodels.tsa.statespace.kalman_filter.FilterResults.html) class, so you can look at the class documentation (linked) for details. – cfulton Aug 27 '21 at 23:39
  • The design matrix `Z` is the matrix linking the unobserved state vector to the output (`Y_tr`). The exogenous terms are all accounted for in the `obs_intercept` term, but that term contains the product of the coefficients and the exogenous variables. It seems like you want the coefficients themselves, and those are available as part of `best_model.params`. – cfulton Aug 27 '21 at 23:43
  • Hello @cfulton, why do I need to apply a kalman filter to the identified SARIMAX model to extract the state-space matrices Zt, dt, Tt, ct? Me and giulia are trying to get these matrices to apply the identified state-space model by ourselves on other data. – Pier Sep 01 '21 at 08:42
  • I'm not sure what you're asking. @gulia's comment said that he was creating `best_model` as in the code example above, and wanted to get the state space matrices out of `best_model`. That's what the code in my answer shows how to do. Perhaps you can clarify your question? – cfulton Sep 01 '21 at 14:43
  • @cfulton, what I mean is that reading your comment and documentation, it seems that the matrices that I get using e.g. `best_model.filter_results.design` are the results of a kalman filter applied to the best_model state space equation. So my question is: why do I have to apply a Kalman filter to extract the state-space matrices? I would expect `design` to be an attribute of `best_model' instead of an attribute of 'filter_results'. – Pier Sep 02 '21 at 06:31
  • 1
    When you call `fit`, the parameters of the SARIMAX model are estimated numerically via maximum likelihood, where the likelihood function is evaluated using the prediction error decomposition after applying the Kalman filter to the state space form of the model. So when you create `best_model`, you already have the results from the Kalman filter applied to the state space model that corresponds to the estimated parameters. – cfulton Sep 02 '21 at 13:24