06-28-2016-Plot-KDEs-OF-point-forecasts-V.0.2



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 [16]:
df_big = pd.DataFrame(columns = ['source','target','id','point'])

years = [2015,2016]
quarters = [1,2,3,4]
df_List = []
for year in years:
    for q in quarters:
        f = str(year) + 'Q' + str(q)
        fname = f + '.csv'
        if os.path.isfile('../data/' + fname):
            raw_df = pd.read_csv('../data/' + fname,header = 1)
            # find the row where the growth expectations start
            dum = raw_df[raw_df['TARGET_PERIOD'] == 'GROWTH EXPECTATIONS; YEAR-ON-YEAR CHANGE IN REAL GDP'].index[0]
            mask_columns = ~raw_df.columns.str.contains('Unnamed')
            df = raw_df.iloc[0:dum-1,mask_columns]    
            df['source'] = str(year) + '-Q' + str(q)
            df = df.rename(columns={'TARGET_PERIOD':'target','FCT_SOURCE':'id','POINT':'point',
                                   'TN1_0':'[-2.0,-1.1]','FN1_0TN0_6':'[-1.0,-0.6]',
                                   'FN0_5TN0_1':'[-0.5,-0.1]','F0_0T0_4':'[0.0,0.4]',
                                   'F0_5T0_9':'[0.5,0.9]','F1_0T1_4':'[1.0,1.4]',
                                   'F1_5T1_9':'[1.5,1.9]','F2_0T2_4':'[2.0,2.4]',
                                   'F2_5T2_9':'[2.5,2.9]','F3_0T3_4':'[3.0,3.4]',
                                   'F3_5T3_9':'[3.5,3.9]','F4_0':'[4.0,5.0]'})
            df = df[['source','target','id','point']]
            # remove rows where point is missing
            maskNaN = df.point.isnull()
            df = df[~maskNaN]                    
            df.fillna(0,inplace = True)
            for colname in df.columns[3:]:
                df[colname] = df[colname].astype('float')
            df_big = pd.concat([df_big,df], axis=0, ignore_index=True)
            #df_List.append(df)

In [17]:
df = df_big
df.tail()


Out[17]:
source target id point
1706 2016-Q2 2020 101 1.7
1707 2016-Q2 2020 102 1.6
1708 2016-Q2 2020 103 1.8
1709 2016-Q2 2020 105 2.0
1710 2016-Q2 2020 112 2.0

In [18]:
df.shape


Out[18]:
(1711, 4)

In [25]:
print(df.loc[df.target=='2017','source'].unique())
print(df.loc[df.target=='2020','source'].unique())


['2015-Q1' '2015-Q2' '2015-Q3' '2015-Q4' '2016-Q1' '2016-Q2']
['2015-Q3' '2015-Q4' '2016-Q1' '2016-Q2']

In [47]:
target = '2017'
mask1 = (df.target == target) & (df.source == '2015-Q3')
mask2 = (df.target == target) & (df.source == '2015-Q4')
mask3 = (df.target == target) & (df.source == '2016-Q1')
mask4 = (df.target == target) & (df.source == '2016-Q2')

x1 = df.loc[mask1,'point'].values
x2 = df.loc[mask2,'point'].values
x3 = df.loc[mask3,'point'].values
x4 = df.loc[mask4,'point'].values
#x1 = x1.reindex()
#x2 = x2.reindex()
x1 = pd.Series(x1, name="$X_1$")
x2 = pd.Series(x2, name="$X_2$")
x3 = pd.Series(x3, name="$X_3$")
x4 = pd.Series(x4, name="$X_4$")

In [65]:
plt.style.use('fivethirtyeight')
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]


target = str(years[i-1])

mask1 = (df.target == target) & (df.source == '2015-Q3')
mask2 = (df.target == target) & (df.source == '2015-Q4')
mask3 = (df.target == target) & (df.source == '2016-Q1')
mask4 = (df.target == target) & (df.source == '2016-Q2')
    
