<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 [...]
StarcoderdataJupyterStructuredCleanDedup