Skip to content

Instantly share code, notes, and snippets.

@ryanberckmans
Created April 4, 2020 18:50
Show Gist options
  • Save ryanberckmans/cbd40fb6adce828a93368bc0711bfc58 to your computer and use it in GitHub Desktop.
Save ryanberckmans/cbd40fb6adce828a93368bc0711bfc58 to your computer and use it in GitHub Desktop.
Monte Carlo simulation to estimate American deaths from COVID-19
// Monte Carlo simulation to estimate American deaths from COVID-19.
// The probability distribution of estimated deaths is taken from the
// Good Judgment Superforecaster project.
// "How many deaths attributed to COVID-19 in the U.S. will be reported/estimated as of 31 March 2021?"
// https://goodjudgment.io/covid/dashboard/
type Deaths = Int
def simulateEstimatedDeaths: Deaths = {
import scala.util.Random
// number returns a random number between from and to, both inclusive.
def number(rand: Random, from: Int, to: Int): Int = {
if (from > to) from
else rand.nextInt(to - from + 1) + 1
}
val rand = new Random(System.currentTimeMillis())
val iterations = 1000
val placeholder = 0
val startingDeaths = 0.0f
Math.round(
Array
.fill(iterations)(placeholder)
.foldLeft(startingDeaths)((deaths, _) => {
deaths + (
rand.nextFloat match {
// These probabilities and death ranges correspond to values from
// "How many deaths attributed to COVID-19 in the U.S. will be reported/estimated as of 31 March 2021?"
// https://goodjudgment.io/covid/dashboard/
case x if x < 0.06 =>
number(rand, 8162, 35000) // 8,162 Americans died from COVID-19 as of 2020/4/4
case x if x < 0.75 => number(rand, 35000, 350000)
// A naive approach would draw from a uniform distribution in each bucket.
// This yields an estimate of about 500k American deaths.
// But it seems incorrect to draw uniformly from [350k, 3.5m] because no
// superforecasters estimated that over 3.5m Americans would die from COVID-19,
// so it may be the case that 99% of Superforecasters estimate that less than
// 1m Americans will die. We don't know. Instead we'll use the lower bound of 350k.
//case _ => number(rand, 350000, 3500000) // potentially overly pessimistic
case _ => 350000
}
)
}) / iterations
)
}
val estimatedDeaths = simulateEstimatedDeaths
println(s"""
A Monte Carlo simulation estimates that ${java.text.NumberFormat.getIntegerInstance
.format(estimatedDeaths)} Americans will die from COVID-19.
The probability distribution of estimated deaths is taken from the Good Judgment
Superforecaster project.
goodjudgment.io/covid/dashboard/
""")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment