def wright_distn_helper(M, T, R_mut): """ @param M: index to states @param T: states to index @param R_mut: scaled mutation rate matrix @return: stationary distribution of the process """ #FIXME: remove dependence on T lmcs = wrightcore.get_lmcs(M) lps = wrightcore.create_selection_neutral(M) log_drift = wrightcore.create_neutral_drift(lmcs, lps, M) P_drift = np.exp(log_drift) P_mut = scipy.linalg.expm(R_mut) P = np.dot(P_mut, P_drift) v = MatrixUtil.get_stationary_distribution(P) return v
def do_full_simplex_then_collapse(mutrate, popsize): #mutrate = 0.01 #mutrate = 0.2 #mutrate = 10 #mutrate = 100 #mutrate = 1 N = popsize k = 4 M = np.array(list(multinomstate.gen_states(N, k)), dtype=int) T = multinomstate.get_inverse_map(M) # Create the joint site pair mutation rate matrix. R = mutrate * wrightcore.create_mutation(M, T) # Create the joint site pair drift transition matrix. lmcs = wrightcore.get_lmcs(M) lps = wrightcore.create_selection_neutral(M) log_drift = wrightcore.create_neutral_drift(lmcs, lps, M) # Define the drift and mutation transition matrices. P_drift = np.exp(log_drift) P_mut = scipy.linalg.expm(R) # Define the composite per-generation transition matrix. P = np.dot(P_mut, P_drift) # Solve a system of equations to find the stationary distribution. v = MatrixUtil.get_stationary_distribution(P) for state, value in zip(M, v): print state, value # collapse the two middle states nstates_collapsed = multinomstate.get_nstates(N, k-1) M_collapsed = np.array(list(multinomstate.gen_states(N, k-1)), dtype=int) T_collapsed = multinomstate.get_inverse_map(M_collapsed) v_collapsed = np.zeros(nstates_collapsed) for i, bigstate in enumerate(M): AB, Ab, aB, ab = bigstate.tolist() Ab_aB = Ab + aB j = T_collapsed[AB, Ab_aB, ab] v_collapsed[j] += v[i] for state, value in zip(M_collapsed, v_collapsed): print state, value # draw an equilateral triangle #drawtri(M_collapsed, T_collapsed, v_collapsed) #test_mesh() return M_collapsed, T_collapsed, v_collapsed
def do_full_simplex_then_collapse(mutrate, popsize): #mutrate = 0.01 #mutrate = 0.2 #mutrate = 10 #mutrate = 100 #mutrate = 1 N = popsize k = 4 M = np.array(list(multinomstate.gen_states(N, k)), dtype=int) T = multinomstate.get_inverse_map(M) # Create the joint site pair mutation rate matrix. R = mutrate * wrightcore.create_mutation(M, T) # Create the joint site pair drift transition matrix. lmcs = wrightcore.get_lmcs(M) lps = wrightcore.create_selection_neutral(M) log_drift = wrightcore.create_neutral_drift(lmcs, lps, M) # Define the drift and mutation transition matrices. P_drift = np.exp(log_drift) P_mut = scipy.linalg.expm(R) # Define the composite per-generation transition matrix. P = np.dot(P_mut, P_drift) # Solve a system of equations to find the stationary distribution. v = MatrixUtil.get_stationary_distribution(P) for state, value in zip(M, v): print state, value # collapse the two middle states nstates_collapsed = multinomstate.get_nstates(N, k - 1) M_collapsed = np.array(list(multinomstate.gen_states(N, k - 1)), dtype=int) T_collapsed = multinomstate.get_inverse_map(M_collapsed) v_collapsed = np.zeros(nstates_collapsed) for i, bigstate in enumerate(M): AB, Ab, aB, ab = bigstate.tolist() Ab_aB = Ab + aB j = T_collapsed[AB, Ab_aB, ab] v_collapsed[j] += v[i] for state, value in zip(M_collapsed, v_collapsed): print state, value # draw an equilateral triangle #drawtri(M_collapsed, T_collapsed, v_collapsed) #test_mesh() return M_collapsed, T_collapsed, v_collapsed
def do_collapsed_simplex(scaled_mut, N): """ @param N: population size """ k = 3 M = np.array(list(multinomstate.gen_states(N, k)), dtype=int) T = multinomstate.get_inverse_map(M) # Create the joint site pair mutation rate matrix. # This is scaled so that there are about popsize mutations per generation. R_mut_raw = wrightcore.create_mutation_collapsed(M, T) R_mut = (scaled_mut / float(N)) * R_mut_raw # Create the joint site pair drift transition matrix. lmcs = wrightcore.get_lmcs(M) lps = wrightcore.create_selection_neutral(M) #log_drift = wrightcore.create_neutral_drift(lmcs, lps, M) # Define the drift and mutation transition matrices. #P_drift = np.exp(log_drift) #P_mut = scipy.linalg.expm(R) # Define the composite per-generation transition matrix. #P = np.dot(P_mut, P_drift) # Solve a system of equations to find the stationary distribution. #v = MatrixUtil.get_stationary_distribution(P) # Try a new thing. # The raw drift matrix is scaled so that there are about N*N # replacements per generation. generation_rate = 1.0 R_drift_raw = wrightcore.create_moran_drift_rate_k3(M, T) R_drift = (generation_rate / float(N)) * R_drift_raw #FIXME: you should get the stationary distn directly from the rate matrix P = scipy.linalg.expm(R_mut + R_drift) v = MatrixUtil.get_stationary_distribution(P) """ for state, value in zip(M, v): print state, value """ # draw an equilateral triangle #drawtri(M, T, v) return M, T, v