Exemple #1
0
import pandas as p
import matplotlib

from pandas import ExcelWriter

from matplotlib.ticker import MultipleLocator, FixedLocator


baseline = load_simulation()
nozlb = load_simulation(sim_dir='results/alt-sims/nozlb_2008')

gdp_diff = 100*(np.log(baseline.gdp) - np.log(nozlb.gdp))
gdp_mean = gdp_diff.groupby(level=0).mean()['2007Q4':]
gdp_q16 = gdp_diff.groupby(level=0).apply(lambda x: np.percentile(x, 16))['2007Q4':]
gdp_q84 = gdp_diff.groupby(level=0).apply(lambda x: np.percentile(x, 84))['2007Q4':]
col = figure_defaults()

#nozlbq16 = nozlb.groupby(level=0).quantile(0.16)['Interest Rate']['2007Q4':]
#nozlbq84 = nozlb.groupby(level=0).quantile(0.84)['Interest Rate']['2007Q4':]

not_mean = baseline.groupby(level=0).mean()['Notional Rate']['2007Q4':]
not_q16 = baseline.groupby(level=0)['Notional Rate'].apply(lambda x: np.percentile(x, 16))['2007Q4':]
not_q84 = baseline.groupby(level=0)['Notional Rate'].apply(lambda x: np.percentile(x, 84))['2007Q4':]

inv_diff = 100*(np.log(baseline.inv) - np.log(nozlb.inv))
inv_mean = inv_diff.groupby(level=0).mean()['2007Q4':]
inv_q16 = inv_diff.groupby(level=0).apply(lambda x: np.percentile(x, 16))['2007Q4':]
inv_q84 = inv_diff.groupby(level=0).apply(lambda x: np.percentile(x, 84))['2007Q4':]

cons_diff = 100*(np.log(baseline.cc) - np.log(nozlb.cc))
cons_mean = cons_diff.groupby(level=0).mean()['2007Q4':]
premium = 400 * np.log(premium)

premium_mean = premium.groupby(level=0).mean()
premium_q16 = premium.groupby(level=0).quantile(0.16)
premium_q84 = premium.groupby(level=0).quantile(0.84)

ep_mean = p.Series(premium_mean['endogvar_29'])
ep_mean.index = date_index

ep_q16 = p.Series(premium_q16['endogvar_29'])
ep_q16.index = date_index

ep_q84 = p.Series(premium_q84['endogvar_29'])
ep_q84.index = date_index

figure_defaults()

with saved_figure('shocks_combo.pdf', nrows=2, ncols=2) as (fig, ax):

    # Risk Premium Shock (top left)
    mean['scaled_liq'].plot(ax=ax[0, 0], linewidth=2)
    ax[0, 0].fill_between(q16['scaled_liq'].index,
                          q16['scaled_liq'],
                          q84['scaled_liq'],
                          alpha=0.3)
    ebpq['2008':].plot(ax=ax[0, 0], linestyle='dashed', color='green')
    baaq['2008':].plot(ax=ax[0, 0], linestyle='dotted', color='red')
    ax[0, 0].set_title(r'Risk Premium Shock', fontsize=16)
    ax[0, 0].set_xlim(['2008Q1', '2014Q1'])
    ax[0, 0].get_xaxis().set_minor_locator(MultipleLocator(4))
    ax[0, 0].tick_params('x',
gamQ = sim['inputs']['para'][9]
var_names = ['c', 'ppi', 'R', 'z', 'y', 'g', 'ylag', 'Eppi', 'Ec', 'Ey', 'Ez']

forecast = p.DataFrame(sim['output']['001']['fcst_states']['024'])
forecast.columns = var_names + ['weights']
forecast['ygr'] = forecast.y - forecast.ylag + forecast.z + gamQ

update = p.DataFrame(sim['output']['001']['update_states']['024'])
update.columns = var_names + ['weights']
update['ygr'] = update.y - update.ylag + update.z + gamQ

from scipy.stats import norm
true_filtered_density = norm(loc=-2.977 + gamQ, scale=np.sqrt(0.013452427))
xgrid = np.linspace(-6, 6, 1000)
cp = figure_defaults(grayscale=False)
with saved_figure(args.output) as (fig, ax):

    forecast.ygr.plot(kind='kde',
                      ax=ax,
                      linewidth=5,
                      alpha=0.7,
                      linestyle='dashed')
    despine()
    ax.axvline(x=update.ygr.mean(), linewidth=7, color=cp[1], alpha=0.7)
    ax.plot(xgrid,
            true_filtered_density.pdf(xgrid),
            linewidth=5,
            color=cp[2],
            alpha=0.7)
    ax.set_xlim(-4, 2)
        index = p.period_range(freq='Q', start='1983Q1', periods=80)
    else:
        index = p.period_range(start='2003Q1', freq='Q', periods=44)


    reps = filter(lambda x: x.isdigit(), results['output'].keys())

    T = len(index)
    sim_results = p.DataFrame()
    for r in reps:
        time_series = [results['output'][r]['mean_filtered_states']['{:03d}'.format(n+1)]
                       for n in range(T)]

        res_r = p.DataFrame(np.array(time_series), index=index, columns=var_names)
        res_r['sim'] = int(r)
        sim_results = sim_results.append(res_r)

    stds.append(sim_results.groupby(sim_results.index).std())
    means.append(sim_results.groupby(sim_results.index).mean())

cp = figure_defaults(n=5)
with saved_figure(args.output) as (fig,ax):
    for i, std in enumerate(stds):
        std.g.plot(ax=ax, linewidth=5, color=cp[i])

    ax.legend(['BSPF, M=40k', 'TPF(r*=2), M=40k', 'TPF(r*=3), M=40k',
               'TPF(r*=2), M=4k', 'TPF(r*=3), M=4k'], loc='lower right', ncol=2)

    despine()