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/notebooks/chap12.ipynb
Views: 531
Modeling and Simulation in Python
Chapter 12
Copyright 2017 Allen Downey
Code
Here's the code from the previous notebook that we'll need.
Metrics
Given the results, we can compute metrics that quantify whatever we are interested in, like the total number of sick students, for example.
Here's an example.|
Exercise: Write functions that take a TimeFrame
object as a parameter and compute the other metrics mentioned in the book:
The fraction of students who are sick at the peak of the outbreak.
The day the outbreak peaks.
The fraction of students who are sick at the end of the semester.
Note: Not all of these functions require the System
object, but when you write a set of related functons, it is often convenient if they all take the same parameters.
Hint: If you have a TimeSeries
called I
, you can compute the largest value of the series like this:
And the index of the largest value like this:
You can read about these functions in the Series
documentation.
What if?
We can use this model to evaluate "what if" scenarios. For example, this function models the effect of immunization by moving some fraction of the population from S to R before the simulation starts.
Let's start again with the system we used in the previous sections.
And run the model without immunization.
Now with 10% immunization.
10% immunization leads to a drop in infections of 16 percentage points.
Here's what the time series looks like for S, with and without immunization.
Now we can sweep through a range of values for the fraction of the population who are immunized.
This function does the same thing and stores the results in a Sweep
object.
Here's how we run it.
And here's what the results look like.
If 40% of the population is immunized, less than 4% of the population gets sick.
Logistic function
To model the effect of a hand-washing campaign, I'll use a generalized logistic function (GLF), which is a convenient function for modeling curves that have a generally sigmoid shape. The parameters of the GLF correspond to various features of the curve in a way that makes it easy to find a function that has the shape you want, based on data or background information about the scenario.
The following array represents the range of possible spending.
compute_factor
computes the reduction in beta
for a given level of campaign spending.
M
is chosen so the transition happens around $500.
K
is the maximum reduction in beta
, 20%.
B
is chosen by trial and error to yield a curve that seems feasible.
Here's what it looks like.
Exercise: Modify the parameters M
, K
, and B
, and see what effect they have on the shape of the curve. Read about the generalized logistic function on Wikipedia. Modify the other parameters and see what effect they have.
Hand washing
Now we can model the effect of a hand-washing campaign by modifying beta
Let's start with the same values of beta
and gamma
we've been using.
Now we can sweep different levels of campaign spending.
Here's a function that sweeps a range of spending and stores the results in a SweepSeries
.
Here's how we run it.
And here's what it looks like.
Now let's put it all together to make some public health spending decisions.
Optimization
Suppose we have $1200 to spend on any combination of vaccines and a hand-washing campaign.
We can sweep through a range of doses from, 0 to max_doses
, model the effects of immunization and the hand-washing campaign, and run simulations.
For each scenario, we compute the fraction of students who get sick.
The following function wraps that loop and stores the results in a Sweep
object.
Now we can compute the number of infected students for each possible allocation of the budget.
And plot the results.
Exercises
Exercise: Suppose the price of the vaccine drops to $50 per dose. How does that affect the optimal allocation of the spending?
Exercise: Suppose we have the option to quarantine infected students. For example, a student who feels ill might be moved to an infirmary, or a private dorm room, until they are no longer infectious.
How might you incorporate the effect of quarantine in the SIR model?