繁体   English   中英

使用Python通过加权选择固定人口

[英]Fixation in population with weighted choices using Python

我正在尝试进行仿真,以查看人口固定的速度。 总体由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、0]

对于下一个种群,我根据频率(p_freq和q_freq)随机选择(加权选择),并对其进行迭代,直到种群固定为全1或全0。 修复后,我尝试在p_fix或q_fix列表中记录修复的一代

因此,我已经将此方法用于一个模拟,但是我正尝试使其适用于n = 100个模拟,并且我无法弄清楚如何构造它来获取循环以继续填充p_fix和q_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_fix和q_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(即0s)。 每个人口的频率都会发生变化(这就是我重置它们的原因),并导致凝视。 人口规模保持不变。

如何使它运行多个仿真?

您的问题是,每次仿真后都没有重置p_freq= 0.1q_freq= 1 - p_freq 您需要在您的: for sim in range(n):它们重置: for sim in range(n):循环中(否则它们保留来自最后一个sim的值)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM