예제 #1
0
def test_pairwise_causal_estimation():
    np.random.seed(0)
    n = 6
    data, M = create_data(n, 20000, enb=2)
    dags, scores = sumu.Gadget(data=data, mcmc={"iters": 80000}).sample()
    causal_effects = sumu.beeps(dags, data)
    mse = ((np.linalg.inv(np.eye(n) - M["B"]) - causal_effects.mean(axis=0))**2).mean()
    print(mse)
    assert mse < 0.1
예제 #2
0
def test_Gadget_runs_n_between_193_and_256():
    # NOTE: This does not test all numbers of variables between 193 and 256
    data_path = pathlib.Path(__file__).resolve().parents[2] / "data"
    bn_path = data_path / "andes.dsc"
    bn = sumu.utils.io.read_dsc(bn_path)
    data = bn.sample(200, seed=0)
    g = sumu.Gadget(data=data, cp_algo="top", K=10, d=2, mc3=2, burn_in=100, iterations=100, thinning=2)
    sumu.utils.io.pretty_dict(g.params)
    g.sample()
    assert True
예제 #3
0
def test_Gadget_runs_n_between_129_and_192():
    # NOTE: This does not test all numbers of variables between 129 and 192
    data_path = pathlib.Path(__file__).resolve().parents[2] / "data"
    bn_path = data_path / "munin1.dsc"
    bn = sumu.utils.io.read_dsc(bn_path)
    data = bn.sample(200, seed=0)
    g = sumu.Gadget(data=data,
                    mcmc={"iters": 200, "mc3": 2, "burn_in": 0.5, "n_dags": 50},
                    candp={"name": "top"}, cons={"K": 10, "d": 2})
    g.sample()
    assert True
예제 #4
0
def test_Gadget_empirical_edge_prob_error_decreases():

    params = {

        # generic MCMC parameters
        "mcmc": {"n_indep": 1, "iters": 150000,
                 "mc3": 3, "burn_in": 0.5, "n_dags": 10000},

        # score to use and its parameters
        "score": {"name": "bdeu", "params": {"ess": 10}},

        # modular structure prior and its parameters
        "prior": {"name": "fair"},

        # constraints on the DAG space
        "cons": {
            "max_id": -1,
            "K": 8,
            "d": 3,
            "pruning_eps": 0.001
        },

        # algorithm to use for finding candidate parents
        "candp": {"name": "greedy-lite", "params": {"k": 6}},


        # preparing for catastrofic cancellations
        "catc": {
            "tolerance": 2**-32,
            "cache_size": 10**7
        },

        # Logging
        "logging": {
            "stats_period": 15,
            #"logfile": "gadget.log"
        }
    }

    data_path = pathlib.Path(__file__).resolve().parents[2] / "data"
    bn_path = data_path / "sachs.dsc"
    bn = sumu.utils.io.read_dsc(bn_path)
    data = bn.sample(200, seed=0)
    ls = sumu.gadget.LocalScore(data=data, maxid=-1, score=params["score"])
    pset_probs = sumu.aps(ls.candidate_scores(), as_dict=True)
    edge_probs = sumu.utils.edge_probs_from_pset_probs(pset_probs)

    g = sumu.Gadget(data=data, **params)
    dags, scores = g.sample()
    max_errors = sumu.utils.utils.edge_empirical_prob_max_error(dags,
                                                                edge_probs)
    print(max_errors[-1])
    assert max_errors[-1] < 0.05
예제 #5
0
def test_Gadget_empirical_edge_prob_error_decreases():

    params = {
              # score to use and its parameters
              "score": {"name": "bdeu", "ess": 10},

              # modular structure prior and its parameters
              "prior": {"name": "fair"},

              # constraints on the DAG space
              "max_id": -1,
              "K": 8,
              "d": 3,

              # algorithm to use for finding candidate parents
              "cp_algo": "greedy-lite",

              # generic MCMC parameters
              "mc3": 2,
              "burn_in": 50000,
              "iterations": 50000,
              "thinning": 5,

              # preparing for catastrofic cancellations
              "cc_tolerance": 2**-32,
              "cc_cache_size": 10**7,

              # pruning candidate parent sets
              "pruning_eps": 0.001
    }

    data_path = pathlib.Path(__file__).resolve().parents[2] / "data"
    bn_path = data_path / "sachs.dsc"
    bn = sumu.utils.io.read_dsc(bn_path)
    data = bn.sample(200, seed=0)
    ls = sumu.gadget.LocalScore(data=data, maxid=-1, score=params["score"])
    pset_probs = sumu.aps(ls.all_candidate_restricted_scores(), as_dict=True)
    edge_probs = sumu.utils.edge_probs_from_pset_probs(pset_probs)

    # To set the seed for MCMC
    g = sumu.Gadget(data=data, **params)
    sumu.utils.io.pretty_dict(g.params)
    dags, scores = g.sample()
    max_errors = sumu.utils.utils.edge_empirical_prob_max_error(dags,
                                                                edge_probs)
    print(max_errors[-1])
    assert max_errors[-1] < 0.05
예제 #6
0
arr = np.array(df)

data = sumu.Data(arr)

params = {
    "array": arr,
    "data": data,
    "scoref": "bge",  # Or "bdeu" for discrete data.
    # "ess": 10,        # If using BDeu.
    "max_id": -1,
    "K": 7,
    "d": 10,
    "cp_algo": "greedy-lite",
    "mc3_chains": 10,
    "burn_in": 100,
    "iterations": 5000,
    "thinning": 10
}

g = sumu.Gadget(data=data,
                array=arr,
                burn_in=100,
                iterations=5000,
                thinning=10)
dag = g.sample()
end = time.time()
print(dag)

print(end - start)
예제 #7
0
def test_Gadget_runs_continuous_data():
    data = np.random.rand(200, 10)
    sumu.Gadget(data=data, cons={"K": 8}).sample()
    assert True
예제 #8
0
params = {
    "array": test_arr,
    "data": data,
    "scoref": "bge",  # Or "bdeu" for discrete data.
    # "ess": 10,        # If using BDeu.
    "max_id": -1,
    "K": 7,
    "d": 5,
    "cp_algo": "greedy-lite",
    "mc3_chains": 15,
    "burn_in": 10,
    "iterations": 50000,
    "thinning": 10
}

g = sumu.Gadget(**params)
dag = g.sample()
end = time.time()


def dag_validation(dag, result):

    relations = dict()
    for x in list(dag.nodes):
        relations[x] = []
    for e in list(dag.edges):
        relations[e[0]].append(e[1])
    relations_result = dict()
    for x in list(dag.nodes):
        relations_result[x] = []
    for x in relations: