pandas_additionalEconomic.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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"] = (df["Adjusted Close"]-df["Adjusted Close"][0]) / df["Adjusted Close"][0] * 100.0
  18. df=df.resample('M').mean()
  19. df.rename(columns={'Adjusted Close':'sp500'}, inplace=True)
  20. df = df['sp500']
  21. return df
  22. def gdp_data():
  23. df = quandl.get("BCB/4385", trim_start="1975-01-01")
  24. df["Value"] = (df["Value"]-df["Value"][0]) / df["Value"][0] * 100.0
  25. df=df.resample('M').mean()
  26. df.rename(columns={'Value':'GDP'}, inplace=True)
  27. df = df['GDP'] # DataFrame to Series
  28. return df
  29. def us_unemployment():
  30. df = quandl.get("ECPI/JOB_G", trim_start="1975-01-01")
  31. df["Unemployment Rate"] = (df["Unemployment Rate"]-df["Unemployment Rate"][0]) / df["Unemployment Rate"][0] * 100.0
  32. df=df.resample('1D').mean()
  33. df=df.resample('M').mean()
  34. return df
  35. # m30 = mortgage_30yr() # Series
  36. # sp500 = sp500_data() # Series
  37. # gdp = gdp_data() # Series
  38. # unemployment = us_unemployment() # DataFrame
  39. # HPI = HPI_data.join([m30, unemployment, gdp, sp500])
  40. ax1 = plt.subplot(2,1,1)
  41. ax2 = plt.subplot(2,1,2, sharex=ax1)
  42. # initial_state_data()
  43. pickle_in = open('fifty_states_pct.pickle' , 'rb')
  44. HPI_data = pickle.load(pickle_in)
  45. # HPI_Benchmark()
  46. pickle_in = open('us_pct.pickle','rb')
  47. benchmark = pickle.load(pickle_in)
  48. pickle_in = open('HPI_complete.pickle', 'rb')
  49. HPI = pickle.load(pickle_in)
  50. HPI.dropna(inplace=True)
  51. print(HPI.head())
  52. state_HPI_M30 = HPI_data.join(HPI['M30'])
  53. # print(state_HPI_M30.corr().describe()['M30'])