示例#1
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
示例#2
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
示例#3
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
示例#4
0
def test_calibration():
    year = 2006
    input_data_frame = get_input_data_frame(year)
    survey_scenario = SurveyScenario().init_from_data_frame(
        input_data_frame = input_data_frame,
        year = year,
        )
    survey_scenario.initialize_weights()
    calibration = Calibration()
    calibration.set_survey_scenario(survey_scenario)
    calibration.parameters['method'] = 'linear'
    print calibration.initial_total_population
    calibration.total_population = calibration.initial_total_population * 1.123
    print calibration.total_population

    filename = os.path.join(
        openfisca_france_data_location,
        "openfisca_france_data",
        "calibrations",
        "calib_2006.csv"
        )
    calibration.set_inputs_margins_from_file(filename, 2006)
    calibration.set_parameters('invlo', 3)
    calibration.set_parameters('up', 3)
    calibration.set_parameters('method', 'logit')
    calibration.calibrate()
def test_calibration():
    year = 2009
    input_data_frame = get_input_data_frame(year)
    survey_scenario = SurveyScenario().init_from_data_frame(
        input_data_frame = input_data_frame,
        year = year,
        )
    survey_scenario.initialize_weights()
    calibration = Calibration(survey_scenario)
    calibration.parameters['method'] = 'linear'
    print calibration.initial_total_population
    calibration.total_population = calibration.initial_total_population * 1.123
    print calibration.total_population

    filename = os.path.join(
        openfisca_france_data_location,
        "openfisca_france_data",
        "calibrations",
        "calib_2006.csv"
        )
    calibration.set_inputs_margins_from_file(filename, 2006)
    calibration.set_parameters('invlo', 3)
    calibration.set_parameters('up', 3)
    calibration.set_parameters('method', 'logit')
    calibration.calibrate()
示例#6
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
示例#7
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
示例#8
0
def test_pivot_table_1d_mean(year=2009):
    input_data_frame = get_input_data_frame(year)
    survey_scenario = SurveyScenario().init_from_data_frame(
        input_data_frame=input_data_frame,
        year=year,
    )
    pivot_table = survey_scenario.compute_pivot_table(columns=['decile_rfr'],
                                                      values=['irpp'])
    return pivot_table
def test_weights_building():
    year = 2009
    input_data_frame = get_input_data_frame(year)
    survey_scenario = SurveyScenario().init_from_data_frame(
        input_data_frame = input_data_frame,
        year = year,
        )
    survey_scenario.new_simulation()
    return survey_scenario.simulation
def test_weights_building():
    year = 2009
    input_data_frame = get_input_data_frame(year)
    survey_scenario = SurveyScenario().init_from_data_frame(
        input_data_frame=input_data_frame,
        year=year,
    )
    survey_scenario.new_simulation()
    return survey_scenario.simulation
示例#11
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
示例#12
0
def test_weights_building():
    year = 2009
    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'],
        year = year,
        )
    survey_scenario.new_simulation()
    return survey_scenario.simulation
def create_survey_scenario(year=None):
    assert year is not None
    input_data_frame = get_input_data_frame(year)
    survey_scenario = SurveyScenario().init_from_data_frame(
        input_data_frame=input_data_frame,
        year=year,
    )
    survey_scenario.initialize_weights()

    return survey_scenario
示例#14
0
def test_weights_building():
    year = 2009
    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'],
        year=year,
    )
    survey_scenario.new_simulation()
    return survey_scenario.simulation
def test_pivot_table_1d_mean(year = 2009):
    input_data_frame = get_input_data_frame(year)
    survey_scenario = SurveyScenario().init_from_data_frame(
        input_data_frame = input_data_frame,
        year = year,
        )
    pivot_table = survey_scenario.compute_pivot_table(
        columns = ['decile_rfr'],
        values = ['irpp']
        )
    return pivot_table
def test_inflation():
    year = 2009
    input_data_frame = get_input_data_frame(year)
    survey_scenario = SurveyScenario().init_from_data_frame(
        input_data_frame = input_data_frame,
        year = year,
        )
    survey_scenario.new_simulation()
    target_by_variable = dict(
        salaire_imposable = 1.2e08
        )
    survey_scenario.inflate(target_by_variable = target_by_variable)
def test_fake_survey_simulation():
    year = 2006
    input_data_frame = get_fake_input_data_frame(year)

    assert input_data_frame.sal.loc[0] == 20000
    assert input_data_frame.sal.loc[1] == 10000

    survey_scenario = SurveyScenario().init_from_data_frame(
        input_data_frame=input_data_frame,
        used_as_input_variables=['sal', 'cho', 'rst', 'age_en_mois', 'age'],
        year=year,
    )
    assert (survey_scenario.input_data_frame.sal.loc[0] == 20000).all()
    assert (survey_scenario.input_data_frame.sal.loc[1] == 10000).all()

    simulation = survey_scenario.new_simulation()

    sal = simulation.calculate('sal')
    assert (sal[0:1] == 20000).all()
    age = simulation.calculate('age')
    assert age[0] == 77
    assert age[1] == 37
    age_en_mois = simulation.calculate('age_en_mois')
    assert age_en_mois[0] == 924
    assert age_en_mois[1] == 444

    data_frame_by_entity_key_plural = dict(
        individus=pandas.DataFrame(
            dict([
                (name, simulation.calculate(name)) for name in [
                    'idmen',
                    'quimen',
                    'idfoy',
                    'quifoy',
                    'idfam',
                    'quifam',
                    'age',
                    'champm_individus',
                    'sal',
                    'salaire_net',
                    'txtppb',
                    # salsuperbrut # TODO bug in 2006
                ]
            ])),
        foyers_fiscaux=pandas.DataFrame(
            dict([(name, simulation.calculate(name)) for name in ['impo']])),
        menages=pandas.DataFrame(
            dict([(name, simulation.calculate(name))
                  for name in ['revdisp']])),
    )

    return data_frame_by_entity_key_plural
def create_fake_calibration():
    year = 2006
    input_data_frame = get_fake_input_data_frame(year)
    survey_scenario = SurveyScenario().init_from_data_frame(
        input_data_frame = input_data_frame,
        year = year,
        )
    survey_scenario.new_simulation()
    calibration = Calibration(survey_scenario = survey_scenario)
    calibration.set_parameters('invlo', 3)
    calibration.set_parameters('up', 3)
    calibration.set_parameters('method', 'logit')
    return calibration
def create_fake_calibration():
    year = 2006
    input_data_frame = get_fake_input_data_frame(year)
    survey_scenario = SurveyScenario().init_from_data_frame(
        input_data_frame = input_data_frame,
        year = year,
        )
    survey_scenario.new_simulation()
    calibration = Calibration(survey_scenario = survey_scenario)
    calibration.set_parameters('invlo', 3)
    calibration.set_parameters('up', 3)
    calibration.set_parameters('method', 'logit')
    return calibration
示例#20
0
def survey_simulate(used_as_input_variables, year, ind_variables = None, fam_variables = None, foy_variables = None,
                    men_variables = None):
    year = year
    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 = used_as_input_variables,
        year = year,
        )
    simulation = survey_scenario.new_simulation()

    data_frame_by_entity_key_plural = from_simulation_to_data_frame_by_entity_key_plural(
        simulation, ind_variables, fam_variables, foy_variables, men_variables)

    return data_frame_by_entity_key_plural, simulation
def test_survey_simulation():
    year = 2009
    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,
        )
    simulation = survey_scenario.new_simulation()

    data_frame_by_entity_key_plural = dict(
        individus = pandas.DataFrame(
            dict([(name, simulation.calculate(name)) for name in [
                'idmen',
                'quimen',
                'idfoy',
                'quifoy',
                'idfam',
                'quifam',
                'age',
                'champm_individus',
                'sal',
                'salaire_net',
                # 'smic55',
                'txtppb',
                # salsuperbrut # TODO bug in 2006
                ]])
            ),
        familles = pandas.DataFrame(
            dict([(name, simulation.calculate_add(name)) for name in [
                'af_nbenf',
                'af',
                'weight_familles',
                ]])
            ),

        menages = pandas.DataFrame(
            dict([(name, simulation.calculate(name)) for name in [
                'revdisp',
                ]])
            ),
        )

    (data_frame_familles.weight_familles * data_frame_familles.af).sum() / 1e9 > 10

    return data_frame_by_entity_key_plural, simulation
def test_survey_simulation():
    year = 2009
    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,
    )
    simulation = survey_scenario.new_simulation()

    data_frame_by_entity_key_plural = dict(
        individus=pandas.DataFrame(
            dict([
                (name, simulation.calculate(name)) for name in [
                    'idmen',
                    'quimen',
                    'idfoy',
                    'quifoy',
                    'idfam',
                    'quifam',
                    'age',
                    'champm_individus',
                    'sal',
                    'salaire_net',
                    # 'smic55',
                    'txtppb',
                    # salsuperbrut # TODO bug in 2006
                ]
            ])),
        familles=pandas.DataFrame(
            dict([(name, simulation.calculate_add(name)) for name in [
                'af_nbenf',
                'af',
                'weight_familles',
            ]])),
        menages=pandas.DataFrame(
            dict([(name, simulation.calculate(name)) for name in [
                'revdisp',
            ]])),
    )

    (data_frame_familles.weight_familles *
     data_frame_familles.af).sum() / 1e9 > 10

    return data_frame_by_entity_key_plural, simulation
示例#23
0
def create_survey_scenario(year = None):
    assert year is not None
    input_data_frame = get_input_data_frame(year)
    assert "wprm" in input_data_frame.columns
    survey_scenario = SurveyScenario().init_from_data_frame(
        input_data_frame = input_data_frame,
        tax_benefit_system = base.france_data_tax_benefit_system,
        year = year,
        )
    return survey_scenario
def test_survey_simulation():
    year = 2009
    input_data_frame = get_input_data_frame(year)
    survey_scenario = SurveyScenario().init_from_data_frame(
        input_data_frame = input_data_frame,
        year = year,
        )
    simulation = survey_scenario.new_simulation(trace = True)
    data_frame_by_entity_key_plural = survey_scenario.create_data_frame_by_entity_key_plural(
        variables = [
            'aspa',
            'aide_logement_montant_brut',
            'idmen',
            'quimen',
            'idfoy',
            'quifoy',
            'idfam',
            'quifam',
            'age',
            'activite',
            'br_rmi_i',
            'champm_individus',
            'pensions_alimentaires_percues',
            'salaire_imposable',
            'salaire_net',
            'smic55',
            'txtppb',
            'af_nbenf',
            'af',
            'br_rmi',
            'rsa',
            'rstnet',
            'weight_familles',
            'revdisp',
            ]
        )

    assert (
        data_frame_by_entity_key_plural['familles'].weight_familles * data_frame_by_entity_key_plural['familles'].af
        ).sum() / 1e9 > 10

    return data_frame_by_entity_key_plural, simulation
示例#25
0
def create_survey_scenario(data_year=2009, year=2013, reform=None):
    assert year is not None
    assert data_year is not None
    input_data_frame = get_input_data_frame(data_year)
    survey_scenario = SurveyScenario().init_from_data_frame(
        input_data_frame=input_data_frame,
        tax_benefit_system=reform,
        reference_tax_benefit_system=base.france_data_tax_benefit_system,
        year=year,
    )

    return survey_scenario
def test_survey_simulation():
    year = 2009
    input_data_frame = get_input_data_frame(year)
    survey_scenario = SurveyScenario().init_from_data_frame(
        input_data_frame=input_data_frame,
        year=year,
    )
    simulation = survey_scenario.new_simulation(trace=True)
    data_frame_by_entity_key_plural = survey_scenario.create_data_frame_by_entity_key_plural(
        variables=[
            'aspa',
            'aide_logement_montant_brut',
            'idmen',
            'quimen',
            'idfoy',
            'quifoy',
            'idfam',
            'quifam',
            'age',
            'activite',
            'br_rmi_i',
            'champm_individus',
            'pensions_alimentaires_percues',
            'salaire_imposable',
            'salaire_net',
            'smic55',
            'txtppb',
            'af_nbenf',
            'af',
            'br_rmi',
            'rsa',
            'rstnet',
            'weight_familles',
            'revdisp',
        ])

    assert (data_frame_by_entity_key_plural['familles'].weight_familles *
            data_frame_by_entity_key_plural['familles'].af).sum() / 1e9 > 10

    return data_frame_by_entity_key_plural, simulation
示例#27
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 test_reform():
    year = 2006
    input_data_frame = get_fake_input_data_frame(year)

    input_data_frame.salaire_imposable = [20000, 18000]

    reform = plf2015.build_reform(base.france_data_tax_benefit_system)

    survey_scenario = SurveyScenario().init_from_data_frame(
        input_data_frame = input_data_frame,
        tax_benefit_system = reform,
        reference_tax_benefit_system = base.france_data_tax_benefit_system,
        year = 2013,
        )
    reference_simulation = survey_scenario.new_simulation(reference = True)
    reform_simulation = survey_scenario.new_simulation()

    assert 'weight_individus' in reform_simulation.tax_benefit_system.column_by_name
    assert 'weight_individus' in reference_simulation.tax_benefit_system.column_by_name

    error_margin = 1
    assert_near(reference_simulation.calculate('irpp'), [-10124, -869], error_margin)
    assert_near(reform_simulation.calculate('irpp'), [-10118, -911.4 + (1135 - 911.4)], error_margin)
def test_reform():
    year = 2006
    input_data_frame = get_fake_input_data_frame(year)

    input_data_frame.salaire_imposable = [20000, 18000]

    reform = plf2015.build_reform(base.france_data_tax_benefit_system)

    survey_scenario = SurveyScenario().init_from_data_frame(
        input_data_frame = input_data_frame,
        tax_benefit_system = reform,
        reference_tax_benefit_system = base.france_data_tax_benefit_system,
        year = 2013,
        )
    reference_simulation = survey_scenario.new_simulation(reference = True)
    reform_simulation = survey_scenario.new_simulation()

    assert 'weight_individus' in reform_simulation.tax_benefit_system.column_by_name
    assert 'weight_individus' in reference_simulation.tax_benefit_system.column_by_name

    error_margin = 1
    assert_near(reference_simulation.calculate('irpp'), [-10124, -869], error_margin)
    assert_near(reform_simulation.calculate('irpp'), [-10118, -911.4 + (1135 - 911.4)], error_margin)
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
def test_inflation():
    year = 2009
    input_data_frame = get_input_data_frame(year)
    survey_scenario = SurveyScenario().init_from_data_frame(
        input_data_frame=input_data_frame,
        year=year,
    )
    survey_scenario.new_simulation()
    target_by_variable = dict(salaire_imposable=1.2e08)
    survey_scenario.inflate(target_by_variable=target_by_variable)
def test_fake_survey_simulation():
    year = 2006
    input_data_frame = get_fake_input_data_frame(year)

    assert input_data_frame.salaire_imposable.loc[0] == 20000
    assert input_data_frame.salaire_imposable.loc[1] == 10000

    survey_scenario = SurveyScenario().init_from_data_frame(
        input_data_frame = input_data_frame,
        year = year,
        )
    assert (survey_scenario.input_data_frame.salaire_imposable.loc[0] == 20000).all()
    assert (survey_scenario.input_data_frame.salaire_imposable.loc[1] == 10000).all()

    simulation = survey_scenario.new_simulation()

    salaire_imposable = simulation.calculate('salaire_imposable')
    assert (salaire_imposable[0:1] == 20000).all()
    assert (salaire_imposable[1:2] == 10000).all()
    age = simulation.calculate('age')
    assert age[0] == 77
    assert age[1] == 37
    age_en_mois = simulation.calculate('age_en_mois')
    assert age_en_mois[0] == 924
    assert age_en_mois[1] == 444
    sal_2003 = simulation.calculate_add('salaire_imposable', period = "2003")
    sal_2004 = simulation.calculate_add('salaire_imposable', period = "2004")
    sal_2005 = simulation.calculate_add('salaire_imposable', period = "2005")
    sal_2006 = simulation.calculate_add('salaire_imposable', period = "2006")

    assert (sal_2003 == 0).all()
    assert (sal_2004 == sal_2006).all()
    assert (sal_2005 == sal_2006).all()
    import itertools

    for year, month in itertools.product(range(2002, 2004), range(1, 13)):
        assert (simulation.calculate_add('salaire_imposable', period = "{}-{}".format(year, month)) == 0).all()

    for year, month in itertools.product(range(2004, 2007), range(1, 13)):
        print "{}-{}".format(year, month)
        print simulation.calculate_add_divide('salaire_imposable', period = "{}-{}".format(year, month))
        print sal_2006 / 12
        assert (simulation.calculate('salaire_imposable', period = "{}-{}".format(year, month)) == sal_2006 / 12).all()

    data_frame_by_entity_key_plural = survey_scenario.create_data_frame_by_entity_key_plural(
        variables = [
            'idmen',
            'quimen',
            'idfoy',
            'quifoy',
            'idfam',
            'quifam',
            'age',
            'activite',
            'br_rmi_i',
            'champm_individus',
            'salaire_imposable',
            'salaire_net',
            'smic55',
            'txtppb',
            'af_nbenf',
            'af',
            'br_rmi',
            'rsa',
            'aspa',
            'aide_logement_montant_brut',
            'weight_familles',
            'revdisp',
            'impo',
            ]
        )
    return data_frame_by_entity_key_plural, simulation
