Model

Main model

covid19_npis.model.main_model(modelParams)[source]

Todo

Create Docstring for this function.

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:

  1. Converts the given E_0 values to an exponential distributed initial E_{0_t} with an length of l this can be seen in _construct_E_0_t().

  2. Calculates R_{eff} for each time step using the given contact matrix C:

    R_{diag} &= \text{diag}(\sqrt{R}) \\
R_{eff}  &= R_{diag} \cdot C \cdot R_{diag}

  3. Calculates the \tilde{I} arrays i.e. new infections for each age group and country, with the efficient reproduction matrix R_{eff}, the susceptible pool S, the population size N and the generation interval g(\tau). This is done recursive for every time step.

    \tilde{I}(t) &= \frac{S(t)}{N} \cdot R_{eff} \cdot \sum_{\tau=0}^{t} \tilde{I}(t-1-\tau) g(\tau) \\
S(t) &= S(t-1) - \tilde{I}(t-1)

Parameters
  • E_0 – Initial number of infectious.
    Dimensions: batch_dims, country, age_group

  • R_t – Reproduction number matrix.
    Dimensions: time, batch_dims, country, age_group

  • N – Total population per country
    Dimensions: country, age_group

  • C – inter-age-group Contact-Matrix (see 8)
    Dimensions: country, age_group, age_group

  • gen_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

g(\tau) = Gamma(\tau;
k = \frac{\mu_{D_{\text{gene}}}}{\theta_{D_\text{gene}}},
\theta=\theta_{D_\text{gene}})

whereby the underlying distribution are modeled as follows

\mu_{D_{\text{gene}}} &\sim Gamma(k = 4.8/0.04, \theta=0.04) \\
\theta_{D_\text{gene}} &\sim Gamma(k = 0.8/0.1, \theta=0.1)

Parameters
  • name (string) – Name of the distribution for trace and debugging.

  • mu_k (number, optional) – Concentration/k parameter for underlying gamma distribution of mu (\mu_{D_{\text{gene}}}).
    Default: 120

  • mu_theta (number, optional) – Scale/theta parameter for underlying gamma distribution of mu (\mu_{D_{\text{gene}}}).
    Default: 0.04

  • theta_k (number, optional) – Concentration/k parameter for underlying gamma distribution of theta (\theta_{D_\text{gene}}).
    Default: 8

  • theta_theta (number, optional) – Scale/theta parameter for underlying gamma distribution of theta (\theta_{D_\text{gene}}).
    Default: 0.1

  • l (number, optional) – Length of generation interval i.e t 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 R(t).
    Dimensions: time, batch, country, age group

  • mean_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 d in hierarchical manner:

\mu_c^d &\sim \text{LogNormal}\left(\mu=2.5,\sigma=0.1\right) \quad \forall c \\
\sigma^d_c &\sim \\
d_{c} &= \text{PDF-Gamma}(\mu^d_c,\sigma_d)

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

\frac{1}{1+e^{-4/l*(t-d)}}

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:

\alpha^\dagger_i &\sim \mathcal{N}\left(-1, 2\right)\quad \forall i,\\
\Delta \alpha^\dagger_c &\sim \mathcal{N}\left(0, \sigma_{\alpha, \text{country}}\right) \quad \forall c, \\
\Delta \alpha^\dagger_a &\sim \mathcal{N}\left(0, \sigma_{\alpha, \text{age}}\right)\quad \forall a, \\
\sigma_{\alpha, \text{country}}  &\sim HalfNormal\left(0.1\right),\\
\sigma_{\alpha, \text{age}} &\sim HalfNormal\left(0.1\right)

l^\dagger_{\text{positive}} &\sim \mathcal{N}\left(3, 1\right),\\
l^\dagger_{\text{negative}} &\sim \mathcal{N}\left(5, 2\right),\\
\Delta l^\dagger_i &\sim \mathcal{N}\left(0,\sigma_{l, \text{interv.}} \right)\quad \forall i,\\
\sigma_{l, \text{interv.}}&\sim HalfNormal\left(1\right)

