Populations

class openfisca_core.populations.Population(entity)[source]
__call__(variable_name, period, options=None)

Calculate variable_name for period, using the formula if it exists.

Parameters:
  • variable_name (NewType(VariableName, str)) – The name of the variable to calculate.

  • period (Union[Period, PeriodStr, NewType(PeriodInt, int)]) – The period to calculate the variable for.

  • options (None | Sequence[Option]) – The options to use for the calculation.

Returns:
  • None – If there is no Simulation.

  • ndarray[generic] – The result of the calculation.

Raises:
  • IncompatibleOptionsError – If the options are incompatible.

  • InvalidOptionError – If the option is invalid.

Return type:

None | ndarray[Any, dtype[generic]]

Examples

>>> from openfisca_core import (
...     entities,
...     periods,
...     populations,
...     simulations,
...     taxbenefitsystems,
...     variables,
... )
>>> class Person(entities.SingleEntity): ...
>>> person = Person("person", "people", "", "")
>>> period = periods.Period.eternity()
>>> population = populations.CorePopulation(person)
>>> population.count = 3
>>> population("salary", period)
>>> tbs = taxbenefitsystems.TaxBenefitSystem([person])
>>> person.set_tax_benefit_system(tbs)
>>> simulation = simulations.Simulation(tbs, {person.key: population})
>>> population("salary", period)
Traceback (most recent call last):
VariableNotFoundError: You tried to calculate or to set a value ...
>>> class Salary(variables.Variable):
...     definition_period = periods.ETERNITY
...     entity = person
...     value_type = int
>>> tbs.add_variable(Salary)
<openfisca_core.populations._core_population.Salary object at...
>>> population(Salary().name, period)
array([0, 0, 0], dtype=int32)
>>> class Tax(Salary):
...     default_value = 100.0
...     definition_period = periods.ETERNITY
...     entity = person
...     value_type = float
>>> tbs.add_variable(Tax)
<openfisca_core.populations._core_population.Tax object at...
>>> population(Tax().name, period)
array([100., 100., 100.], dtype=float32)
>>> population(Tax().name, period, [populations.ADD])
Traceback (most recent call last):
ValueError: Unable to ADD constant variable 'Tax' over the perio...
>>> population(Tax().name, period, [populations.DIVIDE])
Traceback (most recent call last):
ValueError: Unable to DIVIDE constant variable 'Tax' over the pe...
>>> population(Tax().name, period, [populations.ADD, populations.DIVIDE])
Traceback (most recent call last):
IncompatibleOptionsError: Options ADD and DIVIDE are incompatibl...
>>> population(Tax().name, period, ["LAGRANGIAN"])
Traceback (most recent call last):
InvalidOptionError: Option LAGRANGIAN is not a valid option (try...
get_rank(entity, criteria, condition=True)[source]

Get the rank of a person within an entity according to a criteria. The person with rank 0 has the minimum value of criteria. If condition is specified, then the persons who don’t respect it are not taken into account and their rank is -1.

Example: >>> age = person(“age”, period) # e.g [32, 34, 2, 8, 1] >>> person.get_rank(household, age) >>> [3, 4, 0, 2, 1]

>>> is_child = person.has_role(
...     Household.CHILD
... )  # [False, False, True, True, True]
>>> person.get_rank(
...     household, -age, condition=is_child
:rtype: :sphinx_autodoc_typehints_type:`\:py\:class\:\`\~numpy.ndarray\`\\ \\\[\:py\:data\:\`\~typing.Any\`\, \:py\:class\:\`\~numpy.dtype\`\\ \\\[\:py\:class\:\`\~numpy.int32\`\]\]`

… ) # Sort in reverse order so that the eldest child gets the rank 0. >>> [-1, -1, 1, 0, 2]

has_role(role)[source]

Check if a person has a given role within its GroupEntity.

Example: >>> person.has_role(Household.CHILD) >>> array([False])

Return type:

None | ndarray[Any, dtype[bool_]]

class openfisca_core.populations.GroupPopulation(entity, members)[source]
all(array, role=None)[source]

Return True if array is True for all members of the entity.

array must have the dimension of the number of persons in the simulation

If role is provided, only the entity member with the given role are taken into account.

Example: >>> salaries = household.members( … “salary”, “2018-01” … ) # e.g. [2000, 1500, 0, 0, 0] >>> household.all(salaries >= 1800) >>> array([False])

any(array, role=None)[source]

Return True if array is True for any members of the entity.

array must have the dimension of the number of persons in the simulation

If role is provided, only the entity member with the given role are taken into account.

Example: >>> salaries = household.members( … “salary”, “2018-01” … ) # e.g. [2000, 1500, 0, 0, 0] >>> household.any(salaries >= 1800) >>> array([True])

max(array, role=None)[source]

Return the maximum value of array for the entity members.

array must have the dimension of the number of persons in the simulation

If role is provided, only the entity member with the given role are taken into account.

Example: >>> salaries = household.members( … “salary”, “2018-01” … ) # e.g. [2000, 1500, 0, 0, 0] >>> household.max(salaries) >>> array([2000])

min(array, role=None)[source]

Return the minimum value of array for the entity members.

array must have the dimension of the number of persons in the simulation

If role is provided, only the entity member with the given role are taken into account.

Example: >>> salaries = household.members( … “salary”, “2018-01” … ) # e.g. [2000, 1500, 0, 0, 0] >>> household.min(salaries) >>> array([0]) >>> household.min( … salaries, role=Household.PARENT … ) # Assuming the 1st two persons are parents >>> array([1500])

nb_persons(role=None)[source]

Returns the number of persons contained in the entity.

If role is provided, only the entity member with the given role are taken into account.

sum(array, role=None)[source]

Return the sum of array for the members of the entity.

array must have the dimension of the number of persons in the simulation

If role is provided, only the entity member with the given role are taken into account.

Example: >>> salaries = household.members( … “salary”, “2018-01” … ) # e.g. [2000, 1500, 0, 0, 0] >>> household.sum(salaries) >>> array([3500])