<jupyter_start><jupyter_text>Local Linear Trend Model with the Kalman FilterThe one-state local linear trend model is expressed as:$$\begin{align*}  v_t &= \mu_t +\epsilon_t \qquad &\epsilon_t \sim N(0,\sigma_{\epsilon_t}^2)\\  \mu_{t+1} &= \mu_t +\eta_t \qquad &\eta_t \sim N(0,\sigma_{\eta_t}^2)\\\end{align*}  $$where $\epsilon_t$ and $\eta_t$ are assumed random indepedent Normal processes.<jupyter_code>ff_data = pd.read_excel("../data/25_Portfolios_ME_Prior_12.xlsx", sheet_name=[0, 1], header=0, index_col=0)[1] / 100
ff_data.index = pd.date_range(start='07/1963', end='03/2021', freq='m')
ff_data.head()
p25_data = pd.read_excel("../data/25_Portfolios_ME_Prior_12.xlsx", sheet_name=[0, 1], header=0, index_col=0)[0]
p25_data.index = pd.date_range(start='01/1927', end='03/2021', freq='m')
p25_data.head()
df_corner = p25_data[['SMALL LoPRIOR', 'SMALL HiPRIOR', 'BIG LoPRIOR', 'BIG HiPRIOR']] / 100
df_corner = pd.concat([df_corner, ff_data], join='inner', axis=1)
df_corner
# CAPM
fig, axes = plt.subplots(2, 2, figsize=(15, 10))
for i in range(4):
    y = df_corner.iloc[:, i:i+1].values - df_corner.iloc[:, -1:].values
    X = sm.add_constant(df_corner['Mkt-RF'].values)
    mod = KalmanFilter(y, X)
    res = mod.fit()

    print(res.summary())
    ss = pd.DataFrame(res.smoothed_state.T, columns=['alpha', 'beta'])

    axes[i//2][i%2].plot(ss['alpha'])
    axes[i//2][i%2].set_ylabel("Smoothed estimate on alpha")
    ax2 = axes[i//2][i%2].twinx()
    ax2.set_ylabel('Smoothed estimate on beta')
    ax2.plot(ss['beta'], color='m')
    axes[i//2][i%2].set_title(f'Time-varying coefficient: {df_corner.columns[i]}')
    axes[i//2][i%2].autoscale(tight=True)
fig.tight_layout();<jupyter_output>Statespace Model Results                           
==============================================================================
Dep. Variable:                      y   No. Observations:                  692
Model:                   KalmanFilter   Log Likelihood                1051.506
Date:                Mon, 02 Aug 2021   AIC                          -2095.013
Time:                        22:03:07   BIC                          -2076.855
Sample:                             0   HQIC                         -2087.990
                                - 692                                         
Covariance Type:                  opg                                         
================================================================================
                   coef    std err          z      P>|z|      [0.025      0.975]
--------------------------------------------------------------------------------
intercept      6.42e-12   6.11e-16   1.05e+04 [...]
{'fraction_non_alphanumeric': 0.23183004099888185, 'fraction_numerical': 0.0573984345881476, 'mean_word_length': 2.0957324106113036, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '615213', 'license': 'MIT', 'n_tokens_mistral': 986, 'n_tokens_neox': 871, 'n_words': 167, 'path': 'notebooks/kalman_filter.ipynb', 'repo_name': 'sohlin/quantitative-finance-toolbox', 'size': '2683'}