def _decile_net(nivvie_net, champm, wprm): ''' Décile de niveau de vie net 'men' ''' labels = arange(1, 11) method = 2 decile, values = mark_weighted_percentiles(nivvie_net, labels, wprm * champm, method, return_quantiles = True) return decile * champm
def _pauvre60(nivvie, champm, wprm): ''' Indicatrice de pauvreté à 60% du niveau de vie median 'men' ''' labels = arange(1, 3) method = 2 percentile, values = mark_weighted_percentiles(nivvie, labels, wprm * champm, method, return_quantiles = True) threshold = .6 * values[1] return (nivvie <= threshold) * champm
def _decile_net(nivvie_net, champm, wprm): ''' Décile de niveau de vie net 'men' ''' labels = arange(1, 11) method = 2 decile, values = mark_weighted_percentiles(nivvie_net, labels, wprm * champm, method, return_quantiles=True) return decile * champm
def _pauvre60(nivvie, champm, wprm): ''' Indicatrice de pauvreté à 60% du niveau de vie median 'men' ''' labels = arange(1, 3) method = 2 percentile, values = mark_weighted_percentiles(nivvie, labels, wprm * champm, method, return_quantiles=True) threshold = .6 * values[1] return (nivvie <= threshold) * champm
def _decile(nivvie, champm, wprm): ''' Décile de niveau de vie disponible 'men' ''' labels = arange(1, 11) method = 2 decile, values = mark_weighted_percentiles(nivvie, labels, wprm * champm, method, return_quantiles = True) # print values # print len(values) # print (nivvie*champm).min() # print (nivvie*champm).max() # print decile.min() # print decile.max() # print (nivvie*(decile==1)*champm*wprm).sum()/( ((decile==1)*champm*wprm).sum() ) del values return decile * champm
def _decile(nivvie, champm, wprm): ''' Décile de niveau de vie disponible 'men' ''' labels = arange(1, 11) method = 2 decile, values = mark_weighted_percentiles(nivvie, labels, wprm * champm, method, return_quantiles=True) # print values # print len(values) # print (nivvie*champm).min() # print (nivvie*champm).max() # print decile.min() # print decile.max() # print (nivvie*(decile==1)*champm*wprm).sum()/( ((decile==1)*champm*wprm).sum() ) del values return decile * champm
def compute(self): """ Compute inequality dataframe """ output = self.simulation.output_table final_df = None WEIGHT = model.WEIGHT FILTERING_VARS = model.FILTERING_VARS for varname, entities in self.vars.iteritems(): for entity in entities: #idx = output.index[entity] val = output.get_value(varname, entity) weights = output._inputs.get_value(WEIGHT, entity) filter_var_name = FILTERING_VARS[0] filter_var = output._inputs.get_value(filter_var_name, entity) items = [] # Compute mean moy = (weights * filter_var * val).sum() / (weights * filter_var).sum() items.append(("Moyenne", [moy])) # Compute deciles labels = range(1, 11) method = 2 decile, values = mark_weighted_percentiles(val, labels, weights * filter_var, method, return_quantiles=True) labels = ['D' + str(d) for d in range(1, 11)] del decile for l, v in zip(labels[:-1], values[1:-1]): items.append((l, [v])) # Compute Gini gini_coeff = gini(val, weights * filter_var) items.append((_("Gini index"), [gini_coeff])) df = DataFrame.from_items(items, orient='index', columns=[varname]) df = df.reset_index() if final_df is None: final_df = df else: final_df = final_df.merge(df, on='index') final_df[u"Initial à net"] = ( final_df['nivvie_net'] - final_df['nivvie_ini']) / final_df['nivvie_ini'] final_df[u"Net à disponible"] = ( final_df['nivvie'] - final_df['nivvie_net']) / final_df['nivvie_net'] final_df = final_df[[ 'index', 'nivvie_ini', u"Initial à net", 'nivvie_net', u"Net à disponible", 'nivvie' ]] self.inequality_dataframe = final_df # poverty poverty = dict() entity = "men" varname = "nivvie" for percentage in [40, 50, 60]: # idx = output.index[entity] varname = "pauvre" + str(percentage) val = output.get_value(varname, entity) weights = output._inputs.get_value(WEIGHT, entity) filter_var_name = FILTERING_VARS[0] filter_var = output._inputs.get_value(filter_var_name, entity) poverty[percentage] = (weights * filter_var * val).sum() / (weights * filter_var).sum() self.poverty = poverty
year = 2009 erf = ErfsDataTable(year=year) #erf.set_config() vars = [ "zrstm", "zchom", "pfamm", "wprm", "pauvre50m", "pauvre60m", "nivviem", "champm"] df = erf.get_values(variables=vars, table="erf_menage" ) labels = arange(1,11) method = 2 nivvie = df["nivviem"].astype("float64").values wprm = df["wprm"].astype("float64").values decil, values = mark_weighted_percentiles(nivvie, labels, wprm, method, return_quantiles = True) df2 = DataFrame({"decile" : decil}) df["decile"] = df2["decile"] indexes = { "zrstm" : .01, "zchom": .01, "pfamm" : .01} # TODO change 1% results = DataFrame(index =indexes.keys(), columns = ["total", "pauvre50", "pauvre60"] + ["decile>"+str(decile) for decile in range(0,10)] ) for var, index in indexes.iteritems(): total = df[var]*index*df["wprm"]*df["champm"] pauvre50 = df[var]*index*df["wprm"]*(df["pauvre50m"]<=0)*df["champm"] pauvre60 = df[var]*index*df["wprm"]*(df["pauvre60m"]<=0)*df["champm"] results.set_value(var, "total", total.sum()/1e6)
def compute(self): """ Compute inequality dataframe """ output = self.simulation.output_table final_df = None WEIGHT = model.WEIGHT FILTERING_VARS = model.FILTERING_VARS for varname, entities in self.vars.iteritems(): for entity in entities: #idx = output.index[entity] val = output.get_value(varname, entity) weights = output._inputs.get_value(WEIGHT, entity) filter_var_name = FILTERING_VARS[0] filter_var= output._inputs.get_value(filter_var_name, entity) items = [] # Compute mean moy = (weights*filter_var*val).sum()/(weights*filter_var).sum() items.append( ("Moyenne", [moy])) # Compute deciles labels = range(1,11) method = 2 decile, values = mark_weighted_percentiles(val, labels, weights*filter_var, method, return_quantiles=True) labels = [ 'D'+str(d) for d in range(1,11)] del decile for l, v in zip(labels[:-1],values[1:-1]): items.append( (l, [v])) # Compute Gini gini_coeff = gini(val, weights*filter_var) items.append( ( _("Gini index"), [gini_coeff])) df = DataFrame.from_items(items, orient = 'index', columns = [varname]) df = df.reset_index() if final_df is None: final_df = df else: final_df = final_df.merge(df, on='index') final_df[u"Initial à net"] = (final_df['nivvie_net']-final_df['nivvie_ini'])/final_df['nivvie_ini'] final_df[u"Net à disponible"] = (final_df['nivvie']-final_df['nivvie_net'])/final_df['nivvie_net'] final_df = final_df[['index','nivvie_ini', u"Initial à net", 'nivvie_net',u"Net à disponible",'nivvie']] self.inequality_dataframe = final_df # poverty poverty = dict() entity = "men" varname = "nivvie" for percentage in [ 40, 50, 60]: # idx = output.index[entity] varname = "pauvre" + str(percentage) val = output.get_value(varname, entity) weights = output._inputs.get_value(WEIGHT, entity) filter_var_name = FILTERING_VARS[0] filter_var= output._inputs.get_value(filter_var_name, entity) poverty[percentage] = (weights*filter_var*val).sum()/(weights*filter_var).sum() self.poverty = poverty