大家下午好!我对卡尔曼滤波器有点陌生,但我遇到了这篇非常有趣的文章(https://pdfs.semanticscholar.org/d348/37b8e535974c341d8c8a5c38666581e83309.pdf)关于在金融时间序列分析和特别是在动态风格分析的背景下。我想实现这个算法,但我不确定如何指定转换和观察矩阵。实际上,我有一个参考共同基金,有 60 个月回报观察结果和 8 个指数,我想以此作为基金的基准。我专注于弱风格分析,因为它应该是最容易使用 Pykalman 库编写代码的。这是到目前为止的代码,我只需要指定转换和观察矩阵:
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()
在此先感谢您的时间!