Esempio n. 1
0
The Need for Averagesclick to expand

Problem

For a random variable X taking integer values between 1 and n, the expected value of X is E(X)=SUM nk=1kxPr(X=k). The expected value offers us a way of taking the long-term average of a random variable over a large number of trials.

As a motivating example, let X be the number on a six-sided die. Over a large number of rolls, we should expect to obtain an average of 3.5 on the die (even though it's not possible to roll a 3.5). The formula for expected value confirms that E(X)=SUM 6k=1kxPr(X=k)=3.5.

More generally, a random variable for which every one of a number of equally spaced outcomes has the same probability is called a uniform random variable (in the die example, this "equal spacing" is equal to 1). We can generalize our die example to find that if X is a uniform random variable with minimum possible value a and maximum possible value b, then E(X)=a+b2. You may also wish to verify that for the dice example, if Y is the random variable associated with the outcome of a second die roll, then E(X+Y)=7.

Given: Six positive 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
Return: The expected number of offspring displaying the dominant phenotype in the next generation, under the assumption that every couple has exactly two offspring.
============================================================
'''
from rosalind.rosutil import read_ints_str

def iev(n):
    return 2 * (n[0] + n[1] + n[2] + 0.75 * n[3] + 0.5 * n[4])

if __name__ == "__main__":
    print '%.6f' % (iev(read_ints_str('rosalind_iev_sample.dat')),)
    print '%.6f' % (iev(read_ints_str('rosalind_iev.dat')),)
Esempio n. 2
0
Given: A positive integer n <= 7.

Return: The total number of permutations of length n, followed by a list of all such permutations (in any order).
============================================================
'''
from operator import mul
from rosalind.rosutil import read_ints_str

def lexperms(n):
    a, i = range(1, n + 1), n - 2  # Init first perm
    yield a
    if n == 1: return
    while True:
        ai, j = a[i], i + 1
        while j < n and a[j] > ai: j += 1  # j = max{k: k > i, a[k]>a[i]}
        j -= 1
        a[i], a[j] = a[j], a[i]  # Swap the ith and jth elements
        a[i + 1:] = a[:i:-1]  # Reverse the order
        yield a
        for i in xrange(n - 2, -1, -1):  # i = max{i: a[i] < a[i+1]}
            if a[i] < a[i + 1]: break
        if i == 0 and a[0] > a[1]: break  # perm is completely decreasing
    
perm = lambda n: repr(reduce(mul, xrange(1, n + 1))) + '\n' + '\n'.join(' '.join(map(str, a)) for a in lexperms(n))
    
if __name__ == "__main__":
    print perm(read_ints_str('rosalind_perm_sample.dat')[0])
    print perm(read_ints_str('rosalind_perm.dat')[0])
    
Esempio n. 3
0
def nth_fibd(file_name):
    '''Main call.'''
    n, m = read_ints_str(file_name) 
    print '%d' % (it.islice(fibd(m), n - 1, n).next(),)
Esempio n. 4
0
def aspc(f, r=1000000L):
    '''Main driver to solve this problem.'''
    n, m = ro.read_ints_str(f)
    return ro.sum_mod(it.islice(ro.binom_mod(r), n, n + 1).next()[m:], r)
Esempio n. 5
0
def pdpl(f):
    L = restriction_map(ro.read_ints_str(f))
    return ro.join_list(sorted(L))  # Main driver to solve this problem.
Esempio n. 6
0
'''
============================================================
http://rosalind.info/problems/pper

Given: Positive integers n and k such that 100>=n>0 and 10>=k>0.
Return: The total number of partial permutations P(n,k), modulo 1,000,000.
============================================================
'''
import rosalind.rosutil as ro

def pper(n, k, r=1000000L):
    return ro.prod_mod(xrange(n, n - k, -1), r)

if __name__ == "__main__":
    print '%d' % (pper(*ro.read_ints_str('rosalind_pper_sample.dat')),)
    print '%d' % (pper(*ro.read_ints_str('rosalind_pper.dat')),)
Esempio n. 7
0
def wfmd(f):
    '''Main driver to solve this problem.'''
    N, m, g, k = ro.read_ints_str(f)
    n = 2 * N
    return sum(it.islice(ro.wf_pmf(n, m), g - 1, g).next()[:n - k + 1])
Esempio n. 8
0
============================================================
http://rosalind.info/problems/lia

Mendel's Second Lawclick to expand

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.
Two events A and B are independent if Pr(A and B) is equal to Pr(A)xPr(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)xPr(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)xPr(Y is odd)]+[Pr(X is odd)xPr(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<=2**k). 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.

============================================================
'''
from rosalind.rosutil import cumbin, read_ints_str

def lia(k, N):
    '''Return the probability that at least N kth generation members are Aa Bb.'''
    return 1 - cumbin(2 ** k, 0.25, N - 1)

if __name__ == "__main__":
    print '%.6f' % (lia(*read_ints_str('rosalind_lia_sample.dat')),)
    print '%.6f' % (lia(*read_ints_str('rosalind_lia.dat')),)