\Delta d_i  &\sim \mathcal{N}\left(0, \sigma_{d, \text{interv.}}\right)\quad \forall i,\\
\Delta d_c &\sim \mathcal{N}\left(0, \sigma_{d, \text{country}}\right)\quad \forall c,\\
\sigma_{d, \text{interv.}}  &\sim HalfNormal\left(0.3\right),\\
\sigma_{d, \text{country}} &\sim HalfNormal\left(0.3\right)

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 R(t) 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:

\alpha_{i,c,a} &= \frac{1}{1+e^{-\alpha^\dagger_{i,c,a}}},\\
\alpha^\dagger_{i,c,a} &= \alpha^\dagger_i + \Delta \alpha^\dagger_c + \Delta \alpha^\dagger_{a}

The length of the change point depends on the intervention and whether the strength is increasing or decreasing:

l_{i, \text{sign}\left(\Delta \gamma\right)} &= \ln\left(1 + e^{l^\dagger_{i, \text{sign}\left(\Delta \gamma\right)}}\right),\\
l^\dagger_{i, \text{sign}\left(\Delta \gamma\right)} &= l^\dagger_{\text{sign}\left(\Delta \gamma\right)} + \Delta l^\dagger_i,

The date of the begin of the intervention is also allowed to vary slightly around the date d^{\text{data}}_{i,c} given by the Oxford government response tracker:

d_{i,c,p} &= d^{\text{data}}_{i,c,p} + \Delta d_i +\Delta d_c

And finally the time dependent reproduction number R^*_e:

\gamma_{i,c,p}(t) &= \frac{1}{1+e^{-4/l_{i, \text{sign}\left(\Delta \gamma\right)} \cdot (t - d_{i,c,p})}} \cdot \Delta \gamma_{i,c,p}^{\text{data}}\\
\gamma_{i,c}(t) &= \sum_p \gamma_{i,c,p}(t)\\
R^*_e &= R^*_0 e^{-\sum_i^{N_i}\alpha_{i, c, a} \gamma_{i,c}(t)}

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() or construct_R_0_old().
    Dimensions: batch, country, age group

Returns

Time dependent reproduction number tensor R(t).
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:

R^*_{0,c} &= R^*_0 + \Delta R^*_{0,c}, \\
R^*_0 &\sim \mathcal{N}\left(2,0.5\right)\\
\Delta R^*_{0,c} &\sim \mathcal{N}\left(0, \sigma_{R^*, \text{country}}\right)\quad \forall c,\\
\sigma_{R^*, \text{country}} &\sim HalfNormal\left(0.3\right)

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:

\lambda^*_{0,c} &= \lambda^*_0 + \Delta \lambda^*_{0,c}, \\
\lambda^*_0 &\sim \mathcal{N}\left(0.4,0.1\right)\\
\Delta \lambda^*_{0,c} &\sim \mathcal{N}\left(0, \sigma_{\lambda^*, \text{country}}\right)\quad \forall c,\\
\sigma_{\lambda^*, \text{country}} &\sim HalfNormal\left(0.05\right)

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 R_0 using a gamma distribution:

R_0 &\sim Gamma\left(\mu=2.5,\beta=2.0\right)

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 \mu of the gamma distribution.

  • beta – Rate \beta 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:

\text{cases\_modulated} &= \text{cases} \cdot (1-f(t))\,, \qquad\text{with}\\
f(t) &= (1-w) \cdot \left(1 - \left|\sin\left(\frac{\pi}{7} t- \frac{1}{2}\Phi_w\right)\right| \right)

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 E_{\text{age}, a}.
    Dimensions: batch, time, country, age_group

Returns

(n_{\Sigma, c,a}(t), n_{{+}, {c,a}}(t) 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]

n_{{+}, {c,a}}(t) =\Tilde{E}_{\text{delayTest}, {c,a}}(t) \cdot \phi_{+, c}(t) \cdot \phi_{\text{age}, a},

