Rule-based epidemic models

This paper gives an introduction to rule-based modelling applied to topics in infectious diseases. Rule-based models generalise reaction-based models with reagents that have internal state and may be bound together to form complexes, as in chemistry. Rule-based modelling is directly transferable from molecular biology to epidemiology and allows us to express a broad class of models for processes of interest in epidemiology that would not otherwise be feasible in compartmental models. This includes dynamics commonly found in compartmental models such as the spread of a virus from an infectious to a susceptible population, and more complex dynamics outside the typical scope of such models such as social behaviours and decision-making, testing capacity constraints, and tracing of people exposed to a virus but not yet symptomatic. We propose that such dynamics are well-captured with rule-based models, and that doing so combines intuitiveness and transparency of representation with scalability and compositionality. We demonstrate this feasibility of our approach using a suite of seven models to describe a spread of infectious diseases under different scenarios: wearing masks, infection via fomites and prevention by hand-washing, the concept of vector-borne diseases, testing and contact tracing interventions, disease propagation within motif-structured populations with shared environments such as schools, and superspreading events. The machine-readable description of these models corresponds closely to the mathematical description and also functions as a human-readable format so that one knows readily"what is in the model".


Introduction
Compartmental models in epidemiology are mathematically equivalent to, and can be expressed in the same way as, chemical reaction models. The classical model of Kermack and McKendry [1], for example, can be written, (1) This represents infection as an interaction between a susceptible individual and an infectious one that results in two infectious individuals at rate βc N and the recovery or removal of an infectious individual at rate γ. Kermack and McKendry derive differential equations for the case where the rates are constant from first principles and arrive at a system describing the changing quantities of individuals of each kind. These differential equations are identical to those obtained by considering the above as a chemical reaction system, interpreting S, I, and R as chemical species in place of exclusive subpopulations -it is perhaps not a coincidence that Kermack was a biochemist. This class of model is still in current use [2,3,4]. To represent the natural history of a particular disease, compartments may be added. It is common to add a latent compartment for those individuals that are infected but not yet infectious. Sometimes several kinds of infectious compartments are used to represent different severities or stages of disease progression. However, in typical differential equation modelling, increasing the number of compartments comes at the cost of poor scaling: the number of possible interactions increases with the square of the compartments. It is just possible to accommodate the compartment explosion for agestratified models [5]. Dividing the population into 8 age bands requires 64 interactions to capture infection and 8 more for removal. All of the other transitions for disease progression, from latent to infectious, among the various severities of infectiousness, result in a modest increase of 8 each.
Explicitly enumerating stratified compartments begins to become unwieldy when other features that would arbitrarily subdivide the population further, or when multiple geographic regions [6] are considered. Isolation, removal from, or attenuation of participation in the infection dynamics as a result of testing, clinical diagnosis, or simple precaution, induces a doubling of compartments: we must account for the possibility of both isolated and unconfined individuals of each kind. The wearing or not of masks has a similar consequence, as does introducing any feature that subdivides the population, or includes multiple populations. For example, when we consider the simplest Susceptible-Infected-Recovered (SIR) model, it scales as shown in Table 1.  The reason for this large increase in the number of compartments and required transition rates is easily seen. This formulation requires compartments to represent disjoint subsets of the population, and this, in-turn, implies redundant specification of interactions where they are independent of the features. The progression from latent to infectious, for example, is independent of whether or not one is wearing a mask but one nevertheless must specify these cases separately; interactions with peers at school have little to do with the structure and composition of one's family (at least to a first approximation).
This phenomenon also has a negative effect on the ability to inspect and understand reaction-based models. Even if a large model with dozens of compartments and hundreds of reactions is correct, and even if it is available for inspection, there is little hope of understanding the reasoning behind the model. There is also little hope of verifying that the model as written in code is the same as the model that is written in the paper about the model (or, rather, that both are representations of the same abstract model).
There are several strategies used in epidemiological modelling to make some progress in the face of the scaling difficulties posed by adding features to compartmental models [7]. A simple approach is to assert that the additional features that do not alter the structure of the model, e.g. that wearing a mask, for example, reduces the infection rate [8,9] or that contact tracing causes infectious individuals to become isolated at some rate [10,11]. Doing this is not to study contact tracing or masks and the interactions of individuals wearing them, or not, but to presuppose that the effect of these interactions is uniform and can be captured in a single scalar parameter. A more sophisticated approach is to forego the elegance of the chemical reaction or compartmental formulation entirely and explicitly model the individuals in the population as agents interacting arbitrarily as in individual-or agent-based models [12,13,14,15,16,17]. This has the opposite problem: where reducing interactions to a scalar is oversimplification, allowing completely arbitrary interactions brings with it little analytical or structural insight. Agent-based models also have the drawback of needing to specify a large number of assumptions. The quantity of assumptions often implies too many parameters to be reasonably informed from data or fitting. The shortcomings of both of these strategies are a result of the choice of level of abstraction: one too coarse, and the other too fine.
In this paper, we present an alternative approach and show that rule-based modelling [18], already used in modelling molecular biology [19,20,21,22], can be used to express scalable and compositional models in a wide range of relevant epidemiological scenarios.
The advantage of rule-based modelling is that allows for explicit representation of entities in a model and their interactions while disregarding features that are not relevant. The formalism is also parsimonious: minimal extraneous detail is required to specify the model in machine-readable form for simulation. The approach is also transparent: the machine-readable form corresponds closely to the mathematical form resulting in minimal barriers to inspection and verification of models. As we demonstrate in the paper, this allows to compose, run and verify computational models and obtain insights for relevant epidemiological scenarios such as the effects of mask wearing in the transmission of respiratory illnesses, passive transmission by fomites on surfaces or by active vectors such as mosquitoes, testing, tracing and isolation, as well as populations with a hybrid well-mixed and network structure, and superspreading events at gatherings. All of the models described in this paper are available at https://git. sr.ht/~wwaites/rule-epi.
The main contribution of this paper is then to provide a new arrow in the quiver of epidemiological modelling. Rule-based modelling is expressive enough to capture features of disease transmission and interventions that would be impractical to represent in compartmental models. At the same time, the language is sufficiently clear to make the individual mechanisms and interactions explicit and subject to examination and review in a way that is rarely feasible even with the best agent-or individual-based models. We demonstrate the proposed approach by presenting specific models for various phenomena of interest for infectious disease modelling.

Rule-Based Approach
The chemical master equation gives the time-evolution of the distribution of configurations of such a system, the trajectory of distributions [23,24]. Differential equation formulations such as the one derived by Kermack and McKendry approximate the mean number of each chemical species as a function of time, and this approximation becomes increasingly accurate as this number goes to infinity. There exist methods for obtaining approximate differential equations for the higher moments as well. Rule-based modelling generalises this by allowing chemical species, rather than being atomic entities, to have internal structure and bonds between particles.
Rule-based formulations, like reaction-based ones have a useful property: compositionality [25,26]. One can derive differential equations from reactions using the rate equation, a sum over all reactions [27,28]. Adding rules is simply adding more terms to this sum (the same is not true for reactions because it is necessary to account for each combination of reagents). This compositional property of rule-based models means that it is possible to design models in such a way that they can be combined. For example, one may combine a model of the flu with one of covid-19, for example, by simply concatenating them. This is powerful capability has been emphasised in the closely related Petri net formulation [29,30]. The advantage of rules over chemical reactions in this connection is ease of variation: a single change to a rule can cascade to many changes in the corresponding reactions [31].
The entities in rule-based modelling are called agents. These agents should not be confused with the agents as they are in agent-or individual-based modelling in the epidemiology literature; they are much simpler and they have a precise definition [18]. There are several computer languages for writing rule-based models, the most well-known are the κ language as implemented by the KaSim [32] simulator and the BioNetGen language [33]. We will use κ, and introduce the main features of the language here.
An agent with internal states is specified as follows -in text and equivalently in the language of KaSim, The meaning of this is that there is a set of agents, P () that is partitioned into disjoint subsets, P (x S ), P (x E ), P (x I ), P (x R ). This agent, P , might refer to a population made up of individuals whose internal state x corresponds to the compartments of an SEIR model -a standard extension of SIR with an additional latent or exposed compartment whose members are infected but not yet infectious.
It is permitted to have more than one kind of internal state. For example, one could write, to represent wearing or not of masks. One can then refer to those infectious individuals wearing masks, P (x I , m Y ), all individuals not wearing masks, P (m N ) = u P (x u , m N ), or all individuals, P () = u v P (x u , m v ). This is a fundamental difference between rule-based models and compartmental or reaction models: one can refer to specific subsets of agents as required, and those internal states that are not relevant can simply not be mentioned. It is also possible to specify bonds between agents. This is extensively used in molecular biology to represent polymers, chains of molecules. Here, we will make light use of this facility to show how a population can be given structure. As with internal states, binding sites have names, and bonds are numbered.
The number is arbitrary and meaningful only within the scope of an expression. Thus, P (c 1 ), P (c 1 ), denoting two bound agents, means the same thing as P (c 42 ), P (c 42 ), provided that 42 is not used elsewhere in the same expression. An unbound site is written with a ·: P (c · ).
A rule, much like a chemical reaction, has a left-and a right-hand side. A rule for infection, again both in text and in the language of KaSim, is, This is very similar to the chemical reaction representation in Equation 1, and the formulation in code corresponds exactly to the more attractively typeset mathematical version.
A convenient shorthand, with no change of meaning, useful when the only difference between the leftand right-hand side of a rule is a change of state, is the edit notation, The output of such models is defined as a set of observables. An observable is a function of time, and is specified in terms of arithmetic operations on the cardinalities of the sets of agents. For example,

