# How Epidemics Spread 2, Exercise 1: doubling time and growth rate from daily counts import math # Daily new cases for days 1 to 6 (assumed, invented to show the method). new_cases = [12, 15, 19, 24, 30, 38] ratios = [b / a for a, b in zip(new_cases, new_cases[1:])] print("day-to-day ratios:", [round(x, 3) for x in ratios]) # Geometric mean of the ratios = the average daily growth factor. factor = (new_cases[-1] / new_cases[0]) ** (1 / (len(new_cases) - 1)) growth_pct = (factor - 1) * 100 doubling = math.log(2) / math.log(factor) print(f"average daily growth: {growth_pct:.1f}%") print(f"doubling time: {doubling:.2f} days (rule of 70 says {70 / growth_pct:.2f})") # Day on which new cases per day first reach 1000 if growth never changes. day = 6 level = new_cases[-1] while level < 1000: level *= factor day += 1 print("new cases a day reach 1000 on day", day) print("same answer by formula:", 6 + math.ceil(math.log(1000 / new_cases[-1]) / math.log(factor))) # Output when run: # day-to-day ratios: [1.25, 1.267, 1.263, 1.25, 1.267] # average daily growth: 25.9% # doubling time: 3.01 days (rule of 70 says 2.70) # new cases a day reach 1000 on day 21 # same answer by formula: 21