In [2]:
%matplotlib inline
import matplotlib as mpl
import pandas as pd
import numpy as np
import os
import seaborn as sns
sns.set(style="white")
import matplotlib.pyplot as plt
In [11]:
def get_draws(hist):
draws = pd.Series()
for i in range(len(hist)):
lb = float(hist.index[i].split(',')[0].strip('['))
ub = float(hist.index[i].split(',')[1].strip(']'))
n = int(round(hist[i]*10))
draws_ = np.random.uniform(lb,ub,n)
draws_ = pd.Series(draws_)
draws = pd.concat((draws,draws_), axis=0,ignore_index=True)
draws.name = hist.name
return draws
In [85]:
def get_df_draws(df):
df_draws = pd.DataFrame()
for id in range(df.shape[0]):
#print(id)
hist = df.iloc[id,2:14]
hist.name = df.loc[id,'target']
draws = get_draws(hist)
df_draws[draws.name] = draws
return df_draws
In [86]:
f = 'agg_hist_' + str(2015) + 'Q' + str(4)
fname = f + '.csv'
raw_df = pd.read_csv('../clean_data/' + fname,header = 0)
raw_df
Out[86]:
In [87]:
#../clean_data/agg_hist_2016Q1.csv
df_big = pd.DataFrame(columns = ['source','target','point'])
years = [2015,2016]
quarters = [1,2,3,4]
df_List = []
for year in years:
for q in quarters:
f = 'agg_hist_' + str(year) + 'Q' + str(q)
fname = f + '.csv'
if os.path.isfile('../clean_data/' + fname):
raw_df = pd.read_csv('../clean_data/' + fname,header = 0)
# find the row where the growth expectations start
df_draws = get_df_draws(raw_df)
df_melt = pd.melt(df_draws)
df_melt.rename(columns={'variable':'target', 'value':'point'}, inplace=True)
df_melt['source'] = str(year) + '-Q' + str(q)
df_melt = df_melt[['source','target','point']]
df_melt = df_melt[df_melt['point'].notnull()]
df_big = pd.concat([df_big,df_melt], axis=0)
In [73]:
df = df_big
In [75]:
df.head()
Out[75]:
In [ ]:
In [59]:
target = '2020'
mask1 = (df.target == target) & (df.source == '2016-Q1')
mask2 = (df.target == target) & (df.source == '2016-Q2')
In [60]:
x1 = df.loc[mask1,'point'].values
x2 = df.loc[mask2,'point'].values
#x1 = x1.reindex()
#x2 = x2.reindex()
x1 = pd.Series(x1, name="$X_1$")
x2 = pd.Series(x2, name="$X_2$")
In [14]:
#sns.jointplot(x=x1, y = x2,kind='reg');
In [15]:
#sns.distplot(x1,norm_hist=True);
In [17]:
#default_rcParams = dict(mpl.rcParams)
#plt.style.use('fivethirtyeight')
In [284]:
with plt.style.context('ggplot'):
plt.figure(figsize = (10,6))
#fig, ax = plt.subplots()
sns.distplot(x1, hist=False, rug=False, kde_kws={'shade':True}, color='b', label='2016-Q1')
sns.distplot(x2, hist=False, rug=False, kde_kws={'shade':True}, color='g', label='2016-Q2')
plt.xlabel('')
plt.legend(loc='best', fontsize='medium', title='',)
plt.title('Distributions of SPF point forecasts for HICP inflation in '+target, fontsize='large')
;
plt.savefig('../figures/Source2016_Q1_Q2-Target' + target +'.png')
plt.savefig('../figures/Source2016_Q1_Q2-Target' + target +'.pdf')
plt.savefig('../figures/Source2016_Q1_Q2-Target' + target +'.jpeg')
In [236]:
ls ../figures/
In [226]:
g = sns.jointplot(x1,x2, kind="kde", size=7, space=0)
In [ ]:
plt.figure()
for i in range(1, 7):
plt.subplot(3, 2, i)
plt.title(i)
plt.xticks([])
plt.yticks([])
plt.tight_layout()
In [66]:
plt.figure(figsize = (10,6))
sns.set(style="white", palette="muted", color_codes=True)
#fig, axes = plt.subplots(2, 2, figsize = (7,7) )
years = [2016, 2017, 2018, 2020]
bins = [-2.0,-1.0,-0.5,0.0,0.5,1.0,1.5,2.0,2.5,3.0,3.5,4.0,5.0]
for i in range(1,5):
target = str(years[i-1])
mask1 = (df.target == target) & (df.source == '2016-Q1')
mask2 = (df.target == target) & (df.source == '2016-Q2')
x1 = df.loc[mask1,'point'].values
x2 = df.loc[mask2,'point'].values
x1 = pd.Series(x1, name="$X_1$")
x2 = pd.Series(x2, name="$X_2$")
plt.subplot(2, 2, i)
plt.title(str(target)) #, fontsize='large')
sns.distplot(x1, hist=False, bins = bins, rug=False, kde_kws={'shade':True}, color='b', label='2016-Q1')
sns.distplot(x2, hist=False, bins = bins, rug=False, kde_kws={'shade':True}, color='g', label='2016-Q2')
plt.xlabel('')
plt.legend(loc='best', fontsize='medium', title='',)
plt.tight_layout()
plt.suptitle('Distributions of SPF aggregate histograms for HICP inflation', fontsize='large', y = 1.04)
plt.subplots_adjust(hspace=0.4)
#plt.suptitle("I never said they'd be pretty")
;
Out[66]:
In [76]:
target = '2017'
mask1 = (df.target == target) & (df.source == '2016-Q1')
mask2 = (df.target == target) & (df.source == '2016-Q2')
mask3 = (df.target == target) & (df.source == '2015-Q4')
In [82]:
df[mask3]
Out[82]:
In [84]:
plt.figure(figsize = (10,6))
sns.set(style="white", palette="muted", color_codes=True)
#fig, axes = plt.subplots(2, 2, figsize = (7,7) )
years = [2017]
bins = [-2.0,-1.0,-0.5,0.0,0.5,1.0,1.5,2.0,2.5,3.0,3.5,4.0,5.0]
for i in range(1,2):
target = str(years[i-1])
mask1 = (df.target == target) & (df.source == '2016-Q1')
mask2 = (df.target == target) & (df.source == '2016-Q2')
mask3 = (df.target == target) & (df.source == '2015-Q4')
x1 = df.loc[mask1,'point'].values
x2 = df.loc[mask2,'point'].values
x3 = df.loc[mask3,'point'].values
x1 = pd.Series(x1, name="$X_1$")
x2 = pd.Series(x2, name="$X_2$")
x3 = pd.Series(x3, name="$X_3$")
plt.subplot(1, 1, i)
plt.title(str(target)) #, fontsize='large')
sns.distplot(x1, hist=False, bins = bins, rug=False, kde_kws={'shade':True}, color='b', label='2016-Q1')
sns.distplot(x2, hist=False, bins = bins, rug=False, kde_kws={'shade':True}, color='g', label='2016-Q2')
sns.distplot(x3, hist=False, bins = bins, rug=False, kde_kws={'shade':True}, color='y', label='2015-Q4')
plt.xlabel('')
plt.legend(loc='best', fontsize='medium', title='',)
plt.tight_layout()
plt.suptitle('Distributions of SPF aggregate histograms for HICP inflation', fontsize='large', y = 1.04)
plt.subplots_adjust(hspace=0.4)
#plt.suptitle("I never said they'd be pretty")
;
Out[84]:
In [ ]:
plt.savefig('../figures/Source2016_Q1_Q2-Target' + target +'.png')
plt.savefig('../figures/Source2016_Q1_Q2-Target' + target +'.pdf')
plt.savefig('../figures/Source2016_Q1_Q2-Target' + target +'.jpeg')