# The Covid-testing simulation.
# Inputs:
# - fraction of the population that is currently infected
# - information about the test: sensitivity and specificity.
# What we do:
# - test everybody
# - get numbers for true pos, true neg, false pos and false neg
# according to the inputs above. Use population-level statistics rather
# than individual-level statistics, so there's no randomness in any
# result.
# - massage those numbers to show the fraction of people who test positive
# that are actually sick.
# - then retest everyone who tests positive. Compute the same four results
# as above, and massage them the same way. Show that the second test
# gives *much* better results.
N_STUDENTS = 200000 # How many people are in our world.
FRAC_INFECTED = .01 # Fraction of students who are infected
SENSITIVITY = .98 # Frac of sick people who test positive
SPECIFICITY = .97 # Frac of healthy people who test negative
# So the fraction of healthy people who test positive
# is (1-SPECIFICITY)
n_infected = N_STUDENTS * FRAC_INFECTED
n_not_infected = N_STUDENTS - n_infected
# Compute true & false positives, given the infection rate and test properties.
n_true_pos = n_infected * SENSITIVITY
n_false_pos = n_not_infected * (1 - SPECIFICITY)
n_total_pos = n_true_pos + n_false_pos
# Our first big result -- how many positive tests are true positive.
frac_pos_who_are_sick = n_true_pos / n_total_pos
print ("Of {:.2f} total positives, {:.3f}% are true and {:.3f}% are false"
.format (n_total_pos, 100*frac_pos_who_are_sick, 100*(1-frac_pos_who_are_sick)))
# All people who test positive get retested.
n_retest = n_total_pos
n_retest_true_pos = n_true_pos * SENSITIVITY
n_retest_false_pos = n_false_pos * (1 - SPECIFICITY)
n_retest_total_pos = n_retest_true_pos + n_retest_false_pos
# Our second big result!
frac_retest_who_are_sick = n_retest_true_pos / n_retest_total_pos
print ("Retest results: Of {:.2f} total positives, {:.1f}% are true"
.format (n_retest_total_pos, 100* n_retest_true_pos/n_retest_total_pos))