Пример #1
0
def BC_marginalKS(data, marginals):
    # Fourth model: marginal KS + Bernstein copula
    print("Bernstein copula")
    # ot.Log.Show(ot.Log.INFO)
    model = ot.ComposedDistribution(marginals,
                                    ot.BernsteinCopulaFactory().build(data))
    return model
Пример #2
0
def CBN_PC(data, result_structure_path):
    print("CBN with PC")

    skeleton_path = result_structure_path.joinpath("skeleton")
    skeleton_path.mkdir(parents=True, exist_ok=True)

    pdag_path = result_structure_path.joinpath("pdag")
    pdag_path.mkdir(parents=True, exist_ok=True)

    dag_path = result_structure_path.joinpath("dag")
    dag_path.mkdir(parents=True, exist_ok=True)

    skeleton_file_name = "skeleton_" + str(size).zfill(7) + ".dot"
    skeleton_done = skeleton_path.joinpath(skeleton_file_name).exists()

    pdag_file_name = "pdag_" + str(size).zfill(7) + ".dot"
    pdag_done = pdag_path.joinpath(pdag_file_name).exists()

    dag_file_name = "dag_" + str(size).zfill(7) + ".dot"
    dag_done = dag_path.joinpath(dag_file_name).exists()

    alpha = 0.01
    conditioningSet = 4

    learner = otagr.ContinuousPC(data, conditioningSet, alpha)
    learner.setVerbosity(True)

    if not skeleton_done:
        skel = learner.learnSkeleton()
        gu.write_graph(
            skel,
            skeleton_path.joinpath("skeleton_" + str(size).zfill(7) + ".dot"))

    if not pdag_done:
        pdag = learner.learnPDAG()
        gu.write_graph(
            pdag, pdag_path.joinpath("pdag_" + str(size).zfill(7) + ".dot"))

    if not dag_done:
        dag = learner.learnDAG()
        gu.write_graph(dag,
                       dag_path.joinpath("dag_" + str(size).zfill(7) + ".dot"))
    else:
        dag, names = gu.read_graph(
            dag_path.joinpath("dag_" + str(size).zfill(7) + ".dot"))
        dag = otagr.NamedDAG(dag, names)

    print("Learning parameters")
    factories = [
        ot.KernelSmoothing(ot.Epanechnikov()),
        ot.BernsteinCopulaFactory()
    ]
    ot.Log.SetFile("log")
    ot.Log.Show(ot.Log.INFO)
    model = otagr.ContinuousBayesianNetworkFactory(factories, dag, alpha,
                                                   conditioningSet,
                                                   False).build(data)
    ot.Log.Show(ot.Log.INFO)
    return model
def ot_bernstein_copula_fit(Pared):
    sample_Pared = ot.Sample(Pared)
    marginals = ot_kernel_Marginals(sample_Pared)
    ranksTransf = ot.MarginalTransformationEvaluation(
        marginals, ot.MarginalTransformationEvaluation.FROM)
    rankSample = ranksTransf(sample_Pared)
    bernstein_copula_distribution = ot.BernsteinCopulaFactory().build(
        rankSample)
    bivariate_distribution = ot.ComposedDistribution(
        marginals, bernstein_copula_distribution)
    return bivariate_distribution
