Estimating Parameters of Compartmental Models from Observed Data | by Abhay Shukla | Towards Data Science

In this post, we will define SEIR and SEIRD models and minimize nonlinear least squares to estimate their parameters from observed data of coronavirus cases. To perform well, nonlinear optimization techniques such as Levenberg–Marquardt (LM) algorithm need good initial guess to solve Nonlinear least squares optimization. We will use SEIR and SEIRD model simulation to guess parameter estimates and use these values in the optimization.
Note that this is purely a programming and data science exercise, so refrain from drawing conclusions based on the simulations or parameter estimates. It’s best left to the experts in epidemiology and medicine.
Before we get into the thick of things, let's familiarize ourselves with the type of models to which SEIR and SEIRD belong.
However, if you want to jump right into code, the Jupyter notebooks with complete codes are available at Simulation & Parameter Estimation of SEIR Model and Simulation & Parameter Estimation of SEIRD Model.
Mechanistic models are used to model and predict the spread of diseases and infections such as flu, Ebola or even coronavirus. A distinguishing feature of mechanistic models over machine learning models is their ability to encode hypothesized causal nature of disease transmission. This typically involves constructing mathematical formulations of causal mechanism and using analytical tools to validate these hypotheses with observed data [R. Baker et al., 2018].
In short, mechanistic models are interpretable while machine learning models, more often than not, are not. Because of this, a mechanistic model which explains the phenomenon well can give insightful information about how a disease spreads and possibly how it can be dealt with. The differences in types of data and information these two models provide are listed in Table 1 below.
Compartmental models are a type of mechanistic models which divide the population into groups referred to as compartments. They are based on a system of ordinary differential equations which express the dynamics between different epidemiological states of a population (aka compartments). They are useful for short and long-term forecast of the spread of a phenomenon, e.g. a disease, and can also be used to study the effect of different interventions [Chowell, 2017].
The compartments and progression between epidemiological states in SEIR and SEIRD models are shown in Figure 1 and Figure 2 below.
To check more details on dynamical systems of these models and how to simulate them in python, please refer to Simulating Compartmental Models in Epidemiology using Python & Jupyter Widgets.
In this section, we will discuss data, models and minimizing nonlinear least squares for parameter estimation.
We will use the data of coronavirus cases in India available at the COVID-19 REST API for India. The structure of JSON returned from the API is as follows,
{'success': boolean, 'data': [         {'day': 'YYYY-MM-DD',           'summary': {                     'total': int,                     'confirmedCasesIndian': int,                     'confirmedCasesForeign': int,                     'discharged': int,                     'deaths': int,                     'confirmedButLocationUnidentified': int                     },           'regional': [                       {                       'loc': string,                       'confirmedCasesIndian': int,                       'discharged': int,                       'deaths': int,                       'confirmedCasesForeign': int,                       'totalConfirmed': int                       }                                           ]          }         ], 'lastRefreshed': timestamp, 'lastOriginUpdate': timestamp}
Sample of the data looks as below,
{ 'success': True, 'data': [          {'day': '2020-03-10',           'summary': {                      'total': 47,                      'confirmedCasesIndian': 31,                      'confirmedCasesForeign': 16,                      'discharged': 0,                      'deaths': 0,                      'confirmedButLocationUnidentified': 0                      },           'regional': [                        {                        'loc': 'Delhi',                        'confirmedCasesIndian': 4,                        'confirmedCasesForeign': 0,                        'discharged': 0,                        'deaths': 0,                        'totalConfirmed': 4},                        {                        'loc': 'Haryana',                        'confirmedCasesIndian': 0,                        'confirmedCasesForeign': 14,                        'discharged': 0,                        ...                        }                        ...                        ]            }            ...           ], 'lastRefreshed': '2020-04-27T04:30:02.090Z',  'lastOriginUpdate': '2020-04-27T02:30:00.000Z'}
This data can easily be transformed into a pandas data frame, we are familiar with using the following piece of code
Let's see first 5 rows of the pandas data frame in Table 2 below,
The total positive cases for coronavirus are still rising in India due to daily new non-zero positive cases. The trend of cases is shown in Figure 3 below.
Now, let’s define the models in python.
SEIR and SEIRD models are defined by a system of ordinary differential equations (ODEs). Let’s see what these ODEs are and how to code and solve them in python,
SEIR
Mathematical Model
Python Code
SEIRD
Mathematical Model
Python Code
We will use odeint module in scipy to solve these systems of equations. Like the system of ODE’s, the function for solving the ODE’s is also very similar. Let’s see how it can be coded in Python for SEIRD model.
To solve a system of ordinary differential equations, you need initial conditions and the parameters characterizing your ODE’s. You can observe these in the python code above.
Next, we proceed to optimizing parameters of ODE’s using nonlinear least squares minimization.
A nonlinear model is one where the relationship between output and input(s) is not linear in parameters of the model. E.g.
Michaelis–Menten model cannot be expressed as a linear combination of the two betas.
Now, nonlinear least squares can be defined as, “Non-linear least squares is the form of least squares analysis used to fit a set of m observations with a model that is non-linear in n unknown parameters (m ≥ n).” — Wikipedia
Algorithms (such as LM) minimizing nonlinear least squares require a good initial parameter estimates which are closer to the optimal value. Next, we will use a simulation of the models to arrive at a good initial estimate.
Simulation
We will use a simulator of SEIR and SEIRD model built in the post Simulating Compartmental Models in Epidemiology using Python & Jupyter Widgets with some modifications for this purpose.
Upon trying various combinations of parameters, beta (infection rate) = 1.14, sigma (incubation rate) = 0.02, gamma (recovery rate) = 0.02, mu (mortality rate) = 0.01, seems to be a reasonable initial estimates for SEIRD model. The simulator settings can be seen in Figure 7 below.
A similar method can be used for SEIR model. Now, let's use the initial guess to further optimize the parameters using LM algorithm.
Optimization
Without going into too many details, in Levenberg–Marquardt algorithm, also known as the Damped least-squares (DLS) method, the change in weights in successive epochs is given by,
We will use minimize module from lmfit library in python for minimization of nonlinear least squares using LM algorithm. minimize requires residuals as input along with other parameters of the model for optimization. Let's code the residuals and pass it to minimize as shown below,
Now, minimization of nonlinear least squares with LM algorithm (leastsq) is straightforward, as shown in code below,
After the optimization is complete, you can print the optimized model parameters and visualize the fit.
Metrics
We can also compute metrics like MAE and RMSE by using optimized parameters and calling ode_solver defined above. At the time of writing this post I found these values to be,
Fitted MAEInfected:  587.5587793520101Recovered: 216.5347633701369Dead:      21.532532235433454Fitted RMSEInfected:  702.1643902204037Recovered: 284.9841911847741Dead:      26.4463792416788
You can also feed the optimized parameter values in the simulator to view what the epidemiological state curves look like, as shown in Figure 9.
The jupyter notebooks with complete code are available at Simulation & Parameter Estimation of SEIR Model and Simulation & Parameter Estimation of SEIRD Model.
In this post, we learned about,
Mechanistic, compartmental, SEIR and SEIRD modelsCoding ODE’s in Python and solving themNonlinear models and nonlinear least squaresOptimizing parameters of ODE using SEIR/SEIRD simulator and minimize module in lmfit libraryUsing optimized parameters in a simulator to visualize epidemiological state curves
Mechanistic, compartmental, SEIR and SEIRD models
Coding ODE’s in Python and solving them
Nonlinear models and nonlinear least squares
Optimizing parameters of ODE using SEIR/SEIRD simulator and minimize module in lmfit library
Using optimized parameters in a simulator to visualize epidemiological state curves
*Note that the same approach can also be used to code and optimize any system of ODEs.
Things we didn’t learn (a conscious choice)
How to interpret the results of these epidemiological models.When will the curve flatten? How many will infect, recover, die etc? When can we re-start the economy?
How to interpret the results of these epidemiological models.
When will the curve flatten? How many will infect, recover, die etc? When can we re-start the economy?
Let’s leave these important questions for experts to answer.
Baker RE, Peña J-M, Jayamohan J and Jérusalem A., 2018, Mechanistic models versus machine learning, a fight worth fighting for the biological community? Biol. Lett.1420170660http://doi.org/10.1098/rsbl.2017.0660Chowell G., Fitting dynamic models to epidemic outbreaks with quantified uncertainty: A primer for parameter uncertainty, identifiability, and forecasts (2017), Infectious Disease Modelling Volume 2, Issue 3, August 2017, Pages 379–398Non-linear least squares (Wikipedia)Nonlinear regression (Wikipedia)Samarasinghe S., 2007, Neural Networks for Applied Sciences and Engineering
Baker RE, Peña J-M, Jayamohan J and Jérusalem A., 2018, Mechanistic models versus machine learning, a fight worth fighting for the biological community? Biol. Lett.1420170660http://doi.org/10.1098/rsbl.2017.0660
Chowell G., Fitting dynamic models to epidemic outbreaks with quantified uncertainty: A primer for parameter uncertainty, identifiability, and forecasts (2017), Infectious Disease Modelling Volume 2, Issue 3, August 2017, Pages 379–398
Non-linear least squares (Wikipedia)
Nonlinear regression (Wikipedia)
Samarasinghe S., 2007, Neural Networks for Applied Sciences and Engineering