Ejemplo n.º 1
0
def compareApprox(m, bn, evs):
    """
  Compare results in m with a LazyPropagation on bn with evidence evs

  :param m: the approximated algorithm
  :param bn: the bayesian network
  :param evs: the dict of evidence
  :return: void
  """
    ie = gum.LazyPropagation(bn)
    ie.setEvidence(evs)
    ie.makeInference()

    res = []
    for i in bn.ids():
        v, c = m.results(i)
        if v is not None:
            res.append((bn.variable(i).name(), KL(ie.posterior(i), v),
                        compactPot(ie.posterior(i)), compactPot(v), c))
        else:
            print("{}: {}".format(
                bn.variable(i).name(), evs[bn.variable(i).name()]))

    for r in sorted(res, key=lambda item: item[1], reverse=True):
        print(
            "{} : {:3.5f}\n        exact  : {}\n        approx : {}  ({:7.5f})"
            .format(*r))
Ejemplo n.º 2
0
def main():
    bn = gum.loadBN("data/alarm.bif")
    print("BN read")

    evs = {"HR": 1, "PAP": 2}

    m = Gibbs(bn, evs, verbose=True)
    m.run(5e-2, 20)

    print("done")

    ie = gum.LazyPropagation(bn)
    ie.setEvidence(evs)
    ie.makeInference()

    for i in bn.ids():
        v, c = m.results(i)
        if v is not None:
            print("{} : {:3.5f}\n    exact  : {}\n    approx : {}  ({:7.5f})".
                  format(
                      bn.variable(i).name(), utils.KL(ie.posterior(i), v),
                      utils.compactPot(ie.posterior(i)), utils.compactPot(v),
                      c))
        else:
            print("{}: {}".format(
                bn.variable(i).name(), evs[bn.variable(i).name()]))
Ejemplo n.º 3
0
 def traceMsg():
     print("\n\n")
     print("   {}->    {}->".format(compactPot(m._messages[0, 1]),
                                    compactPot(m._messages[1, 2])))
     print(
         "a ------------------------------> b ------------------------------> c"
     )
     print("   <-{}    <-{}".format(compactPot(m._messages[1, 0]),
                                    compactPot(m._messages[2, 1])))
Ejemplo n.º 4
0
def multipleMontecarlo(bn, evs, N=5):
    for i in range(1, N + 1):
        print()
        print("==> montecarlo {}/{}".format(i, N))
        m = MonteCarlo(bn, evs)
        m.run(1e-1, 50, verbose=True)

        for n in ["P3.Duration", "R3.Cost2", "A3.Productivity1"]:
            i = bn.idFromName(n)
            v, c = m.results(i)
            print("{}: {} ({})".format(
                bn.variable(i).name(), utils.compactPot(v), c))