Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
benjello committed Nov 20, 2024
1 parent 02b9d9a commit 674fae0
Show file tree
Hide file tree
Showing 10 changed files with 210 additions and 315 deletions.
19 changes: 13 additions & 6 deletions openfisca_tunisia_pension/regimes/cnrps.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
)

from openfisca_tunisia_pension.variables.helpers import pension_generique
from openfisca_tunisia_pension.tools import make_mean_over_largest
from openfisca_tunisia_pension.tools import make_mean_over_consecutive_largest


# Avant 1985
Expand Down Expand Up @@ -61,7 +61,13 @@ class RegimeCNRPS(AbstractRegimeEnAnnuites):
variable_prefix = 'cnrps'
parameters_prefix = 'cnrps'

class salaire_reference(Variable):
class salaire_de_reference_calcule_sur_demande(Variable):
value_type = bool
entity = Individu
label = "Le salaire de référence du régime de la CNRPS est calculé à la demande de l'agent sur ses meilleures années"
definition_period = ETERNITY

class salaire_de_reference(Variable):
value_type = float
entity = Individu
label = 'Salaires de référence du régime de la CNRPS'
Expand All @@ -72,7 +78,7 @@ def formula(individu, period):
'''3 dernières rémunérations ou les 2 plus élevées sur demande.'''
n = 40
k = 2
mean_over_largest = make_mean_over_largest(k)
mean_over_largest = make_mean_over_consecutive_largest(k)
moyenne_2_salaires_plus_eleves = apply_along_axis(
mean_over_largest,
axis = 0,
Expand All @@ -84,9 +90,10 @@ def formula(individu, period):
for year in range(period.start.year, period.start.year - p, -1)
) / p

