!pip install pandas-datareader==0.10.0
import os
import re
import datetime as dt
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import pandas_datareader.data as web
data_dir = "./data/Fed_vs_Inflation_Analysis"
os.makedirs(data_dir, exist_ok=True)
business_cycle = pd.read_html(
"https://www.nber.org/research/data/us-business-cycle-expansions-and-contractions")[0]
# save the data
business_cycle_file_path = f"{data_dir}/business_cycle.csv"
business_cycle.to_csv(business_cycle_file_path, index=False)
business_cycle = pd.read_csv(business_cycle_file_path, header=1)
business_cycle = business_cycle.iloc[1:].reset_index(drop=True)
business_cycle.head(3)
cycle_list = []
for i in range(len(business_cycle)):
row = business_cycle.iloc[i]
peak_month = re.sub(r"\*", "", row["Peak Month"])
peak = dt.datetime.strptime(f"{row['Peak Year']}{peak_month}", "%Y%B")
trough_month = re.sub(r"\*", "", row["Trough Month"])
trough = dt.datetime.strptime(f"{row['Trough Year']}{trough_month}", "%Y%B")
cycle_list.append((peak, trough))
print(cycle_list[-3:])
fig, ax = plt.subplots(figsize=(20, 5))
for x in cycle_list:
ax.axvspan(x[0], x[1], color="gray", alpha=0.3)
ax.get_yaxis().set_visible(False)
plt.show()
start = dt.datetime(1900, 1, 1)
end = dt.datetime(2022, 7, 6)
cpi = web.DataReader('CPIAUCSL', 'fred', start, end)
# save the data
file_path = f"{data_dir}/CPIAUCSL.csv"
cpi.to_csv(file_path)
cpi = pd.read_csv(file_path, index_col="DATE", parse_dates=True)
print(cpi.shape)
cpi.tail(3)
plt.figure(figsize=(10, 6))
plt.plot(cpi)
plt.grid(axis="y")
plt.xlabel("Date")
plt.ylabel("CPI")
plt.show()
cpi_change = cpi / cpi.shift(12) * 100 - 100
plt.figure(figsize=(10, 6))
plt.plot(cpi_change)
plt.grid(axis="y")
plt.xlabel("Date")
plt.ylabel("CPI change(%)")
plt.show()
ff_rate = web.DataReader('FEDFUNDS', 'fred', start, end)
# save the data
file_path = f"{data_dir}/FEDFUNDS.csv"
ff_rate.to_csv(file_path)
ff_rate = pd.read_csv(file_path, index_col="DATE", parse_dates=True)
print(ff_rate.shape)
ff_rate.tail(3)
plt.figure(figsize=(10, 6))
plt.plot(ff_rate)
plt.grid(axis="y")
plt.xlabel("Date")
plt.ylabel("FF rate(%)")
plt.show()
plt.figure(figsize=(14, 6))
plt.plot(cpi_change, label="CPI")
plt.plot(ff_rate, label="FF Rate")
for x in cycle_list:
plt.axvspan(x[0], x[1], color="gray", alpha=0.3)
plt.grid(axis="y")
plt.xlabel("Date")
plt.ylabel("Percent")
plt.xlim(ff_rate.index[0], ff_rate.index[-1])
plt.legend()
plt.show()
df = ff_rate.join(cpi_change)
df["FFRate-CPI"] = df["FEDFUNDS"] - df["CPIAUCSL"]
df
fig, ax = plt.subplots(figsize=(14, 6))
ax.plot(df[["FFRate-CPI"]])
for x in cycle_list:
ax.axvspan(x[0], x[1], color="gray", alpha=0.3)
ax.grid(axis='y', linestyle='dotted', color='b')
ax.set_xlabel("Date")
ax.set_ylabel("FFRate - CPI")
ax.set_xlim(df.index[0], end)
ax.set_ylim(-10, 10)
plt.show()
unemployment_rate = web.DataReader('UNRATE', 'fred', start, end)
# save the data
file_path = f"{data_dir}/UNRATE.csv"
unemployment_rate.to_csv(file_path)
unemployment_rate = pd.read_csv(file_path, index_col="DATE", parse_dates=True)
print(unemployment_rate.shape)
unemployment_rate.tail(3)
plt.figure(figsize=(10, 6))
plt.plot(unemployment_rate)
for x in cycle_list:
plt.axvspan(x[0], x[1], color="gray", alpha=0.3)
plt.grid(axis="y")
plt.xlabel("Date")
plt.ylabel("unemployment rate(%)")
plt.xlim(unemployment_rate.index[0], end)
plt.show()
plt.figure(figsize=(10, 6))
plt.plot(cpi_change, label="CPI", marker=".")
plt.plot(ff_rate, label="FF Rate", marker=".")
plt.plot(unemployment_rate, label="Unemployment Rate", marker=".")
for x in cycle_list:
plt.axvspan(x[0], x[1], color="gray", alpha=0.3)
plt.grid(axis="y")
plt.xlabel("Date")
plt.ylabel("Percent")
plt.xlim(dt.datetime(1979, 8, 1), dt.datetime(1980, 8, 1))
plt.legend()
plt.show()
display(df.loc["1979-12":"1980-07"])
display(unemployment_rate.loc["1979-12":"1980-07"])
# get historical data of S&P500(^GSPC)
sp_500 = web.DataReader('^GSPC', 'yahoo', start, end)
print(sp_500.shape)
# save the data
sp500_file_path = f"{data_dir}/S&P500.csv"
sp_500.to_csv(sp500_file_path)
sp_500 = pd.read_csv(sp500_file_path, index_col="Date", parse_dates=True)
sp_500.head(3)
sp_500.loc[["1980-5-6"],["Close"]]
plt.figure(figsize=(10, 6))
plt.plot(sp_500[["Close"]])
for x in cycle_list:
plt.axvspan(x[0], x[1], color="gray", alpha=0.3)
plt.scatter(dt.datetime(1980, 5, 6), sp_500.loc["1980-5-6","Close"], marker="x", color="red")
plt.title("S&P 500")
plt.xlabel("Date")
plt.ylabel("Price")
plt.xlim(dt.datetime(1979, 8, 1), dt.datetime(1980, 12, 1))
plt.ylim(0,150)
plt.show()
print(sp_500.loc["1980-01":"1980-07", "Close"].idxmin())
print(sp_500.loc["1980-01":"1980-07", "Close"].min())
sp_500.loc["1980-05", "Close"]