pandas_additionalEconomic.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import pickle
  2. import pandas as pd
  3. import quandl
  4. import matplotlib.pyplot as plt
  5. from matplotlib import style
  6. style.use("seaborn")
  7. quandl.ApiConfig.api_key = "rFsSehe51RLzREtYhLfo"
  8. def mortgage_30yr():
  9. df = quandl.get("FMAC/MORTG", trim_start="1975-01-01")
  10. df["Value"] = (df["Value"] - df["Value"][0]) / df["Value"][0] * 100
  11. df = df.resample("M").mean()
  12. df.rename(columns={"Value": "M30"}, inplace=True)
  13. df = df["M30"]
  14. return df
  15. def sp500_data():
  16. df = quandl.get("YAHOO/INDEX_GSPC", trim_start="1975-01-01")
  17. df["Adjusted Close"] = (
  18. (df["Adjusted Close"] - df["Adjusted Close"][0])
  19. / df["Adjusted Close"][0]
  20. * 100.0
  21. )
  22. df = df.resample("M").mean()
  23. df.rename(columns={"Adjusted Close": "sp500"}, inplace=True)
  24. df = df["sp500"]
  25. return df
  26. def gdp_data():
  27. df = quandl.get("BCB/4385", trim_start="1975-01-01")
  28. df["Value"] = (df["Value"] - df["Value"][0]) / df["Value"][0] * 100.0
  29. df = df.resample("M").mean()
  30. df.rename(columns={"Value": "GDP"}, inplace=True)
  31. df = df["GDP"] # DataFrame to Series
  32. return df
  33. def us_unemployment():
  34. df = quandl.get("ECPI/JOB_G", trim_start="1975-01-01")
  35. df["Unemployment Rate"] = (
  36. (df["Unemployment Rate"] - df["Unemployment Rate"][0])
  37. / df["Unemployment Rate"][0]
  38. * 100.0
  39. )
  40. df = df.resample("1D").mean()
  41. df = df.resample("M").mean()
  42. return df
  43. # m30 = mortgage_30yr() # Series
  44. # sp500 = sp500_data() # Series
  45. # gdp = gdp_data() # Series
  46. # unemployment = us_unemployment() # DataFrame
  47. # HPI = HPI_data.join([m30, unemployment, gdp, sp500])
  48. ax1 = plt.subplot(2, 1, 1)
  49. ax2 = plt.subplot(2, 1, 2, sharex=ax1)
  50. # initial_state_data()
  51. pickle_in = open("fifty_states_pct.pickle", "rb")
  52. HPI_data = pickle.load(pickle_in)
  53. # HPI_Benchmark()
  54. pickle_in = open("us_pct.pickle", "rb")
  55. benchmark = pickle.load(pickle_in)
  56. pickle_in = open("HPI_complete.pickle", "rb")
  57. HPI = pickle.load(pickle_in)
  58. HPI.dropna(inplace=True)
  59. print(HPI.head())
  60. state_HPI_M30 = HPI_data.join(HPI["M30"])
  61. # print(state_HPI_M30.corr().describe()['M30'])