<jupyter_start><jupyter_text>We begin with boilerplate:<jupyter_code>%matplotlib inline 
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [12, 5]
%load_ext autoreload
%autoreload 2
import numpy as np
import random
import pandas as pd
import os
import sys
import logging
logging.basicConfig(format='%(asctime)s %(levelname)s:%(message)s', level=logging.INFO)<jupyter_output><empty_output><jupyter_text>Covid epidemic simulator<jupyter_code>from codit.disease import Covid
from codit.outbreak import Outbreak
from codit.population.networks.city import CityPopulation
from codit.population.covid import PersonCovid
import codit.society as society
import codit.config<jupyter_output><empty_output><jupyter_text>Baseline config of the simulation<jupyter_code>codit.config.print_baseline_config()<jupyter_output>CROSS_IMMUNITY {'other': {'other'}, 'SARS-CoV-2': {'SARS-CoV-2', 'B.1.1.7'}, 'B.1.1.7': {'SARS-CoV-2', 'B.1.1.7'}}
DAILY_TEST_CAPACITY_PER_HEAD 0.0075
DAYS_BEFORE_INFECTIOUS 4
DAYS_INFECTIOUS_TO_SYMPTOMS 2
DAYS_OF_SYMPTOMS 5
DEFAULT_COVID SARS-CoV-2
DURATION_OF_ISOLATION 10
MEAN_NETWORK_SIZE 9.0
PROB_APPLY_FOR_TEST_IF_SYMPTOMS 0.75
PROB_GET_TEST_IF_TRACED 0.75
PROB_INFECT_IF_TOGETHER_ON_A_DAY {'SARS-CoV-2': 0.025, 'B.1.1.7': 0.039}
PROB_ISOLATE_IF_SYMPTOMS 0.75
PROB_ISOLATE_IF_TESTPOS 0.3
PROB_ISOLATE_IF_TRACED 0.3
PROB_NON_C19_SYMPTOMS_PER_DAY 0.01
PROB_SYMPTOMATIC 0.6
PROB_TEST_IF_REQUESTED 1
PROB_TRACING_GIVEN_CONTACT 0.6000000000000001
SIMULATOR_PERIODS_PER_DAY 1
TEST_DAYS_ELAPSED 1
VACCINATION_IMMUNITY {'AstraZeneca': {'SARS-CoV-2', 'B.1.1.7'}, 'Pfizer': {'SARS-CoV-2', 'B.1.1.7'}}
_PROPORTION_OF_INFECTED_WHO_GET_TESTED 0.44999999999999996
_TARGET_R0 1.4<jupyter_text>We are going to work with a small town of a few thousand people.<jupyter_code>pop = CityPopulation(560000, society.Society())<jupyter_output>2021-03-28 19:59:40,688 INFO:Building a set of 224000 households from which to build a population
2021-03-28 20:00:33,047 INFO:220051 households of mean size 2.54
2021-03-28 20:00:35,895 INFO:101252 buildings of mean size 5.53
2021-03-28 20:01:00,548 INFO:1461 classrooms of mean size 28.87
2021-03-28 20:01:00,792 INFO:99 care_homes of mean size 105.68
2021-03-28 20:01:01,845 INFO:65449 workplaces of mean size 5.62
2021-03-28 20:01:07,712 INFO:0% of workplaces closed by lockdown, leaving 54869 open, of average Income Decile 5.07 (and st dev 3.13).
2021-03-28 20:01:07,878 INFO:0% of classrooms closed by lockdown, leaving 1185 open, of average Income Decile 4.75 (and st dev 3.10).
2021-03-28 20:01:07,912 INFO:Adding 276204 permanent contact groups
2021-03-28 20:01:08,060 INFO:Adding 28000 ephemeral contact pairs
2021-03-28 20:01:08,845 INFO:Adding 168417 contacts each within one of the 101252 buildings (contact density of 0.75)<jupyter_text>Randomly, we put them into fixed and overlapping social groupings, where each person has a small network.<jupyter_code>nets = [len(p.contacts) for p in pop.people]
np.mean(nets)
plt.hist(nets, cumulative=True, density=True, bins=2000)
plt.title('Distribution of network sizes')
plt.axvline(np.mean(nets), color='r')
plt.grid()<jupyter_output><empty_output><jupyter_text>Finally ready to simulate: We will place the population that we have created, into various settings and societies in the upcoming simulations<jupyter_code>POP_SIZE = len(pop.people)
PREVALENCE = 1/560 * 4
SCALE_SETTINGS = dict(n_days = 201, pop_size = POP_SIZE, seed_size = int(POP_SIZE*PREVALENCE), population=pop)
SCALE_SETTINGS<jupyter_output><empty_output><jupyter_text>Our baseline simulation is of a runaway infection.We start with 400 people infected in a population of 56,000.We begin by studying a society where people don't know whether or how to self-isolate:<jupyter_code>s_basic = society.Society(config=dict(PROB_ISOLATE_IF_SYMPTOMS = 0))
o_basic = Outbreak(s_basic, Covid(), **SCALE_SETTINGS).simulate()
o_basic.plot(title=str(SCALE_SETTINGS))<jupyter_output>2021-03-28 20:38:36,570 INFO: Realized R0 of early infections is 1.48
2021-03-28 20:38:36,570 INFO: 56.8 percent of the population was infected during the epidemic<jupyter_text>Lets put that on a log scale:<jupyter_code>o_basic.plot(logy=True, title='Non-isolating society: doubling time of about 15 days')<jupyter_output>2021-03-28 20:38:40,830 INFO: Realized R0 of early infections is 1.48
2021-03-28 20:38:40,831 INFO: 56.8 percent of the population was infected during the epidemic<jupyter_text>Next, suppose that people know to isolate if they show symptoms, and 75% do so - this is similar to what is going on in the UK now:<jupyter_code>s_isolate = society.Society(config=dict(PROB_ISOLATE_IF_SYMPTOMS = 0.75))
o_isolate = Outbreak(s_isolate, Covid(), **SCALE_SETTINGS).simulate()
o_isolate.plot(title='Isolating society: small but nasty wave')<jupyter_output>2021-03-28 20:45:01,048 INFO: Realized R0 of early infections is 1.14
2021-03-28 20:45:01,049 INFO: 27.5 percent of the population was infected during the epidemic<jupyter_text>So, now we can add testing: * initially, here, lets suppose that positive test results are just ignored, while -ve results let people out of isolation:<jupyter_code>s_testignored = society.TestingSociety(config=dict(PROB_ISOLATE_IF_TESTPOS=0))
o_testignored = Outbreak(s_testignored, Covid(), **SCALE_SETTINGS).simulate()
o_testignored.plot(title="Testing, but +ve results ignored: in a sense this is counterproductive \n"
                         "(-ve result puts people back into society, and into harm's way)")<jupyter_output>2021-03-28 20:54:03,948 INFO: Realized R0 of early infections is 1.21
