13.08.2022 Views

advanced-algorithmic-trading

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

194

Figure 13.1: Scatterplot of the fixed income ETFs, TFT vs IEI

two-dimensional identity matrix.

Subsequently we create the observation matrix. As we previously described, this matrix is a

row vector consisting of the prices of TFT and a sequence of unity values. To construct this we

utilise the NumPy vstack method to vertically stack these two price series into a single column

vector, which we then transpose.

At this point we use the KalmanFilter class from PyKalman to create the Kalman Filter

instance. We supply it with the dimensionality of the observations, which is unity in this case.

We also supply it with the dimensionality of the states, which is two, as we are looking at the

intercept and slope in the linear regression.

We also need to supply the mean and covariance of the initial state. In this instance we set

the initial state mean to be zero for both intercept and slope, while we take the two-dimensional

identity matrix for the initial state covariance. The transition matrices are also given by the

two-dimensional identity matrix.

The last terms to specify are the observation matrices as above in obs_mat, with its covariance

equal to unity. Finally the transition covariance matrix (controlled by delta) is given by

trans_cov, described above.

Now that we have the kf Kalman Filter instance we can use it to filter based on the adjusted

prices from IEI. This provides us with the state means of the intercept and slope. This is

ultimately what we are after. In addition we also receive the covariances of the states.

This is all wrapped up in the calc_slope_intercept_kalman function:

def calc_slope_intercept_kalman(etfs, prices):

"""

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!