示例#1
0
    def learning(self, sample):
        if self.method == "cpc":
            learner = otagr.ContinuousPC(sample, self.parameters['binNumber'],
                                         self.parameters['alpha'])

            start = time.time()
            ndag = learner.learnDAG()
            end = time.time()

            # TTest = otagr.ContinuousTTest(sample, self.parameters['alpha'])
            # jointDistributions = []
            # for i in range(ndag.getSize()):
            # d = 1+ndag.getParents(i).getSize()
            # if d == 1:
            # bernsteinCopula = ot.Uniform(0.0, 1.0)
            # else:
            # K = TTest.GetK(len(sample), d)
            # indices = [int(n) for n in ndag.getParents(i)]
            # indices = [i] + indices
            # bernsteinCopula = ot.EmpiricalBernsteinCopula(sample.getMarginal(indices), K, False)
            # jointDistributions.append(bernsteinCopula)

        elif self.method == "cbic":
            #print(sample.getDescription())
            max_parents = self.parameters['max_parents']
            n_restart_hc = self.parameters['hc_restart']
            cmode = self.parameters['cmode']
            learner = otagr.TabuList(sample, max_parents, n_restart_hc, 5)
            learner.setCMode(cmode)
            start = time.time()
            ndag = learner.learnDAG()
            end = time.time()
            #bn = dag_to_bn(dag, Tstruct.names())

        elif self.method == "cmiic":
            cmode = self.parameters['cmode']
            kmode = self.parameters['kmode']
            learner = otagr.ContinuousMIIC(sample)
            learner.setCMode(cmode)
            learner.setKMode(kmode)
            learner.setAlpha(self.kalpha)
            # learner.setBeta(self.kbeta)
            start = time.time()
            ndag = learner.learnDAG()
            end = time.time()
            # bn = gu.named_dag_to_bn(ndag)

        elif self.method == "dmiic":
            # learner.setBeta(self.kbeta)
            ndag, start, end = dsc.learnDAG(sample)
            # bn = gu.named_dag_to_bn(ndag)

        elif self.method == "lgbn":
            start = time.time()
            end = time.time()

        else:
            print("Wrong entry for method argument !")

        return ndag, end - start
示例#2
0
def testAsiaDirichlet():
    data = ot.Sample.ImportFromTextFile(
        os.path.join(os.path.dirname(__file__), "asia_dirichlet_5000.csv"),
        ",")
    learner = otagrum.TabuList(data)
    learner.setVerbosity(True)
    dag = learner.learnDAG()
    print(dag.toDot())
示例#3
0
def testSpecificInstance():
    size = 1000
    data = generateDataForSpecificInstance(size)
    learner = otagrum.TabuList(data)

    # skel = learner.learnSkeleton()
    # print(skel.toDot())

    dag = learner.learnDAG()
    print(dag.toDot())
    sys.stdout.flush()
def BIC_learning(data, max_parents=3, restart=1, tabu_list_size=2):
    # Try an estimation of the coefficients distribution using
    # univariate kernel smoothing for the marginals and PC to learn the structure
    # of dependence parameterized by Bernstein copula
    dimension = data.getDimension()
    print("Build BIC coefficients distribution")
    t0 = time()
    print("    Learning structure")
    t1 = time()
    learner = otagrum.TabuList(data, max_parents, restart, tabu_list_size)
    dag = learner.learnDAG()
    with open(
            "dags/new_dag_BIC_{}_{}_{}.dot".format(max_parents, restart,
                                                   tabu_list_size), "w") as f:
        f.write(dag.toDot())
    print("    t=", time() - t1, "s")

    cbn = CBN_parameter_learning(data, dag)
    print("t=", time() - t0, "s")
    # distribution = ot.ComposedDistribution(marginals, cbn)
    return cbn
size_draw = 1000

size_draw = min(size, size_draw)
data_draw = data_ref[0:size_draw]  # Number of realizations taken in order to plot figures

print("Processing reference data")
f = figure_path.joinpath("pairs_ref_" + dataset_name + ".pdf")
pairs(data_draw, f)


#####################LEARNING CBN MODEL############################

# LEARNING STRUCTURE #

# CBIC Algorithm
learner = otagr.TabuList(data_ref, 2, 10, 2) # Using CPC algorithm
cbic_dag = learner.learnDAG() # Learning DAG
write_graph(cbic_dag, structure_path.joinpath("cbic_dag_" + dataset_name + ".dot"))


# CPC Algorithm
learner = otagr.ContinuousPC(data_ref, 4, 0.05) # Using CPC algorithm
cpc_dag = learner.learnDAG() # Learning DAG
write_graph(cpc_dag, structure_path.joinpath("cpc_dag_" + dataset_name + ".dot"))


# CMIIC ALGORITHM

alphas = np.arange(10, 501, 5)/1000
# alphas = [0.04, 0.05]
fig, ax = plt.subplots()
示例#6
0
import pyAgrum as gum
import openturns as ot
import otagrum as otagr

print('Importing data')
data = ot.Sample.ImportFromTextFile(
    '../data/Standard_coefficients_0100000.csv', ';')
data = data[0:20000]
data = data.getMarginal(range(0, 12))

print('Initializing the learners')
learners = {
    'cbic': otagr.TabuList(data, 3, 1, 2),
    'cpc': otagr.ContinuousPC(data, 4, 0.01),
    'cmiic': otagr.ContinuousMIIC(data)
}

dags = {}
for (name, learner) in learners.items():
    print('Learning with ', name)
    dags[name] = learner.learnDAG()

for (name, dag) in dags.items():
    dot = dag.toDot()
    with open("dag_{}.dot".format(name), "w") as f:
        f.write(dot)
示例#7
0
import otagrum
import pyAgrum as gum
import pyAgrum.lib.notebook as gnb

import numpy as np

from pipeline import Pipeline
import graph_utils as gu
import elidan.hill_climbing as hc

data = ot.Sample.ImportFromTextFile("../data/samples/dirichlet/alarm/sample01.csv", ',')

# learner = cmiic.ContinuousMIIC(data,
                               # cmode=cmiic.CModeType.Bernstein,
                               # kmode=cmiic.KModeType.Naive)
learner = otagrum.TabuList(data, 4, 5, 5)
learner.setVerbosity(True)
dag = learner.learnDAG()
gu.write_graph(dag, "output_tabulist_gaussian.dot")

# learner = otagrum.TabuList(data, 4, 5, 5)
# learner.setCMode(otagrum.CorrectedMutualInformation.CModeTypes_Bernstein)
# learner.setVerbosity(True)
# dag = learner.learnDAG()
# gu.write_graph(dag, "output_tabulist_bernstein.dot")

t = hc.hill_climbing(data)
names = list(data.getDescription())
gu.write_graph(otagrum.NamedDAG(t[1], names), "output_hc.dot")
print("Final score hc: ", t[2])
# learner.use3off2()