Rosalind Java|Independent Alleles

该博客探讨了一个遗传学问题,涉及到独立事件的概念和超几何分布的应用。通过举例说明了两个独立事件的概率计算,并解释了如何利用这些概念来解决一个具体问题:在Tom的家族树中,计算第k代至少有n个特定基因型(AaBb)个体的概率。博客给出了一个Java代码示例,用于计算给定的k和n值下的概率。最终,通过计算得出在第7代中至少有37个杂合体后代的概率为0.178。
摘要由CSDN通过智能技术生成

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,得到最终答案。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值