Rosalind编程问题之计算等位基因杂合子比例。
Independent Alleles
Problem
Two events A and B are independent if Pr(A and B) is equal to Pr(A)×Pr(B). 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 variables X and Y are independent if whenever A and B are respective events for X and Y, A and B are independent (i.e., Pr(A and B)=Pr(A)×Pr(B)).
As an example of how helpful independence can be for calculating probabilities, let X and Y represent 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 that X+Y is odd, then we don’t need to draw a tree diagram and consider all possibilities. We simply first note that for X+Y to be odd, either X is even and Y is odd or X is odd and Y is even. In terms of probability, Pr(X+Y is odd)=Pr(X is even and Y is odd)+Pr(X is odd and Y is even). Using independence, this becomes [Pr(X is even)×Pr(Y is odd)]+[Pr(X is odd)×Pr(Y is even)], or (12)2+(12)2=12. You can verify this result in Figure 2, which shows all 36 outcomes for rolling two dice.
Given: Two positive integers k (k≤7) and N (N≤2k). 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 least N Aa Bb organisms will belong to the k-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.
简单概述一下题目需求。我们假设有一个叫Tom的哥们,家族生育能力很强,能够以近似分裂生殖的方式传宗接代。那么此时经历第k代繁衍后,Tom该代家族应该有2k个成员。那么此时再假设每一代成员繁衍时都是两种AaBb型交配产生的结果,求第k代家族至少有n个AaBb个体的概率。
遗传学知识告诉我们二倍体杂合体自交得到二倍体杂合体的概率为1/4。那么其自交得到非二倍体杂合体的概率为3/4。于是我们得到了抽样事件中两种可能发生的事件及其对应的概率。
不难发现,求第k代家族至少有n个AaBb个体的概率可以抽象为数学统计模型:超几何分布。其基本公式如下:
结合两个事件发生的概率,则在第k代家族至少有n个AaBb个体的概率为:
P(X>=n)=1-[Cn2^k * (1/4)n次方 *(3/4)2的k次方-n)]
小编抽到的K和N分别为7和37。意为求第7代中,至少有37个杂合体后代的概率。代码如下:
public class Independent_Alleles {
public static void main(String[] args) {
IndepAll(7,37);
}
public static void IndepAll(int k, int n) {
//1.每代杂合个体的比率.java实现乘方运算需要使用math API
//2.计算特定代数中杂合子的几何分布。排列运算为两个阶乘相除。需要求得最少N个个体为杂合的情况,则只需排除少于N个个体不为杂合即可:
double rate = 0;
for (int i = 0; i < n; i++) {
double combinatorial = factorial(Math.pow(2, k)) / (factorial(Math.pow(2, k) - i) * factorial(i));
rate += combinatorial * Math.pow(0.25, i) * Math.pow(0.75, Math.pow(2, k) - i);
}
System.out.println((float) (Math.round((1 - rate) * 1000)) / 1000);
}
public static double factorial(double n) {
double sum = 1;
for (int i = 2; i <= n; i++) {
sum = sum * i;
}
return sum;
}
}
最终求得结果为0.178,得到最终答案。