Example #1
0
def main():
    print "Starting main()\n"
    u.setup()
    u.calibrate()
    a.hold_cube_and_deliver_ambulance()
    a.hold_cube_and_get_prism()
    u.sd()
Example #2
0
def deliver_ambulance_and_blocks():
    w.check_zones_hospital()
    if c.SAFE_HOSPITAL == c.NEAR_ZONE:
        g.drive_gyro_through_line_right()
        s.align_far()
        m.open_claw(1, 1, c.CLAW_WAY_OPEN_POS)
        msleep(500)
        m.move_arm(c.ARM_HIGH_POS)
        #m.move_claw(c.CLAW_WAY_OPEN_POS)
        #w.close_graphics_window()
        g.turn_left_gyro(190)
    else:
        u.halve_speeds()
        g.drive_gyro_through_line_right()
        s.align_far()
        s.left_forwards_until_white(0)
        s.left_forwards_until_black()
        m.open_claw(1, 1, c.CLAW_WAY_OPEN_POS)
        msleep(500)
        m.move_arm(c.ARM_HIGH_POS)
        #m.move_claw(c.CLAW_WAY_OPEN_POS)
        u.normalize_speeds()
        #w.close_graphics_window()
        u.sd()
        g.turn_left_gyro(190)
    g.backwards_gyro_through_line_left(1100)
    m.lower_ambulance_arm()
    g.backwards_gyro(500)
def cluster(xs):
    """given scalars xs, perform 2-component Gaussian clustering via EM"""
    mu0 = min(xs)
    mu1 = max(xs)
    sigma0 = 1
    sigma1 = 1
    for i in range(10):
        probs0 = [dnorm(x, mu0, sigma0) for x in xs]
        probs1 = [dnorm(x, mu1, sigma1) for x in xs]
        assignments = [
            int(prob1 > prob0) for prob0, prob1 in zip(probs0, probs1)
        ]
        xs0 = [x for (x, a) in zip(xs, assignments) if a == 0]
        xs1 = [x for (x, a) in zip(xs, assignments) if a == 1]
        mu0, sigma0 = mean(xs0), sd(xs0, correct=False)
        mu1, sigma1 = mean(xs1), sd(xs1, correct=False)
        if sigma0 == 0:
            sigma0 = sigma1
        if sigma1 == 0:
            sigma1 = sigma0
        print "mu0: {} sigma0: {} mu1: {}: sigma1: {} xs0: {} xs1: {}".format(
            mu0, sigma0, mu1, sigma1, len(xs0), len(xs1))

    def f(x):
        return dnorm(x, mu1,
                     sigma1) / (dnorm(x, mu0, sigma0) + dnorm(x, mu1, sigma1))

    return f
Example #4
0
def compute_correlation_matrix_loop(x):
    feature_dim = x.shape[1]
    z = np.zeros((feature_dim, feature_dim))
    for i in range(feature_dim):
        for j in range(feature_dim):
            if (sd_i := sd(x, i) != 0) and (sd_j := sd(x, j) != 0):
                z[i][j] = variance_(x, (i, j)) / (sd_i * sd_j)
Example #5
0
def compute_correlation_matrix_loop(x):
    feature_dim = x.shape[1]
    z = np.zeros((feature_dim, feature_dim))
    for i in range(feature_dim):
        for j in range(feature_dim):
            z[i][j] = covar(x, (i, j)) / (sd(x, i) * sd(x, j))
    return z
