16
Stochastic Simulation using MATLAB Systems Biology Recitation 8 11/04/09

Stochastic Simulation using MATLAB - MITweb.mit.edu/biophysics/sbio/PDFs/R8.pdf · Poisson Processes •Fixed number of steps ... –Steady state distribution is Poisson ... Stochastic

Embed Size (px)

Citation preview

Page 1: Stochastic Simulation using MATLAB - MITweb.mit.edu/biophysics/sbio/PDFs/R8.pdf · Poisson Processes •Fixed number of steps ... –Steady state distribution is Poisson ... Stochastic

Stochastic Simulation using MATLAB

Systems Biology Recitation 8

11/04/09

Page 2: Stochastic Simulation using MATLAB - MITweb.mit.edu/biophysics/sbio/PDFs/R8.pdf · Poisson Processes •Fixed number of steps ... –Steady state distribution is Poisson ... Stochastic

clivejames.com

Page 3: Stochastic Simulation using MATLAB - MITweb.mit.edu/biophysics/sbio/PDFs/R8.pdf · Poisson Processes •Fixed number of steps ... –Steady state distribution is Poisson ... Stochastic

Random Numbers from Simple Distributions

• Uniform Distribution

– Pick a number randomly between 0 and 1

– rand(1,1); rand(m,1);

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

2

4

6

8

10

12

14

t = rand(100,1)

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

200

400

600

800

1000

1200

t = rand(10000,1)

Page 4: Stochastic Simulation using MATLAB - MITweb.mit.edu/biophysics/sbio/PDFs/R8.pdf · Poisson Processes •Fixed number of steps ... –Steady state distribution is Poisson ... Stochastic

Random Numbers from Simple Distributions

• Zero-one Distribution

– Flip a coin, let head = 0, tail =1

– (rand(1,1)<0.5); (rand(m,1)<0.5);

– For a biased coin with P(tail) = p, (rand(m,1)<p);

0 0.5 10

200

400

600

random number generated

frequency

Unbiased coin

0 0.5 10

200

400

600

800

1000

random number generated

frequency

Biased coin with p = 0.1

Page 5: Stochastic Simulation using MATLAB - MITweb.mit.edu/biophysics/sbio/PDFs/R8.pdf · Poisson Processes •Fixed number of steps ... –Steady state distribution is Poisson ... Stochastic

Random Numbers from Simple Distributions

• Toss a die

– temp = rand(1,1);

– die =sum((temp<([1:6]/6)&(temp>(([1:6]-1)/6))).*[1:6]);

0 2 4 60

500

1000

1500

2000

random number generated

frequency

Page 6: Stochastic Simulation using MATLAB - MITweb.mit.edu/biophysics/sbio/PDFs/R8.pdf · Poisson Processes •Fixed number of steps ... –Steady state distribution is Poisson ... Stochastic

Random Numbers from Simple Distributions

• Normal Distribution

– Pick a number from normally distributed random numbers from 0 and 1

– randn(1,1); mu+sigma.*randn(m,1)

-2.5 -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 2.50

5

10

15

20

25

t = randn(100,1)

-5 -4 -3 -2 -1 0 10

50

100

150

200

250

300

350

400

t = -2+sqrt(0.5).*randn(10000,1)

Page 7: Stochastic Simulation using MATLAB - MITweb.mit.edu/biophysics/sbio/PDFs/R8.pdf · Poisson Processes •Fixed number of steps ... –Steady state distribution is Poisson ... Stochastic

http://www.mathworks.com/access/helpdesk/help/toolbox/stats/index.html?

Page 8: Stochastic Simulation using MATLAB - MITweb.mit.edu/biophysics/sbio/PDFs/R8.pdf · Poisson Processes •Fixed number of steps ... –Steady state distribution is Poisson ... Stochastic

Poisson Processes

– Events occur independent of each other

– 2 events cannot occur at the same time point

– The events occur with constant rates

Page 9: Stochastic Simulation using MATLAB - MITweb.mit.edu/biophysics/sbio/PDFs/R8.pdf · Poisson Processes •Fixed number of steps ... –Steady state distribution is Poisson ... Stochastic

Gillespie Algorithm

• Generate random numbers to determine the time it takes for

the next reaction to occur

L13 (A. Van Oudenaarden – MIT – 2009)

0 0.5 10

500

1000

1500

random number generated

frequency

u

0 1 2 3 40

2000

4000

6000

8000

random number generated

fre

qu

en

cy

Page 10: Stochastic Simulation using MATLAB - MITweb.mit.edu/biophysics/sbio/PDFs/R8.pdf · Poisson Processes •Fixed number of steps ... –Steady state distribution is Poisson ... Stochastic

Poisson Processes

• Fixed number of steps

– m simulated inter-arrival times

– interarr = [0;–log(rand(m,1))./r];

– stairs(cumsum(interarr), 0:m);

0 50 1000

20

40

60

80

100interarr = [0;-log(rand(100,1))./r];

time

nu

mb

er

of

ste

ps

r = 1

r = 10

r = 100

0 50 100 1500

20

40

60

80

100

time

num

ber

of

ste

ps

interarr = [zeros(1,50);-log(rand(100,50))./1]

Page 11: Stochastic Simulation using MATLAB - MITweb.mit.edu/biophysics/sbio/PDFs/R8.pdf · Poisson Processes •Fixed number of steps ... –Steady state distribution is Poisson ... Stochastic

Poisson Processes

• 2 competing processes with rates r1 and r2

– time_r1 = -log(rand(1,1))/r1;

– time_r2 = -log(rand(1,1))/r2;

– Compare time_r1 and time_r2, the reaction with smaller waiting time happens first

– time = -log(rand(2,1))./[r1;r2];

– time_r1 = time(1,:); time_r2 = time(2,:);

Page 12: Stochastic Simulation using MATLAB - MITweb.mit.edu/biophysics/sbio/PDFs/R8.pdf · Poisson Processes •Fixed number of steps ... –Steady state distribution is Poisson ... Stochastic

Poisson Processes

• 2 competing processes with rates r1 and r2

– interarr = -log(rand(2,m))./repmat([r1 r2],m,1)';

– hist(min(interarr,[],1));

0 0.5 1 1.50

50

100

150

200

first reaction time

frequency

r1 = 1, r

2 = 5

mean = 0.1706, var = 0.0299

0 0.5 1 1.5 20

50

100

150

200

first reaction time

fre

qu

en

cy

r = 6mean = 0.1714, var = 0.0300

Page 13: Stochastic Simulation using MATLAB - MITweb.mit.edu/biophysics/sbio/PDFs/R8.pdf · Poisson Processes •Fixed number of steps ... –Steady state distribution is Poisson ... Stochastic

Birth-and-death Process

• Model: birth rate = , death rate = n– dn/dt = - n

• Matlab code– Initialization

– Monte Carlo step

– Update

– Iterate

Page 14: Stochastic Simulation using MATLAB - MITweb.mit.edu/biophysics/sbio/PDFs/R8.pdf · Poisson Processes •Fixed number of steps ... –Steady state distribution is Poisson ... Stochastic

Birth-and-death Process

• Model: birth rate = , death rate = n– dn/dt = - n

• Matlab code– Initialization

– Monte Carlo step

– Update

– Iterate (for loop)

Page 15: Stochastic Simulation using MATLAB - MITweb.mit.edu/biophysics/sbio/PDFs/R8.pdf · Poisson Processes •Fixed number of steps ... –Steady state distribution is Poisson ... Stochastic

Birth-and-death Process

• Model: birth rate = , death rate = n

– dn/dt = - n

– Steady state distribution is Poisson (L13)

0 2000 4000 60000

1

2

3

4

5

time

nu

mb

er

of

mo

lecu

les

0 2 4 60

10

20

30

40

number of molecules

fre

qu

en

cy

mean = 1.83var = 1.4153

Page 16: Stochastic Simulation using MATLAB - MITweb.mit.edu/biophysics/sbio/PDFs/R8.pdf · Poisson Processes •Fixed number of steps ... –Steady state distribution is Poisson ... Stochastic

Let’s write a simulation together…

dn/dt = (12.5+0.02*n^2)/(1+0.0001*n^2) - n