def df_with_tax_rate(df): target = df['salaire_imposable'].values + df['irpp'].values varying = df['salaire_imposable'].values df2 = pandas.DataFrame({ 'marginal_rate': marginal_rate(target, varying), 'average_rate': average_rate(target, varying)[1:], }) return pandas.concat([df[1:], df2], axis=1)
def df_with_tax_rate(df): target = df['salaire_imposable'].values + df['irpp'].values varying = df['salaire_imposable'].values df2=pandas.DataFrame({ 'marginal_rate': marginal_rate(target, varying), 'average_rate': average_rate(target, varying)[1:], }) return pandas.concat([df[1:], df2], axis = 1)
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
def make_result(year, count, sali, menage): single_entity_kwargs = menage(year, sali) label = single_entity_kwargs.pop("label") single_entity_kwargs.update( dict( axes = [ dict( count = count, max = 1000, min = 0, name = 'loyer', ), ], period = periods.period('year', year), ) ) # print filename # print single_entity_kwargs scenario = base.tax_benefit_system.new_scenario().init_single_entity(**single_entity_kwargs) simulation = scenario.new_simulation(debug = True) T = average_rate( target = simulation.calculate('al'), varying = simulation.calculate('loyer'), ) t = marginal_rate( target = simulation.calculate('al'), varying = simulation.calculate('loyer'), ) df1 = pd.DataFrame({ "t": t, }) df2 = pd.DataFrame({ "T": T, "loyer": simulation.calculate('loyer'), "al": simulation.calculate('al') / 12, }) N = count - 1 df = pd.concat([df2[0:N], df1], axis = 1) df["sal"] = sali df["typmen"] = menage.__name__ df["typmensal"] = "{} et {} euros de salaire imposable".format(label, sali) # df[u"Salaire imposable"] = sali # df[u"Type de ménage"] = menage.__name__ return df
def test_average_tax_rate(): year = 2013 simulation = base.tax_benefit_system.new_scenario().init_single_entity( axes=[ dict( count=100, name='salaire_imposable', max=24000, min=0, ), ], period=year, parent1=dict(age_en_mois=40 * 12 + 6), ).new_simulation() assert (average_rate( target=simulation.calculate('revenu_disponible'), varying=simulation.calculate('revenu_disponible'), ) == 0).all()
def test_average_tax_rate(): year = 2013 simulation = base.tax_benefit_system.new_scenario().init_single_entity( axes = [ dict( count = 100, name = 'salaire_imposable', max = 24000, min = 0, ), ], period = year, parent1 = dict(age_en_mois = 40 * 12 + 6), ).new_simulation() # Remove debug = True, because logging is too slow. assert (average_rate( target = simulation.calculate('revdisp'), varying = simulation.calculate('revdisp'), ) == 0).all()
def test_average_tax_rate(): year = 2013 simulation = tax_benefit_system.new_scenario().init_single_entity( axes = [ dict( count = 100, name = 'salaire_imposable', max = 24000, min = 0, ), ], period = year, parent1 = dict(age = 40), ).new_simulation() assert (average_rate( target = simulation.calculate('revenu_disponible', period = year), varying = simulation.calculate('revenu_disponible', period = year), ) == 0).all()
def test_average_tax_rate(): year = 2013 simulation = base.tax_benefit_system.new_scenario().init_single_entity( axes=[ dict( count=100, name='sal', max=24000, min=0, ), ], period=year, parent1=dict(agem=40 * 12 + 6), ).new_simulation() # Remove debug = True, because logging is too slow. assert (average_rate( target=simulation.calculate('revdisp'), varying=simulation.calculate('revdisp'), ) == 0).all()
def test_average_tax_rate(): year = 2013 simulation = base.tax_benefit_system.new_scenario().init_single_entity( axes = [ dict( count = 100, name = 'sali', max = 24000, min = 0, ), ], period = year, parent1 = dict(agem = 40 * 12 + 6), ).new_simulation(debug = True) assert (average_rate( target = simulation.calculate('revdisp'), varying = simulation.calculate('revdisp'), ) == 0).all()
def draw_rates(simulation, axes, x_axis = None, y_axis = None, reference_simulation = None, legend = True): assert x_axis is not None assert y_axis is not None from openfisca_core.rates import average_rate, marginal_rate varying = simulation.calculate(x_axis) target = simulation.calculate(y_axis) avg_rate = average_rate(target, varying) marg_rate = marginal_rate(target, varying) print avg_rate axes.hold(True) import numpy as np axes.set_xlim(np.amin(varying), np.amax(varying)) axes.set_ylabel(r"$\left(1 - \frac{RevDisponible}{RevInitial} \right)\ et\ \left(1 - \frac{d (RevDisponible)}{d (RevInitial)}\right)$") axes.set_ylabel(r"$\left(1 - \frac{RevDisponible}{RevInitial} \right)\ et\ \left(1 - \frac{d (RevDisponible)}{d (RevInitial)}\right)$") axes.plot(varying, 100*avg_rate, label = u"Taux moyen d'imposition", linewidth = 2) axes.plot(varying[1:], 100*marg_rate, label = u"Taux marginal d'imposition", linewidth = 2) axes.set_ylim(0, 100) from matplotlib.ticker import FuncFormatter axes.yaxis.set_major_formatter(FuncFormatter(percentFormatter)) if legend: createLegend(axes)
def draw_rates(simulation, axes = None, x_axis = None, y_axis = None, reference_simulation = None, legend = True, period = None): if axes is None: fig = plt.figure() axes = fig.gca() assert x_axis is not None assert y_axis is not None varying = simulation.calculate(x_axis, period = period) target = simulation.calculate(y_axis, period = period) avg_rate = average_rate(target, varying) marg_rate = marginal_rate(target, varying) axes.hold(True) axes.set_xlim(np.amin(varying), np.amax(varying)) axes.set_ylabel(r"$\left(1 - \frac{RevDisponible}{RevInitial} \right)\ et\ \left(1 - \frac{d (RevDisponible)}{d (RevInitial)}\right)$") axes.set_ylabel(r"$\left(1 - \frac{RevDisponible}{RevInitial} \right)\ et\ \left(1 - \frac{d (RevDisponible)}{d (RevInitial)}\right)$") axes.plot(varying, 100 * avg_rate, label = u"Taux moyen d'imposition", linewidth = 2) axes.plot(varying[1:], 100 * marg_rate, label = u"Taux marginal d'imposition", linewidth = 2) axes.set_ylim(0, 100) axes.yaxis.set_major_formatter(FuncFormatter(percent_formatter)) if legend: create_legend(axes)
for year in [2008, 2014]: simulation = base.tax_benefit_system.new_scenario().init_single_entity( axes = [ dict( count = count, max = max, min = min, name = name, ), ], parent1 = dict( birth = datetime.date(year - 40, 1, 1), ), menage = dict(statut_occupation = 4), period = periods.period('year', year) ).new_simulation(debug = True, reference = True) df = pandas.DataFrame(dict( salaire_imposable = simulation.calculate('salaire_imposable'), irpp = simulation.calculate('irpp'))) target = df['salaire_imposable'].values + df['irpp'].values varying = df['salaire_imposable'].values df2 = pandas.DataFrame({ 'marginal_rate': marginal_rate(target, varying), 'average_rate': average_rate(target, varying)[1:], }) result[year] = df2.marginal_rate
def test_average_rate(): '''Compute the average tax rate when the gross income is never zero''' target = numpy.array([1, 2, 3]) result = average_rate(target, varying = 2) expected = numpy.array([.5, 0, -.5]) numpy.testing.assert_equal(result, expected)
def test_average_rate(): '''Compute the average tax rate when the gross income is never zero''' target = numpy.array([1, 2, 3]) result = average_rate(target, varying=2) expected = numpy.array([.5, 0, -.5]) numpy.testing.assert_equal(result, expected)
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
plt.xlabel(u"Gross Wage") plt.show() #average rate average_rate = income_tax / gross_wage average_rate = np.nan_to_num(average_rate) print(average_rate) plt.plot(gross_wage, average_rate) plt.ylabel("Averate Tax Rate") plt.xlabel("Gross Wage") plt.show() #marginal tax rate marginal_rate = (income_tax[:-1] - income_tax[1:]) / (taxable_income[:-1] - taxable_income[1:]) plt.plot(gross_wage[:-1], marginal_rate) plt.ylabel("Marginal Tax Rate") plt.xlabel("Gross Wage") plt.show() from openfisca_core.rates import average_rate, marginal_rate #CSG csg = simulation.calculate_add('csg', period=2014) 1 - average_rate(-csg[1:], gross_wage[1:]) plt.ylim(0, 0.1) plt.plot(gross_wage[1:], 1 - average_rate(-csg[1:], gross_wage[1:])) plt.show()
def test_average_rate_when_varying_is_zero(): '''Compute the average tax rate when the varying gross income cross zero (yields infinity)''' target = numpy.array([1, 2, 3]) result = average_rate(target, varying = 0) assert numpy.isinf(result[0]).all()
def test_average_rate_when_varying_is_zero(): '''Compute the average tax rate when the varying gross income cross zero (yields infinity)''' target = numpy.array([1, 2, 3]) result = average_rate(target, varying=0) assert numpy.isinf(result[0]).all()