Populations

class openfisca_core.populations.CorePopulation(entity, *_CorePopulation__args, **_CorePopulation__kwds)[source]

Base class to build populations from.

Parameters:
  • entity (CoreEntity) – The CoreEntity of the population.

  • *__args – Variable length argument list.

  • **__kwds – Arbitrary keyword arguments.

__call__(variable_name, period, options=None)[source]

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...
openfisca_core.populations.SinglePopulation

alias of Population

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])