def test_fake_survey_simulation():
    year = 2006
    input_data_frame = get_fake_input_data_frame(year)

    assert input_data_frame.salaire_imposable.loc[0] == 20000
    assert input_data_frame.salaire_imposable.loc[1] == 10000

    survey_scenario = SurveyScenario().init_from_data_frame(
        input_data_frame = input_data_frame,
        year = year,
        )
    assert (survey_scenario.input_data_frame.salaire_imposable.loc[0] == 20000).all()
    assert (survey_scenario.input_data_frame.salaire_imposable.loc[1] == 10000).all()

    simulation = survey_scenario.new_simulation()

    salaire_imposable = simulation.calculate('salaire_imposable')
    assert (salaire_imposable[0:1] == 20000).all()
    assert (salaire_imposable[1:2] == 10000).all()
    age = simulation.calculate('age')
    assert age[0] == 77
    assert age[1] == 37
    age_en_mois = simulation.calculate('age_en_mois')
    assert age_en_mois[0] == 924
    assert age_en_mois[1] == 444
    sal_2003 = simulation.calculate_add('salaire_imposable', period = "2003")
    sal_2004 = simulation.calculate_add('salaire_imposable', period = "2004")
    sal_2005 = simulation.calculate_add('salaire_imposable', period = "2005")
    sal_2006 = simulation.calculate_add('salaire_imposable', period = "2006")

    assert (sal_2003 == 0).all()
    assert (sal_2004 == sal_2006).all()
    assert (sal_2005 == sal_2006).all()
    import itertools

    for year, month in itertools.product(range(2002, 2004), range(1, 13)):
        assert (simulation.calculate_add('salaire_imposable', period = "{}-{}".format(year, month)) == 0).all()

    for year, month in itertools.product(range(2004, 2007), range(1, 13)):
        print "{}-{}".format(year, month)
        print simulation.calculate_add_divide('salaire_imposable', period = "{}-{}".format(year, month))
        print sal_2006 / 12
        assert (simulation.calculate('salaire_imposable', period = "{}-{}".format(year, month)) == sal_2006 / 12).all()

    data_frame_by_entity_key_plural = survey_scenario.create_data_frame_by_entity_key_plural(
        variables = [
            'idmen',
            'quimen',
            'idfoy',
            'quifoy',
            'idfam',
            'quifam',
            'age',
            'activite',
            'br_rmi_i',
            'champm_individus',
            'salaire_imposable',
            'salaire_net',
            'smic55',
            'txtppb',
            'af_nbenf',
            'af',
            'br_rmi',
            'rsa',
            'aspa',
            'aide_logement_montant_brut',
            'weight_familles',
            'revdisp',
            'impo',
            ]
        )
    return data_frame_by_entity_key_plural, simulation
示例#34
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