Ejemplo n.º 1
0
# Probit
# A = tf.where(Theta_A + tf.random.normal((N, N), 0., var_adj) > 0., 1., 0.)

upper = tf.linalg.band_part(tf.ones_like(A), -1, 0) == 0
A_lower = tf.where(upper, A, np.nan)
print(
    tf.reduce_sum(tf.where(tf.math.is_nan(A_lower), 0., A_lower)) * 2 /
    (N * (N - 1)))
# A_lower = tf.ones_like(A) * np.nan

# ----------------------------------------------------
initial = {"bias": B0, "weights": B, "positions": Z, "heterogeneity": alpha}
self = JointModel2(K=K,
                   A=A_lower,
                   X_cts=X_cts,
                   X_bin=X_bin,
                   link_model="Logistic",
                   bin_model="Logistic",
                   initial=initial)

self.fit(20,
         5,
         5,
         verbose=True,
         X_cts_missing=X_cts_missing,
         X_bin_missing=X_bin_missing,
         positions_true=Z)

tf.reduce_sum(tf.where(tf.math.is_nan(A_lower), 0., A_lower), 1)
tf.reduce_sum(tf.where(tf.math.is_nan(A_lower), 0., A_lower)) / N**2
Ejemplo n.º 2
0
def run(traj):
    try:
        # extract parameters
        N = traj.par.data.N
        K = traj.par.data.K
        p_cts = traj.par.data.p_cts
        p_bin = traj.par.data.p_bin
        var_adj = traj.par.data.var_adj
        var_cov = traj.par.data.var_cov
        missing_rate = traj.par.data.missing_rate
        seed = traj.par.data.seed
        alpha_mean = traj.par.data.alpha_mean

        print(p_bin, N, seed)

        K_model = traj.par.model.K
        adj_model = traj.par.model.adj_model
        bin_model = traj.par.model.bin_model

        n_iter = traj.par.fit.n_iter
        n_vmp = traj.par.fit.n_vmp
        n_gd = traj.par.fit.n_gd
        step_size = traj.par.fit.step_size

        # generate data
        Z, alpha, X_cts, X_cts_missing, X_bin, X_bin_missing, A, B, B0 = generate_dataset(
            N=N,
            K=K,
            p_cts=p_cts,
            p_bin=p_bin,
            var_adj=var_adj,
            alpha_mean=alpha_mean,
            var_cov=var_cov,
            missing_rate=missing_rate,
            seed=seed,
            link_model=adj_model,
            bin_model=bin_model)
        initial = {
            "bias": B0,
            "weights": B,
            "positions": Z,
            "heterogeneity": alpha
        }

        # initialize model
        model = JointModel2(K=K_model,
                            A=A,
                            X_cts=X_cts,
                            X_bin=X_bin,
                            link_model="Logistic",
                            bin_model="Logistic",
                            initial=initial)

        # fit
        model.fit(
            n_iter=n_iter,
            n_vmp=n_vmp,
            n_gd=n_gd,
            verbose=False,
        )

        # metrics
        metrics = model.covariate_metrics(X_cts_missing, X_bin_missing)
        dists = model.latent_distance(Z)
        elbo = model.elbo.numpy()
        density = tf.reduce_sum(tf.where(tf.math.is_nan(A), 0.,
                                         A)).numpy() * 2 / (N * (N - 1))

        # store result
        elbo = elbo
        mse = metrics["mse"]
        auroc = metrics["auroc"]
        dist_inv = dists["inv"]
        dist_proj = dists["proj"]
        density = density
    except:
        elbo = np.nan
        mse = np.nan
        auroc = np.nan
        dist_inv = np.nan
        dist_proj = np.nan
        density = np.nan
    traj.f_add_result("runs.$.p_bin", p_bin, "p_bin")
    traj.f_add_result("runs.$.N", N, "N")
    traj.f_add_result("runs.$.seed", seed, "seed")
    traj.f_add_result("runs.$.elbo", elbo, "ELBO")
    traj.f_add_result("runs.$.mse", mse, "MSE")
    traj.f_add_result("runs.$.auroc", auroc, "AUROC")
    traj.f_add_result("runs.$.dist_inv", dist_inv, "Invariant distance")
    traj.f_add_result("runs.$.dist_proj", dist_proj, "Projection distance")
    traj.f_add_result("runs.$.density", density, "Network density")

    print(p_bin, N, seed, elbo, mse, auroc, dist_inv, dist_proj, density)
    return p_bin, N, seed, elbo, mse, auroc, dist_inv, dist_proj, density
Ejemplo n.º 3
0
if X_bin_missing is None:
    X_bin_missing = torch.zeros((N, 0))
if X_cts_missing is None:
    X_cts_missing = torch.zeros((N, 0))

initial = {
    "bias": tf.convert_to_tensor(B0.numpy()),
    "weights": tf.convert_to_tensor(B.numpy()),
    "positions": tf.convert_to_tensor(Z.numpy()),
    "heterogeneity": tf.convert_to_tensor(alpha.numpy())
}

self = JointModel2(K=K,
                   A=tf.convert_to_tensor(A_lower.numpy(), dtype=np.float32),
                   X_cts=tf.convert_to_tensor(X_cts.numpy(), dtype=np.float32),
                   X_bin=tf.convert_to_tensor(X_bin.numpy(), dtype=np.float32),
                   link_model="Logistic",
                   bin_model="Logistic",
                   initial=initial)

self.set_lr(100.)

self.fit(10,
         1,
         0,
         verbose=True,
         X_cts_missing=tf.convert_to_tensor(X_cts_missing.numpy(),
                                            dtype=np.float32),
         X_bin_missing=tf.convert_to_tensor(X_bin_missing.numpy(),
                                            dtype=np.float32),
         positions_true=tf.convert_to_tensor(Z.numpy(), dtype=np.float32))