Analyzing Size of Armed Forces From 1947 - 1963 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.longley.load_pandas().data
#print(sm.datasets.longley.NOTE)
df.head()
index = pd.Index(sm.tsa.datetools.dates_from_range('1947', '1962'))
df.index = index
df.head()
df['ARMED'].plot()
plt.ylabel("ARMED")
# unpacking
cycle, trend = sm.tsa.filters.hpfilter(df.ARMED)
cycle
type(cycle)
df["trend"] = trend
df[['trend','ARMED']].plot(figsize = (12, 8))
df[['trend','ARMED']]["1950-01-01":"1955-01-01"].plot(figsize = (12, 8))