我试着做一个模拟,看看人口固定的速度有多快。总体由1(或p)和0(或q)组成,而每个个体有2个元素(1-1、1-0或0-0)。你知道吗
N是人口,由于人口的每个成员有2个元素,人口池将是2*N(在本例中为20)
1s的初始频率为0.1,默认情况下,q为1-0.1=0.9
所以初始总体是[1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
对于下一个总体,我将根据频率(p\u freq和q\u freq)随机选择(加权选择),并反复进行,直到总体固定为所有1或所有0。一旦固定,我将尝试在p\u fix或q\u fix列表中记录固定的生成
所以我把它用于一个模拟,但是我试着让它用于n=100个模拟,我不知道如何构造它来让循环继续正确地填充p\u fix和q\u fix列表
#!/usr/bin/env python2.7
import random
N= 10
n= 100
p_freq= 0.1
q_freq= 1 - p_freq
simulation= 0
p_fix= []
q_fix= []
for sim in range(n):
generation= 0
#Current population
p_alleles= int(p_freq * 2*N)*[1]
q_alleles= int(q_freq * 2*N)*[0]
population= p_alleles + q_alleles
while (sum(population) != 2*N) and (sum(population) != 0):
#Checking current population for fixation
#Next generation
next_population= []
for i in range(2*N): next_population.append(random.choice(population))
#Resetting parameters
p_freq= float(sum(next_population))/(2*N)
q_freq= 1 - p_freq
population= next_population
#Counts
generation += 1
if sum(population) == 2*N:
p_fix.append(generation)
if sum(population) == 0:
q_fix.append(generation)
simulation += 1
打印p\u fix和q\u fix时的结果:
p []
q [3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
它不应该是第一次模拟之后所有模拟的第0代。然而,人口固定在q上是有意义的,因为90%的原始人口是q(即0)。每个群体的频率都会改变(这就是我重置它们的原因),这会导致固定。人口规模保持不变。你知道吗
如何让它运行多个模拟?
你的问题是你没有在每次模拟之后重置
p_freq= 0.1
和q_freq= 1 - p_freq
。您需要在:for sim in range(n):
循环中重置它们(否则它们将保留上一个sim的值)。你知道吗相关问题 更多 >
编程相关推荐