Parameters
  • name (str) – Name of the variable for the new positive cases n_{{+}, {c,a}}(t) in the trace.

  • new_E_t_delayed (tf.Tensor) – New cases with reporting delay \Tilde{E}_{\text{delayTest}, c,a}(t).
    Dimensions: batch, time, country, age_group

  • phi_plus (tf.Tensor) – Fraction of positive tests \phi_{+, c}(t).
    Dimensions: batch, time, country

  • phi_age (tf.Tensor) – Fraction of positive tests \phi_{\text{age}, a}.
    Dimensions: batch, age_group

Returns

n_{{+}, {c,a}}(t)
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]

n_{\Sigma, c,a}(t) &= \phi_{\text{tests reported}, c}\nonumber \\
\cdot   (\, & \Tilde{E}_{\text{delayTest}, c,a}(t) \cdot  \phi_{+, c}(t) \nonumber \\
+  \, &\Tilde{E}_{\text{delayTest}, c,a}(t) \cdot  \phi_{+, c}(t) \cdot \eta_{\text{traced}, c}(t)\nonumber \\
+\,  &\xi_c(t))

Parameters
  • name (str) – Name of the variable for the total number of tests performed n_{\Sigma, c,a}(t) in the trace.

  • new_E_t_delayed (tf.Tensor) – New cases with reporting delay \Tilde{E}_{\text{delayTest}, c,a}(t).
    Dimensions: batch, time, country, age_group

  • phi_tests_reported (tf.Tensor) – Difference in fraction for different countries \phi_{\text{tests reported}, c}
    Dimensions: batch, country

  • phi_plus (tf.Tensor) – Fraction of positive tests \phi_{+, c}(t).
    Dimensions: batch, time, country

  • eta (tf.Tensor) – Number of traced persons per case with subsequent negative test per case \eta_{\text{traced}, c}(t).
    Dimensions: batch, time, country

  • xi (tf.Tensor) – Base rate of testing per day that leads to negative tests \xi_c(t).
    Dimensions: batch, time, country

Returns

n_{\Sigma, c,a}(t)
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:

\phi_{\text{tests reported}, c} &= \frac{e^{\phi^\dagger_{\text{tests reported}, c}}}{e^{\phi^\dagger_{\text{tests reported}, c}} + 1},\label{tests_reported}\\
\phi^\dagger_{\text{tests reported}, c} &\sim \mathcal{N}(\mu_{\phi^\dagger_{\text{tests reported}}}, \sigma_{\phi^\dagger_{\text{tests reported}}}),\\
\mu_{\phi^\dagger_{\text{tests reported}}} &\sim \mathcal{N}(1,1),\\
\sigma_{\phi^\dagger_{\text{tests reported}}} &\sim HalfCauchy(1).

Parameters
  • name (str) – Name of the variable \phi_{\text{tests reported}, c}. 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 \mu_{\phi^\dagger_{\text{tests reported}}}.
    Default: 1.0

  • mu_scale (optional) – Scale parameter for the Normal distribution \mu_{\phi^\dagger_{\text{tests reported}}}.
    Default: 1.0

  • sigma_scale (optional) – Scale parameter for the \sigma_{\phi^\dagger_{\text{tests reported}}} HalfCauchy distribution.
    Default: 1.0

Returns

\phi_{\text{tests reported}, c}
Dimensions: batch, country

covid19_npis.model.number_of_tests._construct_phi_age(name, modelParams, sigma_scale=0.2)[source]

Fraction of positive tests \phi_{\text{age}, a}.

\phi_{\text{age}, a} &= e^{\phi^\dagger_{\text{age},a}} \label{eq:phi_age}\\
\phi^\dagger_{\text{age},a} &= \mathcal{N}\left(0, \sigma_{\phi^\dagger_{\text{age},a}}\right)\\
\sigma_{\phi^\dagger_{\text{age},a}}&=HalfNormal\left(0.2\right)

