python - 生成 Gillespie 模型仿真
问题描述
我正在使用 Python,我正在尝试使用循环重现 Gillespie 随机模拟。到目前为止,我在下面进行了编码。我需要添加一个贯穿随机模拟原理图的循环。另外-我需要为每个点添加反应。我找不到图表的代码。先感谢您。
import numpy as np
from scipy.special import factorial
import matplotlib.pyplot as plt
#reaction constrants
c1= 0.5
print ("c1: ", c1)
#initial molecular population numbers of S1 and S2
X0=1000
print ("X0: ", X0)
X1=999
print ("X1: ", X1)
#initialize time variable t and reaction n
t=0
n=0
print ("t: ", t)
print ("n: ",n)
#calculate a1=h1*c1 where h1 is the total number of all possible # combos of pairs of s1
a1= X0*X1*c1
a = np.array([a1])
a0=sum(a)
#print debug
print ("a1: ", a1)
print ("a: ", a)
print ("a0: ", a0)
#generating random #s r1 for normal distribution using np.random.random
r = np.random.random(2)
r1= r[0]
r2= r[1]
#find the incrememnt T in time as (1/a0)*ln(1/r1)
T= (1/a0)*np.log(1/r1)
print ("T: ", T)
#choose next reaction
mu= 0
N= r2*a0 - a[mu
]
while N>0:
mu= mu+1
N = N- a[mu]
next_r = mu
print ("next_r: ", next_r)
#define the time of the next reaction
t1= t + T
print ("t1: ", t1)
#add one to the n
#number of reactions count n
n1 = n+1
print ("n1: ", n1)
#update the system according to the reaction that was chosen
X2=X1+1
X3= X1-1
print ("X2: ", X2)
print ("X3: ", X3)
while N>0:
mu= mu+1
N = N- a[mu]
next_r = mu
print ("next_r: ", next_r)
#define the time of the next reaction
t2=t1+ T
print ("t2: ", t2)
#add one to the n
#number of reactions count n
n2 = n1+1
print ("n2: ", n2)
#update the system according to the reaction that was chosen
X1_all= np.array([X0,X1])
t_all= np.array([0,10])
ax=plt.subplot()
ax.plot(t_all,X1_all)
plt.show()
解决方案
推荐阅读
- c# - 我想将应用程序重定向到 ASP.NET Core 中的外部 url
- python - 使用 ConfigParser 在 python 中读取配置文件
- sharepoint - 如何修复 SharePoint 设计器工作流发布问题?
- vue.js - 如何在带有 div 的 vue js 中动态显示值
- php - 如何在php中比较开始和结束时间与当前时间?
- python - 为什么在训练期间多次缩小参数将有助于 Progressive GAN 中所有权重的学习速度相同?
- c# - 如何在winform中的禁用按钮上显示工具提示?
- python - 无法从 pubmed xml 文件中读取所有抽象文本
- architecture - 清洁架构 - “组件原则三合会”的示例
- php - 语法错误,在 laravel 8 中出现意外的 ')'。\vendor\laravel\framework\src\Illuminate\Bus\BusServiceProvider.php:51