大家下午好!我对卡尔曼滤波器还是个新手,但我读过一篇非常有趣的文章(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()提前感谢您的宝贵时间!
发布于 2020-11-08 04:26:09
设计转换矩阵和观测矩阵是使用卡尔曼滤波器的最困难和最重要的部分。(如您所知,卡尔曼滤波器本身的实现是由您的库pykalm提供的。)
这两个矩阵必须在你的论文中指定,确实是这样。
对于度量矩阵:
请参阅第43页,4.2节“测量方程式”。对于弱风格分析,测量方程由公式(19)提供。请记住第36页公式(4)中卡尔曼方程的定义。正如你所看到的,你的测量矩阵是R'_t,你还需要独立于t的测量噪声sigma^2。
对于转换矩阵:
请看第42页和43页,4.1节“状态方程”。状态方程由公式(18)提供。同样,请记住第36页公式(4)中卡尔曼方程的定义。如您所见,您的转换矩阵就是单位矩阵。过渡噪声是Q。
另请注意
这应该回答了你的问题。但我要指出的是,您必须在代码中指定噪声矩阵,就像转换矩阵和度量矩阵一样。我不知道pykalm,它可以在没有的情况下工作,但结果是错误的。
https://stackoverflow.com/questions/64577674
复制相似问题