'''
Created on Feb 23, 2015

@author: wkarmistead
'''


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import statsmodels.api as sm
import statsmodels.formula.api as smf
import math
from businessLayer.DataAggregator import FREDDataAggregator
from numpy import NaN
strFREDAPIkey = '7960fde948bc3ec0e782bc4fa6629cf7'
strSeriesID_GDPperCapita_quarter = 'A939RC0Q052SBEA'
strSeriesID_GDPperCapita_annual = 'A939RC0A052NBEA'
strSeriesID_CPI_notadj_monthly = 'CPIAUCNS'

def getRealGDP(gdp, cpi, currentCPI):
    return(gdp * currentCPI / cpi)

def getDayNum(currentDay, firstDay):
    return(currentDay - firstDay)

def isPositive(numValue):
    if numValue > 0:
        rtn = 1
    elif math.isnan(numValue):
        return NaN
    else:
        rtn = -1
    return(rtn)

fredGDPperCapita_qtr = FREDDataAggregator(strFREDAPIkey, strSeriesID_GDPperCapita_quarter, fileName = 'GDPperCapita_qtr', tableName = 'GDPperCapita_qtr', runOnInit = 1)
fredGDPperCapita_annual = FREDDataAggregator(strFREDAPIkey, strSeriesID_GDPperCapita_annual, fileName = 'GDPperCapita_annual', tableName = 'GDPperCapita_annual', runOnInit = 1)
fredCPI = FREDDataAggregator(strFREDAPIkey, strSeriesID_CPI_notadj_monthly, fileName = 'CPI', tableName = 'CPI', runOnInit = 1)

lastAnnualDate = fredGDPperCapita_qtr.Data.index[0]
fredGDPperCapita_all = pd.concat([fredGDPperCapita_annual.Data[:lastAnnualDate], fredGDPperCapita_qtr.Data[1:]])
currentCPI = fredCPI.Data['Value'].iloc[-1]
dfEconomy = pd.merge(fredGDPperCapita_all, fredCPI.Data, left_index = True, right_index = True, suffixes = ('_GDPperCapita', '_CPI'))
dayOne = dfEconomy.index[0]
dfEconomy['DateNum'] = (dfEconomy.index - dfEconomy.index[0]).days

#dfEconomy['DateNum'] = dfEconomy.apply(lambda x: getDayNum(x.index, dayOne), axis = 1)
dfEconomy['RealGDPperCapita'] = dfEconomy.apply(lambda x: getRealGDP(x['Value_GDPperCapita'], x['Value_CPI'], currentCPI), axis = 1)

regEconomy = smf.ols('RealGDPperCapita ~ DateNum', data = dfEconomy).fit()
dfEconomy['RealGDPperCapita_fitted'] = regEconomy.fittedvalues
dfEconomy['RealGDPperCapita_residual'] = dfEconomy['RealGDPperCapita'] - regEconomy.fittedvalues
dfEconomy['RealGDPperCapita_residual_norm'] = dfEconomy['RealGDPperCapita_residual'] / max(abs(dfEconomy['RealGDPperCapita_residual'] ))

dfEconomy['RealGDPperCapita_residual_norm_smooth'] = sm.nonparametric.lowess(endog = np.array(dfEconomy['RealGDPperCapita_residual_norm']), exog = np.array(dfEconomy['DateNum']), frac=0.06)[:,1]
dfEconomy['RealGDPperCapita_residual_smooth_diff'] = dfEconomy['RealGDPperCapita_residual_norm_smooth'].diff()

dfEconomy['RealGDPperCapita_state'] = dfEconomy.apply(lambda x: isPositive(x['RealGDPperCapita_residual_norm_smooth']), axis = 1)
dfEconomy['RealGDPperCapita_growth'] = dfEconomy.apply(lambda x: isPositive(x['RealGDPperCapita_residual_smooth_diff']), axis = 1)


#plt.scatter(dfEconomy.index, dfEconomy['RealGDPperCapita'])
#plt.scatter(dfEconomy.index, dfEconomy['RealGDPperCapita_fitted'])
plt.scatter(dfEconomy.index, dfEconomy['RealGDPperCapita_residual_norm'])
plt.plot(dfEconomy.index, dfEconomy['RealGDPperCapita_residual_norm_smooth'])
plt.plot(dfEconomy.index, dfEconomy['RealGDPperCapita_state'])
plt.plot(dfEconomy.index, dfEconomy['RealGDPperCapita_growth'])


plt.show()

#===============================================================================
# def get(url):
#     response = urllib.request.urlopen(url)
#     content = response.read()
#     data = json.loads(content.decode('UTF-8'))
#     aryDate = []
#     aryValue = []
#     for this in data['observations']:
#         aryDate.append(this['date'])
#         aryValue.append(this['value'])
#     df = pd.DataFrame({'Date': aryDate, 'Value': aryValue})
#     df['Date'] = pd.to_datetime(df['Date'])
#     df = df.set_index(['Date'])
#     return(df)
#===============================================================================

# Connecting to MySQL Server
#===============================================================================
# conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='', db='InvestmentAnalysis')
# dfData = pdsql.read_sql('SELECT * FROM InvestmentAnalysis.tblHistoricalData WHERE Symbol = "^GSPC"', conn)
# conn.close()
# 
# dfData['Date'] = pd.to_datetime(dfData['Date'])
# 
# #autocorrelation_plot(dfData['DailyChangePct'].head())
# dfData.plot(x='Date', y='Volatility')
# plt.show()
#===============================================================================