CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
AllenDowney

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: AllenDowney/ModSimPy
Path: blob/master/examples/insulin.ipynb
Views: 531
Kernel: Python 3 (ipykernel)

The Insulin Minimal Model

Modeling and Simulation in Python

Copyright 2021 Allen Downey

License: Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International

# install Pint if necessary try: import pint except ImportError: !pip install pint
# download modsim.py if necessary from os.path import basename, exists def download(url): filename = basename(url) if not exists(filename): from urllib.request import urlretrieve local, _ = urlretrieve(url, filename) print('Downloaded ' + local) download('https://github.com/AllenDowney/ModSimPy/raw/master/modsim.py')
# import functions from modsim from modsim import *

The following cells download and read the data.

download('https://raw.githubusercontent.com/AllenDowney/' + 'ModSim/main/data/glucose_insulin.csv')
data = pd.read_csv('glucose_insulin.csv', index_col='time');

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, II, is governed by this differential equation:

dIdt=kI(t)+γ(G(t)GT)t \frac{dI}{dt} = -k I(t) + \gamma (G(t) - G_T) t

where G(t)G(t) is the concentration of glucose at time tt, and kk, γ\gamma, and GTG_T are positive-valued parameters:

  • kk controls the rate of insulin disappearance.

  • GTG_T determines the glucose threshold: when G(t)G(t) exceeds this threshold, it causes insulin to appear; when G(t)G(t) is below this threshold, it causes insulin to disappear.

  • γ\gamma controls how quickly insulin appears or disappears when the concentration of glucose is elevated or depressed.

Notice that this equation depends on time, tt, 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, I0I_0, to be a free parameter; that is, we will choose the value of I0I_0, 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:

I0 = 360 k = 0.25 gamma = 0.004 G_T = 80 params = I0, k, gamma, G_T
# Solution goes here
# Solution goes here

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.

# Solution goes here
# Solution goes here

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.

# Solution goes here
# Solution goes here
# Solution goes here

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.

# Solution goes here
# Solution goes here

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.

# Solution goes here
# Solution goes here
# Solution goes here
# Solution goes here
# Solution goes here

Exercise: Using the best parameters, estimate the sensitivity to glucose of the first and second phase pancreatic responsivity:

ϕ1=ImaxIbk(G0Gb) \phi_1 = \frac{I_{max} - I_b}{k (G_0 - G_b)}

ϕ2=γ×104 \phi_2 = \gamma \times 10^4

For G0G_0, use the best estimate from the glucose model, 272 mg/dL. For GbG_b and IbI_b, use the initial measurements from the data. For ImaxI_{max} is the maximum measurement of insulin concentration.

# Solution goes here
# Solution goes here
# Solution goes here
# Solution goes here
# Solution goes here

According to Pacini and Bergman, here are the normal ranges for these quantities.

phi_1_interval = 2, 4 phi_1_interval
phi_2_interval = 20, 35 phi_2_interval

Do your estimates fall in these ranges?