def main(): k = 4 print 'reading state distribution from stdin...' rows = [] for line in sys.stdin.readlines(): AB, Ab, aB, ab, p = line.split() row = [int(AB), int(Ab), int(aB), int(ab), float(p)] rows.append(row) N = sum(rows[0][:-1]) print 'defining the state vectors...' M = np.array(list(multinomstate.gen_states(N, k)), dtype=int) m_AB_ab = np.zeros(N+1) m_AB_Ab = np.zeros(N+1) for AB, Ab, aB, ab, p in rows: m_AB_ab[AB+ab] += p m_AB_Ab[AB+Ab] += p print 'marginal distribution of AB+ab:' print m_AB_ab print print 'marginal distribution of AB+Ab:' print m_AB_Ab print plot_AB_plus_Ab(rows) plot_AB_given_only_AB_and_Ab(rows) plot_AB_given_half_AB_plus_Ab(rows) plot_reciprocal_variance_AB_given_AB_plus_Ab(rows)
def main(args): alpha = args.alpha N = args.N k = 3 print 'alpha:', alpha print 'N:', N print 'k:', k print M = np.array(list(multinomstate.gen_states(N, k)), dtype=int) T = multinomstate.get_inverse_map(M) R_mut = wrightcore.create_mutation_abc(M, T) R_drift = wrightcore.create_moran_drift_rate_k3(M, T) Q = alpha * R_mut + R_drift # pick out the correct eigenvector W, V = scipy.linalg.eig(Q.T) w, v = min(zip(np.abs(W), V.T)) print 'rate matrix:' print Q print print 'transpose of rate matrix:' print Q.T print print 'eigendecomposition of transpose of rate matrix as integers:' print scipy.linalg.eig(Q.T) print print 'transpose of rate matrix in mathematica notation:' print MatrixUtil.m_to_mathematica_string(Q.T.astype(int)) print print 'abs eigenvector corresponding to smallest abs eigenvalue:' print np.abs(v) print
def main(): k = 4 print 'reading state distribution from stdin...' rows = [] for line in sys.stdin.readlines(): AB, Ab, aB, ab, p = line.split() row = [int(AB), int(Ab), int(aB), int(ab), float(p)] rows.append(row) N = sum(rows[0][:-1]) print 'defining the state vectors...' M = np.array(list(multinomstate.gen_states(N, k)), dtype=int) m_AB_ab = np.zeros(N + 1) m_AB_Ab = np.zeros(N + 1) for AB, Ab, aB, ab, p in rows: m_AB_ab[AB + ab] += p m_AB_Ab[AB + Ab] += p print 'marginal distribution of AB+ab:' print m_AB_ab print print 'marginal distribution of AB+Ab:' print m_AB_Ab print plot_AB_plus_Ab(rows) plot_AB_given_only_AB_and_Ab(rows) plot_AB_given_half_AB_plus_Ab(rows) plot_reciprocal_variance_AB_given_AB_plus_Ab(rows)
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 main(): # use standard notation Nmu = 1.0 N = 120 mu = Nmu / float(N) print 'N*mu:', Nmu print 'N:', N print # multiply the rate matrix by this scaling factor m_factor = mu # use the moran drift distn_helper = moran_distn_helper # get properties of the collapsed diamond process k = 3 M = np.array(list(multinomstate.gen_states(N, k)), dtype=int) T = multinomstate.get_inverse_map(M) R_mut = m_factor * wrightcore.create_mutation_collapsed(M, T) v = distn_helper(M, T, R_mut) for Ab_aB in range(N + 1): nremaining = N - Ab_aB # compute the volume for normalization volume = 0.0 for AB in range(nremaining + 1): ab = nremaining - AB volume += v[T[AB, Ab_aB, ab]] # print some info print 'X_1 + X_4 =', Ab_aB, '/', N print 'probability =', volume print 'Y = X_2 / (1 - (X_1 + X_4)) = X_2 / (X_2 + X_3)' if not nremaining: print 'conditional distribution of Y is undefined' else: # compute the conditional moments m1 = 0.0 m2 = 0.0 for AB in range(nremaining + 1): ab = nremaining - AB p = v[T[AB, Ab_aB, ab]] / volume x = AB / float(nremaining) m1 += x * p m2 += x * x * p # print some info print 'conditional E(Y) =', m1 print 'conditional E(Y^2) =', m2 print 'conditional V(Y) =', m2 - m1 * m1 print
def main(): # use standard notation Nmu = 1.0 N = 120 mu = Nmu / float(N) print 'N*mu:', Nmu print 'N:', N print # multiply the rate matrix by this scaling factor m_factor = mu # use the moran drift distn_helper = moran_distn_helper # get properties of the collapsed diamond process k = 3 M = np.array(list(multinomstate.gen_states(N, k)), dtype=int) T = multinomstate.get_inverse_map(M) R_mut = m_factor * wrightcore.create_mutation_collapsed(M, T) v = distn_helper(M, T, R_mut) for Ab_aB in range(N+1): nremaining = N - Ab_aB # compute the volume for normalization volume = 0.0 for AB in range(nremaining+1): ab = nremaining - AB volume += v[T[AB, Ab_aB, ab]] # print some info print 'X_1 + X_4 =', Ab_aB, '/', N print 'probability =', volume print 'Y = X_2 / (1 - (X_1 + X_4)) = X_2 / (X_2 + X_3)' if not nremaining: print 'conditional distribution of Y is undefined' else: # compute the conditional moments m1 = 0.0 m2 = 0.0 for AB in range(nremaining+1): ab = nremaining - AB p = v[T[AB, Ab_aB, ab]] / volume x = AB / float(nremaining) m1 += x*p m2 += x*x*p # print some info print 'conditional E(Y) =', m1 print 'conditional E(Y^2) =', m2 print 'conditional V(Y) =', m2 - m1*m1 print
def get_full_simplex(m_factor, N, distn_helper): """ Note that this uses the non-moran formulation of drift. The distn_helper function taken as an argument is expected to be either moran_distn_helper or wright_distn_helper. @param m_factor: the mutation rate matrix is multiplied by this number @param N: population size @param distn_helper: a function (M, T, R_mut) -> v @return: M, T, v """ k = 4 M = np.array(list(multinomstate.gen_states(N, k)), dtype=int) T = multinomstate.get_inverse_map(M) R_mut = m_factor * wrightcore.create_mutation(M, T) v = distn_helper(M, T, R_mut) return M, T, v
def collapse_diamond(N, M, v): """ Collapse the middle two states. @param N: population size @param M: index to state vector @param v: a distribution over a 3-simplex @return: a distribution over a 2-simplex """ k = 4 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] return v_collapsed
def collapse_diamond(N, M, v): """ Collapse the middle two states. @param N: population size @param M: index to state vector @param v: a distribution over a 3-simplex @return: a distribution over a 2-simplex """ k = 4 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] return v_collapsed
def collapse_side(N, M, v): """ Collapse two pairs of states. @param N: population size @param M: index to state vector @param v: a distribution over a 3-simplex @return: a distribution over a 1-simplex """ k = 4 nstates_collapsed = multinomstate.get_nstates(N, k-2) M_collapsed = np.array(list(multinomstate.gen_states(N, k-2)), 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 aB_ab = aB + ab j = T_collapsed[AB_Ab, aB_ab] v_collapsed[j] += v[i] return v_collapsed
def collapse_side(N, M, v): """ Collapse two pairs of states. @param N: population size @param M: index to state vector @param v: a distribution over a 3-simplex @return: a distribution over a 1-simplex """ k = 4 nstates_collapsed = multinomstate.get_nstates(N, k - 2) M_collapsed = np.array(list(multinomstate.gen_states(N, k - 2)), 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 aB_ab = aB + ab j = T_collapsed[AB_Ab, aB_ab] v_collapsed[j] += v[i] return 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
def get_collapsed_diag_process_distn(m_factor, N, distn_helper): k = 2 M = np.array(list(multinomstate.gen_states(N, k)), dtype=int) T = multinomstate.get_inverse_map(M) R_mut = m_factor * wrightcore.create_mutation_collapsed_diag(M, T) return distn_helper(M, T, R_mut)
def main(args): alpha = args.alpha N = args.N k = 4 print 'alpha:', alpha print 'N:', N print 'k:', k print M = np.array(list(multinomstate.gen_states(N, k)), dtype=int) T = multinomstate.get_inverse_map(M) R_mut = wrightcore.create_mutation(M, T) R_drift = wrightcore.create_moran_drift_rate_k4(M, T) Q = alpha * R_mut + R_drift P = scipy.linalg.expm(Q) v = MatrixUtil.get_stationary_distribution(P) # # Define the volumetric data using the stationary distribution. max_prob = np.max(v) d2 = np.zeros((N + 1, N + 1, N + 1, 4), dtype=float) U = np.array([ [0, 0, 0], [0, 1, 1], [1, 0, 1], [1, 1, 0], ], dtype=int) for p, state in zip(v, M): x, y, z = np.dot(state, U).tolist() # r, g, b, alpha d2[x, y, z] = np.array( [ 255 * (p / max_prob), 0, 0, 255 * (p / max_prob), #100, ], dtype=float) #d2[x, y, z, 0] = 255 * (p / max_prob) #d2[x, y, z, 1] = 0 #d2[x, y, z, 2] = 0 #d2[x, y, z, 3] = 100 # fill the empty states for x in range(N + 1): for y in range(N + 1): for z in range(N + 1): if (x + y + z) % 2 == 1: p_accum = np.zeros(4, dtype=float) n_accum = 0 for dx in (-1, 1): if 0 <= x + dx <= N: p_accum += d2[x + dx, y, z] n_accum += 1 for dy in (-1, 1): if 0 <= y + dy <= N: p_accum += d2[x, y + dy, z] n_accum += 1 for dz in (-1, 1): if 0 <= z + dz <= N: p_accum += d2[x, y, z + dz] n_accum += 1 d2[x, y, z] = p_accum / n_accum # # Do things that the example application does. app = QtGui.QApplication([]) w = gl.GLViewWidget() w.opts['distance'] = 2 * N w.show() # # a visual grid or something #g = gl.GLGridItem() #g.scale(10, 10, 1) #w.addItem(g) # # Do some more things that the example application does. vol = gl.GLVolumeItem(d2, sliceDensity=1, smooth=True) #vol.translate(-5,-5,-10) vol.translate(-0.5 * N, -0.5 * N, -0.5 * N) w.addItem(vol) # # add an axis thingy #ax = gl.GLAxisItem() #w.addItem(ax) if sys.flags.interactive != 1: app.exec_()
def main(args): alpha = args.alpha N = args.N k = 4 print 'alpha:', alpha print 'N:', N print 'k:', k print M = np.array(list(multinomstate.gen_states(N, k)), dtype=int) T = multinomstate.get_inverse_map(M) R_mut = wrightcore.create_mutation(M, T) R_drift = wrightcore.create_moran_drift_rate_k4(M, T) Q = alpha * R_mut + R_drift P = scipy.linalg.expm(Q) v = MatrixUtil.get_stationary_distribution(P) # # Define the volumetric data using the stationary distribution. max_prob = np.max(v) d2 = np.zeros((N+1, N+1, N+1, 4), dtype=float) U = np.array([ [0, 0, 0], [0, 1, 1], [1, 0, 1], [1, 1, 0], ], dtype=int) for p, state in zip(v, M): x, y, z = np.dot(state, U).tolist() # r, g, b, alpha d2[x, y, z] = np.array([ 255 * (p / max_prob), 0, 0, 255 * (p / max_prob), #100, ], dtype=float) #d2[x, y, z, 0] = 255 * (p / max_prob) #d2[x, y, z, 1] = 0 #d2[x, y, z, 2] = 0 #d2[x, y, z, 3] = 100 # fill the empty states for x in range(N+1): for y in range(N+1): for z in range(N+1): if (x + y + z) % 2 == 1: p_accum = np.zeros(4, dtype=float) n_accum = 0 for dx in (-1, 1): if 0 <= x+dx <= N: p_accum += d2[x+dx, y, z] n_accum += 1 for dy in (-1, 1): if 0 <= y+dy <= N: p_accum += d2[x, y+dy, z] n_accum += 1 for dz in (-1, 1): if 0 <= z+dz <= N: p_accum += d2[x, y, z+dz] n_accum += 1 d2[x, y, z] = p_accum / n_accum # # Do things that the example application does. app = QtGui.QApplication([]) w = gl.GLViewWidget() w.opts['distance'] = 2*N w.show() # # a visual grid or something #g = gl.GLGridItem() #g.scale(10, 10, 1) #w.addItem(g) # # Do some more things that the example application does. vol = gl.GLVolumeItem(d2, sliceDensity=1, smooth=True) #vol.translate(-5,-5,-10) vol.translate(-0.5*N, -0.5*N, -0.5*N) w.addItem(vol) # # add an axis thingy #ax = gl.GLAxisItem() #w.addItem(ax) if sys.flags.interactive != 1: app.exec_()
def main(): # use standard notation Nmu = 1.0 N = 20 mu = Nmu / float(N) print 'N*mu:', Nmu print 'N:', N print k = 4 M = np.array(list(multinomstate.gen_states(N, k)), dtype=int) T = multinomstate.get_inverse_map(M) nstates = len(M) #R_mut = m_factor * wrightcore.create_mutation_collapsed(M, T) #v = distn_helper(M, T, R_mut) # get the approximations alpha = 2*N*mu approx_1a = get_beta_approx(N+1, alpha) approx_2a = get_beta_approx(N+1, 2*alpha) d4_reduction, d4_nstates = get_d4_reduction(M, T) # for the initial guess all logs of ratios of probs are zero x0 = np.zeros(d4_nstates - 1) # precompute some design matrices X_side = get_design_matrix_side(M) X_diag = get_design_matrix_diag(M) print 'number of variables:', d4_nstates - 1 print f_errors = functools.partial( eval_f, M, T, d4_reduction, d4_nstates, approx_1a, approx_2a, X_side, X_diag, ) g_errors = functools.partial(eval_grad, f_errors) f = functools.partial(apply_sum_of_squares, f_errors) g = functools.partial(eval_grad, f) h = functools.partial(eval_hess, f) g_reverse = functools.partial(eval_grad_reverse_mode, f) """ result = scipy.optimize.leastsq( f_errors, x0, Dfun=g_errors, full_output=1, ) """ """ result = scipy.optimize.fmin_ncg( f, x0, fprime=g, fhess=h, avextol=1e-6, full_output=True, ) """ result = scipy.optimize.fmin_bfgs( f, x0, #fprime=g, fprime=g_reverse, full_output=True, ) print result xopt = result[0] v = unpack_distribution(nstates, d4_reduction, d4_nstates, xopt) # print some variances check_variance(M, T, v)