1. 程式人生 > 其它 >Rosalind第15題:Independent Alleles

Rosalind第15題:Independent Alleles

技術標籤:python控制科學與工程

Problem

Figure 2.The probability of each outcome for the sum of the values on two rolled dice (black and white), broken down depending on the number of pips showing on each die. You can verify that 18 of the 36 equally probable possibilities result in an odd sum.

Twoeventsandare

independentifis equal to. In other words, the events do not influence each other, so that we may simply calculate each of the individual probabilities separately and then multiply.

More generally,random variablesandareindependentif wheneverandare respective events forand,andare independent (i.e.,).

As an example of how helpful independence can be for calculating probabilities, letandrepresent the numbers showing on two six-sided dice. Intuitively, the number of pips showing on one die should not affect the number showing on the other die. If we want to find the probability thatis odd, then we don't need to draw a tree diagram and consider all possibilities. We simply first note that forto be odd, eitheris even andis odd oris odd andis even. In terms of probability,. Using independence, this becomes, or. You can verify this result in

Figure 2, which shows all 36 outcomes for rolling two dice.

Given:Two positive integers() and(). In this problem, we begin with Tom, who in the 0th generation has genotype Aa Bb. Tom has two children in the 1st generation, each of whom has two children, and so on. Each organism always mates with an organism having genotype Aa Bb.

Return:The probability that at leastAa Bb organisms will belong to the-th generation of Tom's family tree (don't count the Aa Bb mates at each level). Assume that Mendel's second law holds for the factors.

兩個事件,並有獨立的,如果等於。換句話說,事件不會相互影響,因此我們可以簡單地分別計算各個概率,然後相乘。

更一般而言,隨機變數和是獨立的(如果和)分別是和的事件,並且是獨立的(即)。

舉一個獨立性如何有助於計算概率的示例,讓並代表兩個六面骰子上顯示的數字。直觀地,一個骰子上顯示的點數不應影響另一個骰子上顯示的點數。如果我們想找到奇數的概率,則無需繪製樹形圖並考慮所有可能性。我們簡單地首先注意到,對於奇數,要麼是偶數且是奇數,要麼是奇數且是偶數。就概率而言,。使用獨立性,它將變為或。您可以在圖2中驗證此結果,該顯示了擲骰子擲骰的全部36個結果。

給定:兩個正整數()和()。在這個問題上,我們從湯姆開始,他在第0代中具有Aa Bb基因型。湯姆在第一代有兩個孩子,每個孩子都有兩個孩子,依此類推。每個生物總是與具有Aa Bb基因型的生物交配。

回報:至少Aa Bb生物屬於湯姆家族樹的第四代的概率(不計算每個級別的Aa Bb配偶)。假設孟德爾的第二定律適用於這些因素。

Sample Dataset

2 1

Sample Output

0.684

python解決方案

import itertools

def f(k, n):
    p = []
    child_num = 2 ** k
    for i in range(n):
        p.append(len(list(itertools.combinations([x for x in range(child_num)], i))) * (0.25 ** i) * (
                    0.75 ** (child_num - i)))
        # combinations('ABCD', 2)		AB AC AD BC BD CD
    return 1 - sum(p)

ret = f(2, 1)
print(ret)