Skip to content

Instantly share code, notes, and snippets.

@ChadFulton
Last active September 21, 2023 23:42
Show Gist options
  • Save ChadFulton/82744b500a5dcb0283624c80fd10c92b to your computer and use it in GitHub Desktop.
Save ChadFulton/82744b500a5dcb0283624c80fd10c92b to your computer and use it in GitHub Desktop.
Statsmodels - ARIMA with exogenous regressors
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@JasperLS
Copy link

Is it possible to model an intervention effect with SARIMAX in Py?

@ChadFulton
Copy link
Author

It is, but there's no special syntax, you just include it in the exog array. In this Gist, the intervention variable was constructed like:

exog_full['intervention'] = np.zeros(len(exog_full))
exog_full.loc['2005-08':'2009', 'intervention'] = 1

@ChadFulton
Copy link
Author

Unless you had something else in mind?

@JasperLS
Copy link

Thanks Chad- this is super helpful. I had to some extent - this will model it as a permanent elevation change in the final Sarim model, right? Is there a possibility to force Sarima to estimate parameters for a different intervention effect (e.g. a pulse)?

@ChadFulton
Copy link
Author

The SARIMAX model is of the form "regression with SARIMA errors", so the intervention will be to the mean of the process. You're right, the one constructed here is a permanent increase in the mean.

I'm not quite sure what your second question is regarding. You can certainly include a pulse intervention variable in exog also, and running fit will estimate a coefficient

@JasperLS
Copy link

Hi Chad, so sorry for reaching out again. I have also asked on StackOverflow: https://stackoverflow.com/questions/64013032/sarimax-pulse-intervention-effect-in-python. I feel like I am stupid or hitting a wall..

Let's say I have data for 5 time stamps, and at time step 3 there is an intervention.
If I want to model the intervention as a permanent shift of the mean, exog would be [0,0,1,0,0].

But if I want to model the intervention as a pulsed effect, that is declining again, would I need to change exog to something like [0,0,1,-1,0] ?

@ChadFulton
Copy link
Author

It looks like you want to model a pulse to the intercept, which would imply a change to the mean that slowly dies out according to the dynamics from the lag polynomial. If so, this isn't immediately available with the SARIMAX class, because as I said above, it models exog as affecting the mean of the process.

You could create the intervention manually to be a pulse that dies out slowly, but the problem is that they you would have to specify that process, rather than letting it follow from the model dynamics.

Possibly a better way is to create a subclass that models exog as affecting the intercept rather than the mean (if this is in fact what you're looking for). In practice, this just means putting the beta * exog term into the state_intercept rather than the obs_intercept component of the state space system. However, this may be more work than you are wanting to do.

@JasperLS
Copy link

Okay, thanks a lot! I'll see what makes sense. Moving the beta*exog sounds possible at first glance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment