{'fraction_non_alphanumeric': 0.036487996293974985, 'fraction_numerical': 0.009537564378559555, 'mean_word_length': 4.089169324642906, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
A modified Next Reaction Method for simulating chemical systems with time dependent propensities and delays

Chemical reaction systems with a low to moderate number of molecules are typically modeled as discrete jump Markov processes. These systems are oftentimes simulated with methods that produce statistically exact sample paths such as the Gillespie Algorithm or the Next Reaction Method. In this paper we make explicit use of the fact that the initiation times of the reactions can be represented as the firing times of independent, unit rate Poisson processes with internal times given by integrated propensity functions. Using this representation we derive a modified Next Reaction Method and, in a way that achieves efficiency over existing approaches for exact simulation, extend it to systems with time dependent propensities as well as to systems with delays.


I. INTRODUCTION
Due to advances in the knowledge of cellular systems, where there are low to moderate numbers of molecules of certain species, there has been a renewed interest in modeling chemical systems as discrete and stochastic as opposed to deterministic and continuous. 1,2,3,4 Because of the intrinsic stochasticity at this level, understanding of a given system is gained through knowledge of the distribution of the state of the system at a given time. As it is typically impracticable to analytically solve for the distribution of the state of the system at a particular time for all but the simplest of examples, simulation methods have been developed that generate statistically exact sample paths so as to approximate the distribution. The two most widely used exact simulation methods are the original Gillespie Algorithm 5,6 and the Next Reaction Method of Gibson and Bruck. 7 In this paper, we will explicitly represent the reaction times of discrete stochastic chemical systems as the firing times of independent, unit rate Poisson processes with internal times given by integrated propensity functions. Such a representation is not novel and is called a random time change representation in the mathematics literature. See, for example, Refs. 8,9,10. However, using such a representation in an explicit attempt to develop new simulation methods has a number of benefits that have seemingly not been explored in the chemistry literature. First, the representation will naturally lead us to a modified version of the Next Reaction Method. 7 Second, the modified Next Reaction Method will be shown to be the natural choice for simulating systems with propensities that depend explicitly on time (such as systems with variable temperature or cell volume). Third, we will be able to easily extend our modified Next Reaction Method to systems that allow delays between the initiation and completion of reactions in a manner that achieves efficiency over existing methods. More precisely, in our modified Next Reaction Method for systems with delays no random numbers or computations will be wasted (such as happens in the method of Bratsun et al. 11 and Barrio et al. 12 ) (see Section VI), and there will be no need for the complicated machinery of the method developed by Cai 13 (see Section VI) in the handling of the stored delayed reactions. We note that the ideas we use to develop our modified Next Reaction Method are analogous to the theories of generalized semi-Markov processes 14,15,16 and stochastic Petri nets 17 , and can also be extended to develop new accurate and efficient approximate tau-leaping methods. 18 The outline of the paper is as follows. In Section II we briefly present the original Gillespie Algorithm. In Section III we introduce our representation of the reaction times as the firing times of independent, unit rate Poisson processes with internal time given by integrated propensity functions. In Section IV we rederive the Next Reaction Method and derive a modified Next Reaction Method using the representation detailed in Section III. In Section V we consider systems with propensities that depend explicitly on time and conclude that our modified Next Reaction Method is the preferable algorithm to use in such cases. In Section VI we consider systems in which there is a delay between the initiation and completion of some of the reactions and develop a new algorithm for simulating such systems that is an extension of our modified Next Reaction Method.

II. THE GILLESPIE ALGORITHM
Consider a system consisting of N ≥ 1 chemical species, {X 1 , . . . , X N }, undergoing M ≥ 1 chemical reactions, each of which is equipped with a propensity function (or intensity function in the mathematics literature), a k (X). For the time being, assume that the time between the initiation and the completion of each reaction is negligible. To accurately simulate the time evolution of the number of each species, X(t) = {X 1 (t), . . . , X N (t)} ∈ N N ≥0 , one needs to be able to calculate 1) how much time will pass before the next reaction takes place (i.e. initiates and completes) and 2) which reaction takes place at that future time. One can then simulate statistically exact sample paths for the system of interest. The following assumption, sometimes called the fundamental premise of chemical kinetics, is based upon physical principles and serves as the base assumption for simulation methods of chemically reacting systems: 6 a k (X(t))∆t + o(∆t) = the probability that reaction k takes place in a small time interval [t, t + ∆t), (1) where o(∆t)/∆t → 0 as ∆t → 0. Based upon the assumption (1), the time until the next reaction, ∆, is exponentially distributed with parameter a 0 (X(t)) = M k=0 a k (X(t)) and the probability that the next reaction is the kth is a k (X(t))/a 0 (X(t)). These observations form the foundation for the well known Gillespie Algorithm. 5,6 Algorithm 1. (Gillespie Algorithm) 1. Initialize. Set the initial number of molecules of each species and set t = 0.
2. Calculate the propensity function, a k , for each reaction.
which is equivalent to choosing from reactions [1, . . . , M] with the kth reaction having probability a k /a 0 . 7. Set t = t + ∆ and update the number of each molecular species according to reaction µ.
8. Return to step 2 or quit.
We point out that the Gillespie Algorithm uses two random numbers per step. The first is used to find when the next reaction occurs and the second is used to determine which reaction occurs at that time. In Section IV we will demonstrate how the Next Reaction Method generates exact sample paths while only needing one random number per step.

III. REPRESENTATION USING POISSON PROCESSES
We will explicitly represent the reaction times of chemical systems as the firing times of Poisson processes with internal times given by integrated propensity functions. 8,9,10 Using such a representation allows us to consider the system as a whole via a stochastic integral equation as opposed to solely considering how to calculate when the next reaction occurs and which reaction occurs at that time. The benefits of such a representation will stem from the fact that the randomness in the model is separated from the state of the system. Let ν k , ν ′ k ∈ N n ≥0 be the vectors representing the number of each species consumed and created in the kth reaction, respectively. Then, if R k (t) is the number of times that the kth reaction has taken place up to time t, the state of the system at time t is However, based upon the assumption (1), R k (t) is a counting process with intensity a k (X(t)) such that Prob(R k (t + ∆t) − R k (t) = 1) = a k (X(t))∆t for small ∆t. Therefore, where the Y k are independent, unit rate Poisson processes. Thus, X(t) can be represented as the solution to the following equation: Note that the state of the system, X(s), and hence each propensity function a k (X(s)), is constant between reaction times. In Section V we will consider systems in which the propensity functions are not constant between reactions, such as arise due to changes in temperature or cellular volume.
We make two points that are crucial to an understanding of how different simulation methods arise from equation (4). First, all of the randomness in the system is encapsulated in the Y k 's and has therefore been separated from the state of the system. Thus, since the system (4) only changes when one of the Y k 's change, the relevant question of each simulation algorithm is how to efficiently calculate the firing times of each Y k and how to translate that information into reaction times for the chemical system. Second, there are actually M + 1 relevant time frames in the system. The first time frame is the actual, or absolute time, t.
However, each Poisson process Y k brings its own time frame. More specifically, if we define t 0 a k (X(s))ds for each k, then it is relevant for us to consider Y k (T k (t)). We will call T k (t) the "internal time" for reaction k. We will use the internal times of the system in an analogous manor to the use of "clocks" in the theory of generalized semi-Markov processes. 14,15,16 We now formulate the Gillespie Algorithm (Algorithm 1) in terms of equation (4). At time t, we know the state of the system, X(t), the propensity functions, a k (X(t)), and the internal times, T k (t). Calculating 1) how much time will pass before the next reaction takes place and 2) which reaction takes place at that future time is equivalent to calculating 1) how much time passes before one of the Poisson processes, Y k , fires and 2) which Y k fires at that later time. Combining the previous statement with the fact that the intensities of the Poisson processes are a k (X(t)) yields one step of the Gillespie algorithm. Use of the loss of memory property for Poisson processes (which negates knowledge of the internal times T k (t)) allows us to perform subsequent steps independently of previous steps.
Note that in the Gillespie Algorithm the firing times of the individual processes Y k were calculated by first finding the time required until any of them fired, and then calculating which reaction fired at that future time. In the next section we show how the Next Reaction Method and our modified Next Reaction Method first calculates when each of the Y k fires next, and then finds the specific reaction that fires by taking the minimum of such times. By not invoking the loss of memory property (and, hence, differentiating themselves from the First Reaction Method 6 ), the Next Reaction Method and modified Next Reaction Method make use of the internal times T k (t) to nearly cut in half the number of random variables needed per simulation.

IV. A MODIFIED NEXT REACTION METHOD
We again consider the system (4). At time t we know the state of the system X = X(t), the propensity functions a k = a k (X(t)), and the internal times T k = T k (t). We also assume that we know ∆t k , the amount of absolute time that must pass in order for the kth reaction to fire assuming that a k stays constant over the interval [t, t + ∆t k ). Therefore, τ k = t + ∆t k is the time of the next firing of the kth reaction if no other reactions fire first. Note that if t = 0 and this is the first step in the simulation of the system (and so T k = 0), finding each ∆t k is equivalent to taking a draw from an exponential random variable with parameter a k .
Because we know ∆t k , the internal time at which reaction k fires is given by T k + a k ∆t k . In order to simulate one step, we now note that the next reaction occurs after a time period of ∆ = min k {∆t k }, and the reaction that fires is the one for which the minimum is achieved, µ say. Therefore, we may update the system according to reaction µ, update the absolute time by adding ∆ and update the internal times by adding a k ∆ to T k , for each k.
For the moment we denote t = t + ∆ and the updated propensity functions by a k . The relevant question now is: for each k, what is the new absolute time of the firing of Y k , τ k , assuming no other reaction fires first? For reaction µ, we must generate its next firing time from an exponential random variable with parameter a k . For k = µ we note that, in general, the new absolute firing times will not be the same as the old because the propensity functions have changed. However, the internal time of the next firing of Y k has not changed and is still given by T k (t) + a k ∆t k . We also know that the updated internal time of Y k is given by T k (t) = T k (t) + ∆a k . Therefore, the amount of internal time that must pass before the kth reaction fires is given as the difference Thus, the amount of absolute time that must pass before the kth reaction channel fires, ∆t k , is given as the solution to a k ∆t k = a k (∆t k − ∆), and so Thus, we see that We have therefore found the absolute times of the next firings of reactions k = µ without having to generate any new random numbers. Repeated application of the above ideas yields the Next Reaction Method. 7

Algorithm 2. (The Next Reaction Method)
1. Initialize. Set the initial number of molecules of each species and set t = 0.
2. Calculate the propensity function, a k , for each reaction.
5. Set t = min k {τ k } and let τ µ be the time where the minimum is realized.
6. Update the number of each molecular species according to reaction µ.
7. Recalculate the propensity functions for each reaction and denote by a k .
10. For each k, set a k = a k .
11. Return to step 5 or quit.
Note that after the first timestep is taken in the Next Reaction Method, all subsequent timesteps only demand one random number to be generated. This is compared with two random numbers needed for each step of the original Gillespie Algorithm (Algorithm 1).
We also note that the Next Reaction Method was originally developed with the notion of a dependency graph and a priority queue in order to increase computational efficiency (see Ref. 7 for full details). The dependency graph is used in order to only update the propensities that actually change during an iteration (and thereby cut down on unnecessary calculations) and the priority queue was used to quickly determine the minimum value in Step 5. We have omitted the details of these items as they are not necessary for an understanding of the algorithm itself. However, we point out that the use of a dependency graph in order to efficiently update the propensity functions is useful in any of the algorithms presented in this paper, and not just to the Next Reaction Method.
We now present an algorithm that is completely equivalent to Algorithm 2, but makes more explicit use of the internal times T k . In the following algorithm, we will denote by P k the first firing time of Y k , in the time frame of Y k , that is strictly larger than T k . That is, The main idea of the following algorithm is that by equation (4) the value gives the amount of absolute time needed until the Poisson process Y k fires assuming that a k remains constant. Of course, a k does remain constant until the next reaction takes place.
Therefore, a minimum of the different ∆t k gives the time until the next reaction takes place.
Thus, if we keep track of P k and T k explicitly, we can simulate the systems without the time conversions of step 8 of Algorithm 2. 2. Calculate the propensity function, a k for each reaction.

Set ∆t
6. Set ∆ = min k {∆t k } and let ∆t µ be the time where the minimum is realized.
7. Set t = t + ∆ and update the number of each molecular species according to reaction µ.
10. Recalculate the propensity functions, a k .
11. Return to step 5 or quit.
We note that Algorithms 2 and 3 have the same simulation speeds on all systems that we have tested. This was expected as the two are equivalent. However, as will be shown in the next section, Algorithm 3 extends itself to systems with time dependent rate constants in a smooth way, whereas Algorithm 2 does not. We also point out another nice quality of Algorithms 2 and 3. Suppose that a system is governed by equation (4) except that the Y k 's are no longer Poisson processes. That is, we suppose that the reactions do not have exponential waiting times, but have waiting times given by some other distribution. To modify Algorithms 2 and 3 to handle such a situation, one solely needs to change steps 4 and 9 in each so that the waiting times are drawn from the correct distribution.

V. TIME DEPENDENT PROPENSITY FUNCTIONS
Due to changes in temperature and/or volume, the rate constants of a (bio)chemical system may change in time. Therefore, the propensity functions will no longer be constant between reactions. That is, a k (t) = a k (X(t), t), and the full system is given by where the Y k are independent, unit rate Poisson processes. We consider how to simulate system (6) using the Gillespie Algorithm, the Next Reaction Method, and our modified Next Note that X(t) is constant in the above integrals because no reactions take place within the time interval [t, t + ∆). Using equation (7), ∆ is found by first letting r be uniform(0, 1) and then solving the following equation: In Appendix A we show that the reaction that fires at that time will be chosen according to . Solving equation (8) either analytically or numerically will be extremely difficult and time consuming in all but the simplest of cases.
The Next Reaction Method. We begin by considering the first step of the Next Reaction Method. At time t = 0, we need to know the first firing times of independent, inhomogeneous Poisson processes. Therefore, we calculate the time that the kth reaction channel will fire (assuming no other reaction fires first) by solving for τ k from: where r k is uniform(0, 1). Equation (9) can be solved either analytically or numerically. Say that reaction µ is the first to fire and does so at time t. It is clear that to calculate the next firing time of reaction µ we will need to generate another uniform(0, 1) random variable r µ and solve τµ t a k (X(t), s)ds = ln(1/r µ ).
What is less clear is how to reuse the information contained in τ k for k = µ.
Proceeding as in Ref. 7, denote by F n,a the distribution function for the nth firing of a reaction, where a is some parameter of the function. Gibson and Bruck prove the following: Theorem V.1 (Gibson and Bruck's generation of next firing time 7 ). Let τ be a random number generated according to an arbitrary distribution with parameter a n and distribution function F an,n . Suppose the current simulation time is t n , and the new parameter (after a step in the system in which this reaction did not fire) is a n+1 . Then the transformation generates a random variable from the correct (new) distribution. That is, τ * has the correct distribution of the next firing time.
Gibson and Bruck demonstrate use of the above theorem on a system whose volume is In a situation in which Theorem V.1 is not practicable, the following steps must be taken to move one timestep beyond time t. First, generate uniform(0, 1) random variables, r k , and then solve for the time of the next firing of reaction k. We have been forced to use the loss of memory property of Poisson processes and generate new random variables. Thus, we are really now performing the First Reaction Method. 6 The modified Next Reaction Method. As above, we begin by considering the first step of our modified Next Reaction Method. At time t = 0, we set T k = 0 and P k = ln(1/r k ), where each r k is uniform(0, 1). To find the amount of time that must pass before the kth reaction channel will fire if no others do first we solve for ∆t k from: Again supposing that reaction µ fires first at time t, we update T k = t 0 a k (X(0), s)ds for each k. In order to calculate ∆t µ we must generate a new uniform(0, 1) random number, r µ , set P µ = P µ + ln(1/r µ ) and solve t+∆tµ t a µ (X(t), s)ds = P µ − T µ .
For k = µ we still know that P k is the internal time of the next firing of reaction k, and so the amount of absolute time that must pass, ∆t k , before the kth firing is given as the solution to Therefore, by keeping track of the internal times P k and T k we have been able to easily calculate the next firing of each reaction without having to generate another random number.
We point out that using equation (13) to solve for the next firing time is no more difficult than using equation (11) to find the next firing time in the Next Reaction Method, but equation (11) demanded the generation of a random variable. We also point out that if there are closed form solutions to the above integral, such as the case of linearly increasing volume, then this method becomes very efficient. Further, even in this case of time dependent propensity functions, the modified Next Reaction Method easily lends itself to situations in which the waiting times between reactions are not exponential (only the generation of the P k 's changes). We conclude that our modified Next Reaction Method will be preferable to either the Gillespie Algorithm or the Next Reaction Method on systems with propensity functions that depend explicitly on time.

VI. SYSTEMS WITH DELAYS
We now turn our attention to systems in which there are delays, τ k > 0, between the initiation and completion of some, or all, of the reactions. We note that the definition of τ k has therefore changed and is no longer the next reaction time of the Next Reaction Method.
We partition the reactions into three sets, those with no delays, denoted ND, those that change the state of the system only upon completion, denoted CD, and those that change the state of the system at both initiation and completion, denoted ICD. The assumption (1) becomes the following for systems with delays: a k (X(t))∆t + o(∆t) = the probability that reaction k initiates in a small time interval [t, t + ∆t), where o(∆t)/∆t → 0 as ∆t → 0. Thus, no matter whether a reaction is contained in ND, CD, or ICD, the number of initiations at absolute time t will be given by number of initiations of reaction k by time t = Y k t 0 a k (X(s))ds , where the Y k are independent, unit rate Poisson processes.
Because the assumption (14), and hence equation (15), only pertains to the initiation times of reactions we must handle the completions separately. There are three different types of reactions, so there are three cases that need consideration.

Case 1:
If reaction k is in ND and initiates at time t, then the system is updated by losing the reactant species and gaining the product species at the time of initiation.

Case 2:
If reaction k is in CD and initiates at time t, then the system is updated only at the time of completion, t + τ k , by losing the reactant species and gaining the product species.
Case 3: If reaction k is in ICD and initiates at time t, then the system is updated by losing the reactant species at the time of initiation, t, and is updated by gaining the product species at the time of completion, t + τ k .
The system can be written in the following integral form where each a k (s) = 0 for s < 0, and the Y k 's, Z k 's, and W k 's are independent, unit rate Poisson processes.
We note that there are more potential cases than those listed above. For example, the delay times, τ k , may best be described as a random variable as opposed to being fixed or there could be multiple completion times for a single initiation (implying things happen in some order). For the sake of clarity we do not consider such systems in this paper but point out that it is a trivial exercise to extend the results of this section to such systems.

A. Current Algorithms
Based upon the discussion above, we see that simulation methods for systems with delays need to calculate when reactions initiate and store when they complete. However, because of the delayed reactions, the propensity functions can change between initiation times. Bratsun et al. 11 and Barrio et al. 12 used an algorithm for computing the initiation times that is exactly like the original Gillespie Algorithm except that if there is a stored delayed reaction set to finish within a computed timestep, then the computed timestep is discarded, and the system is updated to incorporate the stored delayed reaction. The algorithm then attempts another step starting at its new state. We will refer to this algorithm as the Rejection Method. 2. Calculate the propensity function, a k , for each reaction.

If there is a delayed reaction set to finish in [t, t + ∆)
(a) Discard ∆.
(b) Update t to be the time of the next delayed reaction, µ.
(c) Update x according to the stored reaction µ.
(c) If µ ∈ ND, update the number of each molecular species according to reaction µ.
(d) If µ ∈ CD, store the information that at time t + τ µ the system must be updated according to reaction µ.
(e) If µ ∈ ICD, update the system according to the initiation of µ and store that at time t + τ µ the system must be updated according to the completion of reaction µ.
(f) Set t = t + ∆ (g) Return to step 2 or quit.
At first observation the statistics of the sample paths computed by the above algorithm appear to be skewed because some of the timesteps are discarded in step 5a. However, because the initiation times are governed by Poisson processes via (15), we may invoke the loss of memory property and conclude that the above method is statistically exact.
The number of discarded ∆'s will be approximately equal to the number of delayed reactions that initiate. This follows because, other than the stored completions at the time the script terminates, every delayed completion will cause one computed ∆ to be discarded.
Cai notes that the percentage of random numbers generated in step 4 and discarded in step 5a can approach 50%. 13 Cai then develops an algorithm, called the Direct Method for systems with delays, in which no random variables are discarded. We present Cai's Direct Method below, however we refer the reader to Ref. 13 for full details.
The principle of Cai's Direct Method is the same as that of the original Gillespie Algorithm and the Rejection Method above: use one random variable to calculate when the next reaction initiates and use another random variable to calculate which reaction occurs at that future time. However, Cai updates the state of the system and propensity functions due to stored delayed reactions during the search for the next initiation time. In this way he ensures that no random variables are discarded as in the Rejection Method.
Suppose that at time t there are ongoing delayed reactions set to complete at times t + T 1 , t + T 2 , . . . , t + T d . Define T 0 = 0 and T d+1 = ∞. According to Cai's Direct Method, in order to calculate the time until the next reaction initiates, we first ask if the reaction takes place before t + T 1 . If so, we may perform the step. If not, we must update the system according to the completion of the reaction due to complete at time t + T 1 , update our propensity functions, and ask if the reaction takes place between t + T 1 and t + T 2 . In this manner we will eventually find when the next reaction initiates. Following the lead of Cai, we first present a method used for generating ∆. 13

Algorithm 5. (∆ generation for the Direct Method for systems with delays)
1. Input the time t and a 0 = k a k .
iii. Calculate the propensity functions a k (t + T i ) due to the finish of the delayed reaction at t + T i , and calculate a 0 (t + T i ).
iv. Set a t = a t + a 0 (t + T i )(T i+1 − T i ).
v. If i > 1 update the state vector x due to the finish of the delayed reaction at

EndIf
Because T 1 , . . . , T d are needed to perform the simulation, Cai introduces a d × 2 matrix, T struct, whose ith row contains T i and the index µ i of the reaction due to complete at time t + T i . During a simulation, if we find that ∆ ∈ [T i , T i+1 ), we delete rows 1 through i of T struct and set T j = T j − ∆ for all of the other delay times. Also, rows are added to T struct when delayed reactions are initiated in such a way that we always maintain T struct(i, 1) < T struct(i + 1, 1). We present Cai's direct method below. 2. Calculate the propensity function, a k , for each reaction.
3. Set a 0 = M k=1 a k . 4. Generate ∆ via Algorithm 5. If ∆ ∈ [T i , T i+1 ) update T struct by deleting rows 1 through i and update the other delay times as described in the above paragraph.
6. Find µ ∈ [1, . . . , m] such that where the a k 's and a 0 are generated in step 4.
7. If µ ∈ ND, update the number of each molecular species according to reaction µ.
9. If µ ∈ ICD, update the system according to the initiation of µ and update T struct by adding the row [τ µ , µ] so that T struct(i, 1) < T struct(i + 1, 1) still holds for all i. 11. Return to step 2 or quit.
We note that the Direct Method will use precisely one random number to find each initiation time. In this way the Direct Method is more efficient than the Rejection Method, which discards a ∆ (and therefore a random number) each time a delayed reaction completes.
However, the extra machinery built into the Direct Method in order to find ∆ will slow the algorithm as compared with the Rejection Method. Therefore, it is not immediately clear which method will actually be faster on a given system.

B. The modified Next Reaction Method for systems with delays
We now extend our modified Next Reaction Method to systems with delays. Recall that the central idea behind the modified Next Reaction Method is that knowledge of the internal time at which Y k fires next can be used to generate the absolute time of the next initiation of reaction k. The same idea works in the case of systems with delays because the initiations are still given by the firing times of independent Poisson processes via equation (15). Therefore, if T k is the current internal time of Y k , P k the first internal time after T k at which Y k fires, and the propensity function for the kth reaction channel is given by a k , then the time until the next initiation of reaction k (assuming no other reactions initiate or complete) is still given by ∆t k = (P k − T k )/a k . The only change to the algorithm will be in keeping track and storing the delayed completions. To each delayed reaction channel we therefore assign a vector, s k , that stores the completion times of that reaction in ascending order. Thus, the time until there is a change in the state of the system, be it an initiation or a completion, will be given by where t is the current time of the system. These ideas form the heart of our Next Reaction Method for systems with delays: Algorithm 7. (Next Reaction Method for systems with delays) 6. Set t = t + ∆.
7. If we chose the completion of the delayed reaction µ: • Update the system based upon the completion of the reaction µ.
• Delete the first row of s µ .
8. Elseif reaction µ initiated and µ ∈ ND • Update the system according to reaction µ.

Elseif reaction µ initiated and µ ∈ CD
• Update s µ by inserting t + τ µ into s µ in the second to last position. 10. Elseif reaction µ initiated and µ ∈ ICD • Update the system based upon the initiation of reaction µ.
• Update s µ by inserting t + τ µ into s µ in the second to last position.
11. For each k, set T k = T k + a k ∆.
14. Return to step 4 or quit.
We note that after the first step, the Next Reaction Method for systems with delays only generates one random variable for each initiation as opposed to the two generated in the Direct Method. Further, Algorithm 7 performs the updates in a way that uses every random variable that is calculated yet does not have the complicated machinery necessary in the Direct Method. We should therefore expect that Algorithm 7 will need less time in the simulation of chemical reaction systems with delays then either the Rejection or Direct Method. We also note that similar to our modified Next Reaction Method, Algorithm 7 extends easily to systems with time dependent rate constants, and non-exponential waiting times between initiations.

C. Numerical examples
Example 1. Consider the following system consisting of two reaction channels: The reaction channel R 1 belongs to ICD and R 2 belongs to ND. Therefore, we update X 1 = X 1 −1 and X 2 = X 2 −1 at the moment of initiation of R 1 , but only update X 3 = X 3 +1 after a delay. Following Cai, 13 we chose c 1 = 0.001, c 2 = 0.001, X 1 (0) = 1000, X 2 (0) = 1000 and X 3 (0) = 0. We let the delay of R 1 be τ 1 = 0.1 and simulated this system from time t = 0 until t = 1. These values were chosen so that the number of initiations that have delayed completions is approximately 100% of all initiations. Therefore, nearly 50% of all steps of the Rejection Method will discard a random variable, thereby maximizing its wastefulness.
We performed 10 4 simulations using each of the Rejection, Direct, and Next Reaction TProt. There are four reactions allowed in the model: We suppose that reaction one belongs to CD and has a delay of τ = 5. It is simple to show that the mean value of the state of the system has an equilibrium value of (mRNA, T P rot) = ((k 1 k 3 )/(k 2 k 4 ), k 3 /k 4 ), and the mean values of the propensities of the reactions have equilibrium values of Therefore, the expected percentage of the initiations that have delayed completions can be approximated by γ, which is given by For the Rejection Method, the number of discarded ∆'s will be approximately the number of initiations of delayed reactions. Therefore the Rejection Method becomes more wasteful as the percentage of the total reaction initiations that have delayed completions increases, and so we may expect to see that as γ increases the Direct Method will become relatively faster as compared to the Rejection method. To test this we set k 2 = 1, k 3 = 15, and k 4 = 1 so that γ = (1/2)k 1 /(k 1 + 1). k 1 now acts as a parameter that can be changed in order to see the effect γ has on the relative speeds of the two algorithms. We note that the parameters were not chosen for their biological relevance, but instead were chosen for experimental ease.
For a series of k 1 's we computed the CPU time needed for the Direct Method, Rejection Method, and Next Reaction Method for systems with delays to simulate the above system 10 4 times from time 0 to time 30. See Figure 1. We see that as k 1 increases, the Rejection and Direct Methods remain relatively close in terms of efficiency with the Rejection Method being slightly more efficient for smaller k 1 and slightly less efficient for larger k 1 . However, the Next Reaction Method for systems with delays (Algorithm 7), is significantly more efficient than both for all k 1 . Considering that many models of natural cellular processes such as gene transcription and translation have delays between the initiation and completion of reactions, and that the Rejection method appears to be the most widely used method for simulating such systems, we feel that this extension will be useful.
Also, as is pointed out in the text, our modified Next Reaction Method can be easily extended to systems with non-exponential waiting times between initiations and is preferable to both the Gillespie Algorithm and the original Next Reaction Method for systems with propensities that depend explicitly on time. We feel that having a single, efficient simulation method applicable to such a broad range of chemical systems will prove to be a beneficial contribution.
Acknowledgments I would like to thank Thomas G. Kurtz for introducing me to the notion of representing the reaction times of chemical systems with the firing times of independent, unit rate Poisson processes undergoing random time changes and for making the connection between this work and the theory of generalized semi-Markov processes. I would also like to thank an anonymous reviewer for making several suggestions that improved the clarity of this work.
This work was done under the support of NSF grant DMS-0553687.

APPENDIX A: UNFINISHED CALCULATION
In Section V we showed that if a system has propensity functions that depend explicitly on time, then the amount of absolute time, ∆, that must pass after time t before any reaction fires has distribution function t+∆ t a k (X(t), s)ds .
where r is uniform(0, 1). We will sketch the proof of why the reaction that fires at that time will be chosen according to the probabilities a k (X(t), t+∆)/a 0 , where a 0 = M k=1 a k (X(t), t+ ∆).
Let H(r)= M k=1 t+r t a k (X(t), s)ds. For j ≤ M, let ∆t j be the amount of time that must pass after time t before the jth reaction fires. Let F denote the random variable min{∆t j }.

(A1)
It is a simple exercise to show that for any j ≤ M P (∆t j > s) = exp − Methods are nearly equivalent while the Next Reaction Method for systems with delays is significantly more efficient than both for all k 1 .