【ROSALIND】【練Python,學(xué)生信】13 后代基因型期望

如果第一次閱讀本系列文檔請先移步閱讀【ROSALIND】【練Python,學(xué)生信】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的非負整數(shù),分別對應(yīng)于一個群體中在某基因位點上不同基因型組合的夫妻數(shù)目。六個整數(shù)對應(yīng)的基因型分別如下:
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.
需得:下一代表現(xiàn)顯性性狀的期望數(shù),假設(shè)每對夫妻只有兩個后代。
?
測試數(shù)據(jù)
1 0 0 1 0 1
測試輸出
3.5
?
背景
期望,亦稱均值,是試驗中每次可能結(jié)果的概率乘以其結(jié)果的總和,是變量輸出值的平均數(shù)。大數(shù)定律規(guī)定,隨著重復(fù)次數(shù)接近無窮大,數(shù)值的算術(shù)平均值幾乎肯定地收斂于期望值。
假設(shè)隨機變量取X在1到n之間的整數(shù)值,X的期望值(expected value)為

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
思路
要計算所有后代中出現(xiàn)某性狀的概率,不妨把問題拆為計算每個基因型組合夫妻后代表現(xiàn)型的期望,再相加即可。由于所有夫妻都生育相同數(shù)量的子女,所以后代期望只與該基因型組合所占概率和該基因型組合下生出顯性性狀子女的概率有關(guān)。
以Aa-Aa基因型的夫妻為例:該基因型出現(xiàn)概率為Aa-Aa夫妻對數(shù)a4比上所有夫妻對數(shù)a,該組合生出顯性性狀后代的概率為0.75,相乘即為Aa-Aa基因型夫妻生出顯性性狀子女的概率。再乘上結(jié)果總和(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))