salaire_refererence = max_(
salaire_refererence = where(
individu('regime_name_salaire_de_reference_calcule_sur_demande', period),
moyenne_2_salaires_plus_eleves,
moyenne_3_derniers_salaires,
moyenne_2_salaires_plus_eleves
)
return salaire_refererence

Expand All @@ -98,7 +105,7 @@ class pension(Variable):

def formula(individu, period, parameters):
duree_assurance = individu('regime_name_duree_assurance', period = period)
salaire_reference = individu('regime_name_salaire_reference', period = period)
salaire_reference = individu('regime_name_salaire_de_reference', period = period)
age = individu('age', period = period)

cnrps = parameters(period).retraite.regime_name
Expand Down
107 changes: 34 additions & 73 deletions openfisca_tunisia_pension/regimes/regime.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


from openfisca_core.model_api import *
# from openfisca_core.errors.variable_not_found_error import VariableNotFoundError
from openfisca_core.errors.variable_not_found_error import VariableNotFoundError

# Import the Entities specifically defined for this tax and benefit system
from openfisca_tunisia_pension.entities import Individu
Expand Down Expand Up @@ -148,79 +148,55 @@ class pension(Variable):

def formula(individu, period):
pension_brute = individu('regime_name_pension_brute', period)
majoration_pension = individu('regime_name_majoration_pension', period)
return pension_brute + majoration_pension

class pension_au_31_decembre(Variable):
value_type = float
entity = Individu
definition_period = YEAR
label = 'Pension'

def formula(individu, period):
pension_brute_au_31_decembre = individu('regime_name_pension_brute_au_31_decembre', period)
majoration_pension_au_31_decembre = individu('regime_name_majoration_pension_au_31_decembre', period)
return pension_brute_au_31_decembre + majoration_pension_au_31_decembre

class pension_brute(Variable):
value_type = float
entity = Individu
definition_period = YEAR
label = 'Pension brute'

def formula(individu, period, parameters):
taux_de_liquidation = individu('regime_name_taux_de_liquidation', period)
salaire_de_reference = individu('regime_name_salaire_de_reference', period)
pension_minimale = individu('regime_name_pension_minimale', period)
pension_maximale = individu('regime_name_pension_maximale', period)
try:
pension_minimale = individu('regime_name_pension_minimale', period)
except VariableNotFoundError:
pension_minimale = 0
try:
pension_maximale = individu('regime_name_pension_maximale', period)
except VariableNotFoundError:
return max_(
pension_brute,
pension_minimale
)
return min_(
pension_maximale,
max_(
taux_de_liquidation * salaire_de_reference,
pension_brute,
pension_minimale
)
)

class pension_brute_au_31_decembre(Variable):
class pension_brute(Variable):
value_type = float
entity = Individu
definition_period = YEAR
label = 'Pension brute au 31 décembre'
label = 'Pension brute'

def formula(individu, period, parameters):
annee_de_liquidation = individu('regime_name_liquidation_date', period).astype('datetime64[Y]').astype(int) + 1970
# Raccouci pour arrêter les calculs dans le passé quand toutes les liquidations ont lieu dans le futur
if all(period.start.year < annee_de_liquidation):
return individu.empty_array()
last_year = period.last_year
pension_brute_au_31_decembre_annee_precedente = individu('regime_name_pension_brute_au_31_decembre', last_year)
revalorisation = parameters(period).regime_name.revalorisation_pension_au_31_decembre
pension_brute = individu('regime_name_pension_brute', period)
return revalorise(
pension_brute_au_31_decembre_annee_precedente,
pension_brute,
annee_de_liquidation,
revalorisation,
period,
)
taux_de_liquidation = individu('regime_name_taux_de_liquidation', period)
salaire_de_reference = individu('regime_name_salaire_de_reference', period)
return taux_de_liquidation * salaire_de_reference,

class pension_maximale(Variable):
value_type = float
entity = Individu
definition_period = YEAR
label = 'Pension maximale'
# class pension_maximale(Variable):
# value_type = float
# default_value = np.inf # Pas de pension maximale par défaut
# entity = Individu
# definition_period = YEAR
# label = 'Pension maximale'

def formula(individu, period, parameters):
NotImplementedError
# def formula(individu, period, parameters):
# NotImplementedError

class pension_minimale(Variable):
value_type = float
entity = Individu
definition_period = YEAR
label = 'Pension minimale'
# class pension_minimale(Variable):
# value_type = float
# default_value = 0 # Pas de pension minimale par défaut, elle est à zéro
# entity = Individu
# definition_period = YEAR
# label = 'Pension minimale'

def formula(individu, period, parameters):
NotImplementedError
# def formula(individu, period, parameters):
# NotImplementedError

class pension_servie(Variable):
value_type = float
Expand Down Expand Up @@ -269,18 +245,3 @@ def formula(individu, period, parameters):
duree_assurance = individu('regime_name_duree_assurance', period)
taux_annuite = bareme_annuite.calc(duree_assurance)
return taux_annuite


# def revalorise(variable_31_decembre_annee_precedente, variable_originale, annee_de_liquidation, revalorisation, period):
# return select(
# [
# annee_de_liquidation > period.start.year,
# annee_de_liquidation == period.start.year,
# annee_de_liquidation < period.start.year,
# ],
# [
# 0,
# variable_originale,
# variable_31_decembre_annee_precedente * revalorisation
# ]
# )
28 changes: 28 additions & 0 deletions openfisca_tunisia_pension/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ def mean_over_largest(vector):
return mean_over_largest


def make_mean_over_consecutive_largest(k):
def mean_over_consecutive_largest(vector):
return mean_over_k_consecutive_largest(vector, k = int(k))

return mean_over_consecutive_largest


@jit(float32(float32[:], int64), nopython=True)
def mean_over_k_nonzero_largest(vector, k):
'''Return the mean over the k largest values of a vector.'''
Expand All @@ -24,3 +31,24 @@ def mean_over_k_nonzero_largest(vector, k):
z = -np.partition(-vector, kth = k)
upper_bound = min(k, nonzeros)
return z[:upper_bound].sum() / upper_bound


@jit(float32(float32[:], int64), nopython=True)
def mean_over_k_consecutive_largest(vector, k):
'''Return the mean over the k largest consecutive values of a vector.'''
if k == 0:
return 0
nonzeros = (vector > 0.0).sum()
if k >= nonzeros:
return vector.sum() / (nonzeros + (nonzeros == 0))

if k == 1:
return vector.max()

n = len(vector)
mean = np.zeros(n + 1 - k, dtype = np.float32)
for p in range(n + 1 - k):
for i in range(k):
mean[p] += vector[p + i]
mean[p] = mean[p] / k
return mean.max()
70 changes: 12 additions & 58 deletions openfisca_tunisia_pension/variables/cnrps.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
"""Abstract regimes definition."""
from openfisca_core.model_api import *
from openfisca_core.errors.variable_not_found_error import VariableNotFoundError
from openfisca_tunisia_pension.entities import Individu
'Régime de la Caisse nationale de retraite et de prévoyance sociale (CNRPS).'
from openfisca_core.model_api import *
from openfisca_tunisia_pension.entities import Individu
from openfisca_tunisia_pension.regimes.regime import AbstractRegimeEnAnnuites
from numpy import apply_along_axis, logical_not as not_, maximum as max_, vstack
from openfisca_tunisia_pension.variables.helpers import pension_generique
from openfisca_tunisia_pension.tools import make_mean_over_largest
from openfisca_tunisia_pension.tools import make_mean_over_consecutive_largest

class cnrps_cotisation(Variable):
value_type = float
Expand Down Expand Up @@ -82,7 +83,7 @@ class cnrps_pension(Variable):

def formula(individu, period, parameters):
duree_assurance = individu('cnrps_duree_assurance', period=period)
salaire_reference = individu('cnrps_salaire_reference', period=period)
salaire_reference = individu('cnrps_salaire_de_reference', period=period)
age = individu('age', period=period)
cnrps = parameters(period).retraite.cnrps
taux_annuite_base = cnrps.taux_annuite_base
Expand All @@ -102,17 +103,6 @@ def formula(individu, period, parameters):
montant_pension_percu = max_(montant, pension_minimale * smig)
return eligibilite * montant_pension_percu

class cnrps_pension_au_31_decembre(Variable):
value_type = float
entity = Individu
definition_period = YEAR
label = 'Pension'

def formula(individu, period):
pension_brute_au_31_decembre = individu('cnrps_pension_brute_au_31_decembre', period)
majoration_pension_au_31_decembre = individu('cnrps_majoration_pension_au_31_decembre', period)
return pension_brute_au_31_decembre + majoration_pension_au_31_decembre

class cnrps_pension_brute(Variable):
value_type = float
entity = Individu
Expand All @@ -122,43 +112,7 @@ class cnrps_pension_brute(Variable):
def formula(individu, period, parameters):
taux_de_liquidation = individu('cnrps_taux_de_liquidation', period)
salaire_de_reference = individu('cnrps_salaire_de_reference', period)
pension_minimale = individu('cnrps_pension_minimale', period)
pension_maximale = individu('cnrps_pension_maximale', period)
return min_(pension_maximale, max_(taux_de_liquidation * salaire_de_reference, pension_minimale))

class cnrps_pension_brute_au_31_decembre(Variable):
value_type = float
entity = Individu
definition_period = YEAR
label = 'Pension brute au 31 décembre'

def formula(individu, period, parameters):
annee_de_liquidation = individu('cnrps_liquidation_date', period).astype('datetime64[Y]').astype(int) + 1970
if all(period.start.year < annee_de_liquidation):
return individu.empty_array()
last_year = period.last_year
pension_brute_au_31_decembre_annee_precedente = individu('cnrps_pension_brute_au_31_decembre', last_year)
revalorisation = parameters(period).cnrps.revalorisation_pension_au_31_decembre
pension_brute = individu('cnrps_pension_brute', period)
return revalorise(pension_brute_au_31_decembre_annee_precedente, pension_brute, annee_de_liquidation, revalorisation, period)

class cnrps_pension_maximale(Variable):
value_type = float
entity = Individu
definition_period = YEAR
label = 'Pension maximale'

def formula(individu, period, parameters):
NotImplementedError

class cnrps_pension_minimale(Variable):
value_type = float
entity = Individu
definition_period = YEAR
label = 'Pension minimale'

def formula(individu, period, parameters):
NotImplementedError
return (taux_de_liquidation * salaire_de_reference,)

class cnrps_pension_servie(Variable):
value_type = float
Expand All @@ -184,12 +138,6 @@ class cnrps_salaire_de_base(Variable):
set_input = set_input_divide_by_period

class cnrps_salaire_de_reference(Variable):
value_type = float
entity = Individu
definition_period = ETERNITY
label = 'Salaire de référence'

class cnrps_salaire_reference(Variable):
value_type = float
entity = Individu
label = 'Salaires de référence du régime de la CNRPS'
Expand All @@ -199,13 +147,19 @@ def formula(individu, period):
"""3 dernières rémunérations ou les 2 plus élevées sur demande."""
n = 40
k = 2
mean_over_largest = make_mean_over_largest(k)
mean_over_largest = make_mean_over_consecutive_largest(k)
moyenne_2_salaires_plus_eleves = apply_along_axis(mean_over_largest, axis=0, arr=vstack([individu('cnrps_salaire_de_base', period=year) for year in range(period.start.year, period.start.year - n, -1)]))
p = 3
moyenne_3_derniers_salaires = sum((individu('cnrps_salaire_de_base', period=year) for year in range(period.start.year, period.start.year - p, -1))) / p
salaire_refererence = max_(moyenne_3_derniers_salaires, moyenne_2_salaires_plus_eleves)
salaire_refererence = where(individu('cnrps_salaire_de_reference_calcule_sur_demande', period), moyenne_2_salaires_plus_eleves, moyenne_3_derniers_salaires)
return salaire_refererence

class cnrps_salaire_de_reference_calcule_sur_demande(Variable):
value_type = bool
entity = Individu
label = "Le salaire de référence du régime de la CNRPS est calculé à la demande de l'agent sur ses meilleures années"
definition_period = ETERNITY

class cnrps_taux_de_liquidation(Variable):
value_type = float
entity = Individu
Expand Down
Loading

0 comments on commit 674fae0

Please sign in to comment.