Analyzing US Unemployment From 1959 - 2009 with statsmodels
This post includes code adapted from python for finance and trading algorithms udemy course and python for finance and trading algorithms udemy course notebooks.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import statsmodels.api as sm
%matplotlib inline
df = sm.datasets.macrodata.load_pandas().data
print(sm.datasets.macrodata.NOTE)
df.head()
index = pd.Index(sm.tsa.datetools.dates_from_range('1959Q1', '2009Q3'))
df.index = index
df.head()
df['unemp'].plot()
plt.ylabel("unemp")
$\min_{\\{ \tau_{t}\\} }\sum_{t}^{T}\zeta_{t}^{2}+\lambda\sum_{t=1}^{T}\left[\left(\tau_{t}-\tau_{t-1}\right)-\left(\tau_{t-1}-\tau_{t-2}\right)\right]^{2}$
# unpacking
unemp_cycle, unemp_trend = sm.tsa.filters.hpfilter(df.unemp)
unemp_cycle
type(unemp_cycle)
df["trend"] = unemp_trend
df[['trend','unemp']].plot(figsize = (12, 8))
df[['trend','unemp']]["2000-03-31":].plot(figsize = (12, 8))