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/chap14.ipynb
Views: 531
Modeling and Simulation in Python
Chapter 14
Copyright 2017 Allen Downey
Code from previous chapters
Contact number
Here's the SweepFrame
from the previous chapter, with one row for each value of beta
and one column for each value of gamma
.
The following loop shows how we can loop through the columns and rows of the SweepFrame
. With 11 rows and 4 columns, there are 44 elements.
Now we can wrap that loop in a function and plot the results. For each element of the SweepFrame
, we have beta
, gamma
, and frac_infected
, and we plot beta/gamma
on the x-axis and frac_infected
on the y-axis.
Here's what it looks like:
It turns out that the ratio beta/gamma
, called the "contact number" is sufficient to predict the total number of infections; we don't have to know beta
and gamma
separately.
We can see that in the previous plot: when we plot the fraction infected versus the contact number, the results fall close to a curve.
Analysis
In the book we figured out the relationship between and analytically. Now we can compute it for a range of values:
total_infected
is the change in from the beginning to the end.
Now we can plot the analytic results and compare them to the simulations.
The agreement is generally good, except for values of c
less than 1.
Exercises
Exercise: If we didn't know about contact numbers, we might have explored other possibilities, like the difference between beta
and gamma
, rather than their ratio.
Write a version of plot_sweep_frame
, called plot_sweep_frame_difference
, that plots the fraction infected versus the difference beta-gamma
.
What do the results look like, and what does that imply?
Exercise: Suppose you run a survey at the end of the semester and find that 26% of students had the Freshman Plague at some point.
What is your best estimate of c
?
Hint: if you print frac_infected_series
, you can read off the answer.