Parameters
  • name (str) – Name of the variable \phi_{\text{age}, a}. 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 \sigma_{\phi^\dagger_{\text{age},a}}.
    Default: 0.2

Returns

\phi_{\text{age}, a}
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]

m_{D_\text{test},c,b} &= m^\ast_{D_\text{test}, c,b} + \Delta m_{D_\text{test}, c}\\

\Delta m_{D_\text{test}, c} &\sim \mathcal{N} (0, \sigma_{m_{D\, \text{test}}}), \\
\sigma_{m_{D\, \text{test}}} &\sim HalfNormal(3), \label{eq:prior_delta_m_delay}\\
\theta_{D_\text{test}, c} &\sim \mathcal{N}(\mu_{\theta_{D_\text{test}}},\sigma_{\theta_{D_\text{test}}}),\\
\mu_{\theta_{D_\text{test}}} &\sim \mathcal{N}(1.5, 0.4),\\
\sigma_{\theta_{D_\text{test}}} &\sim HalfNormal(0.2).

Parameters
  • name (str) – Name of the reporting delay variable m_{D_\text{test},c,b}.

  • modelParams (covid19_npis.ModelParams) – Instance of modelParams, mainly used for number of age groups and number of countries.

  • m_ast (tf.Tensor) – m^\ast_{D_\text{test}, c,b}
    Dimensions: batch, country, spline

  • mu_loc (optional) – Location parameter for the Normal distribution \mu_{\theta_{D_\text{test}}}.
    Default: 1.5

  • mu_scale (optional) – Scale parameter for the Normal distribution \mu_{\theta_{D_\text{test}}}.
    Default: 0.4

  • theta_sigma_scale (optional) – Scale parameter for the HalfNorml distribution \sigma_{\theta_{D_\text{test}}}.
    Default: 0.2

  • m_sigma_scale (optional) – Scale parameter for the HalfNorml distribution \sigma_{m_{D\, \text{test}}}.
    Default: 3.0

Returns

m_{D_\text{test},c,b}
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.

f_{c,t}(\tau) =  Gamma(\tau ; \alpha = \frac{m_{D_{\text{test}},c}(t)}{\theta_{D_\text{test}},c}
+ 1, \beta=\frac{1}{\theta_{D_\text{test},c}}),\nonumber\\
\text{with $f_{c,t}$ normalized such that} \:\: \sum_{\tau=0}^T f_{c,t}(\tau) = 1.

Parameters
  • name – Name of the reporting delay kernel f_{c,t}(\tau)

  • m
    Dimensions: batch, time, country

  • theta
    Dimensions: batch, country

  • length_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]

(\phi^\dagger_{\text{tested},c,b},
\: \eta^\dagger_{\text{traced},c,b},
\: \xi^\dagger_{c,b},
\: m^\ast_{D_\text{test},c,b})
&\sim StudentT_{\nu=4} \left(\boldsymbol{\mu}, \mathbf{\Sigma}\right)

where

\boldsymbol{\mu} &= \left(\mu_{\phi^\dagger_+},
\mu_{\eta^\dagger_\text{traced}},
\mu_{\xi^\dagger},
\mu_{m_{D_\text{test}}} \right) \\

\mathbf{\Sigma} &\sim LKJ(\eta=2,
\boldsymbol{\sigma} = \left(\sigma_\phi, \sigma_\eta,\sigma_\xi,\sigma_m)\right)

with the distributions parametarized in the following hierarchical manner:

\mu_{\phi^\dagger_+},\: \mu_{\eta^\dagger_\text{traced}},\: \mu_{\xi^\dagger} &\sim \mathcal {N}(0, 10),\\
\mu_{m_{D_\text{test}}} &\sim \mathcal {N}(12, 2), \\
\sigma_\phi, \sigma_\eta, \sigma_\xi &\sim HalfCauchy(10), \\
\sigma_m &\sim HalfNormal(1) \label{eq:prior_sigma_delay_time}\\

