Table of Contents


In [35]:
from IPython.display import display, HTML
from nxpd import draw

import networkx as nx

In [67]:
def draw_graph(
    graph, labels=None
):
    # create networkx graph
    G = nx.DiGraph()
    G.graph['dpi'] = 120

    G.add_nodes_from(set([
        graph[k1][k2] 
        for k1 in range(len(graph)) 
        for k2 in range(len(graph[k1]))
    ]))
    G.add_edges_from(graph)
    return draw(G, show='ipynb')

In [68]:
class BinaryVariable():
    name = ''
    probability_value = 0.0
    
    def __init__(self, name: str, probability_value: float=None):
        self.name = name
        self.probability_value = probability_value
    
    def __repr__(self):
        return '%s: %s' % (self.name, self.probability_value)
    
    def __str__(self):
        return self.name[0]

class Graph():
    graph = []
    
    def __init__(self, graph: list):
        self.graph = graph
    
    def show(self):
        display(draw_graph(self.graph))
    
    def _repr_png_(self):
        return draw_graph(self.graph).data
    
# Happiness problem
H = BinaryVariable('Happy')
R = BinaryVariable('Raise', 0.01)
S = BinaryVariable('Sunny', 0.7)

G = Graph([(S, H), (R, H)])
G.show()



In [63]:
def of(**kwargs):
    return kwargs

def given(**kwargs):
    return kwargs

class BayesianNetwork:
    graph = []
    
    def __init__(self, graph: Graph):
        self.graph = graph
        
    def is_independent(of: dict, given: dict=None):
        pass
    
    def P(self, of: dict, given: dict=None):
        print(of)
        pass

# Happiness problem
H = BinaryVariable('Happy')
R = BinaryVariable('Raise', 0.01)
S = BinaryVariable('Sunny', 0.7)

G = Graph([(S, H), (R, H)])
G.show()

bnet = BayesianNetwork(G)
bnet.P(of(S=True, R=True))


{'S': True, 'R': True}