How to specify transition and observation matrices with Pykalman

1.1k Views Asked by At

Good afternoon to everyone! I am kind of new to the Kalman Filter, but I have came across this really interesting article (https://pdfs.semanticscholar.org/d348/37b8e535974c341d8c8a5c38666581e83309.pdf) about the possibility of using this filter in financial time-series analysis and in particular in the context of dynamic style analysis. I would like to implement this algorithm but I am not sure on how to specify the transition and observation matrices. Practically speaking I have a mutual fund of reference with 60 observations of monthly returns and 8 indexes with which I want to benchmark the fund. I am focusing on the weak style analysis since it should be the easiest to code with the Pykalman library. This is the code up to now, I only need to specify the transition and observation matrices:

import matplotlib.pyplot as plt
from pykalman import KalmanFilter
import numpy as np
import pandas as pd

df1 = pd.read_excel('/Users/marco/Desktop/SA_trial.xlsx')
df1.drop(['Mth'], axis='columns', inplace=True)
fund = df1.iloc[:, 0:1]
index = df1.iloc[:, 2:10]

fund = np.array(fund)
index = np.array(index)
n_timesteps = index.shape[0]
measurements = np.asarray(fund)

kf = KalmanFilter(transition_matrices=np.identity(8),
                  observation_matrices=index[0, :],
                  #transition_offsets=[0, 0, 0, 0, 0, 0, 0, 0],
                  initial_state_mean=index[0])

(filtered_state_means, filtered_state_covariances) = kf.filter(measurements)
kf.em(measurements).smooth(measurements)[0]

print('------\n')
print(filtered_state_means)
print(len(filtered_state_means.shape))

weights = filtered_state_means

benchmark = []


for i in range(len(weights)):
    benchmark_return = index[i] @ weights[i]
    benchmark.append(benchmark_return)

print('------\n')
print(benchmark)

plt.plot(measurements, '-r', label='measurment')
plt.plot(benchmark, '-g', label='kalman-filter output')
plt.legend(loc='upper left')
plt.show()

Thanks in advance for your time!

1

There are 1 best solutions below

3
Lho On

Designing the transition and observation matrices is the most difficult and important part of using a kalman filter. (As you know, the implementation of the kalman filter itself is provided by your libary, pykalm.)

These two matrices must be specified in your paper and indeed they are.

For the measurement matrix:

Look at page 43, section 4.2 "Measurement equations". For your weak style analysis, the measurement equation is provided by formula (19). Remember the definition of the kalman equations in formula (4), page 36. As you can see, your measurement matrix is R'_t. You also need the measurement noise which is sigma^2 independently of t.

For the transition matrix:

Look at page 42 and 43, section 4.1 "State equation". The state equation is provided by formula (18). Again, remember the definition of the kalman equations in formula (4), page 36. As you can see, your transition matrix is the identity matrix. The transition noise is Q.

Please note also

That should answer your question. But let me point out that you must specify the noise matrices in your code just like the transition and measurement matrices. I do not know pykalm, it may will work without, but the results are then wrong.