Example #1
0
def run_expt(num_graph, num_data_per_graph, n, d, s0, graph_type, sem_type,
             w_ranges, noise_scale, expt_name):
    os.mkdir(expt_name)
    os.chmod(expt_name, 0o777)
    perf = defaultdict(list)
    for ii in tqdm(range(num_graph)):
        B_true = utils.simulate_dag(d, s0, graph_type)
        W_true = utils.simulate_parameter(B_true, w_ranges=w_ranges)
        W_true_fn = os.path.join(expt_name, f'graph{ii:05}_W_true.csv')
        np.savetxt(W_true_fn, W_true, delimiter=',')
        for jj in range(num_data_per_graph):
            X = utils.simulate_linear_sem(W_true,
                                          n,
                                          sem_type,
                                          noise_scale=noise_scale)
            X_fn = os.path.join(expt_name, f'graph{ii:05}_data{jj:05}_X.csv')
            np.savetxt(X_fn, X, delimiter=',')
            # notears
            W_notears = notears.notears_linear_l1(X, lambda1=0, loss_type='l2')
            assert utils.is_dag(W_notears)
            W_notears_fn = os.path.join(
                expt_name, f'graph{ii:05}_data{jj:05}_W_notears.csv')
            np.savetxt(W_notears_fn, W_notears, delimiter=',')
            # eval
            B_notears = (W_notears != 0)
            acc = utils.count_accuracy(B_true, B_notears)
            for metric in acc:
                perf[metric].append(acc[metric])
    # print stats
    print(expt_name)
    for metric in perf:
        print(metric, f'{np.mean(perf[metric]):.4f}', '+/-',
              f'{np.std(perf[metric]):.4f}')
Example #2
0
def main():
    torch.set_default_dtype(torch.double)
    np.set_printoptions(precision=3)

    import notears.utils as ut
    ut.set_random_seed(123)

    n, d, s0, graph_type, sem_type = 200, 5, 9, 'ER', 'mim'
    B_true = ut.simulate_dag(d, s0, graph_type)
    np.savetxt('W_true.csv', B_true, delimiter=',')

    X = ut.simulate_nonlinear_sem(B_true, n, sem_type)
    np.savetxt('X.csv', X, delimiter=',')

    model = NotearsMLP(dims=[d, 10, 1], bias=True)
    W_est = notears_nonlinear(model, X, lambda1=0.01, lambda2=0.01)
    assert ut.is_dag(W_est)
    np.savetxt('W_est.csv', W_est, delimiter=',')
    acc = ut.count_accuracy(B_true, W_est != 0)
    print(acc)
Example #3
0
            if h_new > 0.25 * h:
                rho *= 10
            else:
                break
        w_est, h = w_new, h_new
        alpha += rho * h
        if h <= h_tol or rho >= rho_max:
            break
    W_est = _adj(w_est)
    W_est[np.abs(W_est) < w_threshold] = 0
    return W_est


if __name__ == '__main__':
    from notears import utils
    utils.set_random_seed(1)

    n, d, s0, graph_type, sem_type = 100, 20, 20, 'ER', 'gauss'
    B_true = utils.simulate_dag(d, s0, graph_type)
    W_true = utils.simulate_parameter(B_true)
    np.savetxt('W_true.csv', W_true, delimiter=',')

    X = utils.simulate_linear_sem(W_true, n, sem_type)
    np.savetxt('X.csv', X, delimiter=',')

    W_est = notears_linear(X, lambda1=0.1, loss_type='l2')
    assert utils.is_dag(W_est)
    np.savetxt('W_est.csv', W_est, delimiter=',')
    acc = utils.count_accuracy(B_true, W_est != 0)
    print(acc)
Example #4
0
                rho *= 10
            else:
                break
        w_est, h = w_new, h_new
        alpha += rho * h
        if h <= h_tol or rho >= rho_max:
            break
    W_est = _adj(w_est)
    W_est[np.abs(W_est) < w_threshold] = 0
    return W_est


if __name__ == '__main__':
    import notears.utils as ut
    ut.set_random_seed(1)

    n, d, s0, graph_type, sem_type = 100, 20, 20, 'ER', 'gauss'
    B_true = ut.simulate_dag(d, s0, graph_type)
    W_true = ut.simulate_parameter(B_true)
    np.savetxt('W_true.csv', W_true, delimiter=',')

    X = ut.simulate_linear_sem(W_true, n, sem_type)
    np.savetxt('X.csv', X, delimiter=',')

    W_est = notears_linear(X, lambda1=0.1, loss_type='l2')
    assert ut.is_dag(W_est)
    np.savetxt('W_est.csv', W_est, delimiter=',')
    acc = ut.count_accuracy(B_true, W_est != 0)
    print(acc)