at last we transform the variables \phi_{+,c,b},\: \eta_{\text{traced},c,b},\: \xi_{c,b}

\phi_{+,c,b} &= \frac{e^{\phi^\dagger_{+,c,b}}}{e^{\phi^\dagger_{+,c,b}} + 1},\\
\eta_{\text{traced},c,b} &= \ln \left(1 + e^{ \eta^\dagger_{\text{traced},c,b}} \right),\\
\xi_{c,b} &= \ln \left(1 + e^{\xi_{c,b}^\dagger}\right)\frac{n_\text{inhabitants}}{10 000}\\

Parameters
  • name_phi (str) – Name of the fraction of positive tests variable \phi_{+,c,b}.

  • name_eta (str) – Name of the number of traced persons variable \eta_{\text{traced},c,b}.

  • name_xi (str) – Name of the base tests rate variable \xi_{c,b}.

  • name_m_ast (str) – Name of the testing delay variable m^*_{D_{test},c,b}.

  • 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 \mu_{\phi^\dagger_+},\: \mu_{\eta^\dagger_\text{traced}},\: \mu_{\xi^\dagger}.
    Default: 0.0

  • mu_cross_scale (optional) – Scale parameter for the three Normal distributions \mu_{\phi^\dagger_+},\: \mu_{\eta^\dagger_\text{traced}},\: \mu_{\xi^\dagger}.
    Default: 10.0

  • m_mu_loc (optional) – Location parameter for the Normal distribution \mu_{m_{D_\text{test}}}.
    Default: 12.0

  • m_mu_scale (optional) – Scale parameter for the Normal distribution \mu_{m_{D_\text{test}}}.
    Default: 2.0

  • sigma_cross_scale (optional) – Scale parameter for the three HalfCauchy distributions \sigma_\phi, \sigma_\eta, \sigma_\xi.
    Default: 10.0

  • m_sigma_scale (optional) – Scale parameter for the HalfNormal distribution \sigma_m.
    Default: 1.0

Returns

Testing state tuple (\phi_{+,c,b},
\: \eta_{\text{traced},c,b},\: \xi_{c,b},\: m_{D_\text{test},c,b}),\: \theta_{D_\text{test}}.
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: 3

  • knots (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.

x(t) = \sum_{b} x_b B_b(t)

Parameters
  • coef – Coefficients x.
    Dimensions: …,country, spline

  • basis – Basis functions tensor B.
    Dimensions: time, spline

Returns

x(t)
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]

m_{D_\text{death}, c} &= \ln \left(1 + e^{m^*_{D_\text{death}, c}} \right)\\
m^*_{D_\text{death}, c} &\sim \mathcal{N} (\mu_{m_{D_\text{death}}}, \sigma_{m_{D_\text{death}}}), \\
\mu_{m_{D_\text{death}}}&\sim \mathcal{N}(21, 2), \\
\sigma_{m_{D_\text{test}}} &\sim HalfNormal(4), \label{eq:prior_delta_m_delay}\\
\theta_{D_\text{death}, c} &=\frac{1}{4} \ln \left(1 + e^{4\theta^*_{D_\text{death}, c}} \right)\\
\theta^*_{D_\text{death}, c} &\sim \mathcal{N}(\mu_{\theta_{D_\text{test}}},\sigma_{\theta_{D_\text{test}}}),\\
\mu_{\theta_{D_\text{death}}} &\sim \mathcal{N}(1.5, 0.3),\\
\sigma_{\theta_{D_\text{death}}} &\sim HalfNormal(0.3).

