pykalman: Analysis of USD/CHF with Kalman Filter

In a previous tutorial, we saw how the Kalman Filter can account for “shocks”, or sudden changes in a time series. The analysis was done within R. Let’s now see how we can analyse the USD/CHF currency pair with the pykalman library in Python.

To recap, the case of the Swiss franc is quite an interesting one when it comes to modelling time series shocks. In January 2015, the Swiss National Bank decided to depeg the franc from the euro, which resulted in one of the biggest shocks the currency market has ever seen. Major currencies went tumbling against the Swiss franc as a result.

Import libraries and install pykalman

Firstly, we import the pykalman, numpy, quandl, and matplotlib libraries:

from pykalman import KalmanFilter
import numpy as np
import quandl
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt

Note that if you do not have the pykalman library available (I’m using Python 3.6.5 at the time of writing), you can install it by opening a command line and typing:

pip3 install pykalman

Download currency data with quandl

Now, we will use quandl again to download January 2015 data for the USD/CHF currency pair:

usdchf = quandl.get("FRED/DEXSZUS", start_date="2015-01-01",end_date="2015-01-30",type="xts")

usdchf
             Value
Date              
2015-01-02  1.0004
2015-01-05  1.0079
2015-01-06  1.0062
2015-01-07  1.0160
2015-01-08  1.0169
2015-01-09  1.0151
2015-01-12  1.0150
2015-01-13  1.0195
2015-01-14  1.0172
2015-01-15  0.8930
2015-01-16  0.8488
2015-01-20  0.8751
2015-01-21  0.8596
2015-01-22  0.8678
2015-01-23  0.8760
2015-01-26  0.8988
2015-01-27  0.9042
2015-01-28  0.9055
2015-01-29  0.9226
2015-01-30  0.9210

Preprocessing Data

When invoking a Kalman Filter in Python, we firstly wish to transform our time series into a two-dimensional array which is suitable for analysis, i.e. the currency value as the first value, and 1.0 as the second value.

ones=np.ones((20,1))
array=np.concatenate((usdchf, ones), axis=1) #0 for vertical, 1 for horizontal

When we generate our array, here is what we come up with:

array([[1.0004, 1.    ],
       [1.0079, 1.    ],
       [1.0062, 1.    ],
       [1.016 , 1.    ],
       [1.0169, 1.    ],
       [1.0151, 1.    ],
       [1.015 , 1.    ],
       [1.0195, 1.    ],
       [1.0172, 1.    ],
       [0.893 , 1.    ],
       [0.8488, 1.    ],
       [0.8751, 1.    ],
       [0.8596, 1.    ],
       [0.8678, 1.    ],
       [0.876 , 1.    ],
       [0.8988, 1.    ],
       [0.9042, 1.    ],
       [0.9055, 1.    ],
       [0.9226, 1.    ],
       [0.921 , 1.    ]])

Kalman Filter with pykalman

Now, we can go ahead and run the KalmanFilter command.

Note that when we run the analysis on the array, we are also specifying our transition matrices and observation matrices.

In simple terms, these matrices are set to determine the degree of responsiveness that we wish to induce in the Kalman Filter, i.e. how quickly does the filter react to a sharp change in the time series trend.

kf = KalmanFilter(transition_matrices = [[1, 1], [0, 1]], observation_matrices = [[0.1, 0.5], [-0.3, 0.0]])
kf = kf.em(array, n_iter=5)

For simplification purposes, I choose to use the same matrix values as are included by default in the pykalman documentation.

kf = kf.em(array, n_iter=5)
state_means, state_covs = kf.filter(array)

When the time series is analysed with the Kalman Filter, the following state means are calculated:

x                y
-1.93406775, 0.46238027
-1.81053083, 0.51141757
-1.77753367, 0.48421003
-1.75484873, 0.45814124
-1.74464914, 0.43890391
-1.7417623, 0.42686202
-1.74050205, 0.42138847
-1.7364863, 0.42168805
-1.73680258, 0.41903379
-1.83168482, 0.34337637
-1.89791857, 0.32210772
-1.90120726, 0.35296344
-1.91490059, 0.35577656
-1.91362908, 0.36639675
-1.90714337, 0.37469761
-1.8875834, 0.3891841
-1.87673897, 0.39093582
-1.87188237, 0.38864235
-1.85702862, 0.39646528
-1.85307634, 0.39322574

Taking x (the array on the left) and y (the array on the right), we can now calculate and visualise the change in slope and intercept.

slope                intercept
0.396944492383329, -0.711606554125222
-0.824541869663932, 0.330371999376829
-1.149167244877, 0.227183330802564
-1.88608855846166, 0.13338262757657
-4.17130495628446, 0.058752545518276
-4.34322555048576, 0.05574369151549
0.074601257548409, -3.25517082358378
8.39212090552528, -0.028749272361684
0.797382313065122, -0.23510023362817
0.321114990469361, -0.528521889062118
-9.38237413681378, 0.019787344377937
-0.205437245724744, 0.904382010013404
8.35242349647247, -0.022923522016356
1.27986912766683, -0.153508354759155
0.740619234078579, -0.278390206419167
0.161531772532073, -1.28956617809066
-0.472237779516539, 0.439653810321929
0.526663637128668, -0.405371516277162
-0.819663586587999, 0.258888634770407

Slope

(Click to enlarge)

slope pykalman

Intercept

(Click to enlarge)

intercept pykalman

As we can observe, time period t=12 shows a sharp depreciation in the slope. Considering that we started calculating the slope from t=2, this indicates that the Kalman Filter adjusts for the shock (the sudden drop in USD/CHF) one day after the event occurs.

(Click to enlarge)

Conclusion

In this tutorial, you have learned:

  • How to use pykalman in Python
  • The importance of a Kalman Filter in adjusting for time series shocks
  • How to configure a Kalman Filter and interpret its output

Many thanks for reading this tutorial, and please leave a comment if I can answer any questions.

Leave a Reply

Your email address will not be published. Required fields are marked *

2 × five =