示例#1
0
 def test_map_medium_bn(self):
     medium_bn = parse_network_from_file(
         "./sample_bayesian_networks/insurance.bif")
     prob, assgn = map_ask(dict(PropCost="Thousand",
                                RiskAversion="Adventurous"),
                           medium_bn,
                           map_vars=['Theft'])
     self.assertEqual(prob, 0.13093217869278678)
     self.assertEqual(assgn, [('Theft', 'False')])
示例#2
0
 def test_map_small_bn_2(self):
     small_bn = parse_network_from_file(
         "./sample_bayesian_networks/earthquake.bif")
     prob, assgn = map_ask(dict(JohnCalls="True", Alarm="True"),
                           small_bn,
                           not_map_vars=['Alarm'])
     self.assertEqual(prob, 0.00580356)
     self.assertEqual(assgn, [('Burglary', 'True'), ('Earthquake', 'False'),
                              ('MaryCalls', 'True')])
示例#3
0
def plot_map_time_growing_var_fixed_evidence(bn_path):
    bn = parse_network_from_file(bn_path)
    variables = bn.variables
    var_values = [bn.variable_values(var) for var in variables]
    random_sel_values = [random.choice(val_list) for val_list in var_values]
    dic = dict(zip(variables, random_sel_values))
    times_list = []
    first_n_pairs = {k: dic[k] for k in list(dic)[:2]}
    print(first_n_pairs)
    del variables[:2]
    print(variables)
    for i, var in enumerate(variables):
        bn.last_max_out = None
        map_vars = variables[:i]
        if map_vars:
            ts = time.time()
            map_ask(first_n_pairs, bn, map_vars=map_vars)
            te = time.time()
            times_list.append(te - ts)
    plot_list(times_list, "MAP growing map vars fixed evidence", "# map vars",
              "time")
示例#4
0
def plot_map_chain_var_network_size():
    times_list = []
    for i in range(4, 500):
        size = i
        bn = generate_chain(size=size)
        ts = time.time()
        map_vars = [F"S{r}" for r in range(int(size / 2), size - 1)]
        prob, assgn = map_ask(dict([(F"S{size}", "True")]),
                              bn,
                              not_map_vars=["S0"])
        te = time.time()
        times_list.append(te - ts)
    plot_list(times_list, "MAP on chain bayesian networks",
              "size of the chain", "computation time")
示例#5
0
def plot_map_insurance_network_var_mapvars():
    times_list = []
    bn = parse_network_from_file("./sample_bayesian_networks/insurance.bif")
    for i in range(1, len(bn.variables)):
        size = i
        bn = parse_network_from_file(
            "./sample_bayesian_networks/insurance.bif")
        map_vars = bn.variables[0:i]
        ts = time.time()
        prob, assgn = map_ask(dict(), bn, map_vars=map_vars)
        te = time.time()
        times_list.append(te - ts)
    plot_list(times_list, "MAP on Insurance network", "# of MAP variables",
              "computation time")
示例#6
0
def plot_map_var_paper_network_size():
    times_list = []
    for i in range(4, 15):
        size = i
        bn = parse_network(generate_polytree_network_bif(size=size))
        map_vars = [F"X{r}" for r in range(1, size + 1)]
        ts = time.time()
        prob, assgn = map_ask(dict([(F"S{size}", "TRUE")]),
                              bn,
                              map_vars=map_vars)
        te = time.time()
        times_list.append(te - ts)
    plot_list(times_list, "MAP on dynamic bayesian networks",
              "size of the network", "computation time")
示例#7
0
def test_map_paper_network():
    size = 17
    bn = parse_network(generate_polytree_network_bif(size=size))
    map_vars = [F"X{r}" for r in range(1, size + 1)]
    prob, assgn = map_ask(dict([(F"S{size}", "TRUE")]), bn, map_vars=map_vars)
    print(F"P(map,e): {prob}, MAP: {assgn}")
示例#8
0
def test_map():
    bn = parse_network_from_file("./sample_bayesian_networks/earthquake.bif")
    prob, assgn = map_ask(dict(Burglary="True", JohnCalls="True"),
                          bn,
                          not_map_vars=['Alarm'])
    print(F"P(map,e): {prob}, MAP: {assgn}")