def compute(self):
        """
        Compute inequality dataframe
        """
        final_df = None
        simulation = self.survey_scenario.new_simulation()
        column_by_name = simulation.tax_benefit_system.column_by_name

        # amounts and beneficiaries from current data and default data if exists
        # Build weights for each entity

        from openfisca_france_data import FILTERING_VARS
        for varname, entities in self.vars.iteritems():
            for entity_key_plural in entities:
                column = column_by_name[varname]
                weight_name = self.survey_scenario.weight_column_name_by_entity_key_plural[
                    column.entity_key_plural]
                filter_by = FILTERING_VARS[0]
                filter_by_name = FILTERING_VARS[0]
                if column.entity_key_plural is not 'menages':
                    filter_by_name = "{}_{}".format(filter_by,
                                                    column.entity_key_plural)
                val = simulation.calculate(varname)
                weights = simulation.calculate(weight_name)
                filter_var = simulation.calculate(filter_by_name)

            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_data_frame = final_df

        # Poverty
        poverty = dict()
        varname = "nivvie"
        for percentage in [40, 50, 60]:
            varname = "pauvre{}".format(percentage)
            column = column_by_name[varname]
            weight_name = self.survey_scenario.weight_column_name_by_entity_key_plural[
                column.entity_key_plural]
            filter_by_name = FILTERING_VARS[0]
            if column.entity_key_plural is not 'menages':
                filter_by_name = "{}_{}".format(filter_by,
                                                column.entity_key_plural)
            val = simulation.calculate(varname)
            weights = simulation.calculate(weight_name)
            filter_var = simulation.calculate(filter_by_name)
            poverty[percentage] = (weights * filter_var *
                                   val).sum() / (weights * filter_var).sum()

        self.poverty = poverty
    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
Example #3
0
    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
    def compute(self):
        """
        Compute inequality dataframe
        """
        final_df = None
        simulation = self.survey_scenario.new_simulation()
        column_by_name = simulation.tax_benefit_system.column_by_name

        # amounts and beneficiaries from current data and default data if exists
        # Build weights for each entity

        from openfisca_france_data import FILTERING_VARS
        for varname, entities in self.vars.iteritems():
            for entity_key_plural in entities:
                column = column_by_name[varname]
                weight_name = self.survey_scenario.weight_column_name_by_entity_key_plural[column.entity_key_plural]
                filter_by = FILTERING_VARS[0]
                filter_by_name = FILTERING_VARS[0]
                if column.entity_key_plural is not 'menages':
                    filter_by_name = "{}_{}".format(filter_by, column.entity_key_plural)
                val = simulation.calculate(varname)
                weights = simulation.calculate(weight_name)
                filter_var = simulation.calculate(filter_by_name)

            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_data_frame = final_df

        # Poverty
        poverty = dict()
        varname = "nivvie"
        for percentage in [40, 50, 60]:
            varname = "pauvre{}".format(percentage)
            column = column_by_name[varname]
            weight_name = self.survey_scenario.weight_column_name_by_entity_key_plural[column.entity_key_plural]
            filter_by_name = FILTERING_VARS[0]
            if column.entity_key_plural is not 'menages':
                filter_by_name = "{}_{}".format(filter_by, column.entity_key_plural)
            val = simulation.calculate(varname)
            weights = simulation.calculate(weight_name)
            filter_var = simulation.calculate(filter_by_name)
            poverty[percentage] = (weights * filter_var * val).sum() / (weights * filter_var).sum()

        self.poverty = poverty