def gradient_it(m, eps): cnt = 0 (A, B) = ut.split_at(m) x0 = [0] * len(B) r0 = ut.minus(B, ut.subst(A, x0)) p0 = r0 z0 = r0 s0 = r0 for i in range(0, 2 * len(B)): sub = ut.subst(A, z0) prev_p = p0 prev_r = r0 At = ut.transpose_matrix(A) # print(At) a = ut.scalar_product(p0, r0) / ut.scalar_product(s0, sub) x0 = ut.plus(x0, ut.mul(a, z0)) r0 = ut.minus(r0, ut.mul(a, sub)) p0 = ut.minus(p0, ut.mul(a, ut.subst(At, s0))) b = ut.scalar_product(p0, r0) / ut.scalar_product(prev_p, prev_r) z0 = ut.plus(r0, ut.mul(b, z0)) s0 = ut.plus(p0, ut.mul(b, s0)) if abs(ut.norm(r0) / ut.norm(B)) < eps: break cnt += 1 return (x0, cnt)
def estremo_gibbs(iterations=50000, verbose=False, every=1000, sigma=1, mu=-10, Ne=5): nu = Ne - 1 L = 10 N = 20 code, motif = (sample_code(L=10, sigma=1), random_motif(length=L, num_sites=N)) def log_f((code, motif)): eps = map(lambda x: -log(x), pw_prob_sites(motif, code)) return sum(nu * log(1 / (1 + exp(ep - mu))) for ep in eps) chain = [(code, motif[:])] print log_f((code, motif)) for iteration in trange(iterations): for i in range(N): site = motif[i] for j in range(L): b = site[j] log_ps = [] bps = [bp for bp in "ACGT" if not bp == b] for bp in bps: site_p = subst(site, bp, j) log_ps.append(log_f((code, [site_p]))) log_ps = [p - min(log_ps) for p in log_ps] bp = inverse_cdf_sample(bps, map(exp, log_ps), normalized=False) motif[i] = subst(site, bp, j) for k in range(L - 1): for b1 in "ACGT": for b2 in "ACGT": dws = [random.gauss(0, 0.1) for _ in range(10)] code_ps = [[d.copy() for d in code] for _ in range(10)] for code_p, dw in zip(code_ps, dws): code_p[k][b1, b2] += dw log_ps = [log_f((code_p, motif)) for code_p in code_ps] log_ps = [p - min(log_ps) for p in log_ps] code_p = inverse_cdf_sample(code_ps, map(exp, log_ps), normalized=False) code = code_p print log_f((code, motif)) chain.append((code, motif[:])) return chain x0 = (sample_code(L=10, sigma=1), random_motif(length=10, num_sites=20)) chain = mh(log_f, prop, x0, use_log=True, iterations=iterations, verbose=verbose, every=every) return chain
def jacobi(m, eps): (A, f) = split_at(m) (B, c) = to_iter(A, f) prev = c x = plus(subst(B, prev), c) cnt = 1 n = norma(B) if (n >= 1): print("norma > 1, Jacobi error") return ([[]], 0) e1 = (1 - n) / n * eps while (myNorm(minus(x, prev)) > e1): cnt += 1 prev = x x = plus(subst(B, prev), c) return (x, cnt)
def maxent_site_cftp(N, beta): top_site = ["A"]*N bottom_site = ["T"]*N def mutate_site(site, (ri, rdir)): i = "ACGT".index(site[ri]) ip = max(0, min(3, i + rdir)) bp = "ACGT"[ip] return subst(site,[bp],ri)
def maxent_site_cftp(N, beta): top_site = ["A"] * N bottom_site = ["T"] * N def mutate_site(site, (ri, rdir)): i = "ACGT".index(site[ri]) ip = max(0, min(3, i + rdir)) bp = "ACGT"[ip] return subst(site, [bp], ri)
def sample_site_cftp_dep(matrix, mu, Ne): L = len(matrix) def log_phat(s): ep = score_seq(matrix,s) nu = Ne - 1 return -nu*log(1 + exp(ep - mu)) first_site = "A"*L last_site = "T"*L best_site = "".join(["ACGT"[argmin(row)] for row in matrix]) worst_site = "".join(["ACGT"[argmax(row)] for row in matrix]) trajs = [[best_site],[random_site(L)],[random_site(L)],[random_site(L)], [worst_site]] def mutate_site(site,(ri,rb)): return subst(site,"ACGT"[rb],ri)
def mutate_sites_spec(sites, site_mu): n = len(sites) L = len(sites[0]) muts = np.random.binomial(n * L, site_mu) if muts == 0: return sites # else... new_sites = sites[:] for mut in range(muts): i = random.randrange(n) j = random.randrange(L) site = new_sites[i] b = site[j] new_b = random.choice([c for c in "ACGT" if not c == b]) new_sites[i] = subst(site, new_b, j) return new_sites
def mutate_sites_spec(sites,site_mu): n = len(sites) L = len(sites[0]) muts = np.random.binomial(n*L,site_mu) if muts == 0: return sites # else... new_sites = sites[:] for mut in range(muts): i = random.randrange(n) j = random.randrange(L) site = new_sites[i] b = site[j] new_b = random.choice([c for c in "ACGT" if not c == b]) new_sites[i] = subst(site,new_b,j) return new_sites
def mutate_motif_k_times(motif, k): motif_ = motif[:] n = len(motif) L = len(motif[0]) N = n * L rs = range(N) choices = [] for _ in range(k): r = random.choice(rs) choices.append(r) rs.remove(r) for r in choices: i = r / L j = r % L b = motif[i][j] new_b = random.choice([c for c in "ACGT" if not c == b]) motif_[i] = subst(motif_[i], new_b, j) return motif_
def mutate_motif_k_times_ref(motif, k): motif_ = motif[:] n = len(motif) L = len(motif[0]) N = n * L k_so_far = 0 choices = [] while k_so_far < k: i = random.randrange(n) j = random.randrange(L) if (i, j) in choices: continue else: choices.append((i, j)) k_so_far += 1 b = motif[i][j] new_b = random.choice([c for c in "ACGT" if not c == b]) motif_[i] = subst(motif_[i], new_b, j) return motif_
def sample_site_cftp(matrix, mu, Ne): L = len(matrix) f = seq_scorer(matrix) def log_phat(s): ep = f(s) nu = Ne - 1 return -nu*log(1 + exp(ep - mu)) first_site = "A"*L last_site = "T"*L best_site = "".join(["ACGT"[argmin(row)] for row in matrix]) worst_site = "".join(["ACGT"[argmax(row)] for row in matrix]) #middle_sites = [[random_site(L)] for i in range(10)] #trajs = [[best_site]] + middle_sites + [[worst_site]] trajs = [[best_site],[worst_site]] ords = [rslice("ACGT",sorted_indices(row)) for row in matrix] def mutate_site(site,(ri,direction)): b = (site[ri]) idx = ords[ri].index(b) idxp = min(max(idx + direction,0),3) bp = ords[ri][idxp] return subst(site,bp,ri)
def do_query(self, q): """ Query solution. Consider the following example; > f(1) := 1. > f(2) := 4. There a few versions of query: - `vquery` shows variable bindings > vquery f(X) 1 where {X=1} 4 where {X=1} - `query` shows variable bindings applied to query > query f(X) f(1) = 1. f(2) = 4. - `trace` is an introspection tool for visualizing the derivation of an item and its value. Type `help trace` for more information. """ results = self._query(q) if results is None: return if len(results) == 0: print 'No results.' return print for term, result in sorted( (subst(q, result), result) for result in results): print '%s = %s.' % (term, _repr(todyna(result['$val']))) print
def do_query(self, q): """ Query solution. Consider the following example; > f(1) := 1. > f(2) := 4. There a few versions of query: - `vquery` shows variable bindings > vquery f(X) 1 where {X=1} 4 where {X=1} - `query` shows variable bindings applied to query > query f(X) f(1) = 1. f(2) = 4. - `trace` is an introspection tool for visualizing the derivation of an item and its value. Type `help trace` for more information. """ results = self._query(q) if results is None: return if len(results) == 0: print "No results." return print for term, result in sorted((subst(q, result), result) for result in results): print "%s = %s." % (term, _repr(todyna(result["$val"]))) print