Commons#

The policyengine_core.commons module contains a number of classes and functions that are used throughout the rest of the library.

formulas#

apply_thresholds#

policyengine_core.commons.formulas.apply_thresholds(input: NDArray[None, Float], thresholds: Union[NDArray[None, Object], Sequence[float]], choices: Union[NDArray[None, Object], Sequence[float]]) NDArray[None, Float][source]#

Makes a choice based on an input and thresholds.

From a list of choices, this function selects one of these values based on a list of inputs, depending on the value of each input within a list of thresholds.

Parameters:
  • input – A list of inputs to make a choice from.

  • thresholds – A list of thresholds to choose.

  • choices – A list of the possible values to choose from.

Returns:

A list of the values chosen.

Return type:

numpy.ndarray of float

Raises:

AssertionError – When the number of thresholds (t) and the number of choices (c) are not either t == c or t == c - 1.

Examples

>>> input = numpy.array([4, 5, 6, 7, 8])
>>> thresholds = [5, 7]
>>> choices = [10, 15, 20]
>>> apply_thresholds(input, thresholds, choices)
array([10, 10, 15, 15, 20])

concat#

policyengine_core.commons.formulas.concat(this: Union[NDArray[None, Object], Sequence[str]], that: Union[NDArray[None, Object], Sequence[str]]) NDArray[None, Unicode][source]#

Concatenates the values of two arrays.

Parameters:
  • this – An array to concatenate.

  • that – Another array to concatenate.

Returns:

An array with the concatenated values.

Return type:

numpy.ndarray of float

Examples

>>> this = ["this", "that"]
>>> that = numpy.array([1, 2.5])
>>> concat(this, that)
array(['this1.0', 'that2.5']...)

switch#

policyengine_core.commons.formulas.switch(conditions: NDArray[Any, None], value_by_condition: Dict[float, T]) NDArray[None, Object][source]#

Mimicks a switch statement.

Given an array of conditions, returns an array of the same size, replacing each condition item with the matching given value.

Parameters:
  • conditions – An array of conditions.

  • value_by_condition – Values to replace for each condition.

Returns:

An array with the replaced values.

Return type:

numpy.ndarray

Raises:

AssertionError – When value_by_condition is empty.

Examples

>>> conditions = numpy.array([1, 1, 1, 2])
>>> value_by_condition = {1: 80, 2: 90}
>>> switch(conditions, value_by_condition)
array([80, 80, 80, 90])