developing numerical solutions to stochastic di erential ... · nomics, biology, medicine and many...

21
Developing Numerical Solutions to Stochastic Differential Equations for Population Modelling Joachim Worthington May 14, 2012 1 Introduction Modelling is one of the most direct applications of mathematics in areas such as eco- nomics, biology, medicine and many others. Two of the simplest and most widely-used models are the logistic model and the Lotka-Volterra model. Both of these models cor- relate with many real-life situations, and can be used for making basic predictions. However, both models have the limitation that their behaviour is very predictable: always tending to a fixed limit for the logistic model, and always following fixed cycles for the Lotka-Volterra model. Almost all real-life systems will demonstrate complexities that can be thought of as “random”, and so will stray from these models. We thus seek to develop new models which take these two deterministic systems and introduce small random elements, and use these new models to make probabilistic predictions about the system’s long-term behaviour. In this paper, I will first discuss the historical development and current use of the two aforementioned models. I will then implement a stochastic differential equation solver, endeavouring to keep our code as general as possible so it can be reused. I will derive stochastic versions of the two models, and discuss the various interpretations. Finally, I will use the solvers to perform various experiments on these stochastic equations, and discuss the use of our results. 2 Historical Discussion 2.1 The Logistic Model The general concepts of population fluctuations have been intuitively understood for centuries. In the late 18th century, the first widely-known reconciliations between these ideas and mathematical formulations were made, and the field of population modeling was birthed. The early work on these concepts was mostly by non-mathematicians from fields such as biology, medicine, and economics. A notable example is Reverend Thomas Malthus whose essays on the interplay of population and economics suggested the inevitability of a geometric population growth or decay, albeit within a philosophical 1

Upload: others

Post on 22-Jul-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Developing Numerical Solutions to Stochastic Di erential ... · nomics, biology, medicine and many others. Two of the simplest and most widely-used models are the logistic model and

Developing Numerical Solutions to Stochastic Differential

Equations for Population Modelling

Joachim Worthington

May 14, 2012

1 Introduction

Modelling is one of the most direct applications of mathematics in areas such as eco-nomics, biology, medicine and many others. Two of the simplest and most widely-usedmodels are the logistic model and the Lotka-Volterra model. Both of these models cor-relate with many real-life situations, and can be used for making basic predictions.

However, both models have the limitation that their behaviour is very predictable:always tending to a fixed limit for the logistic model, and always following fixed cyclesfor the Lotka-Volterra model. Almost all real-life systems will demonstrate complexitiesthat can be thought of as “random”, and so will stray from these models. We thus seekto develop new models which take these two deterministic systems and introduce smallrandom elements, and use these new models to make probabilistic predictions about thesystem’s long-term behaviour.

In this paper, I will first discuss the historical development and current use of the twoaforementioned models. I will then implement a stochastic differential equation solver,endeavouring to keep our code as general as possible so it can be reused. I will derivestochastic versions of the two models, and discuss the various interpretations. Finally,I will use the solvers to perform various experiments on these stochastic equations, anddiscuss the use of our results.

2 Historical Discussion

2.1 The Logistic Model

The general concepts of population fluctuations have been intuitively understood forcenturies. In the late 18th century, the first widely-known reconciliations between theseideas and mathematical formulations were made, and the field of population modelingwas birthed. The early work on these concepts was mostly by non-mathematiciansfrom fields such as biology, medicine, and economics. A notable example is ReverendThomas Malthus whose essays on the interplay of population and economics suggestedthe inevitability of a geometric population growth or decay, albeit within a philosophical

1

Page 2: Developing Numerical Solutions to Stochastic Di erential ... · nomics, biology, medicine and many others. Two of the simplest and most widely-used models are the logistic model and

rather than analytic context. In more constrained fields, such as the study of bacteria,it was known that the populations would not grow without restriction forever and wouldexhibit some kind of limiting behaviour. These ideas were also (often independently)widely accepted within many other scientific communities.

In 1838, Pierre Francois Verhulsta, a Belgian mathematician, devised the first well-known formulation of these ideas in the logistic model. The logistic model is a verysimple model that demonstrates both exponential-type growth, proportional to the cur-rent population, and a limiting population. The model is usually stated in terms of adifferential equation, as

dX

dt= µX(K −X) (1)

where µ and K are constants. In this formulation, K is the maximum value of X,often known as the carrying capacity for a population. The interpretation of µ is morecomplex, but can be safely thought of as the growth rate; the actual growth rate, dX/dt,is proportional to µ.

This equation admits an exact solution; with the parameters as above, the solutionfor initial condition X(0) = X0 is

X(t) =KX0

X0 + (K −X0)e−µKt(2)

There are two possible behaviours of this model (note that we only consider X(0) ≥ 0,consistent with the usual interpretation of X as a population). If X(0) = 0, there is anunstable equilibrium, and so X(t) = 0 for all t. Otherwise, X(t) will tend towards thestable equilibrium at X = K, as demonstrated in Figure 1.

After its discovery by Verhulst, the model was only used rarely until its “rediscovery”in 1920 by Raymond Pearl and Lowell Reed, who promoted its use (and perhaps misuse)for a multitude of applications. The logistic function (of which equation (2) is a particularcase) is used across disciplines such neural networks, linguistics, and statistics as a generalcontinuum with easily-described dynamics. However, it is of more interest to us as agrowth model. It is used in medicine to model tumour growth, in chemistry to measurereactions, and in biology to predict simple populations. In economics, it is used topredict the growth and density of organisations (see [4]).