def CBN_parameter_learning(data, dag):
    size = data.getSize()
    dimension = data.getDimension()
    print("    Learning parameters")
    t1 = time()
    print("        Learning the CBN parameters")
    t2 = time()
    ot.ResourceMap.SetAsUnsignedInteger("BernsteinCopulaFactory-kFraction", 2)
    ot.ResourceMap.SetAsUnsignedInteger("BernsteinCopulaFactory-MinM",
                                        size // 2 - 2)
    ot.ResourceMap.SetAsUnsignedInteger("BernsteinCopulaFactory-MaxM",
                                        size // 2 - 1)
    cbn = otagrum.ContinuousBayesianNetworkFactory(ot.HistogramFactory(),
                                                   ot.BernsteinCopulaFactory(),
                                                   dag, 0, 0,
                                                   False).build(data)
    print("        t=", time() - t2, "s")
    print("        Learning the marginal parameters")
    t2 = time()
    # marginals = [ot.KernelSmoothing().build(data.getMarginal(i)) for i in range(dimension)]
    # marginals = [ot.HistogramFactory().build(data.getMarginal(i)) for i in range(dimension)]
    print("        t=", time() - t2, "s")
    print("    t=", time() - t1, "s")
    return cbn
    dimension = est_copula.getDimension()
    for d in range(dimension):
        print("Is marginal %d a copula ? --> %s" % (d, est_copula.isCopula()))


try:
    coll = [ot.GumbelCopula(3.0), ot.ClaytonCopula(3.0), ot.FrankCopula(3.0)]
    size = 100
    for i, ref_copula in enumerate(coll):
        ref_copula = coll[i]
        print("Reference copula", str(ref_copula))
        sample = ref_copula.getSample(size)
        # Default method: log-likelihood
        m = ot.BernsteinCopulaFactory.ComputeLogLikelihoodBinNumber(sample)
        print("Log-likelihood m=", m)
        est_copula = ot.BernsteinCopulaFactory().build(sample, m)
        max_error = compute_max_error(ref_copula, est_copula)
        print("Max. error=%.5f" % max_error)
        check_bernstein_copula(est_copula)
        # AMISE method
        m = ot.BernsteinCopulaFactory.ComputeAMISEBinNumber(sample)
        print("AMISE m=", m)
        est_copula = ot.BernsteinCopulaFactory().build(sample, m)
        max_error = compute_max_error(ref_copula, est_copula)
        print("Max. error=%.5f" % max_error)
        check_bernstein_copula(est_copula)
        # Penalized Csiszar divergence method
        f = ot.SymbolicFunction("t", "-log(t)")
        m = ot.BernsteinCopulaFactory.ComputePenalizedCsiszarDivergenceBinNumber(
            sample, f)
        print("Penalized Csiszar divergence m=", m)
Пример #6
0
    data_ref = data_ref.getMarginal([0, 1, 2, 8])
    size = data_ref.getSize()  # Size of data
    dim = data_ref.getDimension()  # Dimension of data

    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("test_pairs_ref.pdf")
    pairs(data_draw, f)

    print("Bernstein copula")
    marginals = get_KS_marginals(data_ref)
    KSBC = ot.ComposedDistribution(marginals,
                                   ot.BernsteinCopulaFactory().build(data_ref))

    f = figure_path.joinpath("test_pairs_KSBC.pdf")
    pairs(KSBC.getSample(size_draw), f)

    print("Constructing CBN model")

    print("\tLearning structure")
    learner = otagr.ContinuousMIIC(data_ref)  # Using CPC algorithm
    learner.setAlpha(-10)
    dag = learner.learnDAG()  # Learning DAG
    write_graph(dag, result_path.joinpath("test_dag.dot"))

    print("\tLearning parameters")
    cbn = otagr.ContinuousBayesianNetworkFactory(
        ot.KernelSmoothing(ot.Epanechnikov()), ot.BernsteinCopulaFactory(),
# In this example we are going to estimate a normal copula from a sample using non parametric representations.

# %%
import openturns as ot
import openturns.viewer as viewer
from matplotlib import pylab as plt
ot.Log.Show(ot.Log.NONE)

# %%
# Create data
R = ot.CorrelationMatrix(2)
R[1, 0] = 0.4
copula = ot.NormalCopula(R)
sample = copula.getSample(30)

# %%
# Estimate a normal copula using BernsteinCopulaFactory
distribution = ot.BernsteinCopulaFactory().build(sample)

# %%
# Draw fitted distribution
graph = distribution.drawPDF()
view = viewer.View(graph)

# %%
# Estimate a normal copula using KernelSmoothing
distribution = ot.KernelSmoothing().build(sample).getCopula()
graph = distribution.drawPDF()
view = viewer.View(graph)
plt.show()
Пример #8
0
# for (i, (train, test)) in enumerate(splits):
for (i, (train, test)) in enumerate(list(splits)):
    print("Learning with fold number {}".format(i))
    likelihood_curve = []
    bic_curve = []
    for alpha in alphas:
        ot.Log.Show(ot.Log.NONE)
        print("\tLearning with alpha={}".format(alpha))
        learner = otagr.ContinuousMIIC(
            data_ref.select(train))  # Using CMIIC algorithm
        learner.setAlpha(alpha)
        cmiic_dag = learner.learnDAG()  # Learning DAG
        ot.Log.Show(ot.Log.NONE)
        if True:
            cmiic_cbn = otagr.ContinuousBayesianNetworkFactory(
                ot.KernelSmoothing(ot.Normal()), ot.BernsteinCopulaFactory(),
                cmiic_dag, 0.05, 4, False).build(data_ref.select(train))
        else:
            cmiic_cbn = otagr.ContinuousBayesianNetworkFactory(
                ot.NormalFactory(), ot.NormalCopulaFactory(), cmiic_dag, 0.05,
                4, False).build(data_ref.select(train))
        # sampled = cmiic_cbn.getSample(1000)
        # sampled = (sampled.rank() +1)/(sampled.getSize()+2)
        # pairs(sampled, figure_path.joinpath('pairs_test.pdf')
        ll = 0
        s = 0
        for point in data_ref.select(test):
            point_ll = cmiic_cbn.computeLogPDF(point)
            if np.abs(point_ll) <= 10e20:
                s += 1
                ll += point_ll
Пример #9
0
    print("children(", nod, ") : ", ndag.getChildren(nod))

order = ndag.getTopologicalOrder()
marginals = [ot.Uniform(0.0, 1.0) for i in range(order.getSize())]
copulas = list()
for i in range(order.getSize()):
    d = 1 + ndag.getParents(i).getSize()
    print("i=", i, ", d=", d)
    if d == 1:
        copulas.append(ot.IndependentCopula(1))
    else:
        R = ot.CorrelationMatrix(d)
        for i in range(d):
            for j in range(i):
                R[i, j] = 0.5 / d
        copulas.append(ot.Student(5.0, [0.0] * d, [1.0] * d, R).getCopula())

cbn = otagrum.ContinuousBayesianNetwork(ndag, marginals, copulas)
size = 300
sample = cbn.getSample(size)
# ContinuousBayesianNetworkFactory
marginalsFactory = ot.UniformFactory()
copulasFactory = ot.BernsteinCopulaFactory()
threshold = 0.1
maxParents = 5
factory = otagrum.ContinuousBayesianNetworkFactory(marginalsFactory,
                                                   copulasFactory, ndag,
                                                   threshold, maxParents)
cbn = factory.build(sample)
print('cbn=', cbn)