Example #6
0
def bisect_interval_noisy(f, epsilon=0.01, sigma=None, debug=False):
    """find zero of stochastic function f using linear regression"""
    print "in bisect"
    xmin = 1
    xmax = 2
    xs = [xmin, xmax]
    print xmin, xmax
    print f(1)
    ys = map(f, xs)
    print ys
    print "ys[-1]:", ys[-1]
    while ys[-1] < 0:
        xmax += 1
        xs.append(xmax)
        y = f(xmax)
        ys.append(y)
    xs2 = [x + xs[-1] for x in xs]
    ys2 = map(f, xs2)
    xs = xs + xs2
    ys = ys + ys2
    #xs = list(np.linspace(lb,ub,10))
    #ys = map(f,xs)
    print "xs,ys:", xs, ys
    i = 1
    while sd(xs[-3:]) > epsilon:
        print "starting round", i
        i += 1
        ### select xp
        # m = (y2-y1)/float(x2-x1)
        # xp = -y1/m + x1
        # yp = f(xp)
        if sigma is None:
            print "interpolating on:", xs, ys
            r = kde_interpolate(xs, ys, sigma=sd(xs) / 3.0)
        else:
            r = kde_interpolate(xs, ys, sigma=sigma)
        try:
            xp = bisect_interval(r, min(xs), max(xs))
            print "selected xp:", xp
        except:
            "secant regression failed!"
            Exception()
        if debug:
            plt.scatter(xs, ys)
            plt.plot(*pl(r, np.linspace(min(xs), max(xs), 1000)))
            plt.plot([xp, xp], [-10, 10])
            plt.plot([min(xs), max(xs)], [0, 0])
            plt.show()
        yp = f(xp)
        ### end select xp
        print "xp,yp:", xp, yp
        xs.append(xp)
        ys.append(yp)
        #js = sorted_indices(xs)
        #xs = rslice(xs,js)
        #ys = rslice(ys,js)
        #assert xs == sorted(xs)
    return xp, (xs, ys)
Example #7
0
def sigma_from_matrix(matrix):
    """given a GLE matrix, estimate standard deviation of cell weights,
    correcting for bias of sd estimate.  See:
    https://en.wikipedia.org/wiki/Unbiased_estimation_of_standard_deviation
    """
    c = 2*sqrt(2/(3*pi))
    return mean(map(lambda x:sd(x,correct=True),matrix))/c
Example #8
0
def approximate_max_fitness(N=100000):
    """Approximate max fitness from sample, assuming fitnesses log-normally distributed"""
    log_num_bds = (L - 1) * log(num_AAS)
    log_num_motifs = L * n * log(4)
    log_num_genotypes = log_num_bds + log_num_motifs
    random_fits = ([fitness(sample_species()) for i in trange(N)])
    log_fits = map(log, random_fits)
    m, s = mean(log_fits), sd(log_fits)
    standard_max = sqrt(2 * log_num_genotypes)
    shifted_max = standard_max * s + m
def main():
    print "Starting main()\n"
    u.setup()
    u.calibrate()
    a.only_first_three()
    # m.backwards(1300)
    m.turn_right()
    m.backwards(4500)
    u.sd()


    a.get_low_poms_cheeky()
    a.get_frisbee()
    a.get_mid_poms()
    a.get_high_poms_cheeky()
    a.get_farther_high_poms() 
    a.get_farther_mid_poms()
    a.get_farther_low_poms()
    print "Finished main\n"
    u.shutdown(86)
def test_fw_method2(mu, sigma, N, trials=10000):
    xs = [
        sum(exp(random.gauss(mu, sigma)) for i in range(N))
        for j in xrange(trials)
    ]
    M, V = mean(xs), variance(xs)
    print "obs M,V,log(V/(M**2)):", M, V, log(V / (M**2))
    ys = map(log, xs)
    m_obs, s_obs = mean(ys), sd(ys)
    m, s = fw_method(mu, sigma, N)
    print "pred:", m, s
    print "obs:", m_obs, s_obs
