Exemplo n.º 1
0
def fit_clv(observations, time_points, held_out_rel_abun, held_out_time_points,
            denom, method):
    print("Estimating cLV parameters using", method)

    rel_abun = estimate_relative_abundances(observations)
    clv = CompositionalLotkaVolterra(rel_abun, time_points, denom=denom)

    if method == "Elastic Net":
        clv.train()
        predictions = [
            clv.predict(o[0], tpts)
            for (o, tpts) in zip(held_out_rel_abun, held_out_time_points)
        ]
        A, g, B = clv.get_params()
        return A, g, predictions
    elif method == "Ridge":
        clv.train_ridge()
        A, g, B = clv.get_params()
        predictions = [
            clv.predict(o[0], tpts)
            for (o, tpts) in zip(held_out_rel_abun, held_out_time_points)
        ]
        return A, g, predictions
    else:
        print("bad optimization method for cLV", file=sys.stderr)
        return
Exemplo n.º 2
0
    # estimated previously
    r_A = 1
    r_g = 4
    r_B = 0.5

    P = []
    Y_pc = []
    log_Y = []
    for y in Y:
        mass = y.sum(axis=1)
        p = y / y.sum(axis=1, keepdims=True)
        p = (p + 1e-5) / (p + 1e-5).sum(axis=1, keepdims=True)
        P.append(p)
        Y_pc.append((mass.T * p.T).T)
        log_Y.append(np.log(mass.T * p.T).T)

    clv = CompositionalLotkaVolterra(P, T, U, pseudo_count=1e-5)
    clv.r_A = r_A
    clv.r_g = r_g
    clv.r_B = r_B
    clv.train_ridge()
    A_clv, g_clv, B_clv = clv.get_params()

    A_glv, g_glv, B_glv = ridge_regression_glv(log_Y, U, T, clv.r_A, clv.r_g,
                                               clv.r_B)

    A_rel, g_rel, B_rel = compute_relative_parameters(A_glv, g_glv, B_glv,
                                                      clv.denom)
    plot_corr(A_rel, g_rel, B_rel, A_clv, g_clv, B_clv,
              "plots/stein_correlation.pdf")