The use of a logistic model depends, though, on adherence to fairly restrictive con-straints. For instance when modeling the population of a life-form, the carrying capacityK is rarely constant or predictable, excepting in very controlled laboratory situations.The growth rate too is rarely as simple as in the case above. The biggest factor in thediscrepancy is the existence of outside forces in nearly any population, which is not takeninto account by the logistic model. Newer, better models were developed to overcomethis limitation.

2.2 The Lotka-Volterra Equations

Having identified the limitation of independence in the logistic model, mathematiciansand scientist began developing models for interspecies interaction. A very simple instance

2

Page 3: Developing Numerical Solutions to Stochastic Di erential ... · nomics, biology, medicine and many others. Two of the simplest and most widely-used models are the logistic model and

0 1 2 3 4 5 60

200

400

600

800

1000

1200

1400

1600

Time t

Pop

ulat

ion

X(t

)

K

X0=1500

X0=400

Figure 1: Two solutions to the deterministic Logistic Equation, with K = 1000 andµ = 0.001. Shown are populations with initial conditions 1500(> K) and 400(< K),both of which approach the stable equilibrium at X = K.

of such a model is the Lotka-Volterra model, which is often known as a predator-preymodel. The Lotka-Volterra equations were independently developed by Alfred J. Lotkaand Vito Volterra in the mid-1920s.

The Lotka-Volterra equations are usually given in the general form

dX

dt= αX − βXY dY

dt= −γY + δXY (3)

where X is the prey-like quantity and Y is the predator -like quantity. The prey hasa growth rate, α, proportional to the current prey population, and the predator has adeath rate, γ, proportional to its population. What elevates the model above the logisticmodel, though, is the XY terms. These correspond to the prey being eaten (or otherwisedestroyed) by the predator, and the predator population increasing accordingly.

The Lotka-Volterra equations are fairly standard to analyse. There are equilbriaat the origin and (γ/δ, α/β). The origin is a saddle point (and thus unstable) andthe system exhibits stable cycles around the other equilibrium. We have a conservedquantity

C = αlnY − βY + γlnX − δX (4)

The level curves given by this are closed, which give the periodic motion of X and Y .When plotted against time, the dynamics of the system are made quite clear, with Xexperiencing a peak, followed by Y experiencing a peak, and so on (see Figure 2).

The Lotka-Volterra model is used in many fields. The best known use is in animalpopulation modelling. The model requires two populations which are essentially inde-pendent of all outside factors. While this appears restrictive, examples have been found

3

Page 4: Developing Numerical Solutions to Stochastic Di erential ... · nomics, biology, medicine and many others. Two of the simplest and most widely-used models are the logistic model and

0 0.5 1 1.5 2 2.5 30

500

1000

1500

2000

2500

3000

3500

Time t

Pop

ulat

ion

Predator (Y)Prey (X)

0 500 1000 1500 2000 2500 3000 3500200

400

600

800

1000

1200

1400

1600

1800

2000

2200

Prey Population (X)

Pre

dato

r P

opul

atio

n (Y

)

Figure 2: A solution to the Lotka-Volterra equations, shown as Population vs Time(left) and Predator vs Prey (right). The left graph shows the periodic nature ofthe two populations (predator in red, prey in blue), and the right graph shows thestability of the cycle (the equilibrium, (1400, 1000), is marked with a +). Thesegraphs use α = 10, β = 0.01, γ = 7, δ − 0.005, and an initial condition of (X,Y ) =(500, 1500).

where the conditions are met closely enough that the model gives a good approximationof the relative dynamics of the two populations. For instance, the moose (prey) and wolf(predator) populations in Isle Royale National Park in Canada have been closely studiedthanks to their relative seclusion, and seem to agree fairly closely with the predictionsof the model (see [7]).

However, these results cannot suggest the Lotka Volterra model as a good fit forany particular class of “real-word” population dynamics, even those which seem to beisolated. For instance, Michael Gilpin’s paper Do Hares Eat Lynx? (see [8]) takesmeasured hare and lynx population data and compares them to the predictions of theLotka-Volterra model. Surprisingly, the dynamics are essentially the opposite of whatis predicted: each spike in the hare (prey) population is preceded by a spike in the lynx(predator) population. Although the conclusion reached by Gilpin is that unconsideredoutside forces were to blame, it remains a powerful example of why the Lotka-Volterramodel cannot be applied without judicious consideration of the assumptions.

Another point of note is the absence of extinction in the Lotka-Volterra model. Asa consideration of the level curves given by Equation (4) suggests, the values of both Xand Y can become arbitrarily close to 0, but still return to the levels previously attained.Obviously, in a population this is completely unphysical, and at least a breeding pairof each animal is required to escape the possibility of extinction. One approach toovercoming this issue is to completely discretise the system, an approach taken by Parkerand Kamenev in Extinction in the Lotka-Volterra model (see [9]). Their modified modelsuggests a new consideration for the population model, that of time to extinction.

Like the Logistic equation, Lotka-Volterra equations are also Economics. RichardGoodwin developed a model for the dynamics between wages and employment, which

4

Page 5: Developing Numerical Solutions to Stochastic Di erential ... · nomics, biology, medicine and many others. Two of the simplest and most widely-used models are the logistic model and

has the same dynamics and similar form to that of the Lotka-Volterra equations above.The results of Goodwin’s models have been well studied and hold to be fairly accuratefor real wage-employment cycles.

3 Stochastic Differential Equations

3.1 Modelling populations with Stochastic Differential Equations

A stochastic differential equation, or SDE, is a differential equation in which one or moreof the terms is a stochastic process. This stochastic process is most often a Wienerprocess (also referred to as Brownian motion), or more accurately its derivative, whitenoise. For this project, we will consider SDEs of the form

dXt = µ(Xt)dt+ σ(Xt)dWt (5)

which can equivalently be written as

Xt = X0 +

∫ t

0µ(Xu)du+

∫ t

0σ(Xu)dWu (6)

Wt is our Wiener process, Xt is our unknown, and µ and σ are deterministic functions ofX. This is a time-independent SDE; µ and σ could depend on t as well in more generalSDEs. Also, that we cannot write dX/dt as we would for ordinary differential equations;by its definition, dW/dt does not exist for a Wiener process. This equation quite readilygeneralises to systems of SDEs, which we will deal with in detail later.

When an SDE is given in the form above, we can think of the function µ as the meanor expected change, and σ2 as the variance in the change. With this interpretation, wecan begin to convert what were previously deterministic models into stochastic models.

By way of explanation, we will first consider the logistic model. As with any givendeterministic model,there are multiple corresponding stochastic models determining onour interpretation. Our first step is to break the model down into events and associatedprobabilities over a small time period dt.

For the deterministic logistic model, we have a growth of µX(K − X)dt over a dttime period. For the stochastic model, we can instead consider this as a µX(K −X)dtprobability of an increase of 1. We write this as pgrowth = µX(K−X)dt, ∆Xgrowth = +1(the change in X every time a “growth” event occurs).

Another probability-event interpretation is to expand the equation into (µKX −µX2)dt, and consider the two parts as separate “growth” and “decay” events. This givesus the pairs pgrowth = µKXdt, ∆Xgrowth = +1 and pdecay = −µX2dt, ∆Xdecay = −1.

We can now calculate the expectation µ and variance σ2 by standard statisticalformulae:

µ(X) =E(X)

dt=

∑pevent∆Xevent

dtσ(X)2 =

E(X2)

dt=

∑pevent∆X

2event

dt(7)

Applying these to our two interpretations give the following for µ and σ:

µ1(X) = µX(K −X) σ1(x) =√µX(K −X) (first interpretation) (8)

5

Page 6: Developing Numerical Solutions to Stochastic Di erential ... · nomics, biology, medicine and many others. Two of the simplest and most widely-used models are the logistic model and

µ2(X) = µX(K −X) σ2(x) =√µXK + µX2 (second interpretation) (9)

Note that the first interpretation requires X ≤ K, so the population cannot exceed thecarrying capacity. This must be a consideration during the use of this model. Bothinterpretations require X ≥ 0, but we are considering populations, so this should besatisfied regardless. However, we should notice that the stochastic nature of the modelmakes it entirely possible for negative populations to occur, and this should be taken intoaccount. Another point to note is that in both cases, if we set σ(X) = 0, we retrieve anidentical differential equation to the definition of the logistic equation. This is becausethe σ(X)dWt term is what contributes the “randomness” to the equation; a variancewhich can be positive or negative with equal likelihood.

3.2 Numerical Methods for SDEs

To solve SDEs numerically, a first approach is to reproduce the standard methods forODEs, making small changes. Euler’s method works by calculating subsequent pointsby the taking derivative at a given point; we can adapt this here to give what is termedthe Euler-Maruyama method. For the general SDE in equation (5), we get

∆Xt = µ(Xt)∆t+ σ(Xt)∆Wt (10)

Notable in this equation is the ∆Wt term. By the definition of W , we know that(Wt+∆t − Wt) ∼ N(0,

√∆t). This can be readily calculated and is independent of

previous calculations (due to the Markov property of the Wiener process).One ambiguity in this process is the choice of where to take the derivative from.

Like in the deterministic case, this can cause differences in our calculations. Unlikethe deterministic case, these differences do not disappear in the limit as ∆t → 0. Thishighlights a major consideration in our calculations and give us two different integralsto choose from: the Ito integral and the Stratonovich integral. Both of these integralsgives us a value for

∫σdWt.

The Ito integral is calculated by taking the derivative at the leftmost point, givingthe formula ∫ t

0σ(u)dWu =

N∑i=0

σ(Xti)(Wti+1 −Wti) (11)

The Stratonovich integral takes the derivative at the midpoint, giving∫ t

0σ(u)dWu =

N∑i=0

σ(X ti+ti+12

)(Wti+1 −Wti) (12)

What is notable here is that taking the limits as ∆t → 0 for these two equations willnot give exactly the same result as they do in the deterministic case. This is due tocomplexities involved in the derivation of a stochastic integral (as we shall see later). Forthis project, σ will always be X-dependent, and so we cannot calculate the Startonovichintegral. An awareness of this subtlety will hopefully give us a greater appreciation ofthe errors involved in the results.

6

Page 7: Developing Numerical Solutions to Stochastic Di erential ... · nomics, biology, medicine and many others. Two of the simplest and most widely-used models are the logistic model and

Combining these ideas and using the Ito definition for our integral we get the followingupdate rule:

Xt+∆t = Xt + µ(Xt)∆t+ σ(Xt)∆Wt (13)

which is our defininiton of the Euler-Maruyama numerical method.The Euler method for deterministic equations has an accuracy of order ∆t. Although

this paper will not give a treatment of the concepts of “strong convergence” for numericalapproximations to SDEs (see [12] for these details), the accuracy of the Euler-Maruyamamethod is

√∆t. This loss of accuracy in the stochastic method is due to simplifications

taken during the derivation of the method. In the deterministic model, we take theTaylor series and truncate terms of order ∆t or larger. In the stochastic model, wemust take the Ito-Taylor expansion, which has some key differences. This will not bediscussed in detail (again, see [12]), but taking an extra term gives us the new updaterule:

Xt+∆t = Xt + µ(Xt)∆t+ σ(Xt)∆Wt +1