2021-03-28 20:54:03,950 INFO: 31.9 percent of the population was infected during the epidemic<jupyter_text>* Now suppose that people respond to test results, some of the time:<jupyter_code>o_test = Outbreak(society.TestingSociety(), Covid(), **SCALE_SETTINGS).simulate()
o_test.plot(title="Testing, paid attention to a bit")<jupyter_output>2021-03-28 21:03:20,224 INFO: Realized R0 of early infections is 1.16
2021-03-28 21:03:20,225 INFO: 30.5 percent of the population was infected during the epidemic<jupyter_text>We add contact-tracing and isolation:<jupyter_code>o_test_trace = Outbreak(society.TestingTracingSociety(), Covid(), **SCALE_SETTINGS).simulate()
o_test_trace.plot(title='Testing, tracing, and isolating', secondary_y=['prop_infected'])<jupyter_output>2021-03-28 21:13:00,473 INFO: Realized R0 of early infections is 0.97
2021-03-28 21:13:00,474 INFO: 10.1 percent of the population was infected during the epidemic<jupyter_text>UK society, however, is characterized by testing bottlenecks:<jupyter_code>import codit.society.alternatives as alternatives
o_UK = Outbreak(alternatives.UKSociety(), Covid(), **SCALE_SETTINGS).simulate()
o_UK.plot(title='UK society with TTI bottlenecks - people isolate for longer')
o_contact_test = Outbreak(society.ContactTestingSociety(), Covid(), **SCALE_SETTINGS).simulate()
o_contact_test.plot(title="Testing, tracing&testing&isolating: "
                    "Also testing contacts doesn't make much difference", 
                    secondary_y=['prop_infected'])
census = pop.census
infector_nets = [len(census[p.infectors[0]].contacts) for p in pop.people if p.infectors]
infected_nets = [len(p.contacts) for p in pop.people if p.infected]
def most_connected_infector(guy):
    if len(guy.infectors) == 0:
        raise NotImplementedError
    return max([len(i.contacts) for i in guy.chain(census) if i is not guy]) 

max_contacts_chain = [most_connected_infector(person)
                      for person in pop.people 
                      if len(person.infectors)]
opts = dict(cumulative=True, bins=200, density=True, histtype='step')

plt.hist(nets, color='k', **opts)
plt.hist(infected_nets, color='r', **opts)
plt.hist(infector_nets, color='b', **opts)
plt.hist(max_contacts_chain, color='g', **opts)

plt.title("CDFs of valency for: people (black); infected (red); infectors (blue); max connected in chains (green)")
plt.axhline(1, color='k'); plt.axvline(0, color='k')
plt.grid()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10744416873449132, 'fraction_numerical': 0.08449131513647642, 'mean_word_length': 6.216651745747538, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '96849', 'license': 'MIT', 'n_tokens_mistral': 3389, 'n_tokens_neox': 2873, 'n_words': 752, 'path': 'share/codit/notebooks/overview.ipynb', 'repo_name': 'jeremy-large/town_covid_simulator', 'size': '8060'}