develop-aggregate-uncertainty



In [1]:
"""
This NB is used to develop the .py script that makes csv files with
aggregate uncertainty in ../output/agg_uncert
"""
import os
import pandas as pd
import scipy.stats as stats
import scipy.optimize as opt
import numpy as np

In [2]:
input_dir = "../output/indiv_uncert/"
series = 'HICP' #'UN' #'HICP' 
year = '2014'
quarter = 'Q4'

input_file = series + year + quarter + '.csv'
fname = input_dir + input_file
print(input_file)


HICP2014Q4.csv

In [3]:
if not os.path.isfile(fname):
    print(input_file + ' does not exist in ' + input_dir)
else:
    print("it's all good!")


it's all good!

In [5]:
df = pd.read_csv(fname,)
df.head()


Out[5]:
source target newTarget id point [-2.0,-1.1] [-1.0,-0.6] [-0.5,-0.1] [0.0,0.4] [0.5,0.9] [1.0,1.4] [1.5,1.9] [2.0,2.4] [2.5,2.9] [3.0,3.4] [3.5,3.9] [4.0,5.0] GA_std ZL_std Hist_std
0 2014-Q4 2014 t 1 0.4 0 0.0 0 50 50 0.0 0 0 0 0 0 0 0.541179 0.275379 0.184780
1 2014-Q4 2014 t 2 0.5 0 0.0 0 40 60 0.0 0 0 0 0 0 0 0.524163 0.270801 0.177886
2 2014-Q4 2014 t 3 0.5 0 2.5 5 45 45 2.5 0 0 0 0 0 0 0.687504 0.385141 0.326563
3 2014-Q4 2014 t 6 0.5 0 0.0 0 10 90 0.0 0 0 0 0 0 0 0.337515 0.189297 0.150000
4 2014-Q4 2014 t 7 0.8 0 0.0 0 0 0 0.0 0 0 0 0 0 0 NaN NaN NaN

In [6]:
# Define the aggregation calculations
aggregations = {
    'point': { #
        '1.mean': 'mean',  #
         '2.std': 'std', #
         '3.IQR': lambda x: x.quantile(.75) - x.quantile(.25)
    },
    'GA_std': {#
       # 'mean': 'mean',
      'median': 'median'
    },
    'ZL_std': {#
      #  'mean': 'mean',
      'median': 'median'
    },
    'Hist_std': {#
     #   'mean': 'mean',
      'median': 'median'
    }
}

In [7]:
grouped = df.groupby(['source','newTarget']).agg(aggregations)
grouped = grouped.rename(columns={'point':'a. point','ZL_std':'b. ZL_std',
'GA_std':'c. GA_std','Hist_std':'d. Hist_std'})

In [8]:
df.loc[:,'newTarget'].unique()


Out[8]:
array(['t', 't+1', 'roll 1', 't+2', 'roll 2', 't+4'], dtype=object)

In [ ]:
for h in df.loc[:,'newTarget'].unique():
    df_h = grouped.xs(h,level=1).sortlevel(0,axis=1)
    print(df_h)
#    df_h.to_excel(writer,h)
#writer.save()

In [13]:
grouped.sortlevel(0,axis=1)


Out[13]:
a. point b. ZL_std c. GA_std d. Hist_std
1.mean 2.std 3.IQR median median median
source newTarget
2014-Q4 roll 1 1.065511 0.245142 0.210000 0.514984 0.985757 0.472777
roll 2 1.369630 0.283991 0.343750 0.620652 1.157617 0.586105
t 0.526748 0.074569 0.049898 0.274241 0.525454 0.183081
t+1 0.957279 0.211443 0.240835 0.493810 0.933684 0.449614
t+2 1.351357 0.270751 0.310321 0.578972 1.096569 0.541773
t+4 1.801161 0.181649 0.300000 0.644851 1.324874 0.611673

In [ ]:
df.to_csv()

In [10]:
grouped.sortlevel(0,axis=1).to_csv('temp.csv',)

In [11]:
dd = pd.read_csv('temp.csv',heade)

In [12]:
dd


Out[12]:
Unnamed: 0 Unnamed: 1 a. point a. point.1 a. point.2 b. ZL_std c. GA_std d. Hist_std
0 NaN NaN 1.mean 2.std 3.IQR median median median
1 source newTarget NaN NaN NaN NaN NaN NaN
2 2014-Q4 roll 1 1.0655108194145835 0.24514198674735999 0.20999999999999996 0.5149838185160123 0.9857566233927586 0.4727774513642952
3 2014-Q4 roll 2 1.3696302886409086 0.2839907967053319 0.3437499999999998 0.620651539378847 1.1576167938509454 0.5861045286623525
4 2014-Q4 t 0.526748138880328 0.07456932214709217 0.04989825009999993 0.2742413778650721 0.5254538554927635 0.18308063392537868
5 2014-Q4 t+1 0.9572793070816666 0.2114425207008638 0.24083518675000015 0.49381035262620643 0.9336839322731612 0.4496135487583991
6 2014-Q4 t+2 1.351357007915385 0.27075147407254724 0.3103205173000001 0.5789717897560575 1.0965685132163672 0.5417734937393288
7 2014-Q4 t+4 1.8011606921020404 0.18164877164689394 0.30000000000000004 0.6448514040717699 1.3248744395116292 0.611672721738119

In [14]:
dd = pd.read_csv('../output/agg_uncert/GDP2013Q1.csv')
dd.head()


Out[14]:
a. point a. point.1 a. point.2 b. ZL_std c. GA_std d. Hist_std
0 1.mean 2.std 3.IQR median median median
1 NaN NaN NaN NaN NaN NaN
2 0.09589854892916672 0.4506105609369838 0.6000000000000001 0.5236943660849184 1.2058351616744827 0.5207629897894124
3 1.1353846889023258 0.43738089033007466 0.3999999999999999 0.6503396548471165 1.447478344667093 0.637686065802381
4 -0.042941372290909094 0.31674765394233484 0.31690308425 0.43352239465414777 1.1242483698577297 0.4566382797341004

In [ ]: