示例#1
0
def varying_survey_simulation(year = 2009, increment = 10, target = 'irpp', varying = 'rni', used_as_input_variables = None):
    TaxBenefitSystem = openfisca_france_data.init_country()
    tax_benefit_system = TaxBenefitSystem()

    input_data_frame = get_input_data_frame(year)
#    Simulation 1 : get varying and target
    survey_scenario = SurveyScenario().init_from_data_frame(
        input_data_frame = input_data_frame,
        used_as_input_variables = used_as_input_variables,
        year = year,
        tax_benefit_system = tax_benefit_system
        )
    simulation = survey_scenario.new_simulation(debug = False)

    output_data_frame = pandas.DataFrame(
       dict([(name, simulation.calculate_add(name)) for name in [
           target, varying, 'idfoy_original'
           ]]))

#    Make input_data_frame_by_entity_key_plural from the previous input_data_frame and simulation
    input_data_frames_by_entity_key_plural = \
        from_input_df_to_entity_key_plural_df(input_data_frame, tax_benefit_system, simulation)
    foyers = input_data_frames_by_entity_key_plural['idfoy']
    foyers = pandas.merge(foyers, output_data_frame, on = 'idfoy_original')

#    Incrementation of varying:
    foyers[varying] = foyers[varying] + increment

#    On remplace la nouvelle base dans le dictionnaire
    input_data_frames_by_entity_key_plural['idfoy'] = foyers

#   2e simulation à partir de input_data_frame_by_entity_key_plural:
#   TODO: fix used_as_input_variabels in the from_input_df_to_entity_key_plural_df() function
    used_as_input_variables = used_as_input_variables + [varying]
    TaxBenefitSystem = openfisca_france_data.init_country()
    tax_benefit_system = TaxBenefitSystem()

    survey_scenario = SurveyScenario().init_from_data_frame(
        input_data_frame = None,
        input_data_frames_by_entity_key_plural = input_data_frames_by_entity_key_plural,
        used_as_input_variables = used_as_input_variables,
        year = year,
        tax_benefit_system = tax_benefit_system,
        )
    simulation = survey_scenario.new_simulation(debug = False)

    output_data_frame2 = pandas.DataFrame(
       dict([(name, simulation.calculate_add(name)) for name in [
           target, varying, 'idfoy_original'
           ]]))
    output_data_frame2.rename(columns = {varying: '{}_2'.format(varying),
                                         target: '{}_2'.format(target)}, inplace = True)
    merged = pandas.merge(output_data_frame, output_data_frame2, on = 'idfoy_original')
    merged['marginal_rate'] = marginal_rate_survey(merged, '{}'.format(target), '{}_2'.format(target), 'rni', 'rni_2')
    merged['average_rate'] = average_rate(target = merged[target], varying = merged[varying])
    return merged
示例#2
0
def df_survey_simulation(reductions):
    year = 2009
    TaxBenefitSystem = openfisca_france_data.init_country()
    tax_benefit_system = TaxBenefitSystem()
    input_data_frame = get_input_data_frame(year)

    survey_scenario_reference = SurveyScenario().init_from_data_frame(
        input_data_frame=input_data_frame,
        used_as_input_variables=['sal', 'cho', 'rst', 'age_en_mois', 'smic55'],
        year=year,
        tax_benefit_system=tax_benefit_system)

    simulation = survey_scenario_reference.new_simulation()

    #    from openfisca_core import periods
    #    period = periods.period('year', 2007)
    #    period = period.start.offset('first-of', 'month').period('year')
    #    bareme = simulation.legislation_at(period.start).ir.bareme

    data_frame_by_entity_key_plural = dict(foyers=pandas.DataFrame(
        dict([(name, simulation.calculate_add(name)) for name in [
            'rfr',
            'irpp',
            'rbg',
            'iaidrdi',
            'rng',
            'ip_net',
            'reductions',
            'decile_rfr',
            'weight_foyers',
        ] + reductions])), )
    return data_frame_by_entity_key_plural
示例#3
0
def reform_survey_simulation(reform = None, year = None, ind_variables = None, fam_variables = None,
                             foy_variables = None, men_variables = None, used_as_input_variables = None,
                             reform_specific_foy_variables = None):
    assert reform is not None
    assert year is not None

    TaxBenefitSystem = openfisca_france_data.init_country()
    tax_benefit_system = TaxBenefitSystem()
    reform = reform.build_reform(tax_benefit_system)
    input_data_frame = get_input_data_frame(year)
    survey_scenario_reform = SurveyScenario().init_from_data_frame(
        input_data_frame = input_data_frame,
        used_as_input_variables = used_as_input_variables,
        year = year,
        tax_benefit_system = reform
        )

    reference_simulation = survey_scenario_reform.new_simulation(debug = False, reference = True)

    reference_data_frame_by_entity_key_plural = from_simulation_to_data_frame_by_entity_key_plural(
        reference_simulation, ind_variables, fam_variables, foy_variables, men_variables)

    reform_simulation = survey_scenario_reform.new_simulation(debug = False)

    reform_data_frame_by_entity_key_plural = from_simulation_to_data_frame_by_entity_key_plural(
        reform_simulation, ind_variables, fam_variables, foy_variables + reform_specific_foy_variables, men_variables)

    return reform_data_frame_by_entity_key_plural, reference_data_frame_by_entity_key_plural
示例#4
0
    def init_from_data_frame(self,
                             input_data_frame=None,
                             input_data_frames_by_entity_key_plural=None,
                             reference_tax_benefit_system=None,
                             tax_benefit_system=None,
                             used_as_input_variables=None,
                             year=None):

        if used_as_input_variables is None:
            used_as_input_variables = self.default_used_as_input_variables

        FranceDataTaxBenefitSystem = init_country()
        france_data_tax_benefit_system = FranceDataTaxBenefitSystem()
        if tax_benefit_system is None:
            tax_benefit_system = france_data_tax_benefit_system
            reference_tax_benefit_system = None

        return super(SurveyScenario, self).init_from_data_frame(
            input_data_frame=input_data_frame,
            input_data_frames_by_entity_key_plural=
            input_data_frames_by_entity_key_plural,
            reference_tax_benefit_system=reference_tax_benefit_system,
            tax_benefit_system=tax_benefit_system,
            used_as_input_variables=used_as_input_variables,
            year=year)
示例#5
0
def simulate_reform_cd(year):
    assert year is not None
    TaxBenefitSystem = openfisca_france_data.init_country()
    tax_benefit_system = TaxBenefitSystem()
    reform = reform_cd.build_reform(tax_benefit_system)
    input_data_frame = get_input_data_frame(year)
    survey_scenario = SurveyScenario().init_from_data_frame(
        input_data_frame = input_data_frame,
        used_as_input_variables = ['sal', 'cho', 'rst', 'age_en_mois', 'smic55'],
        year = year,
        tax_benefit_system = reform
        )
    reference_simulation = survey_scenario.new_simulation(debug = True, reference = True)
    reform_simulation = survey_scenario.new_simulation(debug = True)

    reform_data_frame_by_entity_key_plural = dict(
        foyers = pandas.DataFrame(
            dict([(name, reference_simulation.calculate_add(name)) for name in [
                'impo',
                'rfr',
                ]])
            ),
        )

    return reform_data_frame_by_entity_key_plural, reference_simulation
示例#6
0
def test_survey_simulation():
    year = 2009
    TaxBenefitSystem = openfisca_france_data.init_country()
    tax_benefit_system = TaxBenefitSystem()
    input_data_frame = get_input_data_frame(year)
    survey_scenario_reform = SurveyScenario().init_from_data_frame(
        input_data_frame = input_data_frame,
        used_as_input_variables = ['sal', 'cho', 'rst', 'age_en_mois', 'smic55'],
        year = year,
        tax_benefit_system = tax_benefit_system
        )

    simulation = survey_scenario_reform.new_simulation(debug = False, reference = True)
    simulation_data_frame_by_entity_key_plural = dict(
        foyers = pandas.DataFrame(
            dict([(name, simulation.calculate_add(name)) for name in [
                'rfr',
                'irpp',
                'rbg',
                'iaidrdi',
                'rng',
                'rni',
                'ip_net',
                'reductions',
                'decile_rfr',
                'weight_foyers',
                ]])
            ),
        )


    return simulation_data_frame_by_entity_key_plural
示例#7
0
def test_survey_simulation():
    year = 2009
    TaxBenefitSystem = openfisca_france_data.init_country()
    tax_benefit_system = TaxBenefitSystem()
    input_data_frame = get_input_data_frame(year)
    survey_scenario_reform = SurveyScenario().init_from_data_frame(
        input_data_frame=input_data_frame,
        used_as_input_variables=['sal', 'cho', 'rst', 'age_en_mois', 'smic55'],
        year=year,
        tax_benefit_system=tax_benefit_system)

    simulation = survey_scenario_reform.new_simulation(debug=False,
                                                       reference=True)
    simulation_data_frame_by_entity_key_plural = dict(foyers=pandas.DataFrame(
        dict([(name, simulation.calculate_add(name)) for name in [
            'rfr',
            'irpp',
            'rbg',
            'iaidrdi',
            'rng',
            'rni',
            'ip_net',
            'reductions',
            'decile_rfr',
            'weight_foyers',
        ]])), )

    return simulation_data_frame_by_entity_key_plural
    def init_from_data_frame(self, input_data_frame = None, tax_benefit_system = None, used_as_input_variables = None,
            year = None):

        if tax_benefit_system is None:
            TaxBenefitSystem = openfisca_france_data.init_country()
            tax_benefit_system = TaxBenefitSystem()

        super(SurveyScenario, self).init_from_data_frame(
            input_data_frame = input_data_frame,
            tax_benefit_system = tax_benefit_system,
            used_as_input_variables = used_as_input_variables,
            year = year)
        return self
示例#9
0
    def init_from_data_frame(self,
                             input_data_frame=None,
                             tax_benefit_system=None,
                             used_as_input_variables=None,
                             year=None):

        if tax_benefit_system is None:
            TaxBenefitSystem = openfisca_france_data.init_country()
            tax_benefit_system = TaxBenefitSystem()

        super(SurveyScenario, self).init_from_data_frame(
            input_data_frame=input_data_frame,
            tax_benefit_system=tax_benefit_system,
            used_as_input_variables=used_as_input_variables,
            year=year)
        return self
示例#10
0
    def init_from_data_frame(self, input_data_frame = None, input_data_frames_by_entity_key_plural = None,
            reference_tax_benefit_system = None, tax_benefit_system = None, used_as_input_variables = None,
            year = None):

        if used_as_input_variables is None:
            used_as_input_variables = self.default_used_as_input_variables

        FranceDataTaxBenefitSystem = init_country()
        france_data_tax_benefit_system = FranceDataTaxBenefitSystem()
        if tax_benefit_system is None:
            tax_benefit_system = france_data_tax_benefit_system
            reference_tax_benefit_system = None

        return super(SurveyScenario, self).init_from_data_frame(
            input_data_frame = input_data_frame,
            input_data_frames_by_entity_key_plural = input_data_frames_by_entity_key_plural,
            reference_tax_benefit_system = reference_tax_benefit_system,
            tax_benefit_system = tax_benefit_system,
            used_as_input_variables = used_as_input_variables,
            year = year
            )
示例#11
0
def simulate_reform_cd(year):
    assert year is not None
    TaxBenefitSystem = openfisca_france_data.init_country()
    tax_benefit_system = TaxBenefitSystem()
    reform = reform_cd.build_reform(tax_benefit_system)
    input_data_frame = get_input_data_frame(year)
    survey_scenario = SurveyScenario().init_from_data_frame(
        input_data_frame=input_data_frame,
        used_as_input_variables=['sal', 'cho', 'rst', 'age_en_mois', 'smic55'],
        year=year,
        tax_benefit_system=reform)
    reference_simulation = survey_scenario.new_simulation(debug=True,
                                                          reference=True)
    reform_simulation = survey_scenario.new_simulation(debug=True)

    reform_data_frame_by_entity_key_plural = dict(foyers=pandas.DataFrame(
        dict([(name, reference_simulation.calculate_add(name)) for name in [
            'impo',
            'rfr',
        ]])), )

    return reform_data_frame_by_entity_key_plural, reference_simulation
def df_survey_simulation(reductions):
    year = 2009
    TaxBenefitSystem = openfisca_france_data.init_country()
    tax_benefit_system = TaxBenefitSystem()
    input_data_frame = get_input_data_frame(year)

    survey_scenario_reference = SurveyScenario().init_from_data_frame(
        input_data_frame = input_data_frame,
        used_as_input_variables = ['sal', 'cho', 'rst', 'age_en_mois', 'smic55'],
        year = year,
        tax_benefit_system = tax_benefit_system
        )

    simulation = survey_scenario_reference.new_simulation()

#    from openfisca_core import periods
#    period = periods.period('year', 2007)
#    period = period.start.offset('first-of', 'month').period('year')
#    bareme = simulation.legislation_at(period.start).ir.bareme

    data_frame_by_entity_key_plural = dict(
        foyers = pandas.DataFrame(
            dict([(name, simulation.calculate_add(name)) for name in [
                'rfr',
                'irpp',
                'rbg',
                'iaidrdi',
                'rng',
                'ip_net',
                'reductions',
                'decile_rfr',
                'weight_foyers',
                ] + reductions
                ])
            ),
        )
    return data_frame_by_entity_key_plural
示例#13
0
def varying_survey_simulation(year=2009,
                              increment=10,
                              target='irpp',
                              varying='rni',
                              used_as_input_variables=None):
    TaxBenefitSystem = openfisca_france_data.init_country()
    tax_benefit_system = TaxBenefitSystem()

    input_data_frame = get_input_data_frame(year)
    #    Simulation 1 : get varying and target
    survey_scenario = SurveyScenario().init_from_data_frame(
        input_data_frame=input_data_frame,
        used_as_input_variables=used_as_input_variables,
        year=year,
        tax_benefit_system=tax_benefit_system)
    simulation = survey_scenario.new_simulation(debug=False)

    output_data_frame = pandas.DataFrame(
        dict([(name, simulation.calculate_add(name))
              for name in [target, varying, 'idfoy_original']]))

    #    Make input_data_frame_by_entity_key_plural from the previous input_data_frame and simulation
    input_data_frames_by_entity_key_plural = \
        from_input_df_to_entity_key_plural_df(input_data_frame, tax_benefit_system, simulation)
    foyers = input_data_frames_by_entity_key_plural['idfoy']
    foyers = pandas.merge(foyers, output_data_frame, on='idfoy_original')

    #    Incrementation of varying:
    foyers[varying] = foyers[varying] + increment

    #    On remplace la nouvelle base dans le dictionnaire
    input_data_frames_by_entity_key_plural['idfoy'] = foyers

    #   2e simulation à partir de input_data_frame_by_entity_key_plural:
    #   TODO: fix used_as_input_variabels in the from_input_df_to_entity_key_plural_df() function
    used_as_input_variables = used_as_input_variables + [varying]
    TaxBenefitSystem = openfisca_france_data.init_country()
    tax_benefit_system = TaxBenefitSystem()

    survey_scenario = SurveyScenario().init_from_data_frame(
        input_data_frame=None,
        input_data_frames_by_entity_key_plural=
        input_data_frames_by_entity_key_plural,
        used_as_input_variables=used_as_input_variables,
        year=year,
        tax_benefit_system=tax_benefit_system,
    )
    simulation = survey_scenario.new_simulation(debug=False)

    output_data_frame2 = pandas.DataFrame(
        dict([(name, simulation.calculate_add(name))
              for name in [target, varying, 'idfoy_original']]))
    output_data_frame2.rename(columns={
        varying: '{}_2'.format(varying),
        target: '{}_2'.format(target)
    },
                              inplace=True)
    merged = pandas.merge(output_data_frame,
                          output_data_frame2,
                          on='idfoy_original')
    merged['marginal_rate'] = marginal_rate_survey(merged, '{}'.format(target),
                                                   '{}_2'.format(target),
                                                   'rni', 'rni_2')
    merged['average_rate'] = average_rate(target=merged[target],
                                          varying=merged[varying])
    return merged
示例#14
0

import pandas


from til_base_model.config import Config
from liam2.simulation import Simulation


path_model = os.path.join(
    pkg_resources.get_distribution('Til-BaseModel').location,
    'til_base_model',
    )

import openfisca_france_data
TaxBenefitSystem = openfisca_france_data.init_country()


def dump(simulation, file_path = None, overwrite = False):
    assert os.path.isabs(file_path)
    assert file_path.endswith('.h5')
    if not overwrite:
        assert not os.path.exists(file_path), "Cannot overwrite on already existing file {}".format(
            file_path)
    store = pandas.HDFStore(file_path)

    tax_benefit_system = simulation.tax_benefit_system
    for key_plural, entity_class in tax_benefit_system.entity_class_by_key_plural.iteritems():
        for variable in entity_class.column_by_name:
            try:
                simulation.calculate(variable)