06-28-2016-Plot-KDEs-OF-point-forecasts



In [1]:
%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 [2]:
years = [2016]
quarters = [1,2]
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_List.append(df)

In [3]:
df.head()


Out[3]:
source target id point
0 2016-Q2 2016 4 0.3
1 2016-Q2 2016 5 0.4
2 2016-Q2 2016 6 0.0
3 2016-Q2 2016 7 0.5
4 2016-Q2 2016 8 0.2

In [4]:
df_List[1].head()


Out[4]:
source target id point
0 2016-Q2 2016 4 0.3
1 2016-Q2 2016 5 0.4
2 2016-Q2 2016 6 0.0
3 2016-Q2 2016 7 0.5
4 2016-Q2 2016 8 0.2

In [5]:
df_merged = pd.merge(left=df_List[0], right=df_List[1], how='inner', left_on='target', right_on='target')

In [6]:
df_merged.head()


Out[6]:
source_x target id_x point_x source_y id_y point_y
0 2016-Q1 2016 1 0.3 2016-Q2 4 0.3
1 2016-Q1 2016 1 0.3 2016-Q2 5 0.4
2 2016-Q1 2016 1 0.3 2016-Q2 6 0.0
3 2016-Q1 2016 1 0.3 2016-Q2 7 0.5
4 2016-Q1 2016 1 0.3 2016-Q2 8 0.2

In [7]:
df_List[0].shape


Out[7]:
(292, 4)

In [8]:
#df_new = df_List[0].join(other=df_List[1],on='target',how='inner')

In [9]:
df = pd.concat([df_List[0],df_List[1]], axis = 0)

In [10]:
df.head(10)


Out[10]:
source target id point
0 2016-Q1 2016 1 0.3
1 2016-Q1 2016 2 0.3
2 2016-Q1 2016 4 0.9
3 2016-Q1 2016 5 0.9
4 2016-Q1 2016 6 0.6
6 2016-Q1 2016 14 0.0
7 2016-Q1 2016 15 1.2
8 2016-Q1 2016 16 0.7
9 2016-Q1 2016 20 0.7
10 2016-Q1 2016 22 0.7

In [11]:
df.shape


Out[11]:
(548, 4)

In [12]:
df_List[1].shape


Out[12]:
(256, 4)

In [58]:
target = '2020'
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 = 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/


2016-04-06-aggregate_histograms_2016.pdf
2016-04-06-aggregate_histograms_2016.png
2016-04-06-aggregate_histograms_2016.svg
2016-04-06ggregate_histograms_2016.eps

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 [ ]:
target = '2020'
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 = x1.reindex()
#x2 = x2.reindex()
x1 = pd.Series(x1, name="$X_1$")
x2 = pd.Series(x2, name="$X_2$")

In [93]:
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]

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, 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.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")
;


Out[93]:
''

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')