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/plague.ipynb
Views: 531
The Freshman Plague
Modeling and Simulation in Python
Copyright 2021 Allen Downey
License: Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International
This case study picks up where Chapter 12 leaves off.
Hand washing
Suppose you are the Dean of Student Life, and you have a budget of just $1200 to combat the Freshman Plague. You have two options for spending this money:
You can pay for vaccinations, at a rate of $100 per dose.
You can spend money on a campaign to remind students to wash hands frequently.
We have already seen how we can model the effect of vaccination. Now let's think about the hand-washing campaign. We'll have to answer two questions:
How should we incorporate the effect of hand washing in the model?
How should we quantify the effect of the money we spend on a hand-washing campaign?
For the sake of simplicity, let's assume that we have data from a similar campaign at another school showing that a well-funded campaign can change student behavior enough to reduce the infection rate by 20%.
In terms of the model, hand washing has the effect of reducing beta
. That's not the only way we could incorporate the effect, but it seems reasonable and it's easy to implement.
Now we have to model the relationship between the money we spend and the effectiveness of the campaign. Again, let's suppose we have data from another school that suggests:
If we spend $500 on posters, materials, and staff time, we can change student behavior in a way that decreases the effective value of
beta
by 10%.If we spend $1000, the total decrease in
beta
is almost 20%.Above $1000, additional spending has little additional benefit.
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.
The result is the following function, which takes spending as a parameter and returns factor
, which is the factor by which beta
is reduced:
I use compute_factor
to write add_hand_washing
, which takes a System
object and a budget, and modifies system.beta
to model the effect of hand washing:
Now we can sweep a range of values for spending
and use the simulation to compute the effect:
Here's how we run it:
The following figure shows the result.
Below $200, the campaign has little effect.
At $800 it has a substantial effect, reducing total infections from more than 45% to about 20%.
Above $800, the additional benefit is small.
Optimization
Let's put it all together. With a fixed budget of $1200, we have to decide how many doses of vaccine to buy and how much to spend on the hand-washing campaign.
Here are the parameters:
The fraction budget/price_per_dose
might not be an integer. int
is a built-in function that converts numbers to integers, rounding down.
We'll sweep the range of possible doses:
In this example we call linrange
with only one argument; it returns a NumPy array with the integers from 0 to max_doses
, including both.
Then we run the simulation for each element of dose_array
:
For each number of doses, we compute the fraction of students we can immunize, fraction
and the remaining budget we can spend on the campaign, spending
. Then we run the simulation with those quantities and store the number of infections.
The following figure shows the result.
If we buy no doses of vaccine and spend the entire budget on the campaign, the fraction infected is around 19%. At 4 doses, we have $800 left for the campaign, and this is the optimal point that minimizes the number of students who get sick.
As we increase the number of doses, we have to cut campaign spending, which turns out to make things worse. But interestingly, when we get above 10 doses, the effect of herd immunity starts to kick in, and the number of sick students goes down again.
Exercise: Suppose the price of the vaccine drops to $50 per dose. How does that affect the optimal allocation of the spending?