2σ(Xt)σ

′(Xt)(∆W2t −∆t) (14)

This extra term is called Milstein’s correction term, and this update rule is termedMilstein’s method. Its derivation requires the use of Ito’s lemma, but the consequencefor our purposes is an increase of accuracy. Milstein’s method has accuracy of order∆t, an improvement on the Euler-Maruyama method on par with Euler’s method fordeterministic ODEs.

3.3 Implementing Numerical Methods for SDEs

To study the effectiveness of these numerical schemes, we begin by implementing ageneral C++ class to solve any SDE of the form given by equation (5). For the purpose ofbrevity here we will only reproduce the class definitions as an appendix (see AppendixA).

Unlike most mathematics-focused programming language, C++ does not include astandard package for generating normally-distributed random number. However, it cangenerate random numbers uniformly distributed in the range [0, 1] using the rand() com-mand. We can use the Box-Muller method to use these to generate normally distributednumbers. If we first calculate U and V as random numbers uniformly generated on[0, 1], then R1 =

√−2lnU cos(2πV ) and R2 =

√−2lnU sin(2πV ) are not only normally

distributed but also independent.Having implemented our normal distribution, we can now begin on a SDE class (the

class definition for which can also be found in Appendix A). An SDE object takes as itsarguments two functions, mu and sigma, which correspond to µ and σ in equation (5).The only notable section in this class is the code which actually takes the timestep:

increment() in SDE.cpp

void SDE:: increment(double timestep){

// Increment the SDE by a given timestep

if (milsteinOn){

double W_0=W.value ();

7

Page 8: Developing Numerical Solutions to Stochastic Di erential ... · nomics, biology, medicine and many others. Two of the simplest and most widely-used models are the logistic model and

W.increment(timestep);

double W_1=W.value ();

double dW=W_1 -W_0;

//The actual timestep

double dX=mu(X)*timestep+sigma(X)*dW+0.5* sigma(X)*dsigma(

X)*(dW*dW -timestep);

X+=dX;

t+= timestep;

} else {

double W_0=W.value ();

W.increment(timestep);

double W_1=W.value ();

double dW=W_1 -W_0;

double dX=mu(X)*timestep+sigma(X)*dW;

X+=dX;

t+= timestep; } }

We are required to supply the dsigma function, which must be hand-coded. For thisimplementation, I decided not to calculate σ′ on-the-fly, as it would introduce errors ofan unknown order. The σ functions we are using are fairly simple, so it is a much saferproposition to just calculate the derivative beforehand and hand-code it.

4 The Stochastic Logistic Model

We now have the tools to implement and test the stochastic models we formulated above.We create functions for the equations (8) and (9)

Snippet: Stochastic Logistic model

#define MU 0.001

#define K 1000

double lgstc_mean(double X){

return MU*X*(K-X);}

double lgstc_drift1(double X){

//For the "Growth Events Only" interpretation

if (X>K || X<0) return 0;

return sqrt(MU*X*(K-X));}

double lgstc_d_drift1(double X){

//For the "Growth Events Only" interpretation

if (X>K || X<0) return 0;

return ((K-2*X)*sqrt(MU))/(2* sqrt(MU*X*(K-X)));}

double lgstc_drift2(double X){

//For the "Growth and Decay Events" interpretation

if (X<0) return 0;

return sqrt(MU*X*(K+X));}

double lgstc_d_drift2(double X){

//For the "Growth and Decay Events" interpretation

8

Page 9: Developing Numerical Solutions to Stochastic Di erential ... · nomics, biology, medicine and many others. Two of the simplest and most widely-used models are the logistic model and

0 2 4 6 8 10 120

100

200

300

400

500

600

700

800

900

1000

1100

Time

Val

ue (

X)

Deterministic ModelModel 1Model 2

Figure 3: Example realisations of the our stochastic Logistic models, withX(0) = 10K = 1000 µ = 0.01 dt = 0.01. Model 2, unlike model 1, can have a populationabove 1000. Also model 2 is “more random”; this is to be expected, as its driftterm will always be larger than model 2’s drift term. These plots use Milstein’scorrection term.

if (X<0) return 0;

return ((K+2*X)*sqrt(MU))/(2* sqrt(MU*X*(K+X)));}

We can see the results of two simulations in Figure 4. By comparing these andother realisations of our two models, it seems acceptable to claim that the second modelwith seperate growth and decay events is preferable. The first model has a very lowvariance, rarely straying very far from the shape of the exact solution, and cannot allowfor populations above K. The second model has a relatively high variance and continuesto oscillate after the population approximately reaches the carrying capacity. We willthus use the second model in our next experiments.

Our next step is to gauge the benefits of Milstein’s method over the Euler-Maruyamamethod. It is difficult to gauge exactly how “accurate” a stochastic method is by itsvery nature. We can look at the mean over many realisations, which should hopefullycorrespond closely to the solution calculated by the deterministic model. The resultsof this are shown in Figure 4. Having confirmed that the number of realisations wassufficient for a reliable result, we can see that there is a consistent improvement by usingMilstein’s correction term (excluding after t = 10, where the results are not smoothenough to give any definitive conclusion). Although there is still a fairly significant errorcomponent with Milstein’s correction turned on, it is still a definite improvement overthe Euler-Maruyama method. We can suggest at this point that the remaining error is

9

Page 10: Developing Numerical Solutions to Stochastic Di erential ... · nomics, biology, medicine and many others. Two of the simplest and most widely-used models are the logistic model and

0 2 4 6 8 10 120

5

10

15

20

25

30

Time t

Err

or in

X

Milstein’s MethodEuler−Maruyama

Figure 4: Difference between the average of 12,000 realisations of the Logistic Equa-tion SDE (9) and the value calculated with the deterministic model. The parametersused are X(0) = 10,dt = 0.1, µ = 0.001, K = 1000. The red line shows the resultwith Milstein’s correction term turned on, and the blue line shows the result withthe standard Euler-Maruyama method.

most likely caused by the (necessary) use of Ito Integrals over Stratonovich Integrals,and/or by the ignored ∼ dt3/2 terms in the Ito-Taylor expansion.

Another notable quantity for us to measure is the time taken to reach the carryingcapacity. We can take many realisations and graph the time taken to get to populationsK/2 and K. The results of these experiments are shown in Figure 5, and are very muchwhat we would expect. The results show an approximately normal distribution witha mean approximately that predicted by the exact solution. (The exact solution, forthe parameters we used, has a population of K/2 at t = ln(99) ≈ 4.60). We can alsoanalyse the number of realisations required to get a good estimate for the average andvariance of time for the population to reach K. The results of these experiments arefairly self-explanatory and are included in Figure 6.

10

Page 11: Developing Numerical Solutions to Stochastic Di erential ... · nomics, biology, medicine and many others. Two of the simplest and most widely-used models are the logistic model and

3.5 4 4.5 5 5.5 6 6.50

0.5

1

1.5

2

2.5x 10

4

Num

ber

of r

ealis

atio

ns

Time to reach population of K/26 8 10 12 14 16

0

0.5

1

1.5

2

2.5x 10

4

Time to reach population of K

Num

ber

of r

ealis

atio

nsFigure 5: Histograms of the times taken over many realisations to reach a populationof K/2 (at left) and K (at right). 100,000 realisations were taken, with K = 1, 000,µ = 0.001, X(0) = 10, and dt = 0.1. The average time to reach K/2 was 4.9168,and the average time taken to reach K was 9.2848.

0 200 400 600 800 10008.5

9

9.5

10

10.5

11

11.5

Cum

ulat

ive

Mea

n

Number of realisations

9.3

0 200 400 600 800 10001

2

3

4

5

6

7

8

Number of realisations

Cum

ulat

ive

Var

ianc

e

2.19

Figure 6: At left: a cumulative plot of the mean time taken to reach population ofK over the first N realisations, with N taken from 1 to 1000. At right, the sameplot showing the variance. We can visually identify that most fluctuations havecalmed after about 500 realisations, suggesting 9.3 as a good estimate for the mean,and 2.19 as a good estimate for the variance.

11

Page 12: Developing Numerical Solutions to Stochastic Di erential ... · nomics, biology, medicine and many others. Two of the simplest and most widely-used models are the logistic model and

5 Implementing The Stochastic Lotka-Volterra Model

We now turn our attention to the Lotka-Volterra equations. Unlike the logistic equation,the Lotka-Volterra model consists of a pair of coupled differential equations, and as suchour stochastic version will be a pair of coupled SDEs. We must therefore begin byadjusting some of the theory we developed in Section 3.

5.1 Systems of SDEs

We begin by taking the n-dimensional extension of equation (5)

dXt = µ(Xt)dt+ B(Xt)dWt (15)

where we now have X : R → Rn and µ : Rn → Rn. W = (W1,W2, ...Wn)T where Wi

are independent wiener processes. We also must consider the introduction of the symbolB : Rn → Rn×n, which replaces the σ of the one-dimensional equation. B is the matrixsuch that B2 = V, where V is the covariance matrix associated with the SDE.

To see how this covariance matrix is calculated, we reformulate the equations at (7)as follows

µ(X) =E(∆X)

dt=

∑pevent∆Xevent

dtV(X) =

E(∆X∆XT )

dt=

∑pevent∆Xevent∆XT

event

dt(16)

5.2 Calculating B

The difficulty now becomes the calculation of B = V1/2. Fortunately, we have twomethods to calculate such a B.

The most straightforward way is to simply calculate a suitable square root matrix byalgebraic methods. We are only dealing with a system of two SDEs in this project, andthe matrix V is both positive semi-definite and symmetric by its definition as a covariancematrix. This dramatically simplifies the otherwise daunting task of calculating its squareroot. In fact, the “square root” of a positive semi-definite matrix always exists and isunique up to a factor of −1 (see [13]). We can thus feel fairly secure in our use ofalgebraic methods (see [14] for full details).

Another way to calculate B is a method known as Chemical-Langevin decomposition.This method is more heuristic, but may give B as a non-square matrix. We have that

V(X) =

∑pevent∆Xevent∆XT

event

dt=∑

(

√pevent

dt∆Xevent)(

√pevent

dt∆Xevent)

T (17)

We can thus define the columns of B by

Bi =

√pidt

∆Xi (18)

where Bi is the i-th column of B. Now clearly BBT = V, as required. This has theadvantage of being calculated naturally from our interpretation of the system. However,

12

Page 13: Developing Numerical Solutions to Stochastic Di erential ... · nomics, biology, medicine and many others. Two of the simplest and most widely-used models are the logistic model and

it has the disadvantage that B is not necessarily a square matrix, so we have to reconsiderequation (15). Fortunately, the only mathematical difference is that we will now needto consider an m-dimensional Wiener process W = (W1,W2, ...Wm)T where m is thenumber of events in our interpretation (and again Wi are independent). It does requireextra work when programming our code, but this should not introduce any numericalproblems.

One final point of note is our use of Milstein’s correction term for multi-dimensionalSDEs. Unfortunately, the Milstein scheme for a multi-dimensional problem is fairlytechnical:

Xt+∆t = Xt + µ(Xt)∆t+ B(X)∆Wt +∑j,k,l

1

2blk∂bij∂Xl

(∆Wj∆Wk − δjk∆t−Ajk) (19)

where δ is the Kronecker delta and Aj,k is a Levy area. These details will not be discussedfurther, as the amount of extra code required to implement this correction would notbe worth the minor improvement in accuracy. This project is more concerned with the“big picture”, so such detail is unnecessary.

Implementing these generalisations in C++ is fairly straightforward, minus allowingfor non-square B. Because C++ does not natively support complicated matrix operations,we make the decision to keep our code for square B as general as possible, and writea new class to solve problems with the Chemical-Langevin decomposition. This is notideal, but sufficient for this project. As before, class definitions are included in theappendix.

5.3 The Stochastic Lotka-Volterra Model

We now turn our attention back to the Lotka-Volterra equations. The process of con-verting the deterministic equations into stochastic versions is essentially the same as forthe Logistic equation, and again we will provide two interpretations.

Our first, perhaps naıve interpretation, is to take four independent events as follows:

p1 = αX ∆X1 =

(+10

), p2 = βXY ∆X2 =

(−10

)(20)

p3 = γY ∆X3 =

(0−1

), p4 = δXY ∆X4 =

(0

+1

)which gives us

µ(X) =

(αX − βXY−γY + δXY

)V =

(αX + βXY 0

0 γY + δXY

)(21)

(Note that X(t) =

(X(t)Y (t)

))

This interpretation essentially amounts to each population having its own Wienerprocess. We can probably conclude, based on our experience with the Logistic equation,that this will be a less useful interpretation.

13

Page 14: Developing Numerical Solutions to Stochastic Di erential ... · nomics, biology, medicine and many others. Two of the simplest and most widely-used models are the logistic model and

Our next interpretation is to link the terms in XY . We take

p1 = X ∆X1 =

(+α0

), p2 = XY ∆X2 =

(−β+δ

), p3 = Y ∆X3 =

(0−γ

)(22)

which gives

µ(X) =

(αX − βXY−γY + δXY

)V =

(α2X + β2XY −βδXY−βδXY γ2Y + δ2XY

)(23)

By using these formulae in equation (15), we have constructed two SDE systems forthe Stochastic Lotka-Volterra equations.

6 Testing the Lotka Volterra Model

We can now implement our above interpretations using the coupled SDE solver we pre-viously implemented. We have four models to test: the first and second intepretation,each of which has a covariance matrix implementation and a Chemical-Langevin imple-mentation. Plots of typical realisations for each of these are included in figure 7. We setα = γ = 1 and β = δ = 0.001, which gives an equilibrium at (γ/δ, α/β) = (1000, 1000).Our results reflect our expectation of counter-clockwise cycles around this equilibriumin (X,Y ) space.

Having established that our solver is working as expected, we turn our attention toother possible behaviours for the model. The plots in figure 7 show our results for aninitial condition near but not on the equilibrium. The results when the simulations arerun for an initial condition at the equilibrium are very similar. The stochastic termensures that the populations almost immediately escape the equilibrium position, atwhich point they exhibit the same vague clockwise cycles as in figure 7.

The other behaviour of our system to check is the other equilibrium at (0, 0. In thedeterministic model this is a saddle point. In general, any population that starts at 0 willremain at 0 for all time. This makes sense, physically. However, the stochastic nature ofour model means that it is entirely possible for a population of exactly zero to rebuilditself. A few tests reveal this does indeed occur, although in almost all realisations thefluctuations remain less than one over a sensible time-scale. It does however suggest anew use for our model: we can measure the probability of going extinct after a giventime.

We will measure the probability of the predator Y going extinct, as X’s extinctionprecludes Y ’s growth but not vice versa. Thus if we model Y staying extinct, X’s limitingbehaviour is exponential growth. The model will thus be stopped when Y drops below2, corresponding to a breeding pair. We take as our initial condition the equilibrium,and take many realisations. The model we will use for this experiment is the Chemical-Langevin decomposition of the second interpretation. The second interpretation seemsmore logical, as the decay of X and the growth of Y should be intrinsically linked.The logical advantage of using the Chemical-Langevin decomposition is that each event

14

Page 15: Developing Numerical Solutions to Stochastic Di erential ... · nomics, biology, medicine and many others. Two of the simplest and most widely-used models are the logistic model and

Interpretation 1 Interpretation 2

Cov

ari

an

ceM

atri

x

0 200 400 600 800 1000 1200 1400 1600 18000

200

400

600

800

1000

1200

1400

1600

1800

X

Y

0 200 400 600 800 1000 1200 1400 1600 18000

200

400

600

800

1000

1200

1400

1600

1800

X

Y

Ch

emic

al-L

ange

vin

0 200 400 600 800 1000 1200 1400 1600 18000

200

400

600

800

1000

1200

1400

1600

1800

X

Y

0 200 400 600 800 1000 1200 1400 1600 18000

200

400

600

800

1000

1200

1400

1600

1800

X

Y

Figure 7: Typical realisations for our four Stochastic Lotka-Volterra models. Theleft plots use the first interpretation at equation (20). The right plots use thesecond, equation (22). The top plots use a covariance matrix, while the bottomplots use Chemical-Langevin decomposition. All plots are shown in X − Y space

(where X =

(XY

)), with α = γ = 1, β = δ = 0.001 (giving an equilibrium at

(1000, 1000) and dt = 0.01). The initial condition is X =

(1100900

). The realisations

are each run until t = 1, 000 (taking t/dt = 100, 000 timesteps). As expected, wesee anti-clockwise motion around the equilibrium. All four models show the cyclesusually getting progressively larger, suggesting the stochastic aspect of the modelintroduces some kind of instability. The top left plot shows the least instability; thismakes sense, as the covariance matrix for this model is always smaller than that ofthe other interpretation. Of the two plots using Chemical-Langevin decomposition,the first interpretation is consistently less stable; this can probably be attributedto the introduction of a fourth Wiener process. In subsequent experiments, we willuse the bottom-right model.

15

Page 16: Developing Numerical Solutions to Stochastic Di erential ... · nomics, biology, medicine and many others. Two of the simplest and most widely-used models are the logistic model and

0 200 400 600 800 10000

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

Time t

Pro

babi

lity

of p

reda

tor

extin

ctio

n

Figure 8: The probability of the predator population being extinct (less than abreeding pair) at any given time. The parameters are α = γ = 1 and β = δ = 0.001,and we started our simulation at the equilibrium X = Y = 1000. The model used isthe Chemical-Langevin decomposition for our second interpretation, equation (22).We took 10,000 realisations, which takes a relatively considerable computing time,but gives a pleasingly smooth curve. Our results show that our stochastic modelwill almost certainly (the phrase “almost certainly” here being used in a statisticalas much as a literal sense) end in the extinction of the predators sooner or later. Itmust be noted that the timescales involved here are probably quite large, so it isunsurprising that extinction is reached. In an actual model the cycles around theequilibrium can represent decades, so our t corresponds to an order of years if notdecades. This kind of model analysis helps us get a good sense of the long-termdynamics of a system.

16

Page 17: Developing Numerical Solutions to Stochastic Di erential ... · nomics, biology, medicine and many others. Two of the simplest and most widely-used models are the logistic model and

should logically have an independent random element, which the pure covariance matrixdoes not allow for. The results of our experiments are shown in Figure 8. Also includedbelow is the code used to generate this, as a demonstration of the use of our systems.We use C++ to carry out our calculations, then MATLAB to plot the results. Hopefullyour code will not require further explanation.

Extract from Test.cpp to generate the data

#define ALPHA 1 // Constants

#define BETA 0.001

#define GAMMA 1

#define DELTA 0.001

#define REALISATIONS 10000

//For the Lotka -Volterra model

double myMu(double X_0 , double X_1 , int row) {

if (row ==0) return ALPHA*X_0 -BETA*X_0*X_1;

return -GAMMA*X_1+DELTA*X_0*X_1; }

//For the Chemical_Langevin decomposition , second interpretation

double my_event2_1(double X_0 , double X_1 , int row) {

if (row ==0) return sqrt(X_0)*ALPHA;

return 0; }

double my_event2_2(double X_0 , double X_1 , int row) {

if (row ==0) return -sqrt(X_0*X_1)*BETA;

return +sqrt(X_0*X_1)*DELTA; }

double my_event2_3(double X_0 , double X_1 , int row) {

if (row ==0) return 0;

return -sqrt(X_1)*GAMMA ;}

int main() {

ofstream out1("lvExtinct.txt");

srand ( time(NULL) );

double X0=1000, Y0 =1000;

double dt =0.01;

int times =1000/ dt;

SDE2D lvChem[REALISATIONS ];

for (int i=0; i<REALISATIONS; i++) {

lvChem[i].setMu(myMu);

lvChem[i]. setValue(X0 ,0);

lvChem[i]. setValue(Y0 ,1);

lvChem[i]. setTimestep(dt);

lvChem[i]. setChem(true);

lvChem[i]. addEvent(my_event2_1);

lvChem[i]. addEvent(my_event2_2);

lvChem[i]. addEvent(my_event2_3);

while (lvChem[i]. value (1) >=2)

lvChem[i]. increment ();

out1 <<lvChem[i].now()<<’\n’;

17

Page 18: Developing Numerical Solutions to Stochastic Di erential ... · nomics, biology, medicine and many others. Two of the simplest and most widely-used models are the logistic model and

return 0;

}

PlotScript.m to plot the graph

clear all

fid = fopen(’lvExtinct.txt’);

temp=textscan(fid ,’%f32’);

X=temp {1};

size =1300;

prob=zeros(size ,1);

for i=1: size

prob(i)=sum(X<i)/10000;

end;

plot(prob ,’k’);

hold on

plot(ones(size ,1),’k:’);

axis ([0 1000 0 1.05]);

xlabel(’Time t’);

ylabel(’Probability of Predator extinction ’);

The results of these experiments (shown in Figure 8) demonstrate two things: thatour system is powerful enough to quickly and easily give consistent results, and that wecan use these stochastic models to give more nuanced predictions about predator-preysystems by dealing in probabilities rather than absolutes.

7 Conclusion

One of the primary goals with this project was to develop all our code as generally aspossible, so it could be quickly and easily adapted for a variety of models. Althoughit was not always successful in this regard (in particular with the implementation ofChemical-Langevin decompositions), the basic framework is fairly flexible and couldwith some work be made more modular and usable.

These issues aside, the mathematical results from our models were very helpful,and relatively easily obtained. The development of the SDEs for the two models wasstraightforward, and by analysing sample paths for our various interpretations, we couldmake decisions as to which models were most appropriate, and see visually the variousqualities of the different models. This gave a very strong idea of the strengths andlimitations of using an SDE to model a system over an ODE.

Having implemented the SDEs, a difficulty that presents itself is how to use themto make statements about the model. Because of the random nature, very little can beconcluded from viewing a small number of realisations of any of our models. However,taking a large number of realisations and measuring key quantities gave us excellentdata for making long-term predictions. Particularly useful are the “time to extinction”graphs, which have very smooth results and could be used to make (hopefully reasonablyaccurate) predictions about the long-term behaviour of a given system. By developingour models further, more of these very useful probabilistic long-term predictions can bemade.

18

Page 19: Developing Numerical Solutions to Stochastic Di erential ... · nomics, biology, medicine and many others. Two of the simplest and most widely-used models are the logistic model and

A Appendix: Class Definitions

SDE.h (class definition for an SDE)

#pragma once

#ifndef PI

#define PI 3.141592654

#endif

class SDE

{

// Calculate an SDE of the form dX=alpha(X)dt + beta(X)dW (time

independent SDE)

public:

SDE();

SDE(double (*m)(double),double (*s)(double), double X0 , double

timestep ,int);

SDE(double (*m)(double),double (*s)(double), double (*ds)(double)

, double X0, double timestep ,int);

~SDE(void);

// Increment by the default timestep

void increment ();

// Increment by a custom timestep

void increment(double timestep);

// Increment forward to a given time

void incrementToT(double T);

// Return the current value for the SDE

double value ();

// Return the current time

double now();

//Set the function to use for the mean

void setMu(double (*m)(double));

//Set the function to use for the drift

void setSigma(double (*s)(double));

//Set the drift derivative (for Milstein)

void setdSigma(double (*db)(double));

//Turn Milstein ’s correction on/off

void setMilstein(bool flag);

//Set a value (for initial condition)

void setValue(double val);

//Set the default timestep

void setTimestep(double val);

void seed(int);

private:

double (*mu)(double);

double (*sigma)(double);

double (* dsigma)(double);

double dt;

double X;

double t;

bool milsteinOn; //True if using the Milstein correction

double normal(double , double);

19

Page 20: Developing Numerical Solutions to Stochastic Di erential ... · nomics, biology, medicine and many others. Two of the simplest and most widely-used models are the logistic model and

};

SDE2D.h (class definition for system of SDE)

#pragma once

#ifndef PI

#define PI 3.141592654

#endif

class SDE2D

{

// Calculate an pair of coupled SDEs

// of the form dX= mu dt + B dW (time independent)

public:

SDE2D ();

SDE2D ::SDE2D(double (* muVal)(double ,double ,int),double (*VVal)(

double ,double ,int ,int),double ,double ,double ,bool ,int);

~SDE2D ();

void seed(int);

void increment ();

// Increment by the default timestep

double value(int n); // Returns

the current value of nth variable

double now();

// Return current time

void setMu(double (* mu_val)(double ,double ,int)); //Set the

mean vector

void setV (double (*V_val)(double ,double ,int ,int));

//Set the covariance matrix

void setValue(double ,int); //Used to

set initial value

void setTimestep(double); //Set the default

timestep

double sqrtV(int ,int);

void setChem(bool);

void addEvent(double (* event_in)(double ,double ,int));

private:

double (*mu)(double ,double ,int);

double (*V)(double ,double ,int ,int);

double dt;

double sqdt; // precompute the square root of dt to save time

double X[2];

double t;

bool chemicalOn;

int noEvents;

double (* event1)(double ,double ,int); // Events for CL

decomposition

double (* event2)(double ,double ,int);

double (* event3)(double ,double ,int); // Maximum of four

double (* event4)(double ,double ,int);

20

Page 21: Developing Numerical Solutions to Stochastic Di erential ... · nomics, biology, medicine and many others. Two of the simplest and most widely-used models are the logistic model and

double normal(double , double);

};

References

[1] S. E. Kinglsand Modeling Nature: Episodes in the History of Population EcologyUniversity of Chicago Press (October 1985)

[2] R. McIntosh The Background of Ecology Cambridge University Press (1985)

[3] E. Weisstein Logistic Equation From MathWorld–A Wolfram Web Resource.http://mathworld.wolfram.com/LogisticEquation.html

[4] A. D. Zimm Derivation of a Logistic Equation for Organizations, and its Expan-sion into a Competitive Organizations Simulation Computational & MathematicalOrganization Theory, 11, 37–57 (2005)

[5] A.J. Lotka Elements of Physical Biology Williams and Wilkins (1925)

[6] V. Volterra Variations and fluctuations of the number of individuals in animal speciesliving together in Animal Ecology Chapman, R.N. (ed), McGraw–Hill (1931)

[7] C. Jost, G. Devulder, J. A. Vucetich, R. Peterson, R. Arditi The wolves of Isle Royaledisplay scale-invariant satiation and density dependent predation on moose J. Anim.Ecol., 74(5), 809-816 (2005)

[8] M. E. Gilpin Do Hares Eat Lynx? The American Naturalist, Vol. 107, No 957, pg727-730 (1973)

[9] M. Parker, A. Kamenev Extinction in the Lotka-Volterra model Physical Review E80 (2009)

[10] V. Vadasz Economic Motion: An Economic Application of the Lotka-VolterraPredator-Prey Model Unpublished (2007)

[11] E. Allen Modeling with Ito Stochastic Differential Equations Springer (2007)

[12] D. J. Higham An Algorithmic Introduction to Numerical Simulation of StochasticDifferential Equations SIAM Review Vol. 43, No. 3 Pg 525-546 (2001)

[13] M. Koever, U. Schafer The unique square root of a positive semidefinite matrixInternational Journal of Mathematical Education in Science and Technology Volume37, Issue 8, (2006)

[14] P. C. Somayya A Method for finding a Square Root of a 2x2 Matrix The Mathe-matics Education Vol. XXXI, No. 1, (1997)

[15] D. P. Kroese, T. Taimre, Z. I. Botev Handbook of Monte Carlo Methods WileySeries in Probability and Statistics (2011)

21