https://youtu.be/FRzfD1FtrsQ?list=PLQVvvaa0QuDc-3szzjeP6N6b0aDrrKyL-
In [1]:
import quandl;
import pandas as pd;
import pickle;
import matplotlib.pyplot as plt;
from matplotlib import style;
style.use("ggplot");
In [2]:
api_key = open("quandlapikey.txt", "r").read();
def state_list():
fiddy_states = pd.read_html("https://simple.wikipedia.org/wiki/List_of_U.S._states");
return fiddy_states[0][0][1:];
def grap_initial_state_data_start_pct():
states = state_list();
main_df = pd.DataFrame();
for ab in states:
querry = "FMAC/HPI_" + ab;
df = quandl.get(querry, authtoken = api_key);
df.columns = [ab];
df[ab] = (df[ab] - df[ab][0]) / df[ab][0] * 100.0; # <-------
if main_df.empty:
main_df = df;
else:
main_df = main_df.join(df);
pickle_out = open("./data/fiddy_states.pickle", "wb");
pickle.dump(main_df, pickle_out);
pickle_out.close();
def HPI_Benchmark():
df = quandl.get("FMAC/HPI_USA", authtoken = api_key);
df.columns = ["US"];
df["US"] = (df["US"] - df["US"][0]) / df["US"][0] * 100.0; # <-------
return df;
In [17]:
#grap_initial_state_data_start_pct();
HPI_data = pd.read_pickle("./data/fiddy_states.pickle");
for shift in range(0, 3): # <------- mean values
avg = 0.;
for i in range(shift, shift + 12):
avg += HPI_data["TX"][i];
avg /= 12.;
print(avg);
HPI_data["TX12MA"] = HPI_data["TX"].rolling(window = 12).mean();
HPI_data["TX12STD"] = HPI_data["TX"].rolling(window = 12).std(); # like root-mean-square deviation from expected value
print(HPI_data[["TX", "TX12MA", "TX12STD"]]);
HPI_data[["TX", "TX12MA", "TX12STD"]].plot();
plt.legend();
plt.show();
In [22]:
fig = plt.figure();
ax1 = plt.subplot2grid((2, 1), (0, 0)); # ((height-graps, width-graps), (height-start, width-start));
ax2 = plt.subplot2grid((2, 1), (1, 0));
HPI_data = pd.read_pickle("./data/fiddy_states.pickle");
HPI_data["TX12MA"] = HPI_data["TX"].rolling(window = 12).mean();
HPI_data["TX12STD"] = HPI_data["TX"].rolling(window = 12).std(); # like root-mean-square deviation from expected value
HPI_data[["TX", "TX12MA"]].plot(ax = ax1);
HPI_data["TX12STD"].plot(ax = ax2)
plt.legend(loc = 2);
plt.show();
In [30]:
HPI_data = pd.read_pickle("./data/fiddy_states.pickle");
fig = plt.figure();
ax1 = plt.subplot2grid((2, 1), (0, 0));
ax2 = plt.subplot2grid((2, 1), (1, 0), sharex = ax1);
TX_AK_12_corr = HPI_data["TX"].rolling(window = 12).corr(HPI_data["AK"]);
HPI_data[["TX", "AK"]].plot(ax = ax1);
TX_AK_12_corr.plot(ax = ax2, label = "Correlation");
plt.legend(loc = 4);
plt.show();
In [ ]: