Example #1
0
def plot_mpe_time_growing_var(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 = []
    for i, item in enumerate(dic):
        # bn = parse_network_from_file("./sample_bayesian_networks/alarm.bif")
        bn.last_max_out = None
        first_n_pairs = {k: dic[k] for k in list(dic)[:i]}
        ts = time.time()
        mpe_ask(first_n_pairs, bn)
        te = time.time()
        times_list.append(te - ts)
    plot_list(times_list, "MPE growing evidences", "# evidences", "time")
Example #2
0
 def test_mpe_small_bn_2(self):
     small_bn = parse_network_from_file(
         "./sample_bayesian_networks/earthquake.bif")
     prob, assgn = mpe_ask(dict(JohnCalls="False", Alarm="True"), small_bn)
     self.assertEqual(prob, 0.0006448399999999999)
     self.assertEqual(assgn, [('Burglary', 'True'), ('Earthquake', 'False'),
                              ('MaryCalls', 'True')])
Example #3
0
def plot_mpe_chain_var_network_size():
    times_list = []
    for i in range(1, 500):
        size = i
        bn = generate_chain(size=size)
        ts = time.time()
        prob, assgn = mpe_ask(dict([(F"S{size}", "True")]), bn)
        te = time.time()
        times_list.append(te - ts)
    plot_list(times_list, "MPE on chain bayesian networks",
              "size of the chain", "computation time")
Example #4
0
 def test_mpe_medium_bn_2(self):
     medium_bn = parse_network_from_file(
         "./sample_bayesian_networks/insurance.bif")
     prob, assgn = mpe_ask(dict(AntiTheft="True", RiskAversion="Cautious"),
                           medium_bn)
     self.assertEqual(prob, 0.0007795974803453906)
     self.assertEqual(assgn,
                      [('Age', 'Adult'), ('Mileage', 'FiftyThou'),
                       ('SocioEcon', 'Prole'), ('OtherCar', 'True'),
                       ('GoodStudent', 'False'), ('MakeModel', 'Economy'),
                       ('SeniorTrain', 'False'), ('HomeBase', 'City'),
                       ('VehicleYear', 'Older'), ('RuggedAuto', 'EggShell'),
                       ('Antilock', 'False'), ('CarValue', 'FiveThou'),
                       ('Airbag', 'False'), ('DrivingSkill', 'Normal'),
                       ('Cushioning', 'Poor'), ('DrivHist', 'Zero'),
                       ('DrivQuality', 'Normal'), ('Theft', 'False'),
                       ('Accident', 'None'), ('OtherCarCost', 'Thousand'),
                       ('ILiCost', 'Thousand'), ('ThisCarDam', 'None'),
                       ('MedCost', 'Thousand'), ('ThisCarCost', 'Thousand'),
                       ('PropCost', 'Thousand')])
Example #5
0
def test_mpe():
    bn = parse_network_from_file("./sample_bayesian_networks/earthquake.bif")
    prob, assgn = mpe_ask(dict(JohnCalls="True", MaryCalls="True"), bn)
    print(F"P(mpe,e): {prob}, MPE: {assgn}")
Example #6
0
def test_mpe_medium():
    bn = parse_network_from_file("./sample_bayesian_networks/insurance.bif")
    prob, assgn = mpe_ask(
        dict(PropCost="Thousand", RiskAversion="Adventurous"), bn)
    print(F"P(mpe,e): {prob}, MPE: {assgn}")