i want use the python pykalman libary to import KalmanFilter and KalmanSmoother to dynamically calculate the style and excess return of the fund
this is the python code sample:
import numpy as np
import pandas as pd
from pykalman import KalmanFilter
# fund and style return data
fund_returns = pd.DataFrame({'fund': [0.1, 0.2, 0.3, 0.4, 0.5], 'market': [0.0, 0.1, 0.2, 0.3, 0.4]})
style_returns = pd.DataFrame({'style1': [0.05, 0.07, 0.09, 0.11, 0.13], 'style2': [0.03, 0.05, 0.07, 0.09, 0.11]})
# style init_weight and transition_matrix
init_weight = [0.5, 0.3, 0.2]
transition_matrix = [[0.99, 0.01, 0.00], [0.01, 0.99, 0.00], [0.00, 0.00, 1.00]]
# KalmanFilter and style position
kf = KalmanFilter(transition_matrices=transition_matrix, initial_state_mean=init_weight)
for t in range(len(fund_returns)):
current_state_mean = kf.filter_update(fund_returns.iloc[t], style_returns.iloc[t])
# KalmanSmoother and optimize style positon
ks = KalmanSmoother(transition_matrices=transition_matrix, initial_state_mean=init_weight)
ks.smooth(fund_returns, style_returns)
smoothed_state_means = ks.smooth_state_means
# calucate fund alpha
simulated_return = smoothed_state_means * style_returns
excess_return = fund_returns - simulated_return
print(excess_return)
but this is a bug, showed as ValueError: shapes (3,3) and (2,) not aligned: 3 (dim 1) != 2 (dim 0)
anyone konw how to fix it?