-
Notifications
You must be signed in to change notification settings - Fork 0
/
gill_fpt.py
83 lines (63 loc) · 1.43 KB
/
gill_fpt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import random
import numpy as np
import scipy.stats
import matplotlib.pyplot as plt
random.seed()
def get_a(x,kb,kd):
a = [ kb, kd*x];
return a
def get_a0(a):
a0 = sum(a)
return a0
def get_mu(a,a0,r2):
mu = 0
if( a[mu] < r2*a0 ):
mu = 1
return mu
def probDensity(x,t,x0,kb,kd):
l = kb/kd + (x0-kb/kd)*np.exp(-kd*t)
p = np.exp(-l)*(l**x)/(np.math.factorial(x))
return p
nEnsemble = 1000
dt = 0.05;
xstop = 30.0;
tstop = 1000;
v = [ 1.0, -1.0]
x0 = 0.0
kb = 2.0
kd = 0.1
tEnsemble = [];
xEnsemble = [];
for i in xrange(nEnsemble):
t = 0.0;
x = x0;
xlist = [x];
tlist = [t];
while (t <= tstop) and (x < xstop):
a = get_a(x,kb,kd)
a0 = get_a0(a)
r1 = np.random.uniform()
r2 = random.random()
tau = np.log(1.0/r1)/a0
mu = get_mu(a,a0,r2)
x += v[mu]
t += tau
# print len(tlist)
tEnsemble.append(t)
# plt.plot(tlist,xlist)
#
# plt.show()
num_bins = 50
n, bins = np.histogram(tEnsemble, num_bins, normed=1)
dbins = bins[1]-bins[0]
n = n*dbins
plt.bar( bins[0:len(bins)-1]-0.5, n, dbins, label='data', alpha=0.7)
sTitle = 'First-Passage Distribution, xStop=%.1f' %(xstop)
plt.title(sTitle)
plt.xlabel('First-Passage Time')
plt.ylabel('Probability Density')
plt.savefig('fpt1.png')
plt.show()
print ' First-Passage Time Simulation Results: '
print ' '
print ' mean first-passage time = %.3f' %np.mean(tEnsemble)