Ejemplo n.º 1
0
def test_reapply():
    """
    Tests reinitializing join tree after updating CPTs.
    :return: None.
    """
    a = BbnNode(Variable(0, 'a', ['t', 'f']), [0.2, 0.8])
    b = BbnNode(Variable(1, 'b', ['t', 'f']), [0.1, 0.9, 0.9, 0.1])
    bbn = Bbn().add_node(a).add_node(b) \
        .add_edge(Edge(a, b, EdgeType.DIRECTED))

    lhs = InferenceController.apply(bbn)
    rhs = InferenceController.reapply(lhs, {
        0: [0.3, 0.7],
        1: [0.2, 0.8, 0.8, 0.2]
    })

    lhs_pot = [lhs.get_bbn_potential(n) for n in lhs.get_bbn_nodes()]
    rhs_pot = [rhs.get_bbn_potential(n) for n in rhs.get_bbn_nodes()]

    lhs_d = Potential.to_dict(lhs_pot)
    rhs_d = Potential.to_dict(rhs_pot)

    # lhs should not match rhs after CPT update
    for k, prob in lhs_d.items():
        assert k in rhs_d
        assert prob != rhs_d[k]

    # now create lhs with same params as param used to update old
    # should match with rhs since params are now the same
    a = BbnNode(Variable(0, 'a', ['t', 'f']), [0.3, 0.7])
    b = BbnNode(Variable(1, 'b', ['t', 'f']), [0.2, 0.8, 0.8, 0.2])
    bbn = Bbn().add_node(a).add_node(b) \
        .add_edge(Edge(a, b, EdgeType.DIRECTED))

    lhs = InferenceController.apply(bbn)
    lhs_pot = [lhs.get_bbn_potential(n) for n in lhs.get_bbn_nodes()]
    lhs_d = Potential.to_dict(lhs_pot)

    for k, prob in lhs_d.items():
        assert k in rhs_d
        assert_almost_equals(prob, rhs_d[k], 0.001)
Ejemplo n.º 2
0
def test_simple_serde():
    """
    Tests join tree serde with only 1 clique.
    :return: None.
    """
    a = BbnNode(Variable(0, 'a', ['t', 'f']), [0.2, 0.8])
    b = BbnNode(Variable(1, 'b', ['t', 'f']), [0.1, 0.9, 0.9, 0.1])
    bbn = Bbn().add_node(a).add_node(b) \
        .add_edge(Edge(a, b, EdgeType.DIRECTED))
    lhs = InferenceController.apply(bbn)

    d = JoinTree.to_dict(lhs)

    rhs = JoinTree.from_dict(d)
    rhs = InferenceController.apply_from_serde(rhs)

    lhs_pot = [lhs.get_bbn_potential(n) for n in lhs.get_bbn_nodes()]
    rhs_pot = [rhs.get_bbn_potential(n) for n in rhs.get_bbn_nodes()]

    lhs_pot = Potential.to_dict(lhs_pot)
    rhs_pot = Potential.to_dict(rhs_pot)

    assert len(lhs_pot) == len(rhs_pot)
Ejemplo n.º 3
0
def test_forest_inference():
    """
    Tests inference on a disconnected DAG; sub-DAGs are a -> b, c -> d and e -> f.
    :return: None.
    """
    a = BbnNode(Variable(0, 'a', ['t', 'f']), [0.2, 0.8])
    b = BbnNode(Variable(1, 'b', ['t', 'f']), [0.1, 0.9, 0.9, 0.1])
    c = BbnNode(Variable(2, 'c', ['t', 'f']), [0.2, 0.8])
    d = BbnNode(Variable(3, 'd', ['t', 'f']), [0.1, 0.9, 0.9, 0.1])
    e = BbnNode(Variable(4, 'e', ['t', 'f']), [0.2, 0.8])
    f = BbnNode(Variable(5, 'f', ['t', 'f']), [0.1, 0.9, 0.9, 0.1])
    bbn = Bbn().add_node(a).add_node(b).add_node(c).add_node(d).add_node(e).add_node(f) \
        .add_edge(Edge(a, b, EdgeType.DIRECTED)) \
        .add_edge(Edge(c, d, EdgeType.DIRECTED)) \
        .add_edge(Edge(e, f, EdgeType.DIRECTED))

    jt = InferenceController.apply(bbn)
    pot = [jt.get_bbn_potential(n) for n in jt.get_bbn_nodes()]
    o = Potential.to_dict(pot)
    e = {
        '0=t': 0.2,
        '0=f': 0.8,
        '1=t': 0.7400000000000001,
        '1=f': 0.26,
        '2=t': 0.2,
        '2=f': 0.8,
        '3=t': 0.7400000000000001,
        '3=f': 0.26,
        '4=t': 0.2,
        '4=f': 0.8,
        '5=t': 0.7400000000000001,
        '5=f': 0.26
    }

    for k, p in e.items():
        assert_almost_equals(p, o[k], 0.001)
Ejemplo n.º 4
0
def test_from_dict():
    """
    Tests deserializing from dictionary.
    :return:
    """
    s = """{
  "bbn_nodes": {
    "0": {
      "probs": [
        0.2,
        0.8
      ],
      "variable": {
        "id": 0,
        "name": "n0",
        "values": [
          "t",
          "f"
        ]
      }
    },
    "1": {
      "probs": [
        0.9,
        0.1,
        0.9,
        0.1
      ],
      "variable": {
        "id": 1,
        "name": "n1",
        "values": [
          "t",
          "f"
        ]
      }
    },
    "2": {
      "probs": [
        0.6,
        0.4,
        0.4,
        0.6
      ],
      "variable": {
        "id": 2,
        "name": "n2",
        "values": [
          "t",
          "f"
        ]
      }
    }
  },
  "jt": {
    "edges": [
      "0-1-1-1-2"
    ],
    "nodes": {
      "0-1": {
        "node_ids": [
          0,
          1
        ],
        "type": "clique"
      },
      "0-1-1-1-2": {
        "left": "0-1",
        "right": "1-2",
        "type": "sepset"
      },
      "1-2": {
        "node_ids": [
          1,
          2
        ],
        "type": "clique"
      }
    },
    "parent_info": {
      "0": [],
      "1": [
        0
      ],
      "2": [
        1
      ]
    }
  }
}"""
    d = json.loads(s)
    lhs = JoinTree.from_dict(d)
    lhs = InferenceController.apply_from_serde(lhs)

    n0 = BbnNode(Variable(0, 'n0', ['t', 'f']), [0.2, 0.8])
    n1 = BbnNode(Variable(1, 'n1', ['t', 'f']), [0.9, 0.1, 0.9, 0.1])
    n2 = BbnNode(Variable(2, 'n2', ['t', 'f']), [0.6, 0.4, 0.4, 0.6])
    bbn = Bbn().add_node(n0).add_node(n1).add_node(n2) \
        .add_edge(Edge(n0, n1, EdgeType.DIRECTED)) \
        .add_edge(Edge(n1, n2, EdgeType.DIRECTED))
    rhs = InferenceController.apply(bbn)

    lhs_pot = [lhs.get_bbn_potential(n) for n in lhs.get_bbn_nodes()]
    rhs_pot = [rhs.get_bbn_potential(n) for n in rhs.get_bbn_nodes()]

    lhs_pot = Potential.to_dict(lhs_pot)
    rhs_pot = Potential.to_dict(rhs_pot)

    assert len(lhs_pot) == len(rhs_pot)

    for k, p in lhs_pot.items():
        assert_almost_equals(p, rhs_pot[k], 0.001)