Model¶
Main model¶
Disease spread¶
-
covid19_npis.model.disease_spread.
InfectionModel
(N, E_0_t, R_t, C, gen_kernel)[source]¶ This function combines a variety of different steps:
Converts the given values to an exponential distributed initial with an length of this can be seen in
_construct_E_0_t()
.Calculates for each time step using the given contact matrix :
Calculates the arrays i.e. new infections for each age group and country, with the efficient reproduction matrix , the susceptible pool , the population size and the generation interval . This is done recursive for every time step.
- Parameters
E_0 – Initial number of infectious.
Dimensions: batch_dims, country, age_groupR_t – Reproduction number matrix.
Dimensions: time, batch_dims, country, age_groupN – Total population per country
Dimensions: country, age_groupC – inter-age-group Contact-Matrix (see 8)
Dimensions: country, age_group, age_groupgen_kernel – Normalized PDF of the generation interval
Dimensions: batch_dims(?), l
- Returns
Sample from distribution of new, daily cases
-
covid19_npis.model.disease_spread.
construct_generation_interval
(name='g', mu_k=120.0, mu_theta=0.04, theta_k=8.0, theta_theta=0.1, l=16)[source]¶ Generates the generation interval with two underlying gamma distributions for mu and theta
whereby the underlying distribution are modeled as follows
- Parameters
name (string) – Name of the distribution for trace and debugging.
mu_k (number, optional) – Concentration/k parameter for underlying gamma distribution of mu ().
Default: 120mu_theta (number, optional) – Scale/theta parameter for underlying gamma distribution of mu ().
Default: 0.04theta_k (number, optional) – Concentration/k parameter for underlying gamma distribution of theta ().
Default: 8theta_theta (number, optional) – Scale/theta parameter for underlying gamma distribution of theta ().
Default: 0.1l (number, optional) – Length of generation interval i.e in the formula above
Default: 16
- Returns
Normalized generation interval pdf
-
covid19_npis.model.disease_spread.
construct_E_0_t
(modelParams, len_gen_interv_kernel, R_t, mean_gen_interv, mean_test_delay=10)[source]¶ Generates a prior for E_0_t, based on the observed number of cases during the first 5 days. Currently it is implemented to take the first value of R_t, and multiply the inverse of R_t with first observed values until the begin of the simulation is reached. This is then used as a prior for a lognormal distribution which set the E_0_t.
- Parameters
modelParams (
covid19_npis.ModelParams
) – Instance of modelParams, mainly used for number of age groups and number of countries.len_gen_interv_kernel (number) – …some description
R_t (tf.tensor) – Time dependent reproduction number tensor .
Dimensions: time, batch, country, age groupmean_gen_interv (countries) – …some description
mean_test_delay (number, optional) – …some description
Default: 10
- Returns
- E_0_t:
some description
Dimensions: time, batch, country, age_group
-
covid19_npis.model.disease_spread.
construct_delay_kernel
(name, modelParams, loc, scale, length_kernel)[source]¶ Constructs delay in hierarchical manner:
- Parameters
name – Name of the delay distribution
modelParams (
covid19_npis.ModelParams
) – Instance of modelParams, mainly used for number of age groups and number of countries.loc – Location of the hierarchical Lognormal distribution for the mean of the delay.
scale – Theta parameter for now#
length_kernel – Length of the delay kernel in days.
- Returns
Generator for gamma probability density function.
Dimensions: batch, country, kernel(time)
Todo
Think about sigma distribution and how to parameterize it. Also implement that.
Reproduction number¶
-
covid19_npis.model.reproduction_number.
_fsigmoid
(t, l, d)[source]¶ Calculates and returns
- Parameters
t – Time, “variable”
l – Length of the change point, determines scale
d – Date of the change point, determines location
-
covid19_npis.model.reproduction_number.
_create_distributions
(modelParams)[source]¶ Returns a dict of distributions for further processing/sampling with the following priors:
- Parameters
modelParams (
covid19_npis.ModelParams
) – Instance of modelParams, mainly used for number of age groups and number of countries.- Returns
interventions, distributions
-
covid19_npis.model.reproduction_number.
construct_R_t
(name, modelParams, R_0, include_noise=True)[source]¶ Constructs the time dependent reproduction number for every country and age group. There are a lot of things happening here be sure to check our paper for more indepth explanations!
We build the effectivity in an hierarchical manner in the unbounded space:
The length of the change point depends on the intervention and whether the strength is increasing or decreasing:
The date of the begin of the intervention is also allowed to vary slightly around the date given by the Oxford government response tracker:
And finally the time dependent reproduction number :
We also sometimes call the time dependent reproduction number R_t!
- Parameters
name (str) – Name of the distribution (gets added to trace).
modelParams (
covid19_npis.ModelParams
) – Instance of modelParams, mainly used for number of age groups and number of countries.R_0 (tf.tensor) – Initial reproduction number. Should be constructed using
construct_R_0()
orconstruct_R_0_old()
.
Dimensions: batch, country, age group
- Returns
Time dependent reproduction number tensor .
Dimensions: time, batch, country, age group
-
covid19_npis.model.reproduction_number.
construct_R_0
(name, modelParams, loc, scale, hn_scale)[source]¶ Constructs R_0 in the following hierarchical manner:
- Parameters
name (str) – Name of the distribution (gets added to trace).
modelParams (
covid19_npis.ModelParams
) – Instance of modelParams, mainly used for number of age groups and number of countries.loc (number) – Location parameter of the R^*_0 Normal distribution.
scale (number) – Scale parameter of the R^*_0 Normal distribution.
hn_scale (number) – Scale parameter of the sigma_{R^*, text{country}} HaflNormal distribution.
- Returns
R_0 tensor
Dimensions: batch, country, age_group
-
covid19_npis.model.reproduction_number.
construct_lambda_0
(name, modelParams, loc, scale, hn_scale)[source]¶ Constructs lambda_0 in the following hierarchical manner:
- Parameters
name (str) – Name of the distribution (gets added to trace).
modelParams (
covid19_npis.ModelParams
) – Instance of modelParams, mainly used for number of age groups and number of countries.loc (number) – Location parameter of the R^*_0 Normal distribution.
scale (number) – Scale paramter of the R^*_0 Normal distribution.
hn_scale (number) – Scale parameter of the sigma_{R^*, text{country}} HaflNormal distribution.
- Returns
R_0 tensor
Dimensions: batch, country, age_group
-
covid19_npis.model.reproduction_number.
construct_R_0_old
(name, modelParams, mean, beta)[source]¶ Old constructor of using a gamma distribution:
- Parameters
name (string) – Name of the distribution for trace and debugging.
modelParams (
covid19_npis.ModelParams
) – Instance of modelParams, mainly used for number of age groups and number of countries.mean – Mean of the gamma distribution.
beta – Rate of the gamma distribution.
- Returns
R_0 tensor
Dimensions: batch, country, age_group
Number of tests¶
-
covid19_npis.model.number_of_tests.
weekly_modulation
(name, modelParams, cases)[source]¶ Adds a weekly modulation of the number of new cases:
The modulation is assumed to be the same for all age-groups within one country and determined by the “weight” and “offset” parameters. The weight follows a sigmoidal distribution with normal prior of “weight_cross”. The “offset” follows a VonMises distribution centered around 0 (Mondays) and a wide SD (concentration parameter = 2).
- Parameters
name (str or None,) – The name of the cases to be modulated (gets added to trace).
modelParams (
covid19_npis.ModelParams
) – Instance of modelParams, mainly used for number of age groups and number of countries.cases (tf.tensor) – The input array of daily new cases for countries and age groups
- Returns
cases_modulated
- Return type
tf.tensor
Todo
check prior parameters
different modulations across: age, country?
check: are (cumulative) case numbers same as in unmodulated case? need some kind of normalization?
store and plot parameters at end
-
covid19_npis.model.number_of_tests.
generate_testing
(name_total, name_positive, modelParams, new_E_t)[source]¶ High level function for generating/simulating testing behaviour.
Constructs B splines Delay cases
- Parameters
name_total (str,) – Name for the total tests performed
name_positive (str,) – Name for the positive tests performed
modelParams (
covid19_npis.ModelParams
) – Instance of modelParams, mainly used for number of age groups and number of countries.new_E_t (tf.Tensor) – New cases
Dimensions: batch, time, country, age_group
- Returns
(, Total and positive tests by age group and country
Dimensions: (batch, time, country, age_group) x 2
Todo
Add more documenation for this function
-
covid19_npis.model.number_of_tests.
_calc_positive_tests
(new_E_t_delayed, phi_plus, phi_age)[source]¶ - Parameters
name (str) – Name of the variable for the new positive cases in the trace.
new_E_t_delayed (tf.Tensor) – New cases with reporting delay
Dimensions: batch, time, country, age_groupphi_plus (tf.Tensor) – Fraction of positive tests
Dimensions: batch, time, countryphi_age (tf.Tensor) – Fraction of positive tests
Dimensions: batch, age_group
- Returns
Dimensions: batch, time, country, age_group
-
covid19_npis.model.number_of_tests.
_calc_total_number_of_tests_performed
(new_E_t_delayed, phi_tests_reported, phi_plus, eta, xi)[source]¶ - Parameters
name (str) – Name of the variable for the total number of tests performed in the trace.
new_E_t_delayed (tf.Tensor) – New cases with reporting delay
Dimensions: batch, time, country, age_groupphi_tests_reported (tf.Tensor) – Difference in fraction for different countries
Dimensions: batch, countryphi_plus (tf.Tensor) – Fraction of positive tests
Dimensions: batch, time, countryeta (tf.Tensor) – Number of traced persons per case with subsequent negative test per case
Dimensions: batch, time, countryxi (tf.Tensor) – Base rate of testing per day that leads to negative tests
Dimensions: batch, time, country
- Returns
Dimensions: batch, time, country, age_group
-
covid19_npis.model.number_of_tests.
_construct_phi_tests_reported
(name, modelParams, mu_loc=1.0, mu_scale=1.0, sigma_scale=1.0)[source]¶ Construct the different of the fraction of tests for each country in the following hierarchical manner:
- Parameters
name (str) – Name of the variable . Will also appear in the trace with this name.
modelParams (
covid19_npis.ModelParams
) – Instance of modelParams, mainly used for number of age groups and number of countries.mu_loc (optional) – Location parameter for the Normal distribution .
Default: 1.0mu_scale (optional) – Scale parameter for the Normal distribution .
Default: 1.0sigma_scale (optional) – Scale parameter for the HalfCauchy distribution.
Default: 1.0
- Returns
Dimensions: batch, country
-
covid19_npis.model.number_of_tests.
_construct_phi_age
(name, modelParams, sigma_scale=0.2)[source]¶ Fraction of positive tests
- Parameters
name (str) – Name of the variable . Will also appear in the trace with this name.
modelParams (
covid19_npis.ModelParams
) – Instance of modelParams, mainly used for number of age groups and number of countries.sigma_scale – Scale parameter for the HalfNormal distribution
Default: 0.2
- Returns
Dimensions: batch, age_group
-
covid19_npis.model.number_of_tests.
_construct_reporting_delay
(name, modelParams, m_ast, mu_loc=1.5, mu_scale=0.4, theta_sigma_scale=0.2, m_sigma_scale=3.0)[source]¶ - Parameters
name (str) – Name of the reporting delay variable
modelParams (
covid19_npis.ModelParams
) – Instance of modelParams, mainly used for number of age groups and number of countries.m_ast (tf.Tensor) –
Dimensions: batch, country, splinemu_loc (optional) – Location parameter for the Normal distribution
Default: 1.5mu_scale (optional) – Scale parameter for the Normal distribution
Default: 0.4theta_sigma_scale (optional) – Scale parameter for the HalfNorml distribution
Default: 0.2m_sigma_scale (optional) – Scale parameter for the HalfNorml distribution
Default: 3.0
- Returns
Dimensions: batch, country, spline
-
covid19_npis.model.number_of_tests.
_calc_reporting_delay_kernel
(name, m, theta, length_kernel=14)[source]¶ Calculates the pdf for the gamma reporting kernel.
- Parameters
name – Name of the reporting delay kernel
m –
Dimensions: batch, time, countrytheta –
Dimensions: batch, countrylength_kernel (optional) – Length of the kernel in days
Default: 14 days
- Returns
Dimensions: batch,country, kernel, time
-
covid19_npis.model.number_of_tests.
construct_testing_state
(name_phi, name_eta, name_xi, name_m_ast, modelParams, num_knots, mu_cross_loc=0.0, mu_cross_scale=10.0, m_mu_loc=12.0, m_mu_scale=2.0, sigma_cross_scale=10.0, m_sigma_scale=1.0)[source]¶ where
with the distributions parametarized in the following hierarchical manner:
at last we transform the variables
- Parameters
name_phi (str) – Name of the fraction of positive tests variable
name_eta (str) – Name of the number of traced persons variable
name_xi (str) – Name of the base tests rate variable
name_m_ast (str) – Name of the testing delay variable
modelParams (
covid19_npis.ModelParams
) – Instance of modelParams, mainly used for number of age groups and number of countries.num_knots – Number of knots for the Bspline dimension.
mu_cross_loc (optional) – Location parameter for the three Normal distributions
Default: 0.0mu_cross_scale (optional) – Scale parameter for the three Normal distributions
Default: 10.0m_mu_loc (optional) – Location parameter for the Normal distribution
Default: 12.0m_mu_scale (optional) – Scale parameter for the Normal distribution
Default: 2.0sigma_cross_scale (optional) – Scale parameter for the three HalfCauchy distributions
Default: 10.0m_sigma_scale (optional) – Scale parameter for the HalfNormal distribution
Default: 1.0
- Returns
Testing state tuple
Dimensions: 4 x (batch, country, spline),
-
covid19_npis.model.number_of_tests.
construct_Bsplines_basis
(modelParams)[source]¶ Function to construct the basis functions for all BSplines, should only be called once. Uses splipy python library.
- Parameters
modelParams (
covid19_npis.ModelParams
) – Instance of modelParams, mainly used for number of age groups and number of countries.degree (optional) – degree corresponds to exponent of the splines i.e. degree of three corresponds to a cubic spline.
Default: 3knots (list, optional) – Knots array used for constructing the BSplines.
Default: one knot every 7 days
- Returns
Dimensions: time, knots?
-
covid19_npis.model.number_of_tests.
_calculate_Bsplines
(coef, basis)[source]¶ Calculates the Bsplines given the basis functions B and the coefficients x.
- Parameters
coef – Coefficients .
Dimensions: …,country, splinebasis – Basis functions tensor
Dimensions: time, spline
- Returns
Dimensions: …,time, country
Deaths¶
-
covid19_npis.model.deaths.
_construct_reporting_delay
(name, modelParams, theta_sigma_scale=0.3, theta_mu_loc=1.5, theta_mu_scale=0.3, m_sigma_scale=4.0, m_mu_loc=21.0, m_mu_scale=2.0)[source]¶ - Parameters
name (str) – Name of the reporting delay variable
modelParams (
covid19_npis.ModelParams
) – Instance of modelParams, mainly used for number of age groups and number of countries.theta_sigma_scale (optional) – Scale parameter for the Normal distribution
Default: 0.3theta_mu_loc (optional) – Location parameter for the Normal distribution
Default: 1.5theta_mu_scale (optional) – Scale parameter for the HalfNormal distribution
Default: 0.3m_sigma_scale (optional) – Scale parameter for the HalfNormal distribution
Default: 4.0m_mu_loc (optional) – Location parameter for the Normal distribution
Default: 21.0m_mu_scale (optional) – Scale parameter for the Normal distribution
Default: 2.0
- Returns
(m, theta)
Dimensions: (batch, country) x 2
-
covid19_npis.model.deaths.
_calc_Phi_IFR
(name, modelParams, alpha_loc=0.119, alpha_scale=0.003, beta_loc=-7.53, beta_scale=0.4)[source]¶ Calculates and construct the IFR and Phi_IFR:
- Parameters
name (str) – Name of the infection fatatlity ratio variable
modelParams (
covid19_npis.ModelParams
) – Instance of modelParams, mainly used for number of age groups and number of countries.alpha_loc (optional) –
Default: 0.119alpha_scale (optional) –
Default: 0.003beta_loc (optional) –
Default: -7.53beta_scale (optional) –
Default: 0.4
- Returns
Phi_IFR
Dimensions: batch, country, age brackets
-
covid19_npis.model.deaths.
calc_delayed_deaths
(name, new_cases, Phi_IFR, m, theta, length_kernel=40)[source]¶ Calculates delayed deahs from IFR and delay kernel.
- Parameters
name (str) – Name of the delayed deaths variable
new_cases (tf.Tensor) – New cases without reporting delay
Dimensions: batch, time, country, age_groupPhi_IFR (tf.Tensor) – Infection fatality ratio of the age brackets
Dimensions: batch, country, age_groupm (tf.Tensor) – Median fatality delay for the delay kernel
Dimensions: batch, countrytheta (tf.Tensor) – Scale fatality delay for the delay kernel
Dimensions: batchlength_kernel (optional) – Length of the kernel in days
Default: 14 days
- Returns
Dimensions: batch, time, country, age_group
Utility¶
-
covid19_npis.model.utils.
gamma
(x, alpha, beta)[source]¶ Returns a gamma kernel evaluated at x. The implementation is the same as defined in the tfp.gamma distribution which is probably quiet numerically stable. :param x: :param alpha: :param beta:
-
covid19_npis.model.utils.
positive_axes
(axes, ndim)[source]¶ Given a list of axes, returns them written as positive numbers
-
covid19_npis.model.utils.
match_axes
(tensor, target_axes, ndim=None)[source]¶ Extend and transpose dimensions, such that the dimension i of tensor is at the position target_axes[i]. Missing dimension are filled with size 1 dimensions. This is therefore a generalization of tf.expand_dims and tf.transpose and implemented using these. If ndim is None, the number of the dimensions of the result is the minimum fullfilling the requirements of target_axes
- Parameters
tensor (tf.Tensor) – The input tensor with len(tensor.dims) == len(target_axes)
target_axes (list of ints) – Target positions of the dimensions. Can be negative.
- Returns
The transposed and expanded tensor.
- Return type
tensor
-
covid19_npis.model.utils.
einsum_indexed
(tensor1, tensor2, inner1=(), inner2=(), outer1=(), outer2=(), vec1=(), vec2=(), targ_outer1=(), targ_outer2=())[source]¶ Calling tf.einsum with indices instead of a string. For example einsum_indexed(t1, t2, inner1=1, inner2=0, outer1=0, outer2=1) corresponds to the tf.einsum string “ab…,bc…->ac…” (Matrix product) and a matrix vector product “…ab,…b,->…a” is parameterized by einsum_indexed(t1, t2, inner1=-1, inner2=-1, vec1=-2)
- Parameters
tensor1 (tensor) – Input tensor 1
tensor2 (tensor) – Input tensor 2
inner1 (int or list) – The axes in tensor 1 over which a inner product is taken
inner2 (int or list) – The axes indices in tensor 2 over which a inner product is taken
outer1 (int or list) – The axes indices in tensor 1 over which a outer product is taken
outer2 (int or list) – The axes indices in tensor 2 over which a outer product is taken
vec1 (int or list) – The axes indices of the matrix in a matrix-vector product which are “staying” in the result. This is for the case where tensor1 corresponds to the matrix.
vec2 (int or list) – The axes indices of the matrix in a matrix-vector product which are “staying” in the result. This is for the case where tensor2 corresponds to the matrix.
targ_outer1 (int or list) – The axes indices in the result where the outer product axes of tensor 1 is mapped to. If omitted, the position is inferred such that the order stays the same, and, if equal, the indices of tensor 1 are to the left of the indices of tensor2 for outer products.
targ_outer2 (int or list) – The axes indices in the result where the outer product axes of tensor 2 is mapped to. If omitted, the position is inferred such that the order stays the same, and, if equal, the indices of tensor 1 are to the left of the indices of tensor2 for outer products.
- Returns
- Return type
tensor
-
covid19_npis.model.utils.
concatenate_axes
(tensor, axis1, axis2)[source]¶ Concatenates two consecutive axess
-
covid19_npis.model.utils.
slice_of_axis
(tensor, axis, begin, end)[source]¶ Returns the tensor where the axis axis is sliced from begin to end
-
covid19_npis.model.utils.
convolution_with_fixed_kernel
(data, kernel, data_time_axis, filter_axes_data=())[source]¶ Convolve data with a time independent kernel. The returned shape is equal to the shape of data. In order avoid constructing a time_length x time_length kernel, the data is decomposed in overlapping frames, with a stride of padding, allowing to construct a only padding x time_length sized kernel.
- Parameters
data (tensor) – The input tensor
kernel (tensor) – Has as shape filter_axes x time. filter_axes can be several axes, where in each dimension a difference kernel is located
data_time_axis (int) – the axis of data which corresponds to the time axis
filter_axes_data (tuple) – the axes of data, to which the filter_axes of kernel should be mapped to. Each of this dimension is therefore subject to a different filter
- Returns
- Return type
A convolved tensor with the same shape as data.
-
covid19_npis.model.utils.
convolution_with_varying_kernel
(data, kernel, data_time_axis, filter_axes_data=())[source]¶ Convolve data with a time dependent kernel. The returned shape is equal to the shape of data. In this implementation, the kernel will be augmented by a time_data axis, and then the inner product with the date will be taken. This is not an optimal implementation, as the most of the entries of the kernel inner product matrix will be zero.
- Parameters
data (tensor) – The input tensor
kernel (tensor) – Has as shape filter_axes x time_kernel x time_data. filter_axes can be several axes, where in each dimension a difference kernel is located
data_time_axis (int) – the axis of data which corresponds to the time axis
filter_axes_data (tuple) – the axes of data, to which the filter_axes of kernel should be mapped to. Each of this dimension is therefore subject to a different filter
- Returns
- Return type
A convolved tensor with the same shape as data.