number of infectious individuals number of infected individuals fraction wearing a mask
The KaSim stochastic simulator samples trajectories [34] from models written in this language. The corresponding differential equations can be generated for integration using GNU Octave, Matlab, Mathematica or Maple with the KaDE program for settings where this is appropriate and stochastic effects can be neglected. This means a large class of epidemiologically interesting models typically expressed as ODEs can be written, often more compactly and elegantly, in rule-based form.
As said, with some assumptions, this rule-based form admits composition. Compositionality is a very convenient property for models: it means that they can be easily combined [25,26]. Provided that all sites and internal states of agents are consistent, composition of rule-based models is simply concatenation. This follows from the form of the master equation as a sum over rules. Care must still be taken that the intended semantics are obtained when composing models. A duplicate rule will obtain at twice the rate that it otherwise would, and this may or may not be the intent. Rules applying to partly overlapping subsets of agents present a similar but less obvious difficulty for composition, which can be solved with rule refinement [35]. With due care, even without using rule refinement, it is possible to create modular models that can be combined to create more complex ones.
The caveat with producing differential equations is that each complex of agents must be treated as a chemical species. If agents can become bound together in arbitrarily large complexes, this results in arbitrarily many chemical species and a combinatorial increase in the corresponding number chemical reactions. To manage this, complexes must be truncated at a certain size if differential equations are to be used. For this paper, we use the stochastic simulation for all models.

Modelling Epidemics with a Rule-Based Approach
We describe the rule-based approach and language by presenting six models representing phenomena of interest in infectious disease modelling and that feature different aspects of compositionality and scalability: 7. Gatherings where subsets of the population are more or less likely to periodically attend gatherings at which contact frequency is much greater than normal.
Each model is described and simulated for reasonable illustrative values of the relevant parameters.

Masks
We develop a model for mask wearing that uses the above agent and show how the proposed rule-based language allows extensions with the compositional addition of new features.
The corresponding code is provided in Appendix Appendix A. The progression rules are exactly the same as with a regular compartmental model, The infection rules are very much like a stratified compartmental model: we explicitly specify the four combinations: where the susceptible individual is wearing a mask, or not, and where the infectious one is, or not. Let 1−m xy be the effectiveness of mask wearing at preventing infection of a susceptible individual, during a contact with an infectious individual, with x, y indicating the no-mask/mask status of said individuals. For example, stipulating that: m N N = 1, and there is no reduction in infection probability; while m Y Y = 0.2 means a very substantial reduction in infection probability if both parties wear masks. If only one is wearing a mask, the benefit is relatively small if it is the susceptible individual and significant if it is the infectious one. The four rules are then given as, where β is the infection probability, c is the number of contacts per unit time, and N is the total population, as usual.
In addition to the above, which might be sufficient to study the circumstance where a constant fraction of the population wears masks, we incorporate a simple mechanism for mask wearing to become popular. We suppose that the more people wear masks, the more likely it is for individuals to decide to wear and not to wear masks, This is a purely crowd-based logic and the result of this positive feedback is like an epidemic of masks. It eventually results in the entire population wearing masks. This is clearly unrealistic when the outbreak has run its course. Therefore, we use a second rule with negative feedback but a different rate constant, It is possible to notice that if µ > ν and there is at least a small number of individuals wearing masks, then mask usage will simply grow at a rate of µ − ν, but if the opposite is true, masks will fall to zero. More than a simple logic of following the crowd is needed. We reason that, in addition to observing the behaviour of others, our agents also have access to information about the outbreak itself, perhaps from watching the nightly news, and spontaneously decide to wear or remove a mask proportionally to the current danger, where p I = |P (x I )| N , or the chance at the current time of any given individual being infectious. Remark: the above four rules show two ways of having a rate proportional to a subpopulation. The first is to have a bimolecular rule and a constant rate. The second is to have a unimolecular rule and a variable rate. To achieve a variable rate a calculation is performed after each event. The difference is largely a matter of taste as the simulator performs substantially the same computation when incrementally computing propensities [36].
There remains the question of how to choose the rates µ and ν and for present purposes we will select them somewhat arbitrarily with wearing masks significantly faster than removing them. An alternative formulation not requiring this arbitrary choice involves using memory of the recent past, a technique that we also use for contact tracing in Section 3.5.
The result of running this model as described, and setting σ = 0.5, are shown in Figure 1. With these simple assumptions about the effect of wearing masks, and a direct implementation of the relevant interactions, we can see that they do have a significant effect on reducing both the peak number of infections and the total. We can also observe that the system settles at an equilibrium of mask wearing. It is possible to work out precisely the nature of this equilibrium. Since there are, at equilibrium, very few infectious individuals, there is very little spontaneous mask wearing, and spontaneous removal happens at a rate of σ. Masks are also removed due to the crowd logic of Equation 8. At equilibrium these two processes must balance with the crowd logic of Equation 7 causing masks to be worn.

Hand washing
Infection due to contact surfaces contaminated by viral particles that have been shed (fomites) is said to be mitigated by hand washing. We model this phenomenon as follows with code in Appendix Appendix B. Individuals in this model have hands. Hands can be clean or dirty. They become clean through washing, and become spontaneously dirty after some time. Our agents, therefore, have the signature, The washing and dirtying of hands are described by the rules,  Individuals wear masks according to a purely crowd-based logic: the more individuals are wearing them, the more likely it is for one to make the transition from not wearing to wearing a mask and vice-versa. This is supplemented with spontaneous mask wearing and removal proportional to the fraction of the population that is infectious.
where ω is the rate of hand washing, and τ is the rate at which hands become dirty. Contamination of surfaces is straightforward and the logic is very similar to infection in the standard model, This is read as, any surface coming into contact with an infectious person with dirty hands becomes contaminated. This happens at a rate κ of contact with surfaces and proportionally to the fraction of the population that is infectious with dirty hands. Decontamination is a degradation rule, where δ is the rate of surface cleaning or fomite degradation. The interpretation of this is left open, it may simply be that the surface contamination becomes incapable of transmitting the virus or that it is cleaned every δ −1 time units. The infection process is similar to the standard model, though it is a consequence of interaction with contaminated surfaces rather than infectious individuals, here, κ is again the rate of contact with surfaces, and the rate of infection is proportional to the fraction of contaminated surfaces. The factor β is, analogously to the standard model, the probability of becoming infected upon exposure to a contaminated surface.
The rules for progression of the disease are exactly as for the model for mask wearing in the previous section.
We can see in Figure 2 the effect of hand washing with plausible values for the rates. The number of shared surfaces is taken to be equal to one quarter of the population, and individuals come into contact with them twelve times per day. For the case with hand washing, hands are washed relatively frequently, eight times per day and become dirty at twice that rate. Surfaces become decontaminated after four hours. Hand washing results in a substantial reduction in the number of contaminated surfaces which, in turn, causes a much smaller peak in the number of infections and a lower number of cumulative infections.   Note also that exactly the same model, though likely with different rate constants, is applicable to a scenario of transmission by aerosol. This simply requires reinterpreting "surface" as "indoor location" since these locations become contaminated through the presence of infected individuals and the aerosols disperse after some time. A slightly more sophisticated treatment that includes the effect of masks analogously to the previous section is left as an exercise for the reader.

Vectors
Animate vectors of disease transmission such as mosquitoes may have a life-cycle much shorter than the duration of a disease outbreak. It would make sense to simply assume a constant population of vectors that becomes carrier of disease and then ceases to be a carrier -this may in fact be the case in some circumstances. However, for purposes of exposition, we choose to represent the birth and death cycle of the vector explicitly. Here, our agents will be, where the individuals simply have the states corresponding to disease progression, and the vector may be susceptible or infectious. We will just use a birth process depending only on the number of individual vectors for simplicity and incorporate a vector control strategy, or in other words, a vector reproduces at rate k b , and all offspring are in the susceptible state regardless of the parent's infection status. There is no transmission through reproduction among vectors, though that would be trivial to do. The factor, w, represents the destruction of breeding habitat and is allowed to take on values in [0, 1]. If the vector is a mosquito, this could represent the fraction of the quantity of standing water at the beginning of the simulation that is allowed to remain. The death process is very simple and just happens at a constant rate, k d , In this model there are two kinds of infection process: people by vectors and infection of vectors by people. These happen with probabilities β and β respectively. Let M = |V | analogously to N = |P |, and we write for these rules, where κ is the frequency at which a vector contacts (e.g. bites) the host. As before, disease progression for the host is unchanged from the above models.     There are clearly elements of this scenario that could be modelled in more detail. An interesting obser-vation, implicit in the birth-death process here, would be the return of vectors to the susceptible state at some rate. This could be made explicit with a V (x I ) → V (x S ) rule. Offspring of vectors could, as mentioned above, inherit susceptibility or infectiousness from the parent. This scenario would then represent two coupled epidemiological models: an SEIR model for the host and an SIS model for the vector. These extensions, as before, are left as an exercise.
The code for this model is in Appendix Appendix C.

