{'fraction_non_alphanumeric': 0.038546098729892606, 'fraction_numerical': 0.011056103000490328, 'mean_word_length': 4.5357674459329305, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 11, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Optimal adaptive testing for epidemic control: combining molecular and serology tests

The COVID-19 crisis highlighted the importance of non-medical interventions, such as testing and isolation of infected individuals, in the control of epidemics. Here, we show how to minimize testing needs while maintaining the number of infected individuals below a desired threshold. We find that the optimal policy is adaptive, with testing rates that depend on the epidemic state. Additionally, we show that such epidemic state is difficult to infer with molecular tests alone, which are highly sensitive but have a short detectability window. Instead, we propose the use of baseline serology testing, which is less sensitive but detects past infections, for the purpose of state estimation. Validation of such combined testing approach with a stochastic model of epidemics shows significant cost savings compared to non-adaptive testing strategies that are the current standard for COVID-19.


Introduction
A large literature in mathematical epidemiology has studied how to control and eradicate diseases by means of therapeutics and vaccinations, [Nowzari et al., 2016, Behncke, 2000. However, the influenza pandemic of 1918 and the current COVID-19 pandemic underscore the difficulty of such eradication in the case of virulent viruses, and have necessitated measures to reduce transmissions, for example with the use of face masks [Chu et al., 2020], social distancing and costly lockdown measures [Flaxman et al., 2020 [Di et al., 2020]. Another powerful tool to limit transmissions is early identification of infected individuals and epidemic hot-spots in local communities, which can both be accomplished by testing [Grassly et al., 2020, OECD, 2020. Nevertheless, during the COVID-19 pandemic testing resources have proven to be limited and expensive in much of the world [AACC, 2020, Apuzzo and Gebredikan, 2020, Mervosh and Fernandez, 2020; in the US, lack of testing capacity not only helped spread the virus but also led to the underestimation of the severity of the pandemic in the first half of 2020, [Fink and Baker, 2020]. This crucial role of testing notwithstanding, the question of how limited testing resources can be deployed to optimally control the spread of a pandemic has attracted relatively little systematic attention.
In this paper, we derive an optimal (dynamic) testing strategy in an SIR (Susceptible, Infected, Recovered) model of epidemics. Because undetected individuals may pass the disease to others and may be more likely to develop serious symptoms requiring hospitalization, we start by assuming that the number of undetected infected individuals has to be kept below a maximum i max at all times. We show that the optimal testing strategy takes a simple form: the testing rate has to be time-varying in order to satisfy the constraint, and takes the form of a most rapid approach path, [Spence and Starrett, 1975]. Namely, there is no testing until undetected infections reach i max , after which testing resources are used to keep infections at the threshold i max until infections decline naturally, bringing the pandemic to an effective close. The intuition for this result is that it is not worth using testing resources to keep undetected infections strictly below i max so long as the pandemic is still ongoing and infections cannot be brought down to zero. Hence, the best approach is to let the infection reach the threshold and then keep it there with a time-varying testing policy. Note that such optimal time-varying strategy is state-dependent, that is, the level of testing is dependent on the epidemic state (the current number of infected, susceptible and recovered individuals).
The second contribution of our paper starts by recognizing that the epidemic state needed to implement the aforementioned optimal testing strategy is typically hard to know precisely, as highlighted by the early stages of COVID-19 spread in the US. In fact, the most common qPCR tests for COVID-19, which are molecular tests based on detection of the virus' genetic material via quantitative polymerase chain reaction, may be ill-suited to obtain such aggregate information. These tests identify infected individuals only during a short window of time. For example, according to [Kucirka et al., 2020] the probability of COVID-19 detection via qPCR is above 75% in a window of roughly a week within active cases, while [Roche, 2020] gives a three weeks window for detectability of cases via qPCR. In contrast, serology tests, which detect antibodies produced by the immune system in response to current and past infections, identify infections during a longer window [Kubina and Dziedzic, 2020], but are typically less sensitive and thus received relatively less attention in the epidemic control literature. These problems are unlikely to be confined to COVID-19 and would probably recur in the event of future pandemics. To systematize these observations we study the effectiveness of both types of tests for identifying the epidemic state and find that serology tests, which have lower sensitivity but are faster, cheaper and can reveal past infections, offer a better alternative than qPCR-like tests (from here on termed molecular tests), which have high accuracy but a short-window of detection. Namely, we show that if the transmission rate in the SIR model is time-varying, then the epidemic state cannot be identified-is not observable-with molecular tests alone. Intuitively, just observing the flow of current infections may not be sufficient to distinguish the nonlinear dynamic evolution of the system due to different initial conditions versus different time-varying trajectories of transmission rates. Serology testing overturns this result, however, by providing a cheap way of estimating the stocks of past infected and recovered individuals. This is despite the fact that serology tests may have significant Type II errors because of low sensitivity (especially in the first stage -0 to 6 days -of infection, [Roche, 2020]). Indeed, we find that Type II errors, which can be very costly when the purpose is to diagnose individual infections, are not problematic for the purpose of estimating the epidemic state (which is aggregate information). For such purpose, detection of recovered individuals is a more important feature than high sensitivity. Hence, serology tests are an ideal complement to molecular tests for the purpose of estimating aggregate infections-an intuition that is formally established by our mathematical analysis.
In addition to our main analysis, we consider two important extensions. First, we study a variant of our baseline model of optimal testing in which both undetected and detected infections have to be kept below a maximum threshold. In this case, the optimal testing strategy is more complex. Nevertheless, we establish that the basic insights from our baseline analysis generalize to this problem. Second, we recognize that in reality the dynamics of epidemics are intrinsically stochastic. To confirm the robustness of the proposed approach, we apply our testing methodology to a stochastic continuous-time Markov chain model of epidemics and develop an extended Kalman filter [Khalil, 2015] to estimate the epidemic state in the presence of stochasticity. To this end, we exploit an expansion of the master equation governing the probability distribution of the Markov chain model [van Kampen, 2007, Gardiner, 2009] to derive a description of the epidemic dynamics in terms of a Langevin equation, whose mean coincides with the deterministic SIR model used for computing the optimal testing strategy. The covariance matrix of the noise term in the Langevin equation (which can be explicitly characterized as a function of the epidemic state) is then given as input to the extended Kalman filter to optimally incorporate new observations in the model predictions.
Our paper is related to the growing literature on SIR models, especially applied to the recent COVID-19 pandemic. Classic references on the SIR model and its applications to model epidemics include [Kermack and McKendrick, 1927, Daley and Gani, 2001, Diekmann and Heesterbeek, 2000, Keeling and Rohani, 2011, Andersson and Britton, 2012. Additionally, see [Pastor-Satorras et al., 2015] for a review of models of epidemic processes over networks and [Anderson andMay, 1992, Nowzari et al., 2016] for analysis and control of epidemic models. Several papers developed more general compartmental models for analyzing the spread of COVID-19 and examining the effects of interventions (see e.g., [Gatto et al., 2020, Atkeson, 2020, Stock, 2020, Cashore et al., 2020, Chinazzi et al., 2020). Other papers, such as [Paré et al., 2017, Hota and Sundaram, 2019, Paré et al., 2020, Hota and Gupta, 2020 analyzed the spread of epidemics over both timevarying and static networks.
Our work is more closely connected to a smaller literature that considers testing within this framework. [Alvarez et al., 2020, Acemoglu et al., 2020a, Brotherhood et al., 2020 consider testing in the context of optimal lockdown policies in SIR models (in the latter two papers with explicit recognition of heterogeneities across different age groups). Neither of these papers studies optimal testing, nor discusses the problem of identifying the underlying epidemic state, which is assumed to be known in this branch of the literature. [Acemoglu et al., 2020b] considers optimal testing in a simple model of disease percolation, but their focus is on the countervailing effects that testing creates by discouraging social distancing among certain groups of individuals. Moreover, their analysis is simplified by focusing on a non-SIR percolation model that enables explicit characterization and they do not discuss the issue of estimating the underlying epidemic state. [Drakopoulos and Randhawa, 2020] studies settings where accurate tests are not available in abundance. They show that moderately good tests provide enough information to have a positive social outcome, and that it is not optimal to wait for tests with very high accuracy. Similarly, [Larremore et al., 2020] compared molecular and antigen tests and found that test sensitivity is less important than testing frequency for screening purposes. [Kraay et al., 2020] suggests the use of serology testing to allow seropositive individuals (i.e., individuals with immunity) to increase their level of social interaction. They conclude through extensive simulations that serology testing has the potential to mitigate the impacts of the COVID-19 pandemic while also allowing a substantial number of individuals to safely return to social interactions and to the workplace. Information about the epidemic state of individuals obtained through both qPCR and serology tests is used in [Li et al., 2020a] to derive disease-dependent lockdown policies. [Vespignani et al., 2020] highlights the need for integrating seroepidemiological data into transmission models to reduce the uncertainty in the parameter estimates of clinical severity and transmission dynamics. [Behncke, 2000] studies the optimal testing policy when the objective is a weighted combination of the cost of infection and testing with no constraints on the state variables. They show that the optimal testing policy takes the maximal value until some time and then zero after. The key novelty of our work is to suggest the combined use of serology testing for the purpose of state estimation with molecular testing for optimal containment of the number of infected within a desired thresholds, together with an analytic derivation of the optimal adaptive testing rates.
In characterizing optimal controls in an SIR framework, our paper is related to a few other papers that study optimal lockdown policies in SIR models. These include [Miclo et al., 2020], which provides an analytical characterization of optimal lockdown policies in a setting where suppression is costly and there is an upper bound on the number of infections, representing a constraint on intensive care unit resources. Another related paper in this regard is [Kruse and Strack, 2020] which studies optimal social distancing measures to minimize a combination of the total health and economic cost of the infected population and the cost of reducing the transmission rate. The key difference of our work is that we focus on testing as a means to identify and isolate infected individuals instead of lockdown policies that impose a degree of isolation to an entire community.
Finally, because of its analytical focus, our paper is distinguished from a large number of recent papers that analyze intervention policies numerically (see e.g., [Zaman et al., 2008, Sharma and Samanta, 2015, Di Giamberardino and Iacoviello, 2017, Farboodi et al., 2020, [Gollier and Gossner, 2020], [Berger et al., 2020] among others). As detailed in the discussion section, we believe that the theoretical insights generated from our analysis of the SIR model highlight fundamental mechanisms and properties related to the use of testing as a tool for controlling an epidemic, that can then be generalized and refined to more sophisticated models of specific epidemics as typically done in the numerical literature above.

Optimal adaptive testing strategies
We model the progression of the epidemic in a population via a Susceptible-Infected-Recovered (SIR) model with three compartments corresponding to susceptible, infected and recovered individuals. Testing is introduced in the model by partitioning the infected compartment into infected individuals that have not yet been detected and are free to circulate, which we term infected-undetected (I u ), and infected individuals that have been detected and are therefore separated from the general population (e.g., quarantined), which we term infected-detected (I d ) (see Fig. 1A). Infected-undetected individuals infect susceptible ones with possibly time-varying transmission rate β(t), denoting the number of contacts per unit time multiplied by the probability that a contact leads to infection. For the purpose of controlling the epidemics, we assume that molecular testing (such as qPCR) is performed at rate θ(t) and has sensitivity η, i.e., infected individuals who get tested are detected with probability η. Individuals that test positive for the infection are quarantined and moved to the infected-detected compartment. Infected individuals become recovered with rate γ. The corresponding model equations are: where small letters denote the fraction of individuals in each compartment. We assume that the population size is constant with time, thus the last equation is redundant since Model for the derivation of optimal testing policies B. Model for state estimation ηθi u Figure 1: The deterministic epidemiological models used for the derivation of optimal testing policies (A) as discussed in Section 2.1 and for state estimation (B) as discussed in Section 2.2. Individuals in the population are divided in compartments according to their epidemiological state. In (A), Susceptible individuals (S) are infected via contact with infected-undetected individuals (I u ), which either transition to the infected-detected (I d ) compartment if they are tested positive for infection, or to the Recovered (R) compartment if they recover from the infection before being tested positive. Infected-detected individuals also recover from the infection. In (B), infected-undetected individuals (I u ) can transition to the infected-detected state by being tested positive via either adaptive molecular testing or baseline testing. Infected-undetected individuals can also transition to the recovered-undetected (R u ) compartment, if they recover from the infection before being tested positive or displaying symptoms. With serology baseline testing, recoveredundetected individuals can transition to the recovered-detected compartment (R d ) if they are tested positive for past infection. Infected-detected individuals transition directly to the recovered-detected compartment when they recover from the infection. Transitions between compartments are indicated along with the corresponding rates, orange arrows indicate transitions due to serology testing.
Subject to these dynamics, we aim at solving the following constrained optimization problem: In words, our goal is to design the optimal adaptive testing rate to minimize the total number of tests needed while controlling the epidemic so that the fraction of infectedundetected individuals always remains below a desired threshold i max 1. This constraint is motivated by two considerations. First, infected-undetected individuals circulate freely in the society and infect others, and thus a high number of such individuals would lead to a rapid takeoff in infections. Second, because they do not receive care, undetected infected individuals may later develop severe complications, and may need emergency intensive care unit (ICU) capacity, which has proven to be in short supply during the COVID-19 pandemic. The appropriate level of i max is a policy choice, and depends on several factors, including whether policymakers are intending to keep the reproduction rate of the pandemic below one and the maximum surge capacity of ICU resources. Our first main result provides a complete characterization of the optimal adaptive policy for problem (2). For simplicity we here discuss the case when the transmission rate β is constant (i.e., β(t) = β for all times). In this case, we say that the system reaches herd immunity when the epidemic state is such that s(t) = γ/β, as from that time on the number of infected-undetected individuals decreases even without testing. We prove an analogous theorem with time-varying monotonic β(t) in the Supplementary Materials. Theorem 1. The optimal testing policy θ † (t) for problem (2) with dynamics as in Eq. (1) and constant transmission rate β is described in three phases: 1. While i u (t) < i max , do not test, i.e., set θ † (t) = 0.
3. Once herd immunity is reached, stop testing, i.e., set θ † (t) = 0.  Figure 2: Illustration of the optimal testing policy for problem (2). The green and lightblue curves are, respectively, the fractions of susceptible and infected-undetected individuals in the population. The fraction of infected-undetected i u is kept below the constraint i max (black, dashed line) at all times by the optimal adaptive testing policy (red, dashed curve), which is equal to zero until i u reaches i max , is then equal to (βs(t) − γ)/η (yellow curve) until herd immunity is reached (i.e., when s(t) = γ/β), and is equal to zero afterwards. For illustration purposes, we set i max = 0.1 and η = 1, the other parameters are as in Table 1.
As illustrated in Fig. 2, the optimal policy for problem (2) starts testing only once the constraint on i u is attained, and then sets a time-varying rate (βs(t) − γ)/η such that di u (t)/dt = 0, keeping the fraction of infected-undetected individuals constant at the threshold i max until herd immunity is reached, after which there is no need for further testing as the epidemic tends naturally towards extinction. Intuitively, given that no testing is needed once herd immunity is reached, the optimal policy takes the form of the most rapid approach path introduced in [Spence and Starrett, 1975] to reach herd immunity as fast as possible, while satisfying the i max constraint. The testing policy detailed above leads to the highest number of infected-undetected individuals by employing no testing until undetected infections reach i max and then utilizing testing resources to keep the infections at this threshold, thus guaranteeing the most rapid feasible path to herd immunity.

State Estimation
The optimal adaptive testing policy θ † (t) derived in the previous section depends on knowledge of the aggregate epidemic state, i.e., the values of s, i u , i d and r at all times.
In practice, because this information is not readily available, the state of the epidemic must be estimated from detected infections. Additionally, the policy makers must know the model parameters. While many of such parameters are related to properties of the disease, the transmission rate is a function of people's behavior [Weitz et al., 2020], is typically time-varying and needs to be estimated from data. This is a nontrivial problem, since the dynamics induced by the SIR model is highly nonlinear and a given path of infections can be due to different β(t) trajectories coupled with different initial conditions.
To address these problems, we propose the use of baseline testing with a constant rate θ B to complement the adaptive testing policy derived above, see Fig. 3. Importantly, the objective of baseline testing is not to control the epidemic, but rather to collect enough data to robustly estimate the state of the epidemic and the parameter β(t). The policy maker can then use the estimated state and parameter to implement the optimal adaptive policy discussed in Section 2.1.

Epidemic Dynamics
State estimator Optimal testing Figure 3: Schematic of the proposed procedure. Measurements of detected-infected and detected-recovered individuals (i d (t), r d (t)) as defined in Eq. (3) obtained via baseline testing with rate θ B are used to estimate the aggregate epidemic state (ŝ(t),î u (t)), which is then used to compute the optimal adaptive testing rate (θ † (t)) according to the results of Theorem 1 (the hat symbol denotes the fact that the optimal testing rate is evaluated as a function of the estimated state). The objective of adaptive testing is to contain the fraction of infected-undetected individuals below the desired threshold i max .

Baseline testing
We next argue that state estimation may be infeasible with molecular testing, which is highly sensitive but detects infections only during a short window of time. Intuitively, detection of current infections from molecular testing is not always sufficient to identify whether a given trajectory of infections is due to a particular time path of β(t) coupled with a given set of initial conditions, or to a different time path of transmission coupled with a different set of initial conditions. In contrast, such identification is always possible with baseline serology testing.
To illustrate these points, we consider a more detailed model where we partition the recovered population into recovered-undetected (R u ), consisting of individuals who had the disease but are not recorded as having immunity (because they were not diagnosed with either test) and recovered-detected (R d ), which consists of individuals that are known to have immunity because they were either detected during their illness or at a later time (via serology testing). Correspondingly, we consider an augmented model that includes both adaptive and baseline testing with different sensitivities (Fig. 1B). All individuals are tested via adaptive molecular testing with rate θ(t) and sensitivity η, and via baseline testing with sensitivity η BI for the detection of current infections and η BR for the detection of past infections. The model equations read: where the last equation is redundant since We use the more detailed model in Eq.
(3) to study the system's observability, that is, the question of whether an outside observer or policymaker can estimate the underlying state from detected cases. We prove that even when i d (t) is observed perfectly and continuously in time but there is no detection of recovered individuals, the underlying state cannot always be estimated (see Lemma 1 in the Materials and Methods Section 4.3). Instead, we prove that when serology testing is used, which allows the correct reconstruction of the time path of both i d (t) and r d (t) via observations of both ongoing and past infections, the underlying state can always be estimated (see Lemma 2 in the Materials and Methods Section 4.3). This result does not depend on the frequency of baseline testing, nor on the exact sensitivity of serology testing.

Extensions
In the previous sections we presented a rigorous analysis of optimal testing and observability for a simple yet insightful deterministic SIR model. Such results are derived under the assumption of perfect and continuous time observations. We next discuss some extensions to account for non-idealities encountered in practice.
First, we consider a model where individuals are detected not only via the testing program but also because they may become symptomatic. Specifically, we assume that infected-undetected individuals may develop symptoms and thus become infected-detected with rate κ (this leads to an additional flow from infected-undetected to infected-detected with rate κ as detailed in Eq. (S4) in the Supplementary Materials). Accordingly, we consider a variant of the original optimization problem where the constraint is imposed on the total number of infected individuals, instead of infected-undetected individuals only. We also impose a constraint on the maximum testing rate, modeling daily limitations in processing capacity. Overall, this results in the following extended optimal control problem: To find the optimal adaptive testing policy θ * (t) for the extended problem of Eq. (4), we adopted a numerical approach using the interior point optimizer library within the GEKKO optimization suite [Beal et al., 2018, Wächter andBiegler, 2006]. The optimal testing policy, computed numerically using parameters taken from the literature on the COVID-19 epidemic (see Materials and Methods Table 1 and Section 4.7) is shown in Fig. 4. Remarkably, the optimal adaptive testing policy for the extended optimization  (4). The green and blue curves are, respectively, the fractions of susceptible and infected individuals in the population, respectively. The black, dashed line represents the constraint on the total fraction of infected individuals, i = i u + i d ≤ i max . The optimal testing policy (red, dashed curve) is equal to zero at first, switches to its maximum value θ max at time t A , such that i u + i d reaches the constraint i max with zero derivative at time t B . Between times t B and t C , the optimal testing policy is equal to At time t C , the optimal testing policy switches back to θ max until time t D , after which it is equal to zero. The times t C and t D are such that, after t D , the total fraction of infected individuals grows initially, reaching the constraint i max tangentially (inset), and then decreases to zero. The switching times can be computed analytically, given the initial condition (as discussed in the Supplementary Materials). For illustration purposes, we set i max = 0.1 and η = 1, while the other parameters are as in Table 1. problem of Eq. (4) follows the same principle as the optimal testing policy for the original optimization problem of Eq. (2), that is, it aims at keeping the constrained quantity (i u for Eq. (2) and i u +i d for Eq. (4)) at the threshold i max for as long as possible, in order to bring the epidemic as fast as possible to a point after which it naturally goes to extinction. Two differences arise in the testing policy that optimizes the modified problem (4). First, in the original problem, one can afford to delay testing right until the time at which i u reaches the constraint i max . This is possible because in the original model the first derivative of the constrained quantity, di u /dt = i u (βs − γ − ηθ), depends explicitly on the testing frequency θ, and thus one can set the testing rate to θ = (βs − γ)/η to instantaneously ensure di u /dt = 0. In the extended model, instead, the first derivative of the constrained quantity i u +i d does not depend directly on the testing frequency θ. Therefore, one cannot instantaneously impose d(i u +i d )/dt = 0 and thus testing must start before the constrained quantity i u + i d reaches the threshold i max . The optimal testing policy thus switches from (Fig. 4). After time t B , the optimal testing strategy switches to a frequency that maintains i u + i d at the i max value (the specific rate can be computed from equating the second derivative of i u + i d to zero). Second, i u + i d can naturally decrease even before herd immunity if i d > 0. For this reason, towards the end of the epidemic the optimal testing policy for the extended problem of Eq. (4) adopts a second phase with maximal testing frequency θ max that decreases i u and increases i d up to a point when, if testing is stopped, i u + i d naturally remains below the threshold i max for all subsequent times (it increases initially but again reaches the threshold tangentially, see inset of Fig. 4). The switching times can be characterized analytically as discussed in the Supplementary Materials.
Next, we allow the dynamics of the epidemic to be governed by a stochastic process rather than a deterministic (albeit time-varying) one. This is, of course, more realistic given the stochastic nature of transmissions and the time-varying and stochastic transition of individuals across different compartments. Additionally, we assume that observation of detected cases happens at discrete time instants (e.g. daily) instead of continuously. To deal with this type of non-idealities and stochasticity in our state estimation, we propose the use of a state-estimator which, given observations at discrete time instants t k , produces estimates of the state of the system (denoted byŝ,î u ,î d ,r u ,r d ), which can then be used to implement the adaptive testing policy. For the purpose of this analysis, we assume that β is known and constant, and use an extended Kalman filter with state constraints as state estimator (Materials and Methods Section 4.5) coupled with a system size expansion of the master equation governing the probability distribution of the stochastic model to derive the dependence of process noise on the epidemic state and population size (Materials and Methods Section 4.4). Extensions to unknown and time-varying β are discussed in the Supplementary Materials Section S.5. To validate our procedure in the presence of non-idealities, we used a receding horizon implementationθ * of the optimal testing policy derived for the deterministic SIR model (see Materials and Methods Section 4.6). Fig. 5A shows the performance of the state estimator and of the adaptive testing policy for multiple stochastic realizations with θ B = 1/14 d −1 (where d stands for day). The extended Kalman filter provides good estimates (black, dashed curves in Fig. 5C-D) of the real state of the epidemic (blue and orange curves), which lies within the confidence bounds of the estimate. In addition, the time-varying testing rate implemented using the estimated state is effective in maintaining the number of infected individuals around the desired threshold I max = N i max (where N is the population size). Fluctuations of order √ N around such threshold are to be expected as the epidemic is simulated as a stochastic, Markov process (Materials and Methods Section 4.4). Finally, we show that the mean of the receding horizon testing policy, computed across realizations of the stochastic model of epidemics, follows the optimal policy derived for the deterministic SIR model (Fig. 5B).

Discussion
A major lesson from the recent COVID-19 crisis is that, in the absence of comprehensive vaccines and therapeutic solutions, rapid testing and isolation become crucial tools to contain the spread of a pandemic. In this paper, we developed an approach to determine an optimal testing strategy, relevant especially when there are scarce or expensive testing resources.
Our approach has two basic pillars. First, we showed that, in the context of a classic SIR model, when the epidemic state in terms of infected, recovered and susceptible individuals is known and the objective can be formulated as keeping the number of undetected infections below a certain threshold, then the optimal testing strategy takes a simple form, similar to a most rapid approach path. In particular, there should be no testing until the aforementioned threshold is reached, and thereafter, testing resources should be used to keep infections at this threshold until herd immunity is reached and the epidemic starts disappearing naturally. The standard molecular tests, which have high accuracy, are crucial for this result, because they enable the identification and isolation of infected individuals.
The second pillar of our approach turns to the identification of the epidemic state. Our  Figure 5: Control of stochastic trajectories by using a receding horizon version of the optimal testing policy θ * in combination with baseline serology testing with rate θ B = 1/(14 days). Panel A shows the mean number of infected-undetected individuals I u (t) (blue, thick curves), the mean total number of infected individuals I(t) (orange, thick curves) across 500 realizations and I u (t), I(t) in five, randomly selected stochastic trajectories (solid, thin lines). Note that capital letters denote absolute numbers instead of fractions of individuals. Colored bands are 95% empirical confidence intervals and the thick, yellow lines show the value of I max . Panel B shows the mean molecular time-varying testing rate in the simulations (thick, green curve) and its 95% confidence interval. The dotted, black line shows (β( S − I u )/N − γ − κ)/η, which is the functional form of the optimal adaptive testing rate θ * (t) for the deterministic SIR model in the interval [t B , t C ] (Eq. S11). The black, dashed line shows the maximum testing rate θ max . Panel C shows the number of infected-undetected individuals I u (t) (blue curve) and the total number of infected individuals I = I u (t) + I d (t) (orange curve) in a single realization. The estimated number of infected-undetected individualsÎ u (t) and estimated total number of infected individualsÎ(t) are shown with black, dashed curves. The 95% confidence intervals for I u (t) andÎ(t) computed using the predicted variance estimated according to the extended Kalman filter are shown as colored bands. The infected threshold value I max is shown as a yellow line. Panel D shows a zoom of the initial phases of the epidemic highlighting the accuracy of the Kalman filter estimates. Model parameters and initial conditions are as in the Materials and Methods Table 1 and Section 4.7.
optimal testing policy crucially depends on such knowledge, but where does this knowledge come from? We tackle this question by adopting a state estimation framework, where the underlying state is unknown but can be estimated from the sequence of infections and additional information obtained from testing. Though molecular tests are also useful in this context (because they reveal the trajectory of infections), our main result in this part is that this information by itself is not sufficient for identifying the underlying state. This is because the dynamics of the SIR model are highly nonlinear and dependent on initial conditions, and it is not always possible to tell apart whether a given sequence of infections is due to one of many time-varying paths of transmission rates coupled with different initial conditions. Instead, we showed that serology testing which is lower-accuracy, cheaper and longer-range (in terms of estimating past infections) can be useful to disentangle this information.
More specifically, we proposed a two-pronged approach in which baseline serology testing is used to collect information about the state of the epidemic, and the more costly and sensitive molecular testing is adaptively deployed based on such information (Fig. 3). Our analysis formalizes the notion that serology offers advantages as a baseline testing tool not only because of cost benefits, but also because it conveys information about past infections, which proves fundamental to correctly and timely estimate the state of the epidemic. We then showed that, based on information about the state of the epidemic, optimal adaptive molecular testing can be adopted and implemented.
Inevitably for a mathematical analysis based on a stylized model, our approach simplified many aspects of the problem. Our extensions dealt with two such aspects. First, we showed that similar insights apply in the context of an SIR model in which both detectedinfected and undetected-infected numbers have to be kept below a certain threshold. Second, our state estimation techniques apply even when we are dealing with a stochastic model of epidemics.
Our analysis suggests that there are tangible gains from the proposed approach. Fig. 6 shows that our two-pillar approach with state estimation plus optimal testing can lead to significant reductions of overall cost with respect to constant testing strategies, leading to up to 60% cost reduction for the parameters we investigated.
In concluding, we make three additional remarks. First, optimality of the proposed approach is claimed in terms of the problems formulated in Eqs. (2) and (4) where the only epidemic constraint comes from keeping infections below a desired threshold i max . Strategies that achieve such an objective are typically classified as "containment strategies", since their objective is to contain the disease to a state that can be handled by the health system. This is very different from "eradication strategies" where instead the objective is to eradicate the disease as fast as possible. It is important to note that the more costly constant testing strategy detailed in the Materials and Methods Section 4.2 would lead to faster eradication of the disease than the adaptive strategy suggested here, but testing would need to continue indefinitely to ensure that outside infections would not create further waves, as for the parameters considered in the simulations herd immunity is not reached before eradication under constant testing. Whether eradication or containment strategies should be preferred depends on considerations about testing availability, possible long-term effects of infections on the health of individuals [Davis et al., 2020] and the impact of the epidemic on the economy, and is outside of the scope of this work. Our objective here was to derive the optimal containment strategy for cases when eradication is simply not possible, e.g., because of test scarcity or budget limitations.
Second, we derived our results for a standard SIR model (yet with extensions to testing and symptomatic individuals). Our objective was to derive analytic insights for a model of epidemics that is general enough to encode the core traits of an epidemic without getting lost in the details of specific diseases. Clearly, caution is required when applying  Figure 6: Cost of the optimal testing policy for the optimization problem 4, as a function of different baseline testing rates θ B . Costs are normalized with respect to the cost of the constant testing policy with the minimum testing frequency required to maintain the constraints I u + I d ≤ I max at all times (Materials and Methods Section 4.2). Data points connected by straight lines are mean statistics across 1000 simulations of the stochastic trajectories. Shaded bands represent 95% confidence intervals. Orange and black data points report the relative contribution of adaptive qPCR testing and baseline serology testing to the total cost (blue points), respectively. All adaptive policies induce significant cost savings, up to 60% reduction with respect to the cost under the optimal constant testing strategy derived in Section 4.2 for the parameters investigated. Model parameters and initial conditions are as in the Materials and Methods Table 1 and Section 4.7.
our findings in the field and additional steps are needed to validate our suggestions with detailed models of any specific disease before translation to practice. We note especially that we did not account explicitly for delays due to test processing time in our model. However, the fact that serology has typically a faster turnaround time than qPCR is an additional argument in support of serology as a baseline testing tool. Finally, we investigated the robustness of our procedure to sources of stochasticity that are intrinsic to the spread of an epidemic, and found that using information on the expected scaling of fluctuations with the population size and the state of the epidemic, one can apply the testing strategies developed for the deterministic SIR model to control epidemics even in the presence of intrinsic stochasticity. In practical applications, one may encounter additional sources of exogenous stochasticity due to people's behavioral responses, changes in policies, infections coming from external sources such as neighboring states, seasonal changes, etc. We believe that a dual approach where our theoretical results are used as a guideline for formulating candidate policies that are then tested extensively with numerical approaches adopting an ensemble of models (as in [Ray et al., 2020, Viboud andVespignani, 2019]) would be a powerful tool in the control of future pandemics.

Model parameters
In our numerical studies, we used model parameters that have been used in the literature to describe the spread of COVID-19. Table 1 reports the parameter values, along with the corresponding sources. Note that we decided to use low sensitivity both for molecular and serology testing to be conservative and to account for the fact that infected agents in the initial incubation period may not be detectable.

Constant testing strategy
One possibility for controlling an epidemic with a constant testing rate is selecting a value of the testing rate θ const that guarantees a basic reproduction number [Daley and Gani, 2001] smaller than unity. For the model with symptomatic agents (Supplementary Materials Eq. S4) the basic reproduction number is corresponding to the number of secondary infections generated by an individual when he/she is free to circulate and the rest of the population is made entirely of susceptible individuals. Setting the basic reproduction number to unity and solving for θ const leads to a testing rate guaranteeing that the number of infected undetected is monotonically decreasing. This is a more restrictive condition than what is needed to satisfy the constraint i u + i d ≤ i max in Problem (4). Indeed, a lower testing rate for which the fraction of infected-undetected initially increases but reaches the constraint i max tangentially would suffice. To be fair in the comparison with the adaptive testing policy, we next derive such lower constant testing rate as a function of the fractions s 0 and i 0 of susceptible and infected individuals (all assumed to be undetected) at time t = 0. Note that to satisfy the constraint with the least amount of constant testing, θ const should be such that i = i u + i d reaches the constraint i max tangentially (i.e., only once at timet with first derivative equal to zero). Our objective is to derive a series or relations between the initial state (s(0), i u (0), i d (0)), the state at timet (i.e., s(t), i u (t), i d (t)), and the testing rate θ const . We then exploit these relations to solve for θ const . From the discussion above we have and Next, by Eq. (1) it holds ds(t)/dt = −βs(t)i u (t) and di d (t)/dt+dr(t)/dt = (ηθ const +γ)i u (t) leading to 1 s(t) Integrating this equation we can derive a constant of motion for the epidemic which leads to the following relation between the epidemic state at time zero and at timet:

Observability notions
We formally define observability for a parametric system as follows.
Lemma 1 (Observability from molecular testing). Consider the system of Eq.
(1) with state x(t) = [s(t), i u (t), i d (t), r(t)] and continuous time output y(t) = i d (t). Suppose that ηθ(t) > 0 for all t and that γ is known.

If β(t) is time-varying, the system is not observable.
Lemma 2 (Observability from serology testing). Consider the system of Eq.
and continuous time output If ηθ(t) + η BR θ B > 0 and γ is known, the system is observable.
The proofs of these lemmas are provided in the Supplementary Materials.

Stochastic model
We performed stochastic simulations of a compartmental model of epidemics in which individuals of a population of size N are assigned to the same compartments S, I u , I d , R u and R d as in the deterministic SIR model with symptomatic individuals (Supplementary Materials Eq. (S4)). In analogy with the deterministic SIR model, transition rates among states are set to: where W (X |X) is the probability per unit time of transitioning from state X to state X and the parameters have the same interpretation as in the deterministic SIR model, and capital letters S, I u , I d and R u indicate the absolute number of individuals in the various compartments of the stochastic model. The compartment R d is not mentioned explicitly, as its abundance is equal to Unlike the deterministic SIR model, the stochastic model of epidemics accounts for the fact that the numbers of individuals in each compartment are integers and that infection, recovery and detection are stochastic events. As such, the stochastic model is better suited to describing epidemics in small populations or the epidemiological dynamics in the initial phases of an epidemic, where number fluctuations can be important. The dynamics of the stochastic model is governed by the master equation, [van Kampen, 2007, Gardiner, 2009: where P (S, I u , I d , R u , t) is the probability of being in state X := [S, I u , I d , R] at time t and the transition operator E +1 S is defined by E ±1 S f (S, I u , I d , R u ) = f (S ± 1, I u , I d , R u ) for a generic function f , and similarly for the other operators. 1 We simulated trajectories of the stochastic model of epidemics by using the Gillespie algorithm [Gillespie, 1976], with the parameters reported in Table 1.
For large N , Eq. (9) can be expanded in powers of 1/N following a Kramers-Moyal or system-size expansion [van Kampen, 2007, Gardiner, 2009. Eq. (9) can be expressed in terms of the rescaled variablesx := X/N = [s,ĩ u ,ĩ d ,r u ] as follows: Note thatx(t) is a stochastic process, whereas x(t) as defined in the main text is the solution to the deterministic SIR model. The right hand side of Eq. (10) is a function of x ± 1/N . Expanding this function aroundx up to the second order (1/N 2 ), one obtains 1 In Eq. (9), operators within parentheses act on all the functions of state variables to their right according to conventional operator precedence, e.g.
the Fokker-Planck equation: where g is the vector field corresponding to the deterministic dynamics used for computing the optimal testing strategy (see Eq. (4) and Eq. (S4) in the Supplementary Materials) and B is the matrix: Eq. (11) is known as the diffusion approximation of the master Eq. (10) and describes the probability distribution of a continuous stochastic process specified by the following Itô Langevin equation: [van Kampen, 2007, Gardiner, 2009 where ε(x, θ, t) is a Gaussian noise with covariance ε j (x(t), θ(t), t)ε k (x(t ), θ(t ), t ) = B jk (x(t), θ(t))δ(t − t ) and zero mean, where δ is the Dirac delta. Intuitively, in Eq. (12) the first term g(x, θ) coincides with the vector field of the deterministic dynamics while the second term captures diffusive fluctuations due to stochasticity, whose amplitude depends both on the population size and on the epidemic state. As detailed in the next section, this approach enables us to characterize the process noise for the extended Kalman filter with the correct scaling in N and highlights its dependence on the current epidemic state (e.g., the number of infected-undetected) as captured by the matrix B.

State estimation for the stochastic simulations
Given the dynamics of Eq. (12), we discuss here how we estimate the state of the epidemic. We assume the following observation model: with: that is, only infected-detected and recovered-detected are observed, and we assume discrete observation times t k (e.g., daily observations). Such observations can be used to estimate the state of the system via a state observer, which we implemented using an extended Kalman filter with state constraints (see [King et al., 2008, Pasetto et al., 2017, Pasetto et al., 2018, Li et al., 2020b for other applications of the Kalman filter in the context of epidemiology). In a Kalman filter, observations y k =ỹ(t k ) are used to create an estimate of the state, denoted byx(t) = [ŝ(t);î u (t);î d (t);r u (t)]. The first step is to initializex 0|0 = Then, the dynamics of the extended Kalman filter [Khalil, 2015] is computed as follows, at any time step t k : 1. Predict the next state, given previous observations: and setx k+1|k =x(t k+1 ), P k+1|k = P (t k ) 2. Update the prediction, given the current observation: where Π X k represents the projection in the feasible set X k = {x ≥ 0 | Cx + c = y k }.
The matrices Q and R are covariance matrices for the process and measurement noise. In our analysis, we assume R = 0 (as the number of infected-detected and recovereddetected is perfectly know by the policy maker and thus there is no measurement error in Eq. (13)), while Q(t) is the covariance of the process noise, which is equal to B/N as derived from the expansion of the master equation in Section 4.4.

Testing strategy for the stochastic simulations
The testing policy derived for the deterministic SIR model is not necessarily robust to the presence of stochastic fluctuations. For this reason, in the stochastic simulations we implemented a receding horizon versionθ * (t) of the testing policy where at any time t k > t A (as defined in Fig. 4) we computed the constant testing rate needed to drive the total fraction of infected to the threshold i max in a horizon of H days (we set H = 3 d), assuming that the dynamics follows the deterministic SIR model, i.e. Eq. (S4) in the Supplementary Materials. According to the principles of receding horizon control, such testing rate is applied for one time step and then a new problem is solved for the next horizon [t k+1 , t k+1 + H] given the new realized state. Thus, at every time step t k the testing rate is set to: where θ rh is the testing rate that would bring the deterministic system to the constraint i u + i d = i max with zero derivative in a time horizon H, starting fromŝ(t k ) andî u (t k ) (see Eq. (S16) in the Supplementary Materials). Note that we assume here that the receding horizon is implemented for any time t k > t A , where t A is the optimal time to start testing as computed for the deterministic SIR model. In practice, the policy maker may prefer to implement the receding horizon control from the beginning, for additional robustness and to compensate for the uncertainty of state estimates in the early phases of the dynamics. This has a minor cost implications, since t A is typically very small with the parameters considered here.

Parameters used in simulations of the stochastic model of epidemics
Simulations of the stochastic model of epidemics were performed with population size N = 50000 and constraint I max = 1000 corresponding to 2% of the population size. This percentage was chosen for illustration purposes and it roughly corresponds to the peak percentage quarantine capacity estimated to be required for the safe reopening of Cornell's Ithaca NY campus during the COVID-19 pandemic in the Fall 2020 [Cashore et al., 2020].
Realizations of stochastic epidemics were initialized with I(0) = I u (0) = 50 infected and S(0) = N − 50 susceptible individuals (the other compartments were initialized at I d (0) = R d (0) = R u (0) = 0). The other parameters were set to the values in Table 1. The initial state estimate for the extended Kalman filter was set toÎ(0) = 0 infected and Ŝ (0) = N susceptible individuals (the estimates for the other compartments were set to zero). All entries of the initial estimate for the covariance matrix P (see Section 4.5) of the extended Kalman filter were set to zero, with the exception of the estimate for the variance ofŜ(0) and ofÎ u (0), which were set to I 2 max /12, to reflect a large uncertainty on the initial condition.
Finally, we characterize the optimality of the proposed control θ † before t herd .
Lemma 5. The optimal control takes the most rapid approach path to reach herd immunity.
Proof. Lemma 3 characterizes how the optimal control behaves after herd immunity. Therefore, we can rewrite the original optimization problem in Eq. (2) as follows where t herd is the time to reach herd immunity (which depends on θ). Integrating the dynamics where we have used Lemma 4 for the last equality. Note that the second term is a constant and η is a positive constant. Therefore, the original optimal control problem can be rewritten as: We next show that the trajectory i † u induced by the control θ † pointwise dominates any other feasible trajectory i u and therefore the corresponding trajectory s † is pointwise smaller than any other feasible trajectory of s, before hitting herd immunity (Fig.  S1). This has two consequences: i) herd immunity is reached sooner under θ † and ii) (β(t)s(t) − γ) is pointwise smaller. These two points prove that θ † minimizes Eq. (S2) and thus also Eq. (S1). To prove points i) and ii) recall that from the first line of Eq. (1) and by integration log which shows that for two feasible trajectories (s † (t), i † u (t)) and (s(t),

Susceptible fraction s
Infected-undetected fraction i u Time t Figure S1: The trajectory i † u (t) induced by the optimal testing strategy θ † pointwise dominates any other feasible trajectory i u . Conversely, the trajectory s † (t) induced by the optimal testing strategy θ † is pointwise smaller than any other feasible trajectory s(t), until herd immunity is reached. In this illustration β is constant, so that s(t herd ) = γ/β = s herd .
Combining the lemmas above proves Theorem 2. The expression of θ † when i u (t) = i max can be obtained from di u /dt = (βs − γ − ηθ † )i u = 0.

S.2.1 Proof of Lemma 1
We prove the two statements separately.
1. From the third line of Eq. (1), it holds hence i u (and all its derivatives) can be reconstructed from the observed output i d (and its derivative). From the second line of Eq. (1), for β constant, we obtain Substituting the expression in Eq. (S3) on the right hand side of (obtained by computing the second derivative of i d ) yields a formula for β as a function of known quantities (i u , i d , θ and their derivatives). Since βs(t) is known from Eq. (S3), this implies that s(t) is known and finally r(t) = 1−s(t)−i u (t)−i d (t).
2. If β(t) is time-varying the system is not observable. To show this we consider two evolutions that start from different initial conditions and have different β evolutions, yet lead to the same observable output i d . This proves that just observing the output it is not possible to distinguish the two scenarios. Specifically consider the two systems: Since i u andī u solve the same differential equation it must beī u (t) ≡ i u (t) for all t. This immediately implies i d (t) ≡ī d (t), yet the evolution of s ands is different as they start from different initial conditions.

S.2.2 Proof of Lemma 2
Since y(t) is observed continuously in time one can use it to compute exactly di d /dt and dr d /dt. The third and fifth equations in Eq.
(3) can then be used to recover i u (t) and r(t) exactly as follows: Using the fact that one can reconstruct s(t) as well. Overall, the state can be estimated exactly from the observed variables. As a byproduct, knowledge of s(t) and i u (t) allows the identification of β(t) from the first equation in Eq. (3).

S.3 Optimal testing policy for the extended problem in Eq. 4
We consider an extension of the model in Eq. (3) that accounts for detection of symptomatic individuals by considering an additional flow from infected-undetected to infecteddetected with rate κ: Time t Figure S2: Structure of the optimal testing strategy (red curve) and corresponding effect on the total fraction of infected individuals (blue curve). The optimal testing policyθ * is zero until time t A , when it switches to the maximum testing rate θ max until time t B , when i = i u + i d reaches the constraint i max with zero derivative. In the interval [t B , t C ] of duration τ 3 , the optimal testing policy maintains i = i max , until switching back to θ max between times t C and t D . After t D , i increases until reaching i max with zero derivative at time t E , and decreases afterwards.
The numerical solution of the problem in Eq. (4) for this extended model (and for the parameters of Fig. 4) has the structure described in Section 2.3 and schematized in Fig. S2. To generalize this analysis to any set of parameters, we here aim at deriving an analytic characterization of the optimal testing policy for Problem 4 within the class of policies with such a structure. More in detail, we aim at deriving analytic expressions for the optimal switching times t A , t B , t C and t D and for the testing rate in the interval [t B , t C ] as a function of the model parameters and initial conditions. With slight abuse of notation, we denote the optimal policy within this class with the symbol θ * . For this analysis we assume β to be constant, and without loss of generality we assume η = 1. Moreover, we work under the assumption that the state is known, hence we set θ B = 0.
The following analytic relationships (adapted from [Harko et al., 2014]) between s and i u at two times t 1 < t 2 under constant testing rate θ will be useful: First, we evaluate s A := s(t A ), i uA := i u (t A ), s B := s(t B ) and i uB := i u (t B ) as functions of s 0 and i 0 . Using Eq. (S5) we have: S-5 Imposing that at time t B one has (i u + i d ) | t B = i max and d(i u + i d )/dt | t B = 0 yields: and integrating di d /dt = (θ max + κ)i u − γi d in [t A , t B ] one finds: where τ 2 can be computed from Eq. S6 as a function of s A , i uA and s B . The quantity i dA can be computed as a function of s A by integrating di d /dt = −γi d + κi u in the interval [0, t A ], which gives: where again we made use of Eq. S6. Eqs. (S7)-(S10) are four equations in four unknowns that can be solved to find s A , i uA , s B , i uB as a function of s 0 and i 0 . In the interval [t A , t B ], the optimal testing policy is θ * (t) = θ max , and thus the cost of the control in the interval [0, t B ] is: In the interval [t B , t C ] the optimal testing policy maintains i = i u + i d ≡ i max . Hence, the first and second derivatives of i must be equal to zero. Note that Substituting this expression in the dynamic equations, we obtain that in the interval [t B , t C ] the undetected fraction of the infected individuals satisfies: . (S13) Moreover, S-6 Thus, s C = s B −γi max τ 3 and i uC = 1/(1/i uB − βτ 3 ). The value of τ 3 is limited from above by the constraints τ 3 < (βi uB ) −1 (for solvability of Eq. (S12)) and θ * ≥ 0, which can be expressed as an upper constraint on τ 3 via Eqs. (S11), (S13) and (S14). We denote byτ 3 the minimum of such constraints. The cost of the optimal testing policy in the interval [t B , t C ] is: The analysis is identical to that of Section S.3.1, the objective is to evaluate s D , i uD ,s E and i uE as a function of s C and i C . From Eq. (S5) we have: f θmax (s C , i uC , s D , i uD ) = 0 f 0 (s D , i uD , s E , i uE ) = 0.
At time t E , i u + i d is tangent to i max , hence: (similar to Eq. (S9)) βs E i uE = γi max .
Integrating di d /dt = θ max i u + κi u − γi d in the interval [t C , t D ] yields:  S.3.4 Minimization over τ 3 yields the optimal testing policy So far we obtained a formula for the overall cost that, for fixed s 0 and i 0 , depends only on τ 3 . Minimizing such cost for τ 3 ∈ [0,τ 3 ] yields the optimal value for τ 3 (Fig. S3), with which the optimal testing strategy θ * is characterized. Note that since s is monotonically decreasing, the testing strategy can be formulated as follows:  Figure S3: Cost of testing policies (red curve) with the structure of Fig. S2 for the deterministic SIR model Eq. (S4), as a function of the duration τ 3 of the time interval [t B , t C ]. Costs are computed relative to the cost of the optimal, constant testing strategy (Materials and Methods Section 4.2). The optimal testing policy adopts the value of τ 3 that minimizes the cost (inset). The dashed, black line indicates the cost of a testing policy with the largest possible value of τ 3 , i.e. a testing policy in which the last phase of testing at maximum testing rate θ max does not occur. The inset shows that the benefit of the last phase of testing at maximum testing rate is marginal.

S.4 Receding horizon implementation of the optimal testing policy for the stochastic simulations
In the receding horizon implementation, at any time t > t A we aim at computing the testing rate θ rh (t) that brings the deterministic dynamics of Eq. (S4) to i u + i d = i max with d(i u + i d )/dt = 0 in a time H. This leads to the following set of equations: f θ rh (t) (s(t), i u (t), s(t + H), i u (t + H)) = 0 (S15) where f θ is as in Eq. (S5) and in the last equation we used the trapezoidal rule to approximate the integral. We denote the solution of Eq. (S15) as: θ rh (s(t), i u (t)) := −β s(t) + i u (t) − s(t + H) ln(s(t + H)/s(t)) + γi max s(t + H) ln(s(t + H)/s(t)) . (S16)

S.5 Validation with time-varying transmission rate
Fig . S4 illustrates the performance of the testing policyθ * for the optimization problem of Eq. 4 when the transmission rate is time-varying (in this case sinusoidal to mimic seasonality). For any time t, the transmission rate β(t) is estimated from the reconstructed state [x(τ )] t−1 τ =t−7 over a moving window of length 7 days by nonlinear least square regression. The testing rate is evaluated by using Eqs. (14) and (S16) with the estimatedβ(t) instead of β. We see from Fig. S4 that the time-varying transmission rate can be accurately predicted (while the epidemic is active) and the testing rate implemented using such prediction effectively stabilizes the epidemics at the desired threshold. This is a preliminary indication that the suggested testing strategy may be effective even in the presence of an unknown and time-varying transmission rate. A detailed numerical study is needed to further support this conclusion and is left as future work.
S-8   Figure S4: Performance of the testing policyθ * when β(t) varies sinusoidally with a one year period centered at β = 0.3 d −1 and amplitude 0.1 d −1 . At any time t k , β(t k ) was estimated using data from the previous 7 days by fitting the epidemiological dynamics to the deterministic SIR model, taking into account the testing rates adopted. Panel A shows the total number of infected I (orange curve) and infected-undetected (blue curve) in a stochastic realization of the epidemic, along with the state estimates obtained from the extended Kalman filter (dashed, black curves). Panel B shows the true (orange curve) and estimated (blue curve) values of β.