Example #1
0
def create_mutation_transition_matrix(npop, mutation_ab, mutation_ba):
    """
    The states are indexed by the number of mutants.
    @param npop: total population size
    @param mutation_ab: wild-type to mutant transition probability
    @param mutation_ba: mutant to wild-type transition probability
    @return: a transition matrix
    """
    StatsUtil.assert_probability(mutation_ab)
    StatsUtil.assert_probability(mutation_ba)
    nstates = npop + 1
    P = np.zeros((nstates, nstates))
    for a in range(nstates):
        for n_mut_to_wild in range(a + 1):
            ba_observed_n = n_mut_to_wild
            ba_max_n = a
            ba_p_success = mutation_ba
            ba_log_p = StatsUtil.binomial_log_pmf(ba_observed_n, ba_max_n,
                                                  ba_p_success)
            for n_wild_to_mut in range(npop - a + 1):
                ab_observed_n = n_wild_to_mut
                ab_max_n = npop - a
                ab_p_success = mutation_ab
                ab_log_p = StatsUtil.binomial_log_pmf(ab_observed_n, ab_max_n,
                                                      ab_p_success)
                #
                p = math.exp(ba_log_p + ab_log_p)
                b = a + n_wild_to_mut - n_mut_to_wild
                P[a, b] += p
    return P
Example #2
0
def create_mutation_transition_matrix(npop, mutation_ab, mutation_ba):
    """
    The states are indexed by the number of mutants.
    @param npop: total population size
    @param mutation_ab: wild-type to mutant transition probability
    @param mutation_ba: mutant to wild-type transition probability
    @return: a transition matrix
    """
    StatsUtil.assert_probability(mutation_ab)
    StatsUtil.assert_probability(mutation_ba)
    nstates = npop + 1
    P = np.zeros((nstates, nstates))
    for a in range(nstates):
        for n_mut_to_wild in range(a+1):
            ba_observed_n = n_mut_to_wild
            ba_max_n = a
            ba_p_success = mutation_ba
            ba_log_p = StatsUtil.binomial_log_pmf(
                    ba_observed_n, ba_max_n, ba_p_success)
            for n_wild_to_mut in range(npop - a + 1):
                ab_observed_n = n_wild_to_mut
                ab_max_n = npop - a
                ab_p_success = mutation_ab
                ab_log_p = StatsUtil.binomial_log_pmf(
                        ab_observed_n, ab_max_n, ab_p_success)
                #
                p = math.exp(ba_log_p + ab_log_p)
                b = a + n_wild_to_mut - n_mut_to_wild
                P[a, b] += p
    return P