Logistic curve fitting in epidemiology

From Rosetta Code
Revision as of 10:58, 6 April 2020 by Wherrera (talk | contribs) (julia example)

Template:Draft Task

The least-squares method (see references below) in statistics is used to fit data to the best of a family of similar curves by finding the parameters for a curve which minimizes the total of the distances from each data point to the curve.

Often, the curve used is a straight line, in which case the method is also called linear regression. If a curve which uses logarithmic growth is fit, the method can be called logistic regression.

A commonly used family of functions used in statistical studies of populations, including the growth of epidemics, are curves akin to the logistic curve:

   f(x) = L / (1 + e-k(x-x0))

Though predictions based on fitting to such curves may err, especially if used to extrapolate from incomplete data, curves similar to the logistic curve have had good fits in population studies, including modeling the growth of past epidemics.

The task:

Task
  • Given the following daily world totals since December 31, 2019 for persons

who have become infected with the novel coronavirus Covid-19:

Daily totals:
27, 27, 27, 44, 44, 59, 59, 59, 59, 59, 59, 59, 59, 60, 60,
61, 61, 66, 83, 219, 239, 392, 534, 631, 897, 1350, 2023, 2820,
4587, 6067, 7823, 9826, 11946, 14554, 17372, 20615, 24522, 28273,
31491, 34933, 37552, 40540, 43105, 45177, 60328, 64543, 67103,
69265, 71332, 73327, 75191, 75723, 76719, 77804, 78812, 79339, 
80132, 80995, 82101, 83365, 85203, 87024, 89068, 90664, 93077, 
95316, 98172, 102133, 105824, 109695, 114232, 118610, 125497, 
133852, 143227, 151367, 167418, 180096, 194836, 213150, 242364, 
271106, 305117, 338133, 377918, 416845, 468049, 527767, 591704, 
656866, 715353, 777796, 851308, 928436, 1000249, 1082054, 1174652
  • Use the following variant of the logistic curve as a formula:
   f(t) = n0 e(r t) / ((1 + n0 (e(r t) - 1) / K)

Where r is the rate of growth of the infection in the population.

The R0 of an infection (different from r above) is a measure of how many new individuals will become infected for every individual currently infected. It is an important measure of how quickly an infectious disease may spread.

R0 is related to the logistic curve's r parameter by the formula

   r ≈ ln(R0) / G

where G, the generation time, is roughly the sum of the incubation time, perhaps 5 days, and the mean contagion period, perhaps 7 days, so, for covid-19, roughly we have

   R0 ≈ e12r

Assume the following constants hold in the formula above:

  • K is the world population, about 7.9 billion
  • n0 is 27, the number of cases found in China at the start of the pandemic.
Task
  • Demonstrate code that finds a least-squares fits of the curve to the data.
  • Show the calculated r for the logistic curve.
  • Show the final R0 parameter you calculate from the logistic curve r value parameter.
See also

Julia

<lang julia>using LsqFit

const K = 7_800_000_000 # approximate world population const n0 = 27 # starting at day 0 with 27 Chinese cases

""" The model for logistic regression with a given r0 """ @. model(t, r) = (n0 * exp(r * t)) / (( 1 + n0 * (exp(r * t) - 1) / K))

  1. Daily world totals of covid cases, all countries

ydata = [ 27, 27, 27, 44, 44, 59, 59, 59, 59, 59, 59, 59, 59, 60, 60, 61, 61, 66, 83, 219, 239, 392, 534, 631, 897, 1350, 2023, 2820, 4587, 6067, 7823, 9826, 11946, 14554, 17372, 20615, 24522, 28273, 31491, 34933, 37552, 40540, 43105, 45177, 60328, 64543, 67103, 69265, 71332, 73327, 75191, 75723, 76719, 77804, 78812, 79339, 80132, 80995, 82101, 83365, 85203, 87024, 89068, 90664, 93077, 95316, 98172, 102133, 105824, 109695, 114232, 118610, 125497, 133852, 143227, 151367, 167418, 180096, 194836, 213150, 242364, 271106, 305117, 338133, 377918, 416845, 468049, 527767, 591704, 656866, 715353, 777796, 851308, 928436, 1000249, 1082054, 1174652, ] tdata = collect(LinRange(0.0, 96, 97))

  1. starting approximation for r of 1/2

rparam = [0.5]

fit = curve_fit(model, tdata, ydata, rparam)

  1. Our answer for r given the world data and simplistic model

r = fit.param println("The logistic curve r for the world data is: ", r) println("The confidence interval at 5% significance is: ",

   confidence_interval(fit, 0.05))

println("Since R0 ≈ exp(G * r), and G ≈ 12 days, R0 ≈ ", exp(12r[1]))

</lang>

Output:
The logistic curve r for the world data is: [0.11230217572265622]
The confidence interval at 5% significance is: [(0.11199074156706985, 0.11261360987824258)]
Since R0 ≈ exp(G * r), and G ≈ 12 days, R0 ≈ 3.8482792820761063


Python

Uses NumPy/SciPy's optimize package. <lang python>import numpy as np import scipy.optimize as opt

n0, K = 27, 7_800_000_000

def f(t, r0):

   return (n0 * np.exp(r0 * t)) / (( 1 + n0 * (np.exp(r0 * t) - 1) / K))

y = [ 27, 27, 27, 44, 44, 59, 59, 59, 59, 59, 59, 59, 59, 60, 60, 61, 61, 66, 83, 219, 239, 392, 534, 631, 897, 1350, 2023, 2820, 4587, 6067, 7823, 9826, 11946, 14554, 17372, 20615, 24522, 28273, 31491, 34933, 37552, 40540, 43105, 45177, 60328, 64543, 67103, 69265, 71332, 73327, 75191, 75723, 76719, 77804, 78812, 79339, 80132, 80995, 82101, 83365, 85203, 87024, 89068, 90664, 93077, 95316, 98172, 102133, 105824, 109695, 114232, 118610, 125497, 133852, 143227, 151367, 167418, 180096, 194836, 213150, 242364, 271106, 305117, 338133, 377918, 416845, 468049, 527767, 591704, 656866, 715353, 777796, 851308, 928436, 1000249, 1082054, 1174652, ] x = np.linspace(0.0, 96, 97)

r, cov = opt.curve_fit(f, x, y, [0.5])

  1. Our answer for r given the world data and simplistic model

print("The r for the world Covid-19 data is:", r,

   ", with covariance of", cov)   

print("The calculated R0 is then", np.exp(12 * r))

</lang>

Output:
The r for the world Covid-19 data is: [0.11230218] , with covariance of [[2.46164331e-08]]
The calculated R0 is then [3.8482793]