Testing
The presented rule-based approach can also allows to express a more sophisticated model of testing than is normally found. In this model, tests, T () are discrete units that are manufactured at a constant rate m and are consumed on use. This has an advantage over a representation where tests are simply asserted to be performed at some rate because if a test is not available, it cannot be performed. Explicitly representing tests as a participant in testing reflects important considerations of manufacturing and the supply chain. The manufacturing rule is simple, and the tests have a characteristic recall (true positive rate), r and specificity (true negative rate), s. Upon a positive test, an individual becomes isolated and can no longer infect others. In addition to the usual disease progression states and the quarantine state, we also endow individuals with a "testable" state. This last has no additional meaning and is true if and only if an individual is infectious. Its use is to allow for a more compact representation of the testing rules. This is an example of where, far from adding complexity, the judicious addition of states can actually simplify a model. Our principal agent has the signature, Our progression and removal rules, while simple, are no longer the same as in the previous models because they govern membership in the testable set. We write, in other words, at the same instant that an individual becomes infectious, they also become subject to correctly testing positive. As they are removed through recovery or death, they are no longer subject to correctly testing positive.
Infection is similar to a standard SEIR model with the caveat that it can only take place among unconfined individuals, Note that testability is not mentioned and isolation status is not changed. This is exactly the standard infection rule applying only between the unconfined subsets of P (x S ) and P (x I ).
There are four testing rules corresponding to the four possibilities of true positives and negatives and false positives and negatives. For realism, we suppose that there is a random testing rate, θ 0 , for sampling the population, and a targetted testing rate, θ I , for individuals that are infectious. This is justified by the fact that infectious individuals are frequently symptomatic, perhaps requiring medical care, and so they are specifically tested. Because infectious individuals may also be randomly testing, the effective testing rate for them is, where the third term on the right hand side corrects for double-counting as we do not suppose that these individuals will be tested by both methods. For present purposes, only unconfined individuals that participate in disease propagation will be tested.
Our four testing rules are, therefore, These are, in order, true negatives, false negatives, true positives and false positives. Note that the test, T (), is consumed in this process and does not appear on the right-hand side of any of the rules. The reason for introducing the extra testable state is evident: it lets us write these testing rules in terms of the relevant feature. If we had not done so, it would have been necessary to write separate true and false negative testing rules for each of S, E, R, resulting in eight rules in total rather than four. Finally, those individuals that became isolated when uninfected, or who have recovered in isolation, exit to the unconfined state at a rate which we take without loss of generality to be equal to the infectious period, which completes this model. The corresponding code is reproduced in Appendix Appendix D. Figure 4 shows example trajectories of this system under conditions of low and high production of tests. For the top row, tests are manufactured at a rate sufficient to test 2.5% of the population daily. For the bottom row there are enough tests for 5%. An evident significant difference is the effect of false negatives with increased testing. In the bottom row, the majority of isolated individuals are, in fact, susceptible or recovered and not infected. The testing regime has a relatively high false positive rate of 20% and because the majority of the population is initially susceptible, there are more of them isolated than the other population subsets. As the outbreak progresses, more individuals are in the removed subset and they become isolated in proportion to the fraction of the population that they make up. In the top row, infectious individuals initially dominate as there are insufficient many tests to randomly sample the population. These are also insufficiently many tests for isolation due to testing to contain the outbreak, so, as it progresses, those who have recovered form the majority of isolated individuals, again due to false positives in testing, simply because they form the largest subset of the population.

Tracing
The final model in this paper which demonstrates the flexibility of the proposed rule-based approach is for contact tracing.
It builds upon the previous testing model because testing triggers contact tracing. It employs a slightly generalised version of the technique used in our previous work [37] that functions as follows. Suppose that each time contact with an infectious individual occurs, a trace is left behind. These traces follow individuals through the disease progression, and eventually degrade. We represent these traces as the agent, and we use the same agents T and P from the previous model. The progression rules for individuals are likewise the same, and to then we add straightforward equivalents for the traces, along with a degradation rule, Contact is, however, now more complex as we need rules for all contacts with infectious individuals, not only those that result in infection. All of these contacts produce traces, but only those which result in infection   change the state of the individual contact, Tracing is an operation that consumes a trace. Individuals may be traced whether or not they are isolated, and are traced in proportion to the fraction of the isolated population: those who have been isolated due to a true or false positive test have their contacts traced. The tracing rules, therefore, are, for u ∈ {S, E, I, R}, and where η is the tracing efficiency, or the number of contacts per unit time that are expected to be traced. Note that this formulation of tracing differs from that of our previous work [37] in two main respects. First, tracing is somewhat recursive: becoming isolated due to tracing also causes contacts to be traced. If recursive tracing is not required, it is sufficient to add a state that records test results. Secondly, here, the rate of being traced is proportional to the number of infectious contacts one has experienced in the time window before the traces degrade. If tracing happens at a constant rate per infectious individual, then having had contact with two such individuals should result in being traced more quickly. There is no mechanism, however, to distinguish between multiple contacts with the same infectious individual and contacts with multiple infectious individuals. In our previous work, tracing happens in proportion to the likelihood of having had at least one infectious contact which may tend to underestimate the influence of contact tracing on containment of outbreaks. The model given here is more eager and this may lead to overestimation the effect of tracing. It is not obvious which model most closely resembles the reality of contact tracing.
(b) Exposed and infectious population Figure 5: Isolation through testing and contact tracing. Note the different layout from previous figures in order to obtain better agreement of scale. On the left are susceptible and removed individuals, both isolated and not. On the right are infectious and exposed individuals, both isolated and not. The parameters are as with those for the scenario with testing alone, identical background rates and targeted rates of testing, and identical 80% accuracy. Here, manufacturing is even more constrained: only 100 tests/day are produced. Contact tracing is performed efficiently with 90% of contacts traced in 48 hours. Figure 5 shows this model performing under less optimal testing conditions than previously. That is, the tests are identically 80% accurate and the aspirational sampling and targetting testing rates are the same. The testing rates are aspirational because manufacturing is even more constrained: only 100 tests produced per day. This poor provision of tests is supplemented with a good contact tracing regime as described above with η = 0.45 meaning that 90% of contacts are traced, on average, within two days. A striking feature is the large and slowly degrading number of susceptible individuals that are isolated due to contact tracing. This is a consequence of the fact that it is far less likely to become infected due to contact with an infectious individual than to escape infection. These contacts are nevertheless traced, resulting in many susceptible individuals becoming isolated. This number is sufficiently large that it appreciably depletes the susceptible pool, rapidly slowing propagation of the disease.

