In [5]:
# requis pour OpenFisca
from datetime import date
# OpenFisca
from openfisca_france import init_country
from openfisca_france.model.base import *
# pour faire des graphiques
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
In [6]:
# moteur de base (calculs de référence)
TaxBenefitSystem = init_country()
tax_benefit_system = TaxBenefitSystem()
In [7]:
# pour créer des réformes
from openfisca_core import reforms
In [8]:
# réforme du revenu de base
ReformeRevenuDeBase = reforms.make_reform(
key = 'reforme_rdb',
name = u"Réforme Revenu de base",
reference = tax_benefit_system,
)
In [9]:
from numpy import logical_not as not_, minimum as min_, maximum as max_
class nbptr(ReformeRevenuDeBase.Variable):
reference = FoyersFiscaux.column_by_name['nbptr']
# On enlève les enfants du calcul du nbptr (quotient_familial.enf*)
def function(self, simulation, period):
'''
Nombre de parts du foyer
'foy'
note 1 enfants et résidence alternée (formulaire 2041 GV page 10)
quotient_familial.conj : nb part associées au conjoint d'un couple marié ou pacsé
quotient_familial.enf1 : nb part 2 premiers enfants
quotient_familial.enf2 : nb part enfants de rang 3 ou plus
quotient_familial.inv1 : nb part supp enfants invalides (I, G)
quotient_familial.inv2 : nb part supp adultes invalides (R)
quotient_familial.not31 : nb part supp note 3 : cases W ou G pour veuf, celib ou div
quotient_familial.not32 : nb part supp note 3 : personne seule ayant élevé des enfants
quotient_familial.not41 : nb part supp adultes invalides (vous et/ou conjoint) note 4
quotient_familial.not42 : nb part supp adultes anciens combattants (vous et/ou conjoint) note 4
quotient_familial.not6 : nb part supp note 6
quotient_familial.isol : demi-part parent isolé (T)
quotient_familial.edcd : enfant issu du mariage avec conjoint décédé;
'''
period = period.start.offset('first-of', 'month').period('year')
nb_pac = simulation.calculate('nb_pac', period)
marpac = simulation.calculate('marpac', period)
celdiv = simulation.calculate('celdiv', period)
veuf = simulation.calculate('veuf', period)
jveuf = simulation.calculate('jveuf', period)
nbF = simulation.calculate('nbF', period)
nbG = simulation.calculate('nbG', period)
nbH = simulation.calculate('nbH', period)
nbI = simulation.calculate('nbI', period)
nbR = simulation.calculate('nbR', period)
nbJ = simulation.calculate('nbJ', period)
caseP = simulation.calculate('caseP', period)
caseW = simulation.calculate('caseW', period)
caseG = simulation.calculate('caseG', period)
caseE = simulation.calculate('caseE', period)
caseK = simulation.calculate('caseK', period)
caseN = simulation.calculate('caseN', period)
caseF = simulation.calculate('caseF', period)
caseS = simulation.calculate('caseS', period)
caseL = simulation.calculate('caseL', period)
caseT = simulation.calculate('caseT', period)
quotient_familial = simulation.legislation_at(period.start).ir.quotient_familial
no_pac = nb_pac == 0 # Aucune personne à charge en garde exclusive
has_pac = not_(no_pac)
no_alt = nbH == 0 # Aucun enfant à charge en garde alternée
has_alt = not_(no_alt)
# # nombre de parts liées aux enfants à charge
# que des enfants en résidence alternée
enf1 = (no_pac & has_alt) * (quotient_familial.enf1 * min_(nbH, 2) * 0.5
+ quotient_familial.enf2 * max_(nbH - 2, 0) * 0.5)
# pas que des enfants en résidence alternée
enf2 = (has_pac & has_alt) * ((nb_pac == 1) * (quotient_familial.enf1 * min_(nbH, 1) * 0.5
+ quotient_familial.enf2 * max_(nbH - 1, 0) * 0.5) + (nb_pac > 1) * (quotient_familial.enf2 * nbH * 0.5))
# pas d'enfant en résidence alternée
enf3 = quotient_familial.enf1 * min_(nb_pac, 2) + quotient_familial.enf2 * max_((nb_pac - 2), 0)
enf = enf1 + enf2 + enf3
# # note 2 : nombre de parts liées aux invalides (enfant + adulte)
n2 = quotient_familial.inv1 * (nbG + nbI / 2) + quotient_familial.inv2 * nbR
# # note 3 : Pas de personne à charge
# - invalide
n31a = quotient_familial.not31a * (no_pac & no_alt & caseP)
# - ancien combatant
n31b = quotient_familial.not31b * (no_pac & no_alt & (caseW | caseG))
n31 = max_(n31a, n31b)
# - personne seule ayant élevé des enfants
n32 = quotient_familial.not32 * (no_pac & no_alt & ((caseE | caseK) & not_(caseN)))
n3 = max_(n31, n32)
# # note 4 Invalidité de la personne ou du conjoint pour les mariés ou
# # jeunes veuf(ve)s
n4 = max_(quotient_familial.not41 * (1 * caseP + 1 * caseF), quotient_familial.not42 * (caseW | caseS))
# # note 5
# - enfant du conjoint décédé
n51 = quotient_familial.cdcd * (caseL & ((nbF + nbJ) > 0))
# - enfant autre et parent isolé
n52 = quotient_familial.isol * caseT * (((no_pac & has_alt) * ((nbH == 1) * 0.5 + (nbH >= 2))) + 1 * has_pac)
n5 = max_(n51, n52)
# # note 6 invalide avec personne à charge
n6 = quotient_familial.not6 * (caseP & (has_pac | has_alt))
# # note 7 Parent isolé
n7 = quotient_familial.isol * caseT * ((no_pac & has_alt) * ((nbH == 1) * 0.5 + (nbH >= 2)) + 1 * has_pac)
# # Régime des mariés ou pacsés
#m = 1 + quotient_familial.conj + enf + n2 + n4
m = 1 + quotient_familial.conj + n2 + n4
# # veufs hors jveuf
# v = 1 + enf + n2 + n3 + n5 + n6
v = 1 + n2 + n3 + n5 + n6
# # celib div
#c = 1 + enf + n2 + n3 + n6 + n7
c = 1 + n2 + n3 + n6 + n7
return period, (marpac | jveuf) * m + (veuf & not_(jveuf)) * v + celdiv * c
In [10]:
# Suppression des allocations familiales
class af(ReformeRevenuDeBase.Variable):
reference = Familles.column_by_name['af']
def function(self, simulation, period):
period = period.this_month
af_base = simulation.calculate('af_base', period)
af_majo = simulation.calculate('af_majo', period)
af_forf = simulation.calculate('af_forf', period)
#return period, af_base + af_majo + af_forf
return period, af_base * 0
In [11]:
from numpy import round
# Suppression du complément familial
class cf(ReformeRevenuDeBase.Variable):
reference = Familles.column_by_name['cf']
def function(self, simulation, period):
'''
L'allocation de base de la paje n'est pas cumulable avec le complément familial
'''
period = period.this_month
paje_base = simulation.calculate('paje_base', period)
apje_temp = simulation.calculate('apje_temp', period)
ape_temp = simulation.calculate('ape_temp', period)
cf_montant = simulation.calculate('cf_montant', period)
residence_mayotte = simulation.calculate('residence_mayotte', period)
cf_brut = not_(paje_base) * (apje_temp <= cf_montant) * (ape_temp <= cf_montant) * cf_montant
# return period, not_(residence_mayotte) * round(cf_brut, 2)
return period, not_(residence_mayotte) * round(cf_brut, 2) * 0
In [12]:
# Suppression de l'allocation de rentrée scolaire
class ars(ReformeRevenuDeBase.Variable):
reference = Familles.column_by_name['ars']
def function(self, simulation, period):
'''
Allocation de rentrée scolaire brute de CRDS
'''
period_br = period.this_year
return period_br, self.zeros()
In [13]:
# Suppression du nombre d'enfants dans le calcul du RSA socle
class rsa_socle(ReformeRevenuDeBase.Variable):
reference = Familles.column_by_name['rsa_socle']
def function(self, simulation, period):
period = period.start.offset('first-of', 'month').period('month')
age_holder = simulation.compute('age', period)
smic55_holder = simulation.compute('smic55', period)
activite_holder = simulation.compute('activite', period)
nb_par = simulation.calculate('nb_par', period)
rmi = simulation.legislation_at(period.start).minim.rmi
age_parents = self.split_by_roles(age_holder, roles = [CHEF, PART])
activite_parents = self.split_by_roles(activite_holder, roles = [CHEF, PART])
age_enf = self.split_by_roles(age_holder, roles = ENFS)
smic55_enf = self.split_by_roles(smic55_holder, roles = ENFS)
nbp = nb_par
eligib = (
(age_parents[CHEF] >= rmi.age_pac)
*
not_(activite_parents[CHEF] == 2)
) | (
(age_parents[PART] >= rmi.age_pac) * not_(activite_parents[PART] == 2)
)
taux = (
1 + (nbp >= 2) * rmi.txp2 +
(nbp >= 3) * rmi.txp3 +
(nbp >= 4) * ((nb_par == 1) * rmi.txps + (nb_par != 1) * rmi.txp3) +
max_(nbp - 4, 0) * rmi.txps
)
return period, eligib * rmi.rmi * taux
In [14]:
# Suppression du nombre d'enfants dans le calcul du RSA forfait logement
class rmi_nbp(ReformeRevenuDeBase.Variable):
reference = Familles.column_by_name['rmi_nbp']
def function(self, simulation, period):
period = period.start.offset('first-of', 'month').period('month')
age_holder = simulation.compute('age', period)
smic55_holder = simulation.compute('smic55', period)
nb_par = simulation.calculate('nb_par', period)
P = simulation.legislation_at(period.start).minim.rmi
age = self.split_by_roles(age_holder, roles = ENFS)
smic55 = self.split_by_roles(smic55_holder, roles = ENFS)
return period, nb_par # + nb_enf(age, smic55, 0, P.age_pac - 1)
In [15]:
# Suppression de la cotisation patronale famille
class famille(ReformeRevenuDeBase.Variable):
reference = Individus.column_by_name['famille']
def function(self, simulation, period):
# cotisation = apply_bareme(
# simulation,
# period,
# cotisation_type = 'employeur',
# bareme_name = 'famille',
# variable_name = self.__class__.__name__,
# )
#return period, cotisation
return period, self.zeros()
In [16]:
# Création d'un revenu de base enfant - Version famille
from openfisca_france.model.prestations.prestations_familiales.base_ressource import nb_enf
class rdb_enfant_famille(ReformeRevenuDeBase.Variable):
column = FloatCol
entity_class = Familles
label = u"Revenu de base enfant"
def function(self, simulation, period):
period = period.start.offset('first-of', 'month').period('month')
age_holder = simulation.compute('age', period)
P = simulation.legislation_at(period.start).fam.af
bmaf = P.bmaf
smic55_holder = simulation.compute('smic55', period)
smic55 = self.split_by_roles(smic55_holder, roles = ENFS)
age = self.split_by_roles(age_holder, roles = ENFS)
smic5 = {
role: array * 0
for role, array in smic55.iteritems()
}
nbenf_inf13 = nb_enf(age, smic5, 0, 13)
nbenf_sup14 = nb_enf(age, smic5, 14, 18)
return period, (nbenf_inf13 * 0.41 + nbenf_sup14 * 0.57) * bmaf
# Les taux 0,41 et 0,16 (0,57-0,41) sont issus des allocations familiales
In [17]:
# Création d'un revenu de base enfant - Version individus
class rdb_enf(ReformeRevenuDeBase.Variable):
column = FloatCol
entity_class = Individus
label = u"Revenu de base enfant"
def function(self, simulation, period):
period = period.start.offset('first-of', 'month').period('month')
age = simulation.calculate('age')
P = simulation.legislation_at(period.start).fam.af
bmaf = P.bmaf
return period, ((age < 14) * 0.41 + not_(age < 14) * 0.57) * bmaf * (age <= 18)
In [18]:
# Création d'une CSG enfant
class csgenf(ReformeRevenuDeBase.Variable):
column = FloatCol
entity_class = Individus
label = u"CSG enfant"
def function(self, simulation, period):
# period = period.start.offset('first-of', 'month').period('month')
revnet = simulation.calculate('revenu_net_individu', period)
montant_csg = revnet * 0.025
return period, - montant_csg
class csg(ReformeRevenuDeBase.Variable):
reference = Individus.column_by_name['csg']
def function(self, simulation, period):
'''
Contribution sociale généralisée
'''
period = period.this_year
csg_imposable_salaire = simulation.calculate_add('csg_imposable_salaire', period)
csg_deductible_salaire = simulation.calculate_add('csg_deductible_salaire', period)
csg_imposable_chomage = simulation.calculate_add('csg_imposable_chomage', period)
csg_deductible_chomage = simulation.calculate_add('csg_deductible_chomage', period)
csg_imposable_retraite = simulation.calculate_add('csg_imposable_retraite', period)
csg_deductible_retraite = simulation.calculate_add('csg_deductible_retraite', period)
csg_fon_holder = simulation.compute('csg_fon', period)
csg_cap_lib_declarant1 = simulation.calculate('csg_cap_lib_declarant1', period)
csg_cap_bar_declarant1 = simulation.calculate('csg_cap_bar_declarant1', period)
csg_pv_mo_holder = simulation.compute('csg_pv_mo', period)
csg_pv_immo_holder = simulation.compute('csg_pv_immo', period)
# add csgenf to csg calc
csgenfant = simulation.calculate('csgenf', period)
csg_fon = self.cast_from_entity_to_role(csg_fon_holder, role = VOUS)
csg_pv_immo = self.cast_from_entity_to_role(csg_pv_immo_holder, role = VOUS)
csg_pv_mo = self.cast_from_entity_to_role(csg_pv_mo_holder, role = VOUS)
return period, (csg_imposable_salaire + csg_deductible_salaire + csg_imposable_chomage +
csg_deductible_chomage + csg_imposable_retraite + csg_deductible_retraite + csg_fon +
csg_cap_lib_declarant1 + csg_pv_mo + csg_pv_immo + csg_cap_bar_declarant1 + csgenfant)
In [19]:
# mise à jour du revenu disponible en tenant compte du revenu de base enfant
class revdisp(ReformeRevenuDeBase.Variable):
reference = Menages.column_by_name['revdisp']
def function(self, simulation, period):
'''
Revenu disponible - ménage
'men'
'''
period = period.start.period('year').offset('first-of')
rev_trav_holder = simulation.compute('rev_trav', period)
pen_holder = simulation.compute('pen', period)
rev_cap_holder = simulation.compute('rev_cap', period)
psoc_holder = simulation.compute('psoc', period)
ppe_holder = simulation.compute('ppe', period)
impo = simulation.calculate('impo', period)
rdb_enfant_holder = simulation.calculate_add('rdb_enf', period)
pen = self.sum_by_entity(pen_holder)
ppe = self.cast_from_entity_to_role(ppe_holder, role = VOUS)
ppe = self.sum_by_entity(ppe)
psoc = self.cast_from_entity_to_role(psoc_holder, role = CHEF)
psoc = self.sum_by_entity(psoc)
rev_cap = self.sum_by_entity(rev_cap_holder)
rev_trav = self.sum_by_entity(rev_trav_holder)
rdb_enfant = self.sum_by_entity(rdb_enfant_holder)
return period, rev_trav + pen + rev_cap + psoc + ppe + impo + rdb_enfant
In [20]:
# on crée un moteur de calcul pour la réforme du revenu de base
reform = ReformeRevenuDeBase()
In [21]:
# pour la partie graph
year = 2014
axes_variable = 'salaire_de_base'
ax_variable_max = 150000
count = 5000
In [22]:
scenario_ref_couple_avec_3_enfants = tax_benefit_system.new_scenario().init_single_entity(
period = year,
parent1 = dict(
birth = date(1980, 1, 1),
salaire_imposable = 12000,
statmarit = u'Marié',
),
parent2 = dict(
birth = date(1980, 1, 1),
salaire_imposable = 46000,
statmarit = u'Marié',
),
enfants = [
dict(
birth = date(2010, 1, 1),
),
dict(
birth = date(2005, 1, 1),
),
dict(
birth = date(1999, 1, 1),
),
],
axes = [[
dict(
count = count,
min = 0,
max = ax_variable_max,
name = axes_variable,
period = year-2,
),
dict(
count = count,
min = 0,
max = ax_variable_max,
name = axes_variable,
period = year-1,
),
dict(
count = count,
min = 0,
max = ax_variable_max,
name = axes_variable,
period = year,
),
]],
)
simulation_ref_couple_avec_3_enfants = scenario_ref_couple_avec_3_enfants.new_simulation(debug = True)
In [23]:
scenario_ref_couple_pauvre_avec_3_enfants = tax_benefit_system.new_scenario().init_single_entity(
period = year,
parent1 = dict(
birth = date(1980, 1, 1),
salaire_imposable = 12000,
statmarit = u'Marié',
),
parent2 = dict(
birth = date(1980, 1, 1),
salaire_imposable = 6000,
statmarit = u'Marié',
),
enfants = [
dict(
birth = date(2010, 1, 1),
),
dict(
birth = date(2005, 1, 1),
),
dict(
birth = date(1999, 1, 1),
),
],
axes = [[
dict(
count = count,
min = 0,
max = ax_variable_max,
name = axes_variable,
period = year-2,
),
dict(
count = count,
min = 0,
max = ax_variable_max,
name = axes_variable,
period = year-1,
),
dict(
count = count,
min = 0,
max = ax_variable_max,
name = axes_variable,
period = year,
),
]],
)
simulation_ref_couple_pauvre_avec_3_enfants = scenario_ref_couple_pauvre_avec_3_enfants.new_simulation(debug = True)
In [24]:
scenario_rdb_couple_avec_3_enfants = reform.new_scenario().init_single_entity(
period = year,
parent1 = dict(
birth = date(1980, 1, 1),
salaire_imposable = 12000,
statmarit = u'Marié',
),
parent2 = dict(
birth = date(1980, 1, 1),
salaire_imposable = 46000,
statmarit = u'Marié',
),
enfants = [
dict(
birth = date(2010, 1, 1),
),
dict(
birth = date(2005, 1, 1),
),
dict(
birth = date(1999, 1, 1),
),
],
axes = [[
dict(
count = count,
min = 0,
max = ax_variable_max,
name = axes_variable,
period = year-2,
),
dict(
count = count,
min = 0,
max = ax_variable_max,
name = axes_variable,
period = year-1,
),
dict(
count = count,
min = 0,
max = ax_variable_max,
name = axes_variable,
period = year,
),
]],
)
simulation_rdb_couple_avec_3_enfants = scenario_rdb_couple_avec_3_enfants.new_simulation(debug = True)
In [25]:
scenario_rdb_couple_pauvre_pauvre_avec_3_enfants = reform.new_scenario().init_single_entity(
period = year,
parent1 = dict(
birth = date(1980, 1, 1),
salaire_imposable = 12000,
statmarit = u'Marié',
),
parent2 = dict(
birth = date(1980, 1, 1),
salaire_imposable = 6000,
statmarit = u'Marié',
),
enfants = [
dict(
birth = date(2010, 1, 1),
),
dict(
birth = date(2005, 1, 1),
),
dict(
birth = date(1999, 1, 1),
),
],
axes = [[
dict(
count = count,
min = 0,
max = ax_variable_max,
name = axes_variable,
period = year-2,
),
dict(
count = count,
min = 0,
max = ax_variable_max,
name = axes_variable,
period = year-1,
),
dict(
count = count,
min = 0,
max = ax_variable_max,
name = axes_variable,
period = year,
),
]],
)
simulation_rdb_couple_pauvre_avec_3_enfants = scenario_rdb_couple_pauvre_pauvre_avec_3_enfants.new_simulation(debug = True)
In [ ]:
simulation_ref_couple_avec_3_enfants.calculate('nbptr')
Out[ ]:
In [ ]:
# test graph
#revdisp_ref = simulation_ref_couple_avec_3_enfants.calculate_add("revdisp")
revdisp_rdb = simulation_rdb_couple_avec_3_enfants.calculate_add("revdisp")
In [ ]:
#salaire_net_ref = simulation_ref_couple_avec_3_enfants.calculate_add("salaire_net")
salaire_net_rdb = simulation_rdb_couple_avec_3_enfants.calculate_add("salaire_net")
plt.plot(salaire_net_rdb[::2], revdisp_rdb)
In [ ]:
simulation_ref_couple_avec_3_enfants.calculate_add('af')
In [ ]:
simulation_rdb_couple_avec_3_enfants.calculate_add('af')
In [ ]:
simulation_ref_couple_avec_3_enfants.calculate_add('cf')
In [ ]:
simulation_rdb_couple_avec_3_enfants.calculate_add('cf')
In [ ]:
simulation_ref_couple_avec_3_enfants.calculate('ars')
In [ ]:
simulation_rdb_couple_avec_3_enfants.calculate_add('ars')
In [ ]:
simulation_ref_couple_avec_3_enfants.calculate_add('rsa')
In [ ]:
from openfisca_core import web_tools
In [ ]:
print web_tools.get_trace_tool_link(scenario, ['rsa'])
In [ ]:
simulation_rdb_couple_avec_3_enfants.calculate_add('rsa')
In [ ]:
simulation_ref_couple_avec_3_enfants.calculate_add('rmi_nbp')
In [ ]:
simulation_rdb_couple_avec_3_enfants.calculate_add('rmi_nbp')
In [ ]:
simulation_ref_couple_avec_3_enfants.calculate('famille')
In [ ]:
simulation_rdb_couple_avec_3_enfants.calculate('famille')
In [ ]:
simulation_rdb_couple_avec_3_enfants.calculate_add('rdb_enfant_famille')
In [ ]:
simulation_rdb_couple_avec_3_enfants.calculate_add('rdb_enf')
In [ ]:
simulation_rdb_couple_avec_3_enfants.calculate('csgenf')
In [ ]:
simulation_ref_couple_avec_3_enfants.calculate('csg')
In [ ]:
simulation_rdb_couple_avec_3_enfants.calculate('csg')
In [ ]:
simulation_ref_couple_avec_3_enfants.calculate('revdisp')
In [ ]:
simulation_rdb_couple_avec_3_enfants.calculate('revdisp')
In [ ]:
simulation_pauvre.calculate('revdisp')
In [ ]:
reform_simulation_pauvre.calculate('revdisp')
In [ ]:
simulation_ref_couple_avec_3_enfants.calculate('salaire_imposable')
In [ ]:
simulation_rdb_couple_avec_3_enfants.calculate('salaire_imposable')
In [ ]:
simulation_ref_couple_avec_3_enfants.calculate('salaire_super_brut')
In [ ]:
simulation_rdb_couple_avec_3_enfants.calculate('salaire_super_brut')
In [ ]:
simulation_ref_couple_avec_3_enfants.calculate('salaire_net')
In [ ]:
simulation_rdb_couple_avec_3_enfants.calculate('salaire_net')
In [ ]:
simulation_ref_couple_avec_3_enfants.calculate('pfam')
In [ ]:
simulation_rdb_couple_avec_3_enfants.calculate('pfam')
In [ ]:
simulation_pauvre.calculate('psoc')
In [ ]:
simulation_ref_couple_avec_3_enfants.calculate('cotisations_salariales')
In [ ]:
reform_simulation_pauvre.calculate('pfam')
In [ ]:
simulation_ref_couple_avec_3_enfants.calculate('irpp')
In [ ]:
simulation_rdb_couple_avec_3_enfants.calculate('irpp')
In [ ]:
simulation_ref_couple_avec_3_enfants.calculate('allegement_fillon')
In [ ]:
print(salaire_net_rdb[::2])
print(revdisp_rdb)
In [ ]: