0

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!

mbronzo
  • 11
  • 4

1 Answers1

0

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.

Lho
  • 289
  • 1
  • 9
  • I would really like to thank you for your time, availability and really clear reasoning. I am sorry I am really new to the topic and your explanation was really simple and helpful. Nevertheless I still can't manage to make the code run due to errors in the dimensions. Right now I have the variable measurements that contains the 60 observations of the fund return (60x1), the transition matrix (identity, 60x8), the observation matrix (initial estimates of Beta, 1x8) and the fund returns (60x8). Am I getting the initial state mean wrong? – mbronzo Nov 23 '20 at 18:28
  • No problem. I don't fully understand what your are doing, it might be helpful if you updated your question. However: 1.) Pykalm wants *all* 60 observations matrices and all 60 transition matrices (for each timestep one). If your state has length 8, a single transition matrix has dimension 8x8. 60 transition matrices have then the dimension (60*8)x8 or 8x(60*8). 2.) The observation matrix is *not* the initial estimate. If you don't have an initial estimate, just pick the zero vector. You must choose R'_t as observation matrix, as I have written in my answer. The dimension of R'_t is 1x8. – Lho Nov 25 '20 at 10:30
  • I have updated the code with what I came up to. At least is working, but I think I did not set up correctly the inputs as the portfolio weights that results from the Kalman Filter do not track correctly the fund performance. I am not sure which is the problem, it might be the fact that I have to constraints the weights to sum up to 1. Thank you a lot for your time. – mbronzo Nov 27 '20 at 16:37