sid.shared

Module Contents

Functions

load_epidemiological_parameters()

Load epidemiological_parameters.

factorize_assortative_variables(states, assort_by)

Factorize assortative variables.

random_choice(choices, probabilities=None, decimals=5)

Return elements of choices for a two-dimensional array of probabilities.

boolean_choice(truth_probability)

Sample boolean value with probability given for True.

boolean_choices(truth_probabilities)

Sample boolean value with probabilities given for True.

separate_contact_model_names(contact_models)

fast_condition_evaluator(df, condition)

load_epidemiological_parameters()[source]

Load epidemiological_parameters.

factorize_assortative_variables(states, assort_by)[source]

Factorize assortative variables.

This function forms unique values by combining the different values of assortative variables. If there are no assortative variables, a single group is assigned to all states.

The unique values of the assort_by variables should be sorted to maintain the relationship, especially if already factorized variables are passed.

The group codes are converted to a lower dtype to save memory.

Parameters
  • states (pandas.DataFrame) – The user-defined initial states.

  • assort_by (List[str]) – List of variable names. Contacts are assortative by these variables.

Returns

Tuple containing

  • group_codes (numpy.ndarray): Array containing the code for each states.

  • group_codes_values (numpy.ndarray): One-dimensional array where positions correspond the values of assortative variables to form the group.

Return type

(tuple)

random_choice(choices, probabilities=None, decimals=5)[source]

Return elements of choices for a two-dimensional array of probabilities.

It is assumed that probabilities are ordered (n_samples, n_choices).

The function is taken from this StackOverflow post as a workaround for numpy.random.choice() as it can only handle one-dimensional probabilities.

Examples

Here is an example with non-zero probabilities.

>>> n_samples = 1_000_000
>>> n_choices = 3
>>> p = np.array([0.15, 0.35, 0.5])
>>> ps = np.tile(p, (n_samples, 1))
>>> choices = random_choice(n_choices, ps)
>>> np.round(np.bincount(choices) / n_samples, decimals=2)
array([0.15, 0.35, 0.5 ])

Here is an example where one choice has probability zero.

>>> choices = np.arange(3)
>>> p = np.array([0.4, 0, 0.6])
>>> ps = np.tile(p, (n_samples, 1))
>>> choices = random_choice(3, ps)
>>> np.round(np.bincount(choices) / n_samples, decimals=2)
array([0.4, 0. , 0.6])
boolean_choice(truth_probability)[source]

Sample boolean value with probability given for True.

Parameters

truth_probability (float) – Must be between 0 and 1.

Returns

Boolean array.

Return type

bool

Example

>>> boolean_choice(1)
True
>>> boolean_choice(0)
False
boolean_choices(truth_probabilities)[source]

Sample boolean value with probabilities given for True.

Parameters

truth_probabilities (float) – Must be between 0 and 1.

Returns

Boolean array.

Return type

bool

Example

>>> boolean_choice(np.array([1, 0]))
array([ True, False])
separate_contact_model_names(contact_models)[source]
fast_condition_evaluator(df, condition)[source]