Schools
Our next example, reproduced in code in Appendix Appendix F and shows how the proposed rule-based approach can express two coupled subpopulations: adults and children. The background environment is a well-mixed epidemic model such as we have seen above, with the usual progression rules and an infection rule attenuated with contact restrictions. Against this background, some structure is added: families. A family may consist of up to two children and up to two adults. The infection is transmitted much more easily within families; family members are in frequent close contact with one another. We also allow children to go to school, a second well-mixed environment. Though children spend only part of their time at school, contact with other children is much more frequent. Let us see how to represent this rather complex situation as a small number of rules.
We begin by defining the primary agent. It has the same infection states as above, as well as an internal state to identify as either a child or an adult. Additionally, there are three binding sites that permit the formation of child-parent or parent-parent bonds, We use fast binding rules to bind pairs of adults at the beginning of the simulation, and then we assign bind to pairs of adults, P (a C , e 1 1 , e 2 2 ), P (a A , e 1 1 , e 3 3 ), P (a A , e 2 2 , e 3 3 ) These three rules are sufficient to generate a small variety of family motifs: single individuals, childless couples, and couples with one or two children.
The main feature of these two rules is that the motifs that they produce are bounded in size. An alternative formulation would first associate children with adults and then preferentially associate the parents of the same children. This would allow for families with two children and three parents, or indeed arbitrarily large families in different configurations. Such a generative rule-set could be, P (a C , e · 2 ), P (a A , e · 2 ) ∞ − −→ P (a C , e · 2 ), P (a A , e · 2 ) (50) P (a C , e 1 1 , e 2 2 ), P (a A , e 1 1 , e · and may reflect human society more accurately. The distribution of motifs could be adjusted by using different large but finite rates rather than ∞.
The simulation substrate being a regular SEIR model, we have standard infection and progression rules that will be familiar from the foregoing sections, where is the factor by which lockdown distancing measures attenuate the normal propagation of the disease. The children in this simulation spend some time in school, a fraction, s ∈ [0, 1], of their waking day. While at school, they have contact with other children at an accelerated rate, κ > 1. This phenomenon will be familiar to any parent of a school-aged child. School is represented simply as a second mass-action infection rule that applies only to children.
Finally, infection does not spread within families with the same dynamic as in the general population. Family members are much more likely to pass the infection to each other. We use three rules for this: from  the child to each parent in proportion to the time they spend away from school, and between parents, P (x S , e 2 2 ), P (x I , e 2 2 ) That is the entire model: two compartmental-style infection processes coupled with a network epidemic model, expressed succinctly in 9 rules. The results at the level of the population are shown in Figure 6. The overall effect of schools being open is clear: the peak in infections is much larger, and the outbreak progresses more rapidly though there is little change in the cumulative infections (equivalent to P (x R )). The underlying mechanism is visible in Figure 7 where the adult and child subpopulations are presented separately. In particular, Figure 7b shows the curve for infectious children, P (x I , a C ), significantly leading that for infectious adults, P (x I , a A ). Schools, modelled as we have done here, are an accelerant of the outbreak. Of course, this is true in this case by construction: we supposed that a sub-population spends some time in circumstances where contact happens at a greater rate than in the general population. However such a representation would seem to correspond reasonably faithfully to reality.

Gatherings
Our final example is one kind of superspreading event. There are several scenarios in which such events can occur, driven by biological, behavioural, environmental factors or indeed happenstance [38]. This example is of the behavioural kind. The agents in this case are placed on a spectrum from "loner" to "socialite". The difference is the propensity to participate in "gatherings" which are daily events where the contact rate is much higher than normal. Whereas in previous examples, the disease was parametrised to have an infectiousness (β) comparable to what we expect from the 2019 novel coronavirus, here we use an contagion that is only half as infectious.
The agent in this simulation is declared as, The g site indicates whether the individual is participating in a gathering, and c is an integer scale from 1 to 10 of how social that individual is implemented using a counter. Beginning with some housekeeping, as the internal state of c will be initialised to zero, we very rapidly assign individuals uniformly to the social scale, for each n.
Progression of the disease are the standard simple rules, P (x E ) α − → P (x I ) and P (x I ) γ − → P (x R ), and we have a pair of infection rules, These are similar to the standard infection rule, though the rates are different. First c g c n , the contact rate at gatherings is much higher than usual, and the normalisation constant is not the entire population but only those that are gathering, or not as appropriate. This represents a true partition of the population into those that are gathering and interacting only with one another, and those that are not. There is no interaction between gatherings and the rest of the population while the gathering is taking place -those social creatures that gather become infected and take the disease home.
The remaining two rules describe joining and leaving a gathering, If k g is the maximum rate of joining gatherings, the rate at which Equation 63 occurs is scaled down according to the social predisposition of the individual. In the simulation k g is chosen such that the most social individuals are expected to gather once per day and k n such that they are expected to leave a gathering after an hour. The g is a binary parameter indicating whether a gathering is taking place or not. This is set and cleared with a pair of perturbations. It is set to 1 once per day and set to 0 after six hours. Figure 8 shows daily gatherings in a two week time period. Individuals spend an hour, on average, at a gathering, and at their peak, gatherings consist of about 2.5% of the population, though for a short time. The contact rate within a gathering is 10× the normal rate. This increase corresponds to a modest increase in the average contact rate of the most social individuals of a factor of 23 24 + 10 1 24 = 1.375. Only 10% of the population is that gregarious; on average these gatherings increase the contact rate by a factor of only 1.17.
The effect of the modest increase in the contact rate is amplified by the partitioning of the population. Individuals who are gathering interact at this elevated rate only with others who are also gathering. Being a small fraction of the total population, once one social individual is infected, if they are gathering, the chance of encountering them is proportionally higher: (0.025N ) −1 = 40N −1 . This phenomenon is readily apparent from Figure 9 where gatherings with the dynamics as described result in a doubling of the peak infectious individuals and a near doubling of the total infections.

Discussion
This study gives a primer for applying rule-based methods used in molecular biology [19,20,21,22] to a selection of problems in epidemiological modelling. Each of the models we have chosen would be very challenging to implement as compartmental models because of the scaling issues we discussed. They could all be implemented using agent-or individual-based techniques, but, we argue in this paper, not as clearly and parsimoniously as we have done here.
In fact, the scenarios described above highlight the features of the proposed modelling framework in terms of transparency and compositionality. The scalability of the approach become clear when the examples presented are expressed as reactions. Table 2 shows, for each example, the number of species or compartments and the number of reactions as well as the number of agents and rules required to capture the same dynamics. model   agents rules species reactions  masks  1  10  9  96  fomites  2  6  11  32  vectors  2  6  7  16  testing  2  10  12  38  tracing  3  21  16  170  schools  1  9  219  89098  gatherings  1  16  85  766   Table 2: Number of modelling entities required for the example models given here when treated as rule-based vs reaction-based models. Note that the schools model requires a number of species or compartments and reactions that is in principle unbounded.
All of the examples are substantially more succinct when written with rules. The simplest models, of fomites, vector-borne diseases and testing alone are only simpler by a factor of 2 or 3 and could feasibly be studied in reaction-based form. The other models, despite not being substantially more complex in rule-based form require orders of magnitude more compartments and reactions to capture the same dynamics.
Moreover, the examples provided show that rule-based modelling allows principled expression of interactions in readily-simulated way that is much easier to specify, and allow for greater flexibility in structure.
Rule-based models are also compositional meaning that they can be easily combined: with some semantic assumptions, combining models can simply mean concatenating their rules.
Rule-based modelling has previously been applied to address the limitations of traditional approaches for modelling chemical kinetics in cell signalling systems [19,20,21]. An attempt to develop a rule-based model for chronic-disease epidemiology has also been made previously [39], but the core methodology there was somewhat intertwined with agent-based models. As we have mentioned in the introduction, the approach that we have demonstrated here of constructing and simulating a chemical master equation, is different from agent-or individual-based modelling. It is also different from reaction-based models that have been considered for epidemiology [40] in that it manages combinatorial explosion well. Rule-based modelling provides a flexible and computationally efficient methodology that can easily be adapted, and expanded to answer existing and emerging questions in epidemiology.
The novelty of our work is in translating an established molecular biology modelling framework to epidemiological modelling, with a view to timely application to the COVID-19 epidemic.
The spread of the SARS-CoV-19 virus during 2020 causing a pandemic of COVID-19 across the world, has highlighted the importance of modelling in decision making. Modelling has been at the forefront of the discussion around imposition of social distancing measures and evaluation of different scenarios to relax them [41,42,43,44,45,46]. Having an appropriate model for the available data at every step of the growing epidemic is important and this requires variety of modelling approaches, each with different strengths and weaknesses [47,48,49]. Our contribution with this paper is to highlight another approach in modelling infectious disease spread -in this case borrowed from molecular biology.
We note that although we have demonstrated the expressive power of rule-based modelling, the examples given here are simply that: examples and represent a proof-of-principle. They are intended to show some scenarios that are detailed enough to be interesting but they are simple and consider specific phenomena in isolation. Each of these examples could usefully be elaborated and studied in greater detail. Because the mechanisms underlying infectious disease propagation and interventions can be explicitly represented, studying these and other examples as rule-based models is likely to yield important insights. Rule-based modelling is a powerful tool to gain a more detailed understanding of the dynamics of outbreaks and the options available for their management. Immediate future work is in applying these techniques to pressing questions from the covid-19 epidemic: how the detail of different testing and tracing strategies affects success, the role played by superspreading events, and the interplay between social dynamics and epidemic dynamics.
Rule-based modelling is not a panacea. There are several practical challenges to its adoption, and some kinds of model that are difficult to express.
First, the notation and approach is not well understood in epidemiology, and this requires a change in practice. There is potential for misunderstanding where key terminology -in particular the words "agent" and "compartment" are used in different senses by the different communities. We argue that the simplicity and elegance of representation, thinking simply in terms of simple individual interactions rather than the set of complicated differential equations that can be derived for them is sufficient to warrant the use of the rule-based representation. The benefit of understandable models, where a single description is suitable both for computer simulation and human digestion is substantial; it turns opaque science and makes it transparent. While there is some inertia and there is some cost to adopting this representation, we think the benefits are immense.
Second, this method is not a "drop-in" replacement for differential equation models. For maximum utility, further work on making the use of rule-based models in other systems easier would be valuable. Most epidemiology packages in Python or R implement a limited set of models, even those that are intended to allow use of varied structures. Adopting rule-based modelling is an easy way to make such packages far more extensible. It is possible to control stochastic simulations of rule-based models today Python, and it is possible to generate differential equations for solving using GNU Octave, Matlab, Mathematica and Maple. It would be useful to generate differential equations for solving in Python from rule-based models, and currently we are not aware of any interface for the R language, commonly used in epidemiological modelling. These are minor practical limitations, easily remedied with some straightforward work and not limitations in principle.
Finally, there are kinds of models that one would like to express that would require extension of existing rule-based modelling tools. True network models are also difficult to implement as binding sites can only have zero or one bonds. This means that, considering these bonds as edges in a graph, it is only possible to have vertices of finite degree and it is cumbersome to have vertices of more than very small degree. Addressing this limitation would require an extension to the core language and cannot be solved with code generation. Partitions (which we would like to call compartments but that would collide with the use of the word in epidemiology) between which agents are permitted to migrate and within which rules are scoped are cumbersome to express. The "gatherings" model of Section 3.7 does this, partitioning the population into those that have gathered together and those that have not, but it is easy to see that this would quickly become unwieldy for a large number of partitions. Spatial extensions to the κ language [50] exist that automate the process of generating rules for partitions of this kind. If the computational expense is tolerable it is possible to conduct rule-based simulations of spatial models.
We have shown that rule-based modelling has a major advantage in expressivity and compositionality over the current practice with compartmental models in epidemiology, and in clarity over individual-based models. This work brings a broad range of phenomena that are both interesting and important to understand within the scope of what can be studied in a systematic and principled way. We have demonstrated this by example, providing seven easy pieces: simple, yet interesting models that provide not only an illustration of the utility of rule-based modelling, but starting points for further study.  Figure 7: Schools are an accelerant. This is a more detailed view of the same scenarios of Figure 6 with the adult and child population shown separately. Note in particular the curve for P (x I , a C ) clearly leading the curve for P (x I , a P ) in Figure 7b. This is different from Figure 7d where the corresponding peaks occur at similar times. This shows that children attending schools are agents of infection of the wider population.