Skip to content

Commit

Permalink
Add doc to marginal_rate
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauko Quiroga committed Aug 30, 2021
1 parent 94d2a5f commit a69ee47
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions openfisca_core/commons/rates.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,24 @@ def average_rate(target: numpy.ndarray, varying: ArrayLike[float], trim: Optiona
return average_rate


def marginal_rate(target = None, varying = None, trim = None):
def marginal_rate(target: numpy.ndarray, varying: numpy.ndarray, trim: Optional[ArrayLike[float]] = None) -> numpy.ndarray:
"""Computes the marginal rate of a target net income.
Given a ``target`` net income, and according to the ``varying`` gross
income. Optionally, a ``trim`` can be applied consisting of the lower and
upper bounds of the marginal rate to be computed.
Args:
target: The targeted net income.
varying: The varying gross income.
trim: The lower and upper bounds of the marginal rate.
Returns:
The marginal rate for each target.
When ``trim`` is provided, values that are out of the provided bounds
are replaced by :obj:`numpy.nan`.
Examples:
>>> target = numpy.array([1, 2, 3])
>>> varying = numpy.array([1, 2, 4])
Expand All @@ -52,7 +67,6 @@ def marginal_rate(target = None, varying = None, trim = None):
"""

# target: numerator, varying: denominator
marginal_rate = 1 - (target[:-1] - target[1:]) / (varying[:-1] - varying[1:])
if trim is not None:
marginal_rate = numpy.where(marginal_rate <= max(trim), marginal_rate, numpy.nan)
Expand Down

0 comments on commit a69ee47

Please sign in to comment.