Analyzing US Inflation 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['infl'].plot()
plt.ylabel("infl")
$\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
infl_cycle, infl_trend = sm.tsa.filters.hpfilter(df.infl)
infl_cycle
type(infl_cycle)
df["trend"] = infl_trend
df[['trend','infl']].plot(figsize = (12, 8))
df[['trend','infl']]["2000-03-31":].plot(figsize = (12, 8))