예제 #1
0
 def test_wright_fisher(self):
     N_diallelic = 3
     s = 0.03
     # compute the wright fisher transition matrix directly
     Ma = np.exp(wfengine.create_genic_diallelic(N_diallelic, s))
     MatrixUtil.assert_transition_matrix(Ma)
     # compute the wright fisher transition matrix less directly
     N = 2 * N_diallelic
     fi = 1.0
     fj = 1.0 - s
     log_distns = np.zeros((N + 1, 2))
     for h in range(0, N + 1):
         probs = genic_diallelic(fi, fj, h, (N - h))
         log_distns[h] = np.log(np.array(probs))
     Mb = np.exp(wfengine.expand_multinomials(N, log_distns))
     MatrixUtil.assert_transition_matrix(Mb)
     # compare the transition matrices
     self.assertTrue(np.allclose(Ma, Mb))
예제 #2
0
 def test_wright_fisher(self):
     N_diallelic = 3
     s = 0.03
     # compute the wright fisher transition matrix directly
     Ma = np.exp(wfengine.create_genic_diallelic(N_diallelic, s))
     MatrixUtil.assert_transition_matrix(Ma)
     # compute the wright fisher transition matrix less directly
     N = 2*N_diallelic
     fi = 1.0
     fj = 1.0 - s
     log_distns = np.zeros((N+1, 2))
     for h in range(0, N+1):
         probs = genic_diallelic(fi, fj, h, (N-h))
         log_distns[h] = np.log(np.array(probs))
     Mb = np.exp(wfengine.expand_multinomials(N, log_distns))
     MatrixUtil.assert_transition_matrix(Mb)
     # compare the transition matrices
     self.assertTrue(np.allclose(Ma, Mb))
예제 #3
0
파일: 20120814a.py 프로젝트: BIGtigr/xgcode
def get_response_content(fs):
    initial_composition = (fs.nAB, fs.nAb, fs.naB, fs.nab)
    npop = sum(initial_composition)
    nstates = get_state_space_size(npop)
    # Check for minimum population size.
    if npop < 1:
        raise ValueError('there should be at least one individual')
    # Check the complexity;
    # solving a system of linear equations takes about n^3 effort.
    if npop > 32:
        raise ValueError('sorry this population size is too large')
    # Compute the transition matrix.
    tm = time.time()
    child_distns = get_child_distns(npop, fs.sAB, fs.sAb, fs.saB, fs.r)
    log_child_distns = np.log(child_distns)
    print time.time() - tm, 'seconds to compute the log child distributions'
    tm = time.time()
    log_P = wfengine.expand_multinomials(npop, log_child_distns)
    print time.time() - tm, 'seconds to compute the log transition matrix'
    tm = time.time()
    P = np.exp(log_P)
    print time.time() - tm, 'seconds to entrywise exponentiate the matrix'
    # Precompute the map from compositions to state index.
    compositions = list(gen_population_compositions(npop))
    c_to_i = dict((c, i) for i, c in enumerate(compositions))
    # Compute the exact probabilities of fixation.
    tm = time.time()
    fixation_distribution = solve(npop, P)[c_to_i[initial_composition]]
    print time.time() - tm, 'seconds to solve the linear system'
    # Write the output
    out = StringIO()
    print >> out, 'distribution over eventual allele fixations:'
    for p, name in zip(fixation_distribution, ('AB', 'Ab', 'aB', 'ab')):
        print >> out, name, ':', p
    """
    print >> out, 'probability of eventual fixation (as opposed to extinction)'
    print >> out, 'of allele B in the population:'
    print >> out, p_fixation
    print >> out
    print >> out, 'Kimura would give the approximation'
    print >> out, k_top / k_bot
    print >> out
    """
    """
    # Compute low-population approximations of probability of fixation of B.
    pB = fs.nB / float(fs.nB + fs.nb)
    for nsmall, name in (
            (10, 'low population size'),
            (20, 'medium population size'),
            ):
        if nsmall >= npop:
            continue
        s_small = fs.s * npop / float(nsmall)
        # Compute all low-population approximations.
        x = solve(nsmall, s_small)
        f_linear = interpolate.interp1d(range(nsmall+1), x, kind='linear')
        f_cubic = interpolate.interp1d(range(nsmall+1), x, kind='cubic')
        print >> out, 'linearly interpolated %s (N=%s)' % (name, nsmall)
        print >> out, 'approximation of probability of eventual'
        print >> out, 'fixation (as opposed to extinction)'
        print >> out, 'of allele B in the population:'
        print >> out, f_linear(pB*nsmall)
        print >> out
        print >> out, 'cubic interpolation:'
        print >> out, f_cubic(pB*nsmall)
        print >> out
    """
    return out.getvalue()
예제 #4
0
def get_response_content(fs):
    initial_composition = (fs.nAB, fs.nAb, fs.naB, fs.nab)
    npop = sum(initial_composition)
    nstates = get_state_space_size(npop)
    # Check for minimum population size.
    if npop < 1:
        raise ValueError('there should be at least one individual')
    # Check the complexity;
    # solving a system of linear equations takes about n^3 effort.
    if npop > 32:
        raise ValueError('sorry this population size is too large')
    # Compute the transition matrix.
    tm = time.time()
    child_distns = get_child_distns(npop, fs.sAB, fs.sAb, fs.saB, fs.r)
    log_child_distns = np.log(child_distns)
    print time.time() - tm, 'seconds to compute the log child distributions'
    tm = time.time()
    log_P = wfengine.expand_multinomials(npop, log_child_distns)
    print time.time() - tm, 'seconds to compute the log transition matrix'
    tm = time.time()
    P = np.exp(log_P)
    print time.time() - tm, 'seconds to entrywise exponentiate the matrix'
    # Precompute the map from compositions to state index.
    compositions = list(gen_population_compositions(npop))
    c_to_i = dict((c, i) for i, c in enumerate(compositions))
    # Compute the exact probabilities of fixation.
    tm = time.time()
    fixation_distribution = solve(npop, P)[c_to_i[initial_composition]]
    print time.time() - tm, 'seconds to solve the linear system'
    # Write the output
    out = StringIO()
    print >> out, 'distribution over eventual allele fixations:'
    for p, name in zip(fixation_distribution, ('AB', 'Ab', 'aB', 'ab')):
        print >> out, name, ':', p
    """
    print >> out, 'probability of eventual fixation (as opposed to extinction)'
    print >> out, 'of allele B in the population:'
    print >> out, p_fixation
    print >> out
    print >> out, 'Kimura would give the approximation'
    print >> out, k_top / k_bot
    print >> out
    """
    """
    # Compute low-population approximations of probability of fixation of B.
    pB = fs.nB / float(fs.nB + fs.nb)
    for nsmall, name in (
            (10, 'low population size'),
            (20, 'medium population size'),
            ):
        if nsmall >= npop:
            continue
        s_small = fs.s * npop / float(nsmall)
        # Compute all low-population approximations.
        x = solve(nsmall, s_small)
        f_linear = interpolate.interp1d(range(nsmall+1), x, kind='linear')
        f_cubic = interpolate.interp1d(range(nsmall+1), x, kind='cubic')
        print >> out, 'linearly interpolated %s (N=%s)' % (name, nsmall)
        print >> out, 'approximation of probability of eventual'
        print >> out, 'fixation (as opposed to extinction)'
        print >> out, 'of allele B in the population:'
        print >> out, f_linear(pB*nsmall)
        print >> out
        print >> out, 'cubic interpolation:'
        print >> out, f_cubic(pB*nsmall)
        print >> out
    """
    return out.getvalue()