Base implementation for SIR models

BaseSIR

class aftercovid.models._base_sir.BaseSIR(p, q, c=None, eq=None, **kwargs)[source]

Base model for SIR models.

Parameters
  • p – list of [(name, initial value or None, comment)] (parameters)

  • q – list of [(name, initial value or None, comment)] (quantities)

  • c – list of [(name, initial value or None, comment)] (constants)

  • eq – equations

property C

Returns the quantities

property P

Returns the parameters

property Q

Returns the quantities

property cst_names

Returns the list of constants names (unsorted).

property cst_param

Returns a dictionary with the constant and the parameters.

enumerate_edges()[source]

Enumerates the list of quantities contributing to others. It ignores constants.

eval_diff(t=0)[source]

Evaluates derivatives. Returns a dictionary.

evalf_eq(eq, t=0)[source]

Evaluates an sympy expression.

evalf_leq(name, t=0)[source]

Evaluates a lambdified expression.

Parameters
  • name – name of the lambdified expresion

  • t – t values

Returns

evaluation

get()[source]

Retrieves all values.

get_index(name)[source]

Returns the index of a name (True or False, position).

property names

Returns the list of names.

property param_names

Returns the list of parameters names (unsorted).

property params_dict

Returns the list of parameters names in a dictionary.

property quantity_names

Returns the list of quantities names (unsorted).

to_dot(verbose=False, full=False)[source]

Produces a graph in DOT format.

to_rst()[source]

Returns a string formatted in RST.

update(**values)[source]

Updates values.

vect(t=0, out=None, derivative=False)[source]

Returns all values as a vector.

Parameters
  • t – time t

  • out – alternative output array in which to place the result. It must have the same shape as the expected output.

  • derivative – returns the derivatives instead of the values

Returns

values or derivatives

property vect_names

Returns the list of names.

BaseSIRSimulation

class aftercovid.models._base_sir_sim.BaseSIRSimulation[source]

Common methods about simulation for SIR models.

R0(t=0)[source]

Returns R0 coefficient.

eqsign(eqname, name)[source]

Returns the sign of the second derivative for equation eqname against name.

Parameters
  • eqname – equation name

  • name – symbol name

Returns

boolean

iterate(n=10, t=0, derivatives=False)[source]

Evalues the quantities for n iterations. Returns a list of dictionaries. If derivatives is True, it returns two dictionaries.

Parameters
  • n – number of iterations

  • t – first t

  • derivatives – returns the derivative as well

Returns

iterator on dictionaries

iterate2array(n=10, t=0, derivatives=False)[source]

Evalues the quantities for n iterations. Returns matrices.

Parameters
  • n – number of iterations

  • t – first t

  • derivatives – returns the derivative as well

Returns

quantities or (quantities, derivatives) if derivatives is True

BaseSIREstimation

class aftercovid.models._base_sir_estimation.BaseSIREstimation[source]

Common methods about training, predicting for SIR models.

fit(X, y, t=0, max_iter=100, learning_rate_init=0.1, lr_schedule='constant', momentum=0.9, power_t=0.5, early_th=None, min_threshold=None, max_threshold=None, verbose=False)[source]

Fits a model BaseSIR.

Parameters
  • X – known values for every quantity at time t, every column is mapped to the list returned by quantity_names

  • y – known derivative for every quantity at time t, comes in the same order as X, both X and y have the same shape.

  • t – implicit feature

  • max_iter – number of iteration

  • learning_rate_init – see SGDOptimizer

  • lr_schedule – see SGDOptimizer

  • momentum – see SGDOptimizer

  • power_t – see SGDOptimizer

  • early_th – see SGDOptimizer

  • verbose – see SGDOptimizer

  • min_threshold – see SGDOptimizer

  • max_threshold – see SGDOptimizer

The training needs two steps. The first one creates a training datasets. The second one estimates the coefficients by using a stochastic gradient descent (see SGDOptimizer). Let’s use a SIR model (see CovidSIR).as an example. Let’s denote the parameters as \(\Omega\) and \(Z_1=S\), …, \(Z_4=R\). The model is defined by \(\frac{dZ_i}{dt} = f_i(\Omega, Z)\) where \(Z=(Z_1, ..., Z_4)\). y is used to compute the expected derivates \(\frac{dZ_i}{dt}\). The loss function is defined as:

\[L(\Omega,Z) = \sum_{i=1}^4 \left( f_i(\Omega,Z) - \frac{dZ_i}{dt}\right)^2\]

Then the gradient is:

\[\frac{\partial L(\Omega,Z)}{\partial\Omega} = 2 \sum_{i=1}^4 \frac{\partial f_i(\Omega,Z)}{\partial\Omega} \left( f_i(\Omega,Z) - \frac{dZ_i}{dt} \right)\]

A stochastic gradient descent takes care of the rest.

predict(X, t=0)[source]

Predicts the derivative at time t.

Parameters
  • X – known values for every quantity at time t, every column is mapped to the list returned by quantity_names

  • t – implicit feature

Returns

predictive derivative

score(X, y, t=0)[source]

Scores the predictions. Returns L2 norm divided by the number of rows.

Parameters
  • X – known values for every quantity at time t, every column is mapped to the list returned by quantity_names

  • y – expected values

  • t – implicit feature

Returns

predictive derivative

score_l1(X, y, t=0)[source]

Scores the predictions. Returns L1 norm divided by the number of rows and the population.

Parameters
  • X – known values for every quantity at time t, every column is mapped to the list returned by quantity_names

  • y – expected values

  • t – implicit feature

Returns

predictive derivative