def eval_f_unconstrained_kb( theta, subs_counts, log_counts, v, h, gtr, syn, nonsyn, compo, asym_compo, ): # unpack theta log_mu = theta[0] log_g = algopy.zeros(6, dtype=theta) log_g[0] = theta[1] log_g[1] = theta[2] log_g[2] = theta[3] log_g[3] = theta[4] log_g[4] = theta[5] log_g[5] = 0 log_omega = theta[6] d = theta[7] log_kb = theta[8] log_nt_weights = algopy.zeros(4, dtype=theta) log_nt_weights[0] = theta[9] log_nt_weights[1] = theta[10] log_nt_weights[2] = theta[11] log_nt_weights[3] = 0 # # construct the transition matrix Q = get_Q_unconstrained_kb( gtr, syn, nonsyn, compo, asym_compo, h, log_counts, log_mu, log_g, log_omega, d, log_kb, log_nt_weights) P = algopy.expm(Q) # # return the neg log likelihood return -get_log_likelihood(P, v, subs_counts)
def build_Q(A, betas): """ computes orthogonal matrix from output of qr_house_basic Parameters ---------- A: array_likse shape(A) = (M,N) upper triangular part contains R lower triangular part contains v with v[0] = 1 betas: array_like list of beta Returns ------- Q: array_like shape(Q) = (M,M) """ M,N = A.shape Q = algopy.zeros((M,M),dtype=A) Q += numpy.eye(M) H = algopy.zeros((M,M),dtype=A) for n in range(N): v = A[n:,n:n+1].copy() v[0] = 1 H[...] = numpy.eye(M) H[n:,n:] -= betas[n] * algopy.dot(v,v.T) Q = algopy.dot(Q,H) return Q
def bar(E, A, L, phi): c = np.cos(phi) s = np.sin(phi) k0 = np.array([[c**2, c*s], [c*s, s**2]]) K = algopy.zeros((4,4), dtype=A) for i in range(2): for j in range(2): coef = 1 if (i == 0 and j == 1) or (i == 1 and j == 0): coef = -1 K[2*i,2*j] = coef*c**2 K[2*i,2*j+1] = coef*c*s K[2*i+1,2*j] = coef*c*s K[2*i+1,2*j+1] = coef*s**2 K = E * A / L * K S = algopy.zeros(4, dtype=A) S[0] = -c S[1] = -s S[2] = c S[3] = s S = E / L * S return K, S
def get_neg_ll(cls, patterns, pattern_weights, ts, tv, syn, nonsyn, full_compo, theta, ): # pick the nt distn parameters from the end of the theta vector log_nt_distns = algopy.zeros((3, 4), dtype=theta) log_nt_distns_block = algopy.reshape(theta[-9:], (3, 3)) log_nt_distns[:, :-1] = log_nt_distns_block reduced_theta = theta[:-9] unnormalized_nt_distns = algopy.exp(log_nt_distns) # normalize each of the three nucleotide distributions row_sums = algopy.sum(unnormalized_nt_distns, axis=1) nt_distns = (unnormalized_nt_distns.T / row_sums).T # get the implied codon distribution stationary_distn = codon1994.get_f3x4_codon_distn( full_compo, nt_distns, ) return A.get_neg_ll( patterns, pattern_weights, stationary_distn, ts, tv, syn, nonsyn, reduced_theta, )
def f(x): A = zeros((2,2),dtype=x) A[0,0] = numpy.log(x[0]*x[1]) A[0,1] = numpy.log(x[1]) + exp(x[0]) A[1,0] = sin(x[0])**2 + abs(cos(x[0]))**3.1 A[1,1] = x[0]**cos(x[1]) return log( dot(x.T, dot( inv(A), x)))
def f_fcn(x): A = algopy.zeros((2, 2), dtype=x) A[0, 0] = x[0] A[1, 0] = x[1] * x[0] A[0, 1] = x[1] Q, R = algopy.qr(A) return R[0, 0]
def eval_g(x, y): """ some vector-valued function """ retval = algopy.zeros(3, dtype=x) retval[0] = algopy.sin(x**2 + y) retval[1] = algopy.cos(x + y) - x retval[2] = algopy.sin(x)**2 + algopy.cos(x)**2 return retval
def f_eqcons( theta, subs_counts, log_counts, v, h, ts, tv, syn, nonsyn, compo, asym_compo, ): #FIXME: obsolete #FIXME: hardcoded for selection without recessivity parameters # # Init the array of values that should be zero when the equality # constraints are satisfied. equality_violations = algopy.zeros(2, dtype=theta) """ # # Add the equality constraint for the expected rate. # This is easily computed through the rate matrix. Q = get_Q_slsqp( ts, tv, syn, nonsyn, compo, asym_compo, h, log_counts, v, theta) expected_rate = -algopy.dot(algopy.diag(Q), v) equality_violations[0] = theta[0] - expected_rate """ # # Add the equality constraint for the mutational process # nucleotide equilibrium distribution. equality_violations[0] = algopy.sum(theta[-4:]) # return equality_violations
def inner_eval_f_gtr( theta, pre_Q_suffix, subs_counts, v, gtr, syn, nonsyn, ): """ This function is meant to be optimized with the help of algopy and ncg. @param theta: vector of unconstrained free variables @param pre_Q_suffix: this has estimates from the outer ML loop @param subs_counts: empirical substitution counts @param v: empirical codon distribution @param ts: precomputed nucleotide transition mask @param tv: precomputed nucleotide transversion mask @param syn: precomputed synonymous codon change mask @param nonsyn: precomputed non-synonymous codon change mask @return: negative log likelihood """ log_mu = theta[0] log_gtr_exch = algopy.zeros(6, dtype=theta) log_gtr_exch[0] = theta[1] log_gtr_exch[1] = theta[2] log_gtr_exch[2] = theta[3] log_gtr_exch[3] = theta[4] log_gtr_exch[4] = theta[5] log_gtr_exch[5] = 0 log_omega = theta[6] pre_Q_prefix = get_Q_prefix_gtr( gtr, syn, nonsyn, log_mu, log_gtr_exch, log_omega) return -get_log_likelihood(pre_Q_prefix, pre_Q_suffix, v, subs_counts)
def g_fun(x): out = algopy.zeros((2, 2), dtype=x) out[0, 0] = x[0] out[0, 1] = x[1] out[1, 0] = x[0] out[1, 1] = x[1] return out
def f_fcn(x): A = algopy.zeros((2,2), dtype=x) A[0,0] = x[0] A[1,0] = x[1] * x[0] A[0,1] = x[1] Q,R = algopy.qr(A) return R[0,0]
def eval_f(Y): """ some reformulations to make eval_f_orig compatible with algopy missing: support for scipy.linalg.expm i.e., this function can't be differentiated with algopy """ a, b, v = transform_params(Y) Q = algopy.zeros((4,4), dtype=Y) Q[0,0] = 0; Q[0,1] = a; Q[0,2] = b; Q[0,3] = b; Q[1,0] = a; Q[1,1] = 0; Q[1,2] = b; Q[1,3] = b; Q[2,0] = b; Q[2,1] = b; Q[2,2] = 0; Q[2,3] = a; Q[3,0] = b; Q[3,1] = b; Q[3,2] = a; Q[3,3] = 0; Q = Q * v Q -= algopy.diag(algopy.sum(Q, axis=1)) #P = linalg.expm(Q) # XXX can I get rid of the 4 on the following line? P = algopy_expm(Q, 4) S = algopy.log(algopy.dot(algopy.diag(v), P)) return -algopy.sum(S * g_data)
def eval_f( theta, subs_counts, log_counts, v, h, ts, tv, syn, nonsyn, compo, asym_compo, ): """ @param theta: length six unconstrained vector of free variables """ # unpack theta log_mu = theta[0] log_kappa = theta[1] log_omega = theta[2] log_nt_weights = algopy.zeros(4, dtype=theta) log_nt_weights[0] = theta[3] log_nt_weights[1] = theta[4] log_nt_weights[2] = theta[5] log_nt_weights[3] = 1. # construct the transition matrix Q = get_Q( ts, tv, syn, nonsyn, compo, asym_compo, h, log_counts, log_mu, log_kappa, log_omega, log_nt_weights) P = algopy.expm(Q) # return the neg log likelihood return -get_log_likelihood(P, v, subs_counts)
def erivector(alpha, coef, xyz, l, nbasis, contr_list, dtype): '''This function returns the eris in form a vector, to get an element of the eris tensor, use eri_index, included in Tools''' ### NOTE: YOU CAN REPLACE A VALUE OF THE ARRAY!!! vec_size = nbasis * (nbasis**3 + 2 * nbasis**2 + 3 * nbasis + 2) / 8 ## Vector size Eri_vec = algopy.zeros((vec_size, ), dtype=dtype) len_vec = nbasis * (nbasis + 1) / 2 for x in range(len_vec): i, j = vec_tomatrix(x, nbasis) contr_i_i = sum(contr_list[0:i]) contr_i_f = sum(contr_list[0:i + 1]) contr_j_i = sum(contr_list[0:j]) contr_j_f = sum(contr_list[0:j + 1]) for y in range(x, len_vec): k, m = vec_tomatrix(y, nbasis) contr_m_i = sum(contr_list[0:m]) contr_m_f = sum(contr_list[0:m + 1]) contr_k_i = sum(contr_list[0:k]) contr_k_f = sum(contr_list[0:k + 1]) index = matrix_tovector(x, y, len_vec) Eri_vec[index] = eri_contracted( alpha[contr_i_i:contr_i_f], coef[contr_i_i:contr_i_f], xyz[i], l[i], alpha[contr_j_i:contr_j_f], coef[contr_j_i:contr_j_f], xyz[j], l[j], alpha[contr_k_i:contr_k_f], coef[contr_k_i:contr_k_f], xyz[k], l[k], alpha[contr_m_i:contr_m_f], coef[contr_m_i:contr_m_f], xyz[m], l[m]) return Eri_vec
def IV_algopy(x, Vd): """ IV curve implemented using algopy instead of numpy """ nobs = x.shape[1] out = zeros((3, nobs), dtype=x) Ee, Tc, Rs, Rsh, Isat1_0, Isat2, Isc0, alpha_Isc, Eg = x Vt = Tc * KB / QE Isc = Ee * Isc0 * (1.0 + (Tc - T0) * alpha_Isc) Isat1 = ( Isat1_0 * (Tc ** 3.0 / T0 ** 3.0) * exp(Eg * QE / KB * (1.0 / T0 - 1.0 / Tc)) ) Vd_sc = Isc * Rs # at short circuit Vc = 0 Id1_sc = Isat1 * (exp(Vd_sc / Vt) - 1.0) Id2_sc = Isat2 * (exp(Vd_sc / 2.0 / Vt) - 1.0) Ish_sc = Vd_sc / Rsh Iph = Isc + Id1_sc + Id2_sc + Ish_sc Id1 = Isat1 * (exp(Vd / Vt) - 1.0) Id2 = Isat2 * (exp(Vd / 2.0 / Vt) - 1.0) Ish = Vd / Rsh Ic = Iph - Id1 - Id2 - Ish Vc = Vd - Ic * Rs out[0] = Ic out[1] = Vc out[2] = Ic * Vc return out
def eval_f_unconstrained_kb( theta, subs_counts, log_counts, v, h, ts, tv, syn, nonsyn, compo, asym_compo, ): """ No dominance/recessivity constraint. @param theta: length seven unconstrained vector of free variables """ # unpack theta log_mu = theta[0] log_kappa = theta[1] log_omega = theta[2] d = theta[3] log_kb = theta[4] log_nt_weights = algopy.zeros(4, dtype=theta) log_nt_weights[0] = theta[5] log_nt_weights[1] = theta[6] log_nt_weights[2] = theta[7] log_nt_weights[3] = 0 # # construct the transition matrix Q = get_Q_unconstrained_kb( ts, tv, syn, nonsyn, compo, asym_compo, h, log_counts, log_mu, log_kappa, log_omega, d, log_kb, log_nt_weights) P = algopy.expm(Q) # # return the neg log likelihood neg_log_likelihood = -get_log_likelihood(P, v, subs_counts) print neg_log_likelihood return neg_log_likelihood
def dalecv2(self, p): """DALECV2 carbon balance model ------------------------------- evolves carbon pools to the next time step, taking the 6 carbon pool values and 17 parameters at time t and evolving them to time t+1. phi_on = phi_onset(d_onset, cronset) phi_off = phi_fall(d_fall, crfall, clspan) gpp = acm(cf, clma, ceff) temp = temp_term(Theta) clab2 = (1 - phi_on)*clab + (1-f_auto)*(1-f_fol)*f_lab*gpp cf2 = (1 - phi_off)*cf + phi_on*clab + (1-f_auto)*f_fol*gpp cr2 = (1 - theta_roo)*cr + (1-f_auto)*(1-f_fol)*(1-f_lab)*f_roo*gpp cw2 = (1 - theta_woo)*cw + (1-f_auto)*(1-f_fol)*(1-f_lab)*(1-f_roo)*gpp cl2 = (1-(theta_lit+theta_min)*temp)*cl + theta_roo*cr + phi_off*cf cs2 = (1 - theta_som*temp)*cs + theta_woo*cw + theta_min*temp*cl """ out = algopy.zeros(23, dtype=p) phi_on = self.phi_onset(p[17], p[19]) phi_off = self.phi_fall(p[20], p[21], p[10]) gpp = self.acm(p[1], p[22], p[16]) temp = self.temp_term(p[15]) out[0] = (1 - phi_on)*p[0] + (1-p[7])*(1-p[8])*p[18]*gpp out[1] = (1 - phi_off)*p[1] + phi_on*p[0] + (1-p[7])*p[8]*gpp out[2] = (1 - p[12])*p[2] + (1-p[7])*(1-p[8])*(1-p[18])*p[9]*gpp out[3] = (1 - p[11])*p[3] + (1-p[7])*(1-p[8])*(1-p[18])*(1-p[9])*gpp out[4] = (1-(p[13]+p[6])*temp)*p[4] + p[12]*p[2] + phi_off*p[1] out[5] = (1 - p[14]*temp)*p[5] + p[11]*p[3] + p[6]*temp*p[4] out[6:23] = p[6:23] return out
def eval_g(self, x): out = algopy.zeros(3, dtype=x) out[0] = 2 * x[0]**2 + 3 * x[1]**4 + x[2] + 4 * x[3]**2 + 5 * x[4] out[1] = 7 * x[0] + 3 * x[1] + 10 * x[2]**2 + x[3] - x[4] out[2] = 23 * x[0] + x[1]**2 + 6 * x[5]**2 - 8 * x[6] - 4 * x[0]**2 - x[ 1]**2 + 3 * x[0] * x[1] - 2 * x[2]**2 - 5 * x[5] + 11 * x[6] return out
def eval_f( theta, subs_counts, log_counts, v, h, ts, tv, syn, nonsyn, compo, asym_compo, ): """ The function formerly known as minimize-me. @param theta: length six unconstrained vector of free variables """ # unpack theta log_mu = theta[0] log_kappa = theta[1] log_omega = theta[2] log_nt_weights = algopy.zeros(4, dtype=theta) log_nt_weights[0] = theta[3] log_nt_weights[1] = theta[4] log_nt_weights[2] = theta[5] log_nt_weights[3] = 0 # # construct the transition matrix Q = get_Q( ts, tv, syn, nonsyn, compo, asym_compo, h, log_counts, log_mu, log_kappa, log_omega, log_nt_weights) P = algopy.expm(Q) # # return the neg log likelihood return -get_log_likelihood(P, v, subs_counts)
def eval_g(x, y): """ some vector-valued function """ retval = algopy.zeros(3, dtype=x) retval[0] = algopy.sin(x**2 + y) retval[1] = algopy.cos(x+y) - x retval[2] = algopy.sin(x)**2 + algopy.cos(x)**2 return retval
def dalecv2_diff(self, p): """DALECV2 carbon balance model ------------------------------- evolves carbon pools to the next time step, taking the 6 carbon pool values and 17 parameters at time t and evolving them to time t+1. Outputs both the 6 evolved C pool values and the 17 constant parameter values. phi_on = phi_onset(d_onset, cronset) phi_off = phi_fall(d_fall, crfall, clspan) gpp = acm(cf, clma, ceff) temp = temp_term(Theta) clab2 = (1 - phi_on)*clab + (1-f_auto)*(1-f_fol)*f_lab*gpp cf2 = (1 - phi_off)*cf + phi_on*clab + (1-f_auto)*f_fol*gpp cr2 = (1 - theta_roo)*cr + (1-f_auto)*(1-f_fol)*(1-f_lab)*f_roo*gpp cw2 = (1 - theta_woo)*cw + (1-f_auto)*(1-f_fol)*(1-f_lab)*(1-f_roo)*gpp cl2 = (1-(theta_lit+theta_min)*temp)*cl + theta_roo*cr + phi_off*cf cs2 = (1 - theta_som*temp)*cs + theta_woo*cw + theta_min*temp*cl """ out = algopy.zeros(6, dtype=p[-6]) phi_on = self.phi_onset(p[11], p[13]) phi_off = self.phi_fall(p[14], p[15], p[4]) gpp = self.acm(p[18], p[16], p[10], self.dC.acm) temp = self.temp_term(p[9], self.dC.t_mean[self.x]) out[0] = (1 - phi_on)*p[17] + (1-p[1])*(1-p[2])*p[12]*gpp out[1] = (1 - phi_off)*p[18] + phi_on*p[17] + (1-p[1])*p[2]*gpp out[2] = (1 - p[6])*p[19] + (1-p[1])*(1-p[2])*(1-p[12])*p[3]*gpp out[3] = (1 - p[5])*p[20] + (1-p[1])*(1-p[2])*(1-p[12])*(1-p[3])*gpp out[4] = (1-(p[7]+p[0])*temp)*p[21] + p[6]*p[19] + phi_off*p[18] out[5] = (1 - p[8]*temp)*p[22] + p[5]*p[20] + p[0]*temp*p[21] return out
def f(x): A = zeros((2, 2), dtype=x) A[0, 0] = numpy.log(x[0] * x[1]) A[0, 1] = numpy.log(x[1]) + exp(x[0]) A[1, 0] = sin(x[0])**2 + abs(cos(x[0]))**3.1 A[1, 1] = x[0]**cos(x[1]) return log(dot(x.T, dot(inv(A), x)))
def eval_f(Y): """ some reformulations to make eval_f_orig compatible with algopy missing: support for scipy.linalg.expm i.e., this function can't be differentiated with algopy """ a, b, v = transform_params(Y) Q = algopy.zeros((4, 4), dtype=Y) Q[0, 0] = 0 Q[0, 1] = a Q[0, 2] = b Q[0, 3] = b Q[1, 0] = a Q[1, 1] = 0 Q[1, 2] = b Q[1, 3] = b Q[2, 0] = b Q[2, 1] = b Q[2, 2] = 0 Q[2, 3] = a Q[3, 0] = b Q[3, 1] = b Q[3, 2] = a Q[3, 3] = 0 Q = Q * v Q -= algopy.diag(algopy.sum(Q, axis=1)) P = algopy.expm(Q) S = algopy.log(algopy.dot(algopy.diag(v), P)) return -algopy.sum(S * g_data)
def eval_f_eigh(Y): """ some reformulations to make eval_f_orig compatible with algopy replaced scipy.linalg.expm by a symmetric eigenvalue decomposition this function **can** be differentiated with algopy """ a, b, v = transform_params(Y) Q = algopy.zeros((4,4), dtype=Y) Q[0,0] = 0; Q[0,1] = a; Q[0,2] = b; Q[0,3] = b; Q[1,0] = a; Q[1,1] = 0; Q[1,2] = b; Q[1,3] = b; Q[2,0] = b; Q[2,1] = b; Q[2,2] = 0; Q[2,3] = a; Q[3,0] = b; Q[3,1] = b; Q[3,2] = a; Q[3,3] = 0; Q = algopy.dot(Q, algopy.diag(v)) Q -= algopy.diag(algopy.sum(Q, axis=1)) va = algopy.diag(algopy.sqrt(v)) vb = algopy.diag(1./algopy.sqrt(v)) W, U = algopy.eigh(algopy.dot(algopy.dot(va, Q), vb)) M = algopy.dot(U, algopy.dot(algopy.diag(algopy.exp(W)), U.T)) P = algopy.dot(vb, algopy.dot(M, va)) S = algopy.log(algopy.dot(algopy.diag(v), P)) return -algopy.sum(S * g_data)
def normalization(alpha, c, l, list_contr, dtype=np.float64(1.0)): contr = 0 coef = algopy.zeros(len(alpha), dtype=dtype) for ib, ci in enumerate(list_contr): div = 1.0 l_large = 0 for m in l[ib]: if (m > 0): l_large += 1 for k in range(1, 2 * m, 2): div = div * k div = div / pow(2, l_large) div = div * pow(np.pi, 1.5) for i in range(ci): coef[contr + i] = normalization_primitive(alpha[contr + i], l[ib], l_large) * c[contr + i] tmp = 0.0 for i in range(ci): for j in range(ci): tmp = tmp + coef[contr + j] * coef[contr + i] / np.power( alpha[contr + i] + alpha[contr + j], l_large + 1.5) tmp = algopy.sqrt(tmp * div) for i in range(contr, contr + ci): coef[i] = coef[i] / tmp contr = contr + ci return coef
def eval_covariance_matrix_naive(J1, J2): M, N = J1.shape K, N = J2.shape tmp = zeros((N + K, N + K), dtype=J1) tmp[:N, :N] = dot(J1.T, J1) tmp[:N, N:] = J2.T tmp[N:, :N] = J2 return inv(tmp)[:N, :N]
def f(x): nobs = x.shape[1:] f0 = x[0]**2 * sin(x[1])**2 f1 = x[0]**2 * cos(x[1])**2 out = zeros((2,) + nobs, dtype=x) out[0,:] = f0 out[1,:] = f1 return out
def eval_covariance_matrix_naive(J1, J2): M,N = J1.shape K,N = J2.shape tmp = zeros((N+K, N+K), dtype=J1) tmp[:N,:N] = dot(J1.T,J1) tmp[:N,N:] = J2.T tmp[N:,:N] = J2 return inv(tmp)[:N,:N]
def get_errors(M, v, approx_1a, approx_2a, X_side, X_diag): nstates = len(M) N = np.sum(M[0]) observed_1a_1 = algopy.zeros(N+1, dtype=v) #observed_1a_2 = algopy.zeros(N+1, dtype=v) observed_2a = algopy.zeros(N+1, dtype=v) """ for i in range(nstates): p = v[i] AB, Ab, aB, ab = M[i].tolist() observed_1a_1[AB + Ab] += p #observed_1a_2[AB + aB] += p #observed_2a[AB + ab] += p observed_2a[Ab + aB] += p """ observed_1a_1 = algopy.dot(v, X_side) observed_2a = algopy.dot(v, X_diag) # errors_1a_1 = observed_1a_1 - approx_1a #errors_1a_2 = observed_1a_2 - approx_1a errors_2a = observed_2a - approx_2a #FIXME: Use algopy.hstack when it becomes available. #FIXME: using the workaround http://projects.scipy.org/scipy/ticket/1454 #FIXME: but this padding of the errors with zeros should not be necessary #nonunif_penalty = 0.01 #nonunif = v - np.ones(nstates) / float(nstates) #nonunif = np.zeros(nstates) errors = algopy.zeros( #len(nonunif) + errors_1a_1.shape[0] + #len(errors_1a_2) + errors_2a.shape[0], dtype=v, ) index = 0 errors[index:index+errors_1a_1.shape[0]] = errors_1a_1 index += errors_1a_1.shape[0] #errors[index:index+len(errors_1a_2)] = errors_1a_2 #index += len(errors_1a_2) errors[index:index+errors_2a.shape[0]] = errors_2a index += errors_2a.shape[0] #errors[index:index+len(nonunif)] = nonunif_penalty * nonunif #index += len(nonunif) return errors
def unpack_distribution(nstates, d4_reduction, d4_nstates, X): log_v = algopy.zeros(nstates, dtype=X) for i_full, i_reduced in enumerate(d4_reduction): if i_reduced == d4_nstates - 1: log_v[i_full] = 0.0 else: log_v[i_full] = X[i_reduced] v = algopy.exp(log_v) v = v / algopy.sum(v) return v
def jac_try(self, p): out = algopy.zeros((self.endrun-self.startrun, len(p)), dtype=p[-6]) self.x = self.startrun pp = p for t in xrange(self.endrun-self.startrun): update = self.dalecv2(self.create_ordered_dic(pp)) pp = update out[t, :] = update self.x += 1 return out
def transform_params(Y): X = algopy.exp(Y) tsrate, tvrate = X[0], X[1] v_unnormalized = algopy.zeros(4, dtype=X) v_unnormalized[0] = X[2] v_unnormalized[1] = X[3] v_unnormalized[2] = X[4] v_unnormalized[3] = 1.0 v = v_unnormalized / algopy.sum(v_unnormalized) return tsrate, tvrate, v
def fockmatrix(Hcore,Eri,D,nbasis,alpha,dtype): F = algopy.zeros((nbasis,nbasis),dtype=dtype) for i in range(nbasis): for j in range(i,nbasis): tmp = Hcore[i,j] for k in range(nbasis): for l in range(nbasis): tmp = D[k,l]*(2.0*Eri[eri_index(i,j,k,l,nbasis)]-Eri[eri_index(i,k,j,l,nbasis)])+ tmp F[i,j] = F[j,i] = tmp return F
def get_ll_root(summary, distn, blink_on, blink_off): """ Parameters ---------- summary : Summary object Summary of blinking process trajectories. distn : dense possibly exotic array Primary state distribution. blink_on : float, or exotic float-like with derivatives information blink rate on blink_off : float, or exotic float-like with derivatives information blink rate off Returns ------- ll : float, or exotic float-like with derivatives information log likelihood contribution from root state """ # construct the blink distribution with the right data type blink_distn = algopy.zeros(2, dtype=distn) blink_distn[0] = blink_off / (blink_on + blink_off) blink_distn[1] = blink_on / (blink_on + blink_off) # initialize expected log likelihood using the right data type ll = algopy.zeros(1, dtype=distn)[0] # root primary state contribution to expected log likelihood obs = algopy.zeros_like(distn) for state, count in summary.root_pri_to_count.items(): if count: ll = ll + count * log(distn[state]) # root blink state contribution to expected log likelihood if summary.root_off_count: ll = ll + summary.root_off_count * log(blink_distn[0]) if summary.root_xon_count: ll = ll + summary.root_xon_count * log(blink_distn[1]) # return expected log likelihood contribution of root return ll / summary.nsamples
def fun(x): f0 = x[0] ** 2 + x[1] ** 2 f1 = x[0] ** 3 + x[1] ** 3 s0 = f0.size s1 = f1.size out = algopy.zeros((2, (s0 + s1) / 2), dtype=x) out[0, :] = f0 out[1, :] = f1 return out
def eval_g(x): print x print type(x) out = zeros(3,dtype=x) o0 = 2*x[0]**2 + 3*x[1]**4 + x[2] + 4*x[3]**2 + 5*x[4] print o0 print type(o0) out[0] = o0 out[1] = 7*x[0] + 3*x[1] + 10*x[2]**2 + x[3] - x[4] out[2] = 23*x[0] + x[1]**2 + 6*x[5]**2 - 8*x[6] -4*x[0]**2 - x[1]**2 + 3*x[0]*x[1] -2*x[2]**2 - 5*x[5]+11*x[6] return out
def fun(x): f0 = x[0] ** 2 + x[1] ** 2 f1 = x[0] ** 3 + x[1] ** 3 s0 = f0.size s1 = f1.size out = algopy.zeros((2, (s0 + s1) // 2), dtype=x) out[0, :] = f0 out[1, :] = f1 return out
def dalecv2_diff(self, p): """DALECV2 carbon balance model ------------------------------- evolves carbon pools to the next time step, taking the 6 carbon pool values and 17 parameters at time t and evolving them to time t+1. Outputs both the 6 evolved C pool values and the 17 constant parameter values. phi_on = phi_onset(d_onset, cronset) phi_off = phi_fall(d_fall, crfall, clspan) gpp = acm(cf, clma, ceff) temp = temp_term(Theta) clab2 = (1 - phi_on)*clab + (1-f_auto)*(1-f_fol)*f_lab*gpp cf2 = (1 - phi_off)*cf + phi_on*clab + (1-f_auto)*f_fol*gpp cr2 = (1 - theta_roo)*cr + (1-f_auto)*(1-f_fol)*(1-f_lab)*f_roo*gpp cw2 = (1 - theta_woo)*cw + (1-f_auto)*(1-f_fol)*(1-f_lab)*(1-f_roo)*gpp cl2 = (1-(theta_lit+theta_min)*temp)*cl + theta_roo*cr + phi_off*cf cs2 = (1 - theta_som*temp)*cs + theta_woo*cw + theta_min*temp*cl """ out = algopy.zeros(6, dtype=p[-6]) # phenology self.phenology(p[11], p[12], p[18], p[16]) # ACM gpp = self.acm(p[18], p[10], self.dC.acm) # temperature term temp = self.temp_term(p[9], self.dC.t_mean[self.x]) # fluxes r_alabt = p[4] * (1 - p[13]) * p[15] * p[18] * self.mtf * temp r_alabf = p[14] * p[15] * p[17] * self.mtl * temp # r_a = p[1]*gpp + r_alabt + r_alabf npp1 = (1 - p[1]) * gpp a_tolab = p[4] * (1 - p[13]) * (1 - p[15]) * p[18] * self.mtf * temp a_fromlab = p[14] * (1 - p[15]) * p[17] * self.mtl * temp a_f = min(p[16] - p[18], p[2] * npp1) * self.mtl + a_fromlab npp2 = npp1 - min(p[16] - p[18], p[2] * npp1) * self.mtl a_r = p[3] * npp2 a_w = (1 - p[4]) * npp2 l_f = p[4] * p[18] * p[13] * self.mtf l_w = p[5] * p[20] l_r = p[6] * p[19] r_h1 = p[7] * p[21] * temp r_h2 = p[8] * p[22] * temp decomp = p[0] * p[21] * temp out[0] = p[17] + a_tolab - a_fromlab - r_alabf out[1] = p[18] + a_f - l_f - a_tolab - r_alabt out[2] = p[19] + a_r - l_r out[3] = p[20] + a_w - l_w out[4] = p[21] + l_f + l_r - r_h1 - decomp out[5] = p[22] + l_w + decomp - r_h2 return out
def qr_house(A): """ computes QR decomposition using Householder relections (Q,R) = qr_house(A) such that 0 = Q R - A 0 = dot(Q.T,Q) - eye(M) R upper triangular Parameters ---------- A: array_like shape(A) = (M, N), M >= N overwritten on exit Returns ------- R: array_like strict lower triangular part contains the Householder vectors v upper triangular matrix R Q: array_like orthogonal matrix """ M,N = A.shape Q = algopy.zeros((M,M),dtype=A) Q += numpy.eye(M) H = algopy.zeros((M,M),dtype=A) for n in range(N): v,beta = house(A[n:,n:n+1]) A[n:,n:] -= beta * algopy.dot(v, algopy.dot(v.T,A[n:,n:])) H[...] = numpy.eye(M) H[n:,n:] -= beta * algopy.dot(v,v.T) Q = algopy.dot(Q,H) return Q, algopy.triu(A)
def kineticmatrix(alpha, coef, xyz, l, nbasis, contr_list, dtype): T = algopy.zeros((nbasis, nbasis), dtype=dtype) cont_i = 0 for i, ci in enumerate(contr_list): cont_j = cont_i for j, cj in enumerate(contr_list[i:]): T[i, j + i] = T[j + i, i] = kinetic_contracted( alpha[cont_i:cont_i + ci], coef[cont_i:cont_i + ci], xyz[i], l[i], alpha[cont_j:cont_j + cj], coef[cont_j:cont_j + cj], xyz[j + i], l[j + i]) cont_j += cj cont_i += ci return T
def eval_f( theta, #subs_counts, patterns, pattern_weights, log_counts, v, h, ts, tv, syn, nonsyn, compo, asym_compo, ): """ @param theta: length six unconstrained vector of free variables """ # unpack theta log_mu = theta[0] log_kappa = theta[1] log_omega = theta[2] log_nt_weights = algopy.zeros(4, dtype=theta) log_nt_weights[0] = theta[3] log_nt_weights[1] = theta[4] log_nt_weights[2] = theta[5] log_nt_weights[3] = 0 # construct the transition matrix Q = get_Q( ts, tv, syn, nonsyn, compo, asym_compo, h, log_counts, log_mu, log_kappa, log_omega, log_nt_weights) P = algopy.expm(Q) # return the neg log likelihood """ log_likelihood = get_log_likelihood(P, v, subs_counts) #A = subs_counts #B = algopy.log(P.T * v) #log_likelihoods = slow_part(A, B) #return algopy.sum(log_likelihoods) """ npatterns = patterns.shape[0] nstates = patterns.shape[1] ov = (0, 1) v_to_children = {1 : [0]} de_to_P = {(1, 0) : P} root_prior = v log_likelihood = alignll.fast_fels( ov, v_to_children, de_to_P, root_prior, patterns, pattern_weights, ) neg_ll = -log_likelihood print neg_ll return neg_ll
def eval_f_unconstrained_kb( theta, subs_counts, log_counts, v, h, gtr, syn, nonsyn, compo, asym_compo, ): # unpack theta log_mu = theta[0] log_g = algopy.zeros(6, dtype=theta) log_g[0] = theta[1] log_g[1] = theta[2] log_g[2] = theta[3] log_g[3] = theta[4] log_g[4] = theta[5] log_g[5] = 0 log_omega = theta[6] d = theta[7] log_kb = theta[8] log_nt_weights = algopy.zeros(4, dtype=theta) log_nt_weights[0] = theta[9] log_nt_weights[1] = theta[10] log_nt_weights[2] = theta[11] log_nt_weights[3] = 0 # # construct the transition matrix Q = get_Q_unconstrained_kb(gtr, syn, nonsyn, compo, asym_compo, h, log_counts, log_mu, log_g, log_omega, d, log_kb, log_nt_weights) P = algopy.expm(Q) # # return the neg log likelihood return -get_log_likelihood(P, v, subs_counts)
def nuclearmatrix(alpha, coef, xyz, l, nbasis, charge, atoms, numatoms, contr_list, dtype): V = algopy.zeros((nbasis, nbasis), dtype=dtype) cont_i = 0 for i, ci in enumerate(list(contr_list)): cont_j = cont_i for j, cj in enumerate(contr_list[i:]): V[i, j + i] = V[j + i, i] = nuclear_contracted( alpha[cont_i:cont_i + ci], coef[cont_i:cont_i + ci], xyz[i], l[i], alpha[cont_j:cont_j + cj], coef[cont_j:cont_j + cj], xyz[j + i], l[j + i], atoms, charge, numatoms) cont_j += cj cont_i += ci return V
def test_svd(self): D,P,M,N = 3,1,5,2 A = UTPM(numpy.random.random((D,P,M,N))) U,s,V = svd(A) S = zeros((M,N),dtype=A) S[:N,:N] = diag(s) assert_array_almost_equal( (dot(dot(U, S), V.T) - A).data, 0.) assert_array_almost_equal( (dot(U.T, U) - numpy.eye(M)).data, 0.) assert_array_almost_equal( (dot(U, U.T) - numpy.eye(M)).data, 0.) assert_array_almost_equal( (dot(V.T, V) - numpy.eye(N)).data, 0.) assert_array_almost_equal( (dot(V, V.T) - numpy.eye(N)).data, 0.)
def eval_f( theta, subs_counts, log_counts, v, h, gtr, syn, nonsyn, compo, asym_compo, ): """ The function formerly known as minimize-me. @param theta: length six unconstrained vector of free variables """ # unpack theta log_mu = theta[0] log_g = algopy.zeros(6, dtype=theta) log_g[0] = theta[1] log_g[1] = theta[2] log_g[2] = theta[3] log_g[3] = theta[4] log_g[4] = theta[5] log_g[5] = 0 log_omega = theta[6] log_nt_weights = algopy.zeros(4, dtype=theta) log_nt_weights[0] = theta[7] log_nt_weights[1] = theta[8] log_nt_weights[2] = theta[9] log_nt_weights[3] = 0 # # construct the transition matrix Q = get_Q( gtr, syn, nonsyn, compo, asym_compo, h, log_counts, log_mu, log_g, log_omega, log_nt_weights) P = algopy.expm(Q) # # return the neg log likelihood return -get_log_likelihood(P, v, subs_counts)
def test_gradient(self): x = numpy.array([3.,7.]) cg = algopy.CGraph() Fx = algopy.Function(x) Fy = algopy.zeros(2, Fx) Fy[0] = Fx[0] Fy[1] = Fy[0]*Fx[1] Fz = 2*Fy[1]**2 cg.trace_off() cg.independentFunctionList = [Fx] cg.dependentFunctionList = [Fz] x = numpy.array([11,13.]) assert_array_almost_equal([4*x[1]**2 * x[0], 4*x[0]**2 * x[1]], cg.gradient([x])[0])
def prod(x, axis=None, dtype=None, out=None): """ generic prod function """ if axis != None or dtype != None or out != None: raise NotImplementedError('') elif isinstance(x, numpy.ndarray): return numpy.prod(x) elif isinstance(x, Function) or isinstance(x, UTPM): y = zeros(1,dtype=x) y[0] = x[0] for xi in x[1:]: y[0] = y[0] * xi return y[0]
def get_pre_Q(genetic_code, kappa, omega, A, C, G, T): """ Compute a Muse-Gaut 1994 pre-rate-matrix without any particular scale. The dtype of the returned object should be like that of its parameters, so that exotic array types like algopy arrays are supported. The diagonal should consist of zeros because it is a pre-rate-matrix. Subsequent function calls can deal with expected rate. """ # define the states and initialize the pre-rate-matrix nstates = len(genetic_code) states = range(nstates) Q = algopy.zeros((nstates, nstates), dtype=kappa) # define state_to_part state_to_residue = dict((s, r) for s, r, c in genetic_code) alphabetic_residues = sorted(set(r for s, r, c in genetic_code)) residue_to_part = dict((r, i) for i, r in enumerate(alphabetic_residues)) state_to_part = dict((s, residue_to_part[r]) for s, r, c in genetic_code) nt_distn = { 'A' : A, 'C' : C, 'G' : G, 'T' : T, } # construct the mg94 rate matrix transitions = ('AG', 'GA', 'CT', 'TC') for a, (state_a, residue_a, codon_a) in enumerate(genetic_code): for b, (state_b, residue_b, codon_b) in enumerate(genetic_code): if hamming_distance(codon_a, codon_b) != 1: continue for nta, ntb in zip(codon_a, codon_b): if nta != ntb: rate = nt_distn[ntb] if nta + ntb in transitions: rate *= kappa if residue_a != residue_b: rate *= omega Q[a, b] = rate # return the pre-rate-matrix return Q
def dalecv2(self, p): """DALECV2 carbon balance model ------------------------------- evolves carbon pools to the next time step, taking the 6 carbon pool values and 17 parameters at time t and evolving them to time t+1. Outputs both the 6 evolved C pool values and the 17 constant parameter values. phi_on = phi_onset(d_onset, cronset) phi_off = phi_fall(d_fall, crfall, clspan) gpp = acm(cf, clma, ceff) temp = temp_term(Theta) clab2 = (1 - phi_on)*clab + (1-f_auto)*(1-f_fol)*f_lab*gpp cf2 = (1 - phi_off)*cf + phi_on*clab + (1-f_auto)*f_fol*gpp cr2 = (1 - theta_roo)*cr + (1-f_auto)*(1-f_fol)*(1-f_lab)*f_roo*gpp cw2 = (1 - theta_woo)*cw + (1-f_auto)*(1-f_fol)*(1-f_lab)*(1-f_roo)*gpp cl2 = (1-(theta_lit+theta_min)*temp)*cl + theta_roo*cr + phi_off*cf cs2 = (1 - theta_som*temp)*cs + theta_woo*cw + theta_min*temp*cl """ out = algopy.zeros(len(p), dtype=p['cf']) # ACM gpp = self.acm(p['cf'], p['clma'], p['ceff'], self.dC.acm) # Labile release and leaf fall factors phi_on = self.phi_onset(p['d_onset'], p['cronset']) phi_off = self.phi_fall(p['d_fall'], p['crfall'], p['clspan']) # Temperature factor temp = self.temp_term(p['Theta'], self.dC.t_mean[self.x]) out[-6] = (1 - phi_on) * p['clab'] + (1 - p['f_auto']) * ( 1 - p['f_fol']) * p['f_lab'] * gpp out[-5] = (1 - phi_off) * p['cf'] + phi_on * p['clab'] + ( 1 - p['f_auto']) * (1 - p['f_fol']) * p['f_lab'] * gpp out[-4] = (1 - p['theta_roo']) * p['cr'] + (1 - p['f_auto']) * ( 1 - p['f_fol']) * (1 - p['f_lab']) * p['f_roo'] * gpp out[-3] = (1 - p['theta_woo']) * p['cw'] + (1 - p['f_auto']) * ( 1 - p['f_fol']) * (1 - p['f_lab']) * (1 - p['f_roo']) * gpp out[-2] = (1 - (p['theta_lit'] + p['theta_min']) * temp ) * p['cl'] + p['theta_roo'] * p['cr'] + phi_off * p['cf'] out[-1] = (1 - p['theta_som'] * temp) * p['cs'] + p['theta_woo'] * p[ 'cw'] + p['theta_min'] * temp * p['cl'] for x in xrange(len(p) - 6): out[x] = p[self.names[x]] return out
def eval_f_eigh(Y): """ some reformulations to make eval_f_orig compatible with algopy replaced scipy.linalg.expm by a symmetric eigenvalue decomposition this function **can** be differentiated with algopy """ a, b, v = transform_params(Y) Q = algopy.zeros((4, 4), dtype=Y) Q[0, 0] = 0 Q[0, 1] = a Q[0, 2] = b Q[0, 3] = b Q[1, 0] = a Q[1, 1] = 0 Q[1, 2] = b Q[1, 3] = b Q[2, 0] = b Q[2, 1] = b Q[2, 2] = 0 Q[2, 3] = a Q[3, 0] = b Q[3, 1] = b Q[3, 2] = a Q[3, 3] = 0 Q = algopy.dot(Q, algopy.diag(v)) Q -= algopy.diag(algopy.sum(Q, axis=1)) va = algopy.diag(algopy.sqrt(v)) vb = algopy.diag(1. / algopy.sqrt(v)) W, U = algopy.eigh(algopy.dot(algopy.dot(va, Q), vb)) M = algopy.dot(U, algopy.dot(algopy.diag(algopy.exp(W)), U.T)) P = algopy.dot(vb, algopy.dot(M, va)) S = algopy.log(algopy.dot(algopy.diag(v), P)) return -algopy.sum(S * g_data)