-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgibbs_ask.py
69 lines (58 loc) · 2.23 KB
/
gibbs_ask.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import numpy as np
from utils import *
def Markov_blanket_prob(node_name, sample):
"""Calculate the conditional probability of the node given its Markov blanket on current sample.
Parameters:
-----------
node_name : str
The name of the node.
sample : dict
A dictionary mapping node names to boolean values (the current sample).
Returns:
--------
float : The normalized probability of the node.
"""
node = Node.nodes[node_name]
parent_list = tuple(sample[p] for p in node.parents)
Blanket_True = CPT(node, parent_list)
Blanket_False = 1 - Blanket_True
for child_name in node.children:
child = Node.nodes[child_name]
child_parent_True = tuple(sample[p] if p != node_name else True for p in child.parents)
child_parent_False = tuple(sample[p] if p != node_name else False for p in child.parents)
child_True = CPT(child, child_parent_True)
child_False = CPT(child, child_parent_False)
if sample[child_name]:
Blanket_True *= child_True
Blanket_False *= child_False
else:
Blanket_True *= 1 - child_True
Blanket_False *= 1 - child_False
# return normalized probability
return Blanket_True / (Blanket_True + Blanket_False)
def gibbs_ask(query, evidence, totalSamples=10000):
"""Perform Gibbs sampling to estimate the normalized probability distribution of the query.
Parameters:
-----------
query : str
The name of the query node.
evidence : dict
A dictionary mapping node names (strings) to boolean values.
totalSamples : int
The total number of samples to generate.
Returns:
--------
float : The estimated probability.
"""
ind = topological().index(query)
results = []
sample = {k: random.choice([0, 1]) for k in topological()}
for k in evidence:
sample[k] = evidence[k]
for _ in range(totalSamples):
for k in topological():
if k not in evidence:
Blanket_True = Markov_blanket_prob(k, sample)
sample[k] = random.random() < Blanket_True
results.append(list(sample.values()))
return np.array(results)[:, ind].mean() if len(results) > 0 else 0