Course Content
Introduction to Finance with Python
Introduction to Finance with Python
Autoregressive model (AR)
What is an autoregression?
What we need for using autoregressive model?
Main assumption, which autoregressive model uses - that current value of time series depends on(correlates with) it's previous values(this is called autocorrelation).
Another important assumption - is that time series is stationary (simply speaking, it means that the mean value and variance of time series is constant). The only possible exception - having trend from a bias coefficient.
Defining model
Formally, when we are talking about autoregression, we should mention such important parameter of this model as p
- model’s order, or, practically - number of previous values, used for prediction. This class of models is denoted as AR(p)
.
For example, in case of p=1
, we work with model AR(1)
, which is represented by the next expression:
Here y_t
- value of time series at moment of time t
, a_1
- coefficient for a previous value(or, alternatively, the first lagged value), ε_t
- random error (practically - we don’t use it for making predictions), c
- bias coefficient.
Analogically, in the second case, we work with AR(2)
model, which is represented by the expression:
Here notation is pretty much the same to the previous example, except that now we additionally have a_2
coefficient, corresponding to the second lagged value.
And so on.
Note also, that we also could find model without using constant trend (practically - assume that c = 0
). But in this case, our time series should be stationary, so - we can use one of the tests, described into previous chapter, in order to check this.
Code implementation
To build autoregressive model we can use the next code:
Here we are setting parameter lags
equal to 7
, meaning, that we want to use previous seven values for prediction.
As alternative, we can set lags
equal to list of integers, where each value is a number of corresponding lag, which will be used for prediction.
For example, here is an alternative for the previous code:
Also, we can skip several lags in our model:
For example, in the next code we are using only the first, and the third lagged values:
If we then want to make prediction of future values, we can use the following code, additionally to the specified above:
Also, if we want to get the predicted values for training data, we can get them, using fittedvalues
attribute:
If you want to see some practical application of AR model - watch the following video:
Thanks for your feedback!