x1 = df.loc[mask1,'point'].values
x2 = df.loc[mask2,'point'].values
x3 = df.loc[mask3,'point'].values
x4 = df.loc[mask4,'point'].values

x1 = pd.Series(x1, name="$X_1$")
x2 = pd.Series(x2, name="$X_2$")
x3 = pd.Series(x3, name="$X_3$")
x4 = pd.Series(x4, name="$X_4$")

    # plt.subplot(1, 1, i)
#    plt.title(str(target)) #, fontsize='large')
    
sns.distplot(x1, hist=False, rug=False, kde_kws={'shade':True}, color='b', label='2015-Q3')
sns.distplot(x2, hist=False, rug=False, kde_kws={'shade':True}, color='g', label='2015-Q4')
    
sns.distplot(x3, hist=False, rug=False, kde_kws={'shade':True}, color='y', label='2016-Q1')
sns.distplot(x4, hist=False, rug=False, kde_kws={'shade':True}, color='r', label='2016-Q2')
    
plt.xlabel('')
plt.legend(loc='best', fontsize='medium', title='       source:',)
plt.title('Distributions of SPF point forecasts for HICP inflation in ' + target, fontsize='large',  y = 1.04)



plt.tight_layout()
#plt.suptitle('Distributions of SPF point forecasts for HICP inflation in ' + target, fontsize='large',  y = 1.04)
#plt.subplots_adjust(hspace=0.4)
#plt.suptitle("I never said they'd be pretty")
;
plt.savefig('../figures/Source2015_Q3_Q4_2016_Q1_Q2-Target' + target +'.png')
plt.savefig('../figures/Source2015_Q3_Q4_2016_Q1_Q2-Target' + target +'.pdf')
plt.savefig('../figures/Source2015_Q3_Q4_2016_Q1_Q2-Target' + target +'.jpeg')



In [49]:
target = '2017'
mask1 = (df.target == target) & (df.source == '2015-Q3')
mask2 = (df.target == target) & (df.source == '2015-Q4')
mask3 = (df.target == target) & (df.source == '2016-Q1')
mask4 = (df.target == target) & (df.source == '2016-Q2')

x1 = df.loc[mask1,'point'].values
x2 = df.loc[mask2,'point'].values
x3 = df.loc[mask3,'point'].values
x4 = df.loc[mask4,'point'].values
#x1 = x1.reindex()
#x2 = x2.reindex()
x1 = pd.Series(x1, name="$X_1$")
x2 = pd.Series(x2, name="$X_2$")
x3 = pd.Series(x3, name="$X_3$")
x4 = pd.Series(x4, name="$X_4$")

In [57]:
plt.figure(figsize = (10,6))

sns.set(style="white", palette="muted", color_codes=True)
#fig, axes = plt.subplots(2, 2, figsize = (7,7) )
sources = ['2015-Q3','2015-Q4','2016-Q1','2016-Q2']    
for i in range(1,5):

    plt.subplot(2, 2, i)
    plt.title(sources[i-1]) #, fontsize='large')
    
    sns.distplot(eval('x'+str(i)), hist=False, rug=False, kde_kws={'shade':True}, color='b', label='')
        
    plt.xlabel('')
    plt.legend(loc='best', fontsize='medium', title='       source:',)
    plt.xlim([0,3])
plt.tight_layout()
plt.suptitle('Distributions of SPF point forecasts for HICP inflation', fontsize='large',  y = 1.04)
plt.subplots_adjust(hspace=0.4)
#plt.suptitle("I never said they'd be pretty")
;


/Users/BadWizard/anaconda3/lib/python3.4/site-packages/matplotlib/axes/_axes.py:475: UserWarning: No labelled objects found. Use label='...' kwarg on individual plots.
  warnings.warn("No labelled objects found. "
Out[57]:
''

In [ ]: