#### Fixation in population with weighted choices using Python

N是人口，并且由于人口的每个成员都有2个元素，因此人口池将为2 * N（在这种情况下为20）

1s的初始频率为0.1，默认情况下，q为1-0.1 = 0.9

``````#!/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 []
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]
``````

1 个回复

1 从固定人口长度生成随机唯一数字的问题

2 SQL 加权平均 - 人口和收入

3 Zxing（v3.2.0）使用Eclipse的相机固定人像模式

4 使用python制作加权直方图？

5 使用Python 3.7.3，在给定目录中的文件加权列表中随机选择/选择

2019-07-02 20:29:35 1 54   series
6 如何在python中进行加权双向固定效应回归

7 各州人口规模的加权面板回归

8 在r中绘制加权人口密度

2012-07-25 21:35:49 1 508   r
9 python 2的高性能加权随机选择？

2018-03-08 14:28:04 2 256   pypy
10 加权数据的逻辑回归（人口）