Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/examples/insulin.ipynb
Views: 531
The Insulin Minimal Model
Modeling and Simulation in Python
Copyright 2021 Allen Downey
License: Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International
The following cells download and read the data.
In Chapter 17 I present the glucose minimal model; in Chapter 18 we implemented it using run_simulation
and run_solve_ivp
. In this case study, we'll implement the other half of the Minimal Model, which describes the concentration of insulin.
In the insulin minimal model, the concentration of insulin, , is governed by this differential equation:
where is the concentration of glucose at time , and , , and are positive-valued parameters:
controls the rate of insulin disappearance.
determines the glucose threshold: when exceeds this threshold, it causes insulin to appear; when is below this threshold, it causes insulin to disappear.
controls how quickly insulin appears or disappears when the concentration of glucose is elevated or depressed.
Notice that this equation depends on time, , since the initial injection. It has the effect of increasing glucose sensitivity over time. If you are familiar with control systems, the effect of this term is similar to the integral term in a PID controller.
In addition to the three parameters in the equation, we will also consider the initial concentration of insulin, , to be a free parameter; that is, we will choose the value of , and the other parameters, that best fit the data.
Exercise: Write a version of make_system
that takes the parameters of the model (I0
, k
, gamma
, and G_T
) as parameters, along with a DataFrame
containing the measurements, and returns a System
object suitable for use with run_solve_ivp
.
Use it to make a System
object with the following parameters:
Exercise: Write a slope function that takes a time stamp, a State
object, and a System
object, and returns the derivative of I
with respect to time. Test your function with the initial conditions from system
.
Exercise: Run run_solve_ivp
with your System
object and slope function, and plot the results, along with the measured insulin levels. Use the keyword argument t_eval=data.index
so the results are evaluated as the same time stamps as the data.
Exercise: Write an error function that takes a sequence of parameters as an argument, along with the DataFrame
containing the measurements. It should make a System
object with the given parameters, call run_solve_ivp
, and compute the difference between the results of the simulation and the measured values. Test your error function by calling it with the parameters from the previous exercise.
Hint: As we did with the glucose model, you might want to drop the first 2-3 elements from the sequence of errors.
Exercise: Use leastsq
to find the parameters that best fit the data. Make a System
object with those parameters, run it, and plot the results along with the measurements.
Exercise: Using the best parameters, estimate the sensitivity to glucose of the first and second phase pancreatic responsivity:
For , use the best estimate from the glucose model, 272 mg/dL. For and , use the initial measurements from the data. For is the maximum measurement of insulin concentration.
According to Pacini and Bergman, here are the normal ranges for these quantities.
Do your estimates fall in these ranges?