Example #11
0
def stoch_logistic_reg(f,
                       xmin,
                       xmax,
                       ymax,
                       desired_ic,
                       stop_sd=0.01,
                       debug=False):
    ymin = 0

    def sigmoid(params, x):
        x0, k, ymax = params
        y = ymax / (1 + np.exp(-k * (x - x0))) + ymin
        return y

    def residuals(params, x, y):
        return y - sigmoid(params, x)

    xs = list(np.linspace(xmin, xmax, 10))
    ys = map(f, xs)
    params = (2, 1, desired_ic * 2)
    while True:
        p_guess = params
        params, cov, infodict, mesg, ier = leastsq(residuals,
                                                   p_guess,
                                                   args=(xs, ys),
                                                   full_output=1)
        try:
            x = secant_interval(lambda x: sigmoid(params, x) - desired_ic,
                                xmin, xmax)
        except:
            print "failed secant interval"
            print params, xs, ys
            raise Exception()
        y = f(x)
        xs.append(x)
        ys.append(y)
        if sd(xs[-3:]) < stop_sd:
            break
        if debug:
            plt.scatter(xs, ys)
            plt.plot(*pl(lambda x: sigmoid(params, x),
                         np.linspace(xmin, xmax, 1000)))
            plt.plot(*pl(lambda x: desired_ic, np.linspace(xmin, xmax, 1000)))
            plt.plot([x, x], [0, 2 * L])
            plt.show()
    # end with one more round of interpolation
    params, cov, infodict, mesg, ier = leastsq(residuals,
                                               p_guess,
                                               args=(xs, ys),
                                               full_output=1)
    x = secant_interval(lambda x: sigmoid(params, x) - desired_ic, xmin, xmax)
    return x, (xs, ys)
Example #12
0
def evaluate(model, loader):
    model.eval()
    y_hat_list = []
    y_list = []
    for batch_data in loader:
        a2a_g, b2a_g, b2b_gl, feats, types, counts, y = batch_data
        _, y_hat = model(a2a_g, b2a_g, b2b_gl, types, counts)
        y_hat_list += y_hat.tolist()
        y_list += y.tolist()

    y_hat = np.array(y_hat_list).reshape(-1,)
    y = np.array(y_list).reshape(-1,)
    return rmse(y, y_hat), mae(y, y_hat), sd(y, y_hat), pearson(y, y_hat)
def trim_motif(motif):
    L = len(motif[0])
    col_hs = map(lambda col: entropy(col, correct=True, alphabet_size=4),
                 transpose(motif))
    mu, sigma = mean(col_hs), sd(col_hs)
    threshold = mu + 1.96 * sigma
    l, r = 0, L - 1
    while col_hs[l] > threshold:
        print "trimming left endpoint:", l, col_hs[l]
        l += 1
    while col_hs[r] > threshold:
        print "trimming left endpoint:", col_hs[r]
        r -= 1
    return [site[l:r + 1] for site in motif]
def test_Zb_approx(trials=10, G=5 * 10**6, L=10):
    predicted_Zb = exp(L * sigma**2 / 2.0 + log(G))  # a priori prediction
    matrix = [[random.gauss(0, sigma) for j in range(4)] for i in range(L)]
    score_mu = sum(mean(row) for row in matrix)
    score_sigma_sq = sum(variance(row, correct=False) for row in matrix)
    predicted_Zb2 = exp(score_mu + score_sigma_sq / 2 +
                        log(G))  # prediction given matrix
    Zbs = []
    for trial in trange(trials):
        eps = [sum(random.choice(row) for row in matrix) for i in range(G)]
        Zb = sum(exp(-ep) for ep in eps)
        Zbs.append(Zb)
    print "Predicted: %1.3e +/- %1.3e" % (predicted_Zb,
                                          sqrt(var_Zb(sigma, L, G)))
    print "Predicted2: %1.3e" % (predicted_Zb2)
    print "Actual: %1.3e +/- %1.3e" % (mean(Zbs), sd(Zbs))
Example #15
0
def stoch_lin_reg(f, lb=1, ub=50, stop_sd=0.1):
    xs = [lb, ub]
    ys = map(f, xs)
    while True:
        m, b = polyfit(xs, ys, 1)
        x = -b / float(m)
        if x < 1:
            x = random.random() * (max(xs) - min(xs)) + min(xs)
        y = f(x)
        xs.append(x)
        ys.append(y)
        if sd(xs[-3:]) < stop_sd:
            break
    # end with one more round of interpolation
    m, b = polyfit(xs, ys, 1)
    x = -b / float(m)
    y = f(x)
    return x, (xs, ys)
Example #16
0
def ou_param_recovery(xs):
    """return MLE estimate of ou parameters: dX = -beta*(X-k) + sigma*dW """
    xs = np.array(xs)
    ys = np.diff(xs)
    xs_ = xs[:-1]
    x0 = xs[0]
    neg_beta, _ = polyfit(xs_, ys, 1)  # find MLE estimate of beta
    beta = -neg_beta
    if beta < 0:
        print "Warning: beta negative"
        return None, None, None
    sigma = sd(ys + beta * xs_)
    rel_time = ou_relaxation_time(beta, sigma, x0)
    if rel_time < len(xs):
        print "estimated relaxation time:", rel_time
        k = mean(xs[int(rel_time):]
                 )  # estimate mean from series after relaxation time
    else:
        print "Warning: process predicted not to reach relaxation time."
        k = None
    return beta, sigma, k
def analyze_bio_motifs(Nes,trials=20):
    results = {}
    for tf_idx,tf in enumerate(Escherichia_coli.tfs):
        Ne = Nes[tf]
        bio_motif = getattr(Escherichia_coli,tf)
        n,L = len(bio_motif),len(bio_motif[0])
        bio_matrix = matrix_from_motif(bio_motif)
        sigma = sigma_from_matrix(bio_matrix)
        matrix_chains = [sella_hirsch_mh(n=n,L=L,sigma=sigma,Ne=Ne,init='ringer') for i in range(trials)]
        ics = [mean(map(motif_ic,chain[-1000:])) for (matrix,chain) in matrix_chains]
        ginis = [mean(map(motif_gini,chain[-1000:])) for (matrix,chain) in matrix_chains]
        mis = [mean(map(total_motif_mi,chain[-1000:])) for (matrix,chain) in matrix_chains]
        print "results for:",tf,tf_idx
        print motif_ic(bio_motif),mean(ics),sd(ics)
        print motif_gini(bio_motif),mean(ginis),sd(ginis)
        print total_motif_mi(bio_motif),mean(mis),sd(mis)
        results[tf] = (mean(ics),sd(ics),mean(ginis),sd(ginis),mean(mis),sd(mis))
    return results
Example #18
0
def estimate_site_sigma_from_matrix(matrix,n=1000):
    L = len(matrix)
    return sd([score_seq(matrix,random_site(L)) for i in xrange(n)])
def test_sample_mean_Zb(L, sigma, trials=1000):
    Zbs = [sample_mean_Zb(L, sigma) for i in trange(trials)]
    pred_mean, pred_sd = predict_mean_Zb(L, sigma)
    obs_mean, obs_sd = mean(Zbs), sd(Zbs)
    print "pred:", pred_mean, pred_sd
    print "obs:", obs_mean, obs_sd
def code_statistics(code):
    mus = [mean([code[(aa, b)] for aa in range(AAS)]) for b in "ACGT"]
    sigmas = [sd([code[(aa, b)] for aa in range(AAS)]) for b in "ACGT"]
    return sum(mus), sum(sigmas)
def compare_Zb2(G, L, sigma, trials=100):
    Zbs = [sample_Zb(G, L, sigma) for trial in trange(trials)]
    Zb_mu, Zb_sigma = predict_Zb2(G, L, sigma)
    print "expected:", Zb_mu, Zb_sigma
    print "observed:", mean(Zbs), sd(Zbs)
def code_statistics(code):
    mus = [mean([code[(aa,b)] for aa in range(AAS)]) for b in "ACGT"]
    sigmas = [sd([code[(aa,b)] for aa in range(AAS)]) for b in "ACGT"]
    return sum(mus),sum(sigmas)
def test_predict_mean_Zb_from_matrix(matrix, G, trials=100):
    # works.
    Zbs = [sample_Zb_from_matrix(matrix, G) for i in trange(trials)]
    m, s = predict_mean_Zb_from_matrix(matrix, G)
    print "expected:", m, s
    print "observed:", mean(Zbs), sd(Zbs)
Example #24
0
def score_sd(motif):
    matrix = matrix_from_motif(motif)
    eps = [score_seq(matrix, site) for site in motif]
    return sd(eps)