Parameters
  • name (str) – Name of the reporting delay variable m_{D_\text{test},c,b}.

  • 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 \sigma_{\theta_{D_\text{death}}}.
    Default: 0.3

  • theta_mu_loc (optional) – Location parameter for the Normal distribution \mu_{\theta_{D_\text{death}}}.
    Default: 1.5

  • theta_mu_scale (optional) – Scale parameter for the HalfNormal distribution \mu_{\theta_{D_\text{death}}}.
    Default: 0.3

  • m_sigma_scale (optional) – Scale parameter for the HalfNormal distribution \sigma_{m_{D_\text{test}}}.
    Default: 4.0

  • m_mu_loc (optional) – Location parameter for the Normal distribution \mu_{m_{D_\text{death}}}.
    Default: 21.0

  • m_mu_scale (optional) – Scale parameter for the Normal distribution \mu_{m_{D_\text{death}}}.
    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:

\beta_{\text{IFR,c}} &= \mathcal{N}\left(-7.53, 0.4\right) \\
\alpha_\text{IFR} &= \mathcal{N}\left(0.119, 0.003\right)

\text{IFR}_c(a^*) &= \frac{1}{100} \exp{\left(\beta_{\text{IFR,c}} + \alpha_\text{IFR} \cdot a\right)} \\

\phi_{\text{IFR}, c,a} = \frac{1}{\sum_{a^* = a_\text{beg}(a)}^{a_\text{end}(a)}  N_\text{pop}\left(a^*\right)}\sum_{a^* = a_\text{beg}(a)}^{a_\text{end}(a)} N_{\text{pop}, c}\left(a^*\right) \text{IFR}_c\left(a^* \right),

Parameters
  • name (str) – Name of the infection fatatlity ratio variable \phi_{\text{IFR}, c,a}.

  • modelParams (covid19_npis.ModelParams) – Instance of modelParams, mainly used for number of age groups and number of countries.

  • alpha_loc (optional) –
    Default: 0.119

  • alpha_scale (optional) –
    Default: 0.003

  • beta_loc (optional) –
    Default: -7.53

  • beta_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.

\Tilde{E}_{\text{delayDeath}, c, a}(t) = \phi_{\text{IFR}, c,a} \sum_{\tau=0}^T \Tilde{E}_{c,a}(t-\tau) \cdot f_{c,t}(\tau) \\
f_{c,t}(\tau) = Gamma(\tau ; \alpha = \frac{m_{D_{\text{death}, c}}}{\theta_{D_\text{death}}}+ 1, \beta=\frac{1}{\theta_{D_\text{death}}})

Parameters
  • name (str) – Name of the delayed deaths variable \Tilde{E}_{\text{delayDeath}, c, a}(t).

  • new_cases (tf.Tensor) – New cases without reporting delay \Tilde{E}_{c,a}(t).
    Dimensions: batch, time, country, age_group

  • Phi_IFR (tf.Tensor) – Infection fatality ratio of the age brackets \phi_{\text{IFR}, c,a}.
    Dimensions: batch, country, age_group

  • m (tf.Tensor) – Median fatality delay for the delay kernel m_{D_{\text{death}, c}}.
    Dimensions: batch, country

  • theta (tf.Tensor) – Scale fatality delay for the delay kernel \theta_{D_\text{death}}.
    Dimensions: batch

  • length_kernel (optional) – Length of the kernel in days
    Default: 14 days

Returns

\Tilde{E}_{\text{delayDeath}, c, a}(t)
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

Parameters
  • axes (array-like, int) – list of axes, positive or negative

  • ndim (int) – number of dimensions of the array

Returns

Return type

positive list of axes

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

Parameters
  • tensor (tensor) – input

  • axis1 (int) – first axis

  • axis2 (int) – second axis

Returns

Return type

Concatenated tensor

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

Parameters
  • tensor (tensor) –

  • axis (int) –

  • begin (int) –

  • end (int) –

Returns

Return type

sliced tensor

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.

covid19_npis.model.utils.convolution_with_map(data, kernel, modelParams)[source]
Parameters

data
Dimensions: batch, time, country, agegroup

covid19_npis.model.utils.get_filter_axis_data_from_dims(ndim)[source]

Returns filter axis data from len(new_I_t.shape)