# How Epidemics Spread 1, Exercise 1: incidence and prevalence from a case list # A village of 200 people (assumed). Each case is (first day ill, last day ill). # Find, for days 1 to 12: new cases (incidence), people ill that day (prevalence), # the cumulative incidence at day 12, and check prevalence against the # running total of new cases minus recoveries. population = 200 cases = [(1, 4), (2, 6), (2, 5), (3, 7), (3, 8), (4, 8), (4, 9), (5, 9), (5, 10), (6, 10), (7, 11), (8, 12)] for day in range(1, 13): incidence = sum(1 for start, end in cases if start == day) prevalence = sum(1 for start, end in cases if start <= day <= end) recovered_before = sum(1 for start, end in cases if end < day) started_by = sum(1 for start, end in cases if start <= day) assert prevalence == started_by - recovered_before print(f"day {day:2d} new {incidence} ill {prevalence:2d}") print("cumulative incidence at day 12:", len(cases) / population) print("mean duration:", round(sum(e - s + 1 for s, e in cases) / len(cases), 2), "days") print("peak prevalence:", max(sum(1 for s, e in cases if s <= d <= e) for d in range(1, 13))) # Output when run: # day 1 new 1 ill 1 # day 2 new 2 ill 3 # day 3 new 2 ill 5 # day 4 new 2 ill 7 # day 5 new 2 ill 8 # day 6 new 1 ill 8 # day 7 new 1 ill 8 # day 8 new 1 ill 8 # day 9 new 0 ill 6 # day 10 new 0 ill 4 # day 11 new 0 ill 2 # day 12 new 0 ill 1 # cumulative incidence at day 12: 0.06 # mean duration: 5.08 days # peak prevalence: 8