sid.testing_demand
¶
Contains the code for calculating the demand for tests.
Module Contents¶
Functions¶
|
Calculate the demand for tests. |
|
Calculate the demand probabilities for each test demand model. |
|
Sample which individuals demand a test. |
|
Sample reason for demanding a test. |
|
Normalize probabilities such that they sum to one. |
- calculate_demand_for_tests(states: pandas.DataFrame, testing_demand_models: Dict[str, Dict[str, Any]], params: pandas.DataFrame, date: pandas.Timestamp, columns_to_keep: List[str], seed: itertools.count) Tuple[pandas.Series, pandas.Series] [source]¶
Calculate the demand for tests.
The following is a three-staged process:
Compute the probability for each demand model that an individual demands a test.
Sample which individuals demand a test.
For those demanding a test, sample why they want a test.
Sampling whether an individual requests any test at all and, then, sampling the reason for the test is computationally beneficial in contract to sampling with the probability of each demand model the demand and the reason. The first approach always involves two steps whereas the complexity of the latter increases with the number of demand models.
- Parameters
states (pandas.DataFrame) – The states of all individuals.
testing_demand_models (dict) – A dictionary containing the demand models for testing.
params (pandas.DataFrame) – The parameter DataFrame.
date (pandas.Timestamp) – Current date.
columns_to_keep (List[str]) – Columns which should be kept.
seed (itertools.count) – The seed counter.
- Returns
Tuple containing.
demands_test (pandas.Series): A boolean series indicating which person demands a test.
channel_demands_test (pandas.Series): A series indicating the demand model which made the individual ask for a test.
- Return type
(tuple)
- _calculate_demand_probabilities(states: pandas.DataFrame, testing_demand_models: Dict[str, Dict[str, Any]], params: pandas.DataFrame, date: pandas.Timestamp, seed: itertools.count) pandas.DataFrame [source]¶
Calculate the demand probabilities for each test demand model.
- Parameters
states (pandas.DataFrame) – The states of all individuals.
testing_demand_models (dict) – A dictionary containing the demand models for testing.
params (pandas.DataFrame) – The parameter DataFrame.
date (pandas.Timestamp) – Current date.
seed (itertools.count) – The seed counter.
- Returns
- Contains for each individual and every
demand model the probability that the individual will request a test.
- Return type
demand_probabilities (pandas.DataFrame)
- _sample_which_individuals_demand_a_test(demand_probabilities: pandas.DataFrame, seed: itertools.count) pandas.Series [source]¶
Sample which individuals demand a test.
At first, compute the probabilities that each individual will demand no test at all and the corresponding probability that an individual demands at least one test.
Then, sample individuals which demand a test.
- Parameters
demand_probabilities (pandas.DataFrame) – Contains for each individual and every demand model the probability that the individual will request a test.
seed (itertools.count) – The seed counter.
- Returns
- A boolean series indicating individuals who demand
a test.
- Return type
demands_test (pandas.Series)
- _sample_reason_for_demanding_a_test(demand_probabilities: pandas.DataFrame, demands_test: pandas.Series, seed: itertools.count) pandas.Series [source]¶
Sample reason for demanding a test.
- Parameters
demand_probabilities (pandas.DataFrame) – Contains for each individual and every demand model the probability that the individual will request a test.
demands_test (pandas.Series) – A boolean series indicating individuals who demand a test.
seed (itertools.count) – The seed counter.
- Returns
Shows which demand model caused the bid.
- Return type
demands_test_reason (pandas.Series)
Examples
>>> import itertools >>> demand_probabilities = pd.DataFrame( ... [[0.1, 0.2], [0.8, 0.4]], columns=["a", "b"] ... ) >>> demands_test = pd.Series([True, True]) >>> seed = itertools.count(2) >>> _sample_reason_for_demanding_a_test( ... demand_probabilities, demands_test, seed ... ) 0 b 1 a dtype: category Categories (2, object): ['a', 'b']
- _normalize_probabilities(probabilities: pandas.DataFrame) pandas.Series [source]¶
Normalize probabilities such that they sum to one.
- Parameters
probabilities (pandas.DataFrame) – Contains probabilities for each individual and demand model to request a test.
- Returns
A series with normalized probabilities summing up to one.
- Return type
Examples
>>> df = pd.DataFrame([[0.1, 0.1], [0.1, 0.3]]) >>> _normalize_probabilities(df) 0 1 0 0.50 0.50 1 0.25 0.75