【ROSALIND】【练Python,学生信】13 后代基因型期望

如果第一次阅读本系列文档请先移步阅读【ROSALIND】【练Python,学生信】00 写在前面 谢谢配合~

题目:
计算后代基因型的期望值
Given: Six nonnegative integers, each of which does not exceed 20,000. The integers correspond to the number of couples in a population possessing each genotype pairing for a given factor. In order, the six given integers represent the number of couples having the following genotypes:
AA-AA
AA-Aa
AA-aa
Aa-Aa
Aa-aa
aa-aa
所给:6个不超过20000的非负整数,分别对应于一个群体中在某基因位点上不同基因型组合的夫妻数目。六个整数对应的基因型分别如下:
AA-AA
AA-Aa
AA-aa
Aa-Aa
Aa-aa
aa-aa
Return: The expected number of offspring displaying the dominant phenotype in the next generation, under the assumption that every couple has exactly two offspring.
需得:下一代表现显性性状的期望数,假设每对夫妻只有两个后代。
测试数据
1 0 0 1 0 1
测试输出
3.5
背景
期望,亦称均值,是试验中每次可能结果的概率乘以其结果的总和,是变量输出值的平均数。大数定律规定,随着重复次数接近无穷大,数值的算术平均值几乎肯定地收敛于期望值。
假设随机变量取X在1到n之间的整数值,X的期望值(expected value)为

思路
要计算所有后代中出现某性状的概率,不妨把问题拆为计算每个基因型组合夫妻后代表现型的期望,再相加即可。由于所有夫妻都生育相同数量的子女,所以后代期望只与该基因型组合所占概率和该基因型组合下生出显性性状子女的概率有关。
以Aa-Aa基因型的夫妻为例:该基因型出现概率为Aa-Aa夫妻对数a4比上所有夫妻对数a,该组合生出显性性状后代的概率为0.75,相乘即为Aa-Aa基因型夫妻生出显性性状子女的概率。再乘上结果总和(2*a),即为Aa-Aa基因型夫妻生出显性性状子女的期望。其他组合与此相似,最后相加即可。
代码
a1 = 1
a2 = 0
a3 = 0
a4 = 1
a5 = 0
a6 = 1
a = a1 + a2 + a3 + a4 + a5 + a6 # 计算共有几对夫妻,以进行下面的概率计算
p1 = a1 / a
p2 = a2 / a
p3 = a3 / a
p4 = a4 / a
p5 = a5 / a
p6 = a6 / a
e = (p1 + p2 + p3 + 0.75 * p4 + 0.5 * p5) * a * 2
print(round(e,1))