View
224
Download
1
Category
Preview:
Citation preview
Stochastic Simulation using MATLAB
Systems Biology Recitation 8
11/04/09
clivejames.com
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)
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
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
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)
http://www.mathworks.com/access/helpdesk/help/toolbox/stats/index.html?
Poisson Processes
– Events occur independent of each other
– 2 events cannot occur at the same time point
– The events occur with constant rates
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
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]
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,:);
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
Birth-and-death Process
• Model: birth rate = , death rate = n– dn/dt = - n
• Matlab code– Initialization
– Monte Carlo step
– Update
– Iterate
Birth-and-death Process
• Model: birth rate = , death rate = n– dn/dt = - n
• Matlab code– Initialization
– Monte Carlo step
– Update
– Iterate (for loop)
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
Let’s write a simulation together…
dn/dt = (12.5+0.02*n^2)/(1+0.0001*n^2) - n
Recommended