def __init__(self, intra, inter, node_sizes, discrete_nodes=None, continuous_nodes=None, node_cpds=None, inference_inference_engine=None): """ Initialize a dynamic Bayesian network. Parameters ---------- intra: numpy array inter: numpy array node_sizes: node_cpds """ # Node sizes are initialized and stored for the 1st and 2nd time slices super(DBN, self).__init__(np.tile(node_sizes, 2), discrete_nodes, continuous_nodes) self.intra = intra self.intra_N = len(self.intra) self.inter = inter self.inter_N = self.inter.sum() self.inference_inference_engine = inference_inference_engine # Adjacency matrix self.adj_mat = np.vstack([ np.hstack([self.intra, self.inter]), np.hstack([np.zeros([self.intra_N] * 2), self.intra]) ]) # Equivalence class. self.equiv_class1 = range(self.intra_N) self.equiv_class2 = self.equiv_class1[:] for i in range(self.intra_N): l1 = np.array( [j + self.intra_N for j in graph.parents(self.adj_mat, i)]) l2 = graph.parents(self.adj_mat, i + self.intra_N) if all(l1 != l2): # Node has non-isomorphic parents self.equiv_class2[i] = max(self.equiv_class2) + 1 #else: # Node has isomorphic parents # Number of CPDs in the DBN. This will be equal to the number of nodes # in the first time-slice plus the number of nodes that have links # the the next time-slice. self.node_cpds_N = self.intra_N + self.inter_N if node_cpds is not None: assert (len(node_cpds) == self.node_cpds_N) self.node_cpds = np.array(node_cpds) else: self.node_cpds = np.empty(self.node_cpds_N, dtype=object) self.node_cpds_meta = [None] * self.node_cpds_N
def __init__(self, intra, inter, node_sizes, discrete_nodes=None, continuous_nodes=None, node_cpds = None, inference_inference_engine = None): """ Initialize a dynamic Bayesian network. Parameters ---------- intra: numpy array inter: numpy array node_sizes: node_cpds """ # Node sizes are initialized and stored for the 1st and 2nd time slices super(DBN, self).__init__(np.tile(node_sizes,2), discrete_nodes, continuous_nodes) self.intra = intra self.intra_N = len(self.intra) self.inter = inter self.inter_N = self.inter.sum() self.inference_inference_engine = inference_inference_engine # Adjacency matrix self.adj_mat = np.vstack([ np.hstack([self.intra, self.inter]), np.hstack([np.zeros([self.intra_N]*2),self.intra]) ]) # Equivalence class. self.equiv_class1 = range(self.intra_N) self.equiv_class2 = self.equiv_class1[:] for i in range(self.intra_N): l1 = np.array([j+self.intra_N for j in graph.parents(self.adj_mat, i)]) l2 = graph.parents(self.adj_mat, i+self.intra_N) if all(l1 != l2): # Node has non-isomorphic parents self.equiv_class2[i] = max(self.equiv_class2)+1 #else: # Node has isomorphic parents # Number of CPDs in the DBN. This will be equal to the number of nodes # in the first time-slice plus the number of nodes that have links # the the next time-slice. self.node_cpds_N = self.intra_N + self.inter_N if node_cpds is not None: assert(len(node_cpds)==self.node_cpds_N) self.node_cpds = np.array(node_cpds) else: self.node_cpds = np.empty(self.node_cpds_N, dtype=object) self.node_cpds_meta = [None]*self.node_cpds_N
def init_cpds(self): self.node_cpds = np.empty(self.node_cpds_N, dtype=object) for i in range(0, self.num_nodes): """Create a blank CPD for the node""" family = graph.family(self.adj_mat, i) if i in self.continuous_nodes: self.node_cpds[i] = node_cpds.GaussianCPD() else: fam = np.hstack([i, graph.parents(self.adj_mat, i)]) fam_sizes = self.node_sizes[fam] self.node_cpds[i] = cpds.TabularCPD(np.ones(fam_sizes))
def init_cpds(self): self.node_cpds = np.empty(self.node_cpds_N, dtype=object) for i in range(0, self.num_nodes): """Create a blank CPD for the node""" family = graph.family(self.adj_mat, i) if i in self.continuous_nodes: self.node_cpds[i] = node_cpds.GaussianCPD() else: fam = np.hstack([i,graph.parents(self.adj_mat, i)]) fam_sizes = self.node_sizes[fam] self.node_cpds[i] = cpds.TabularCPD(np.ones(fam_sizes))
def test_parents(self): """ FUNCTION: parents, in graph.py. """ """Use the function to determine the parents of each node""" parents = [] for i in range(0, self.n): parents.append(graph.parents(self.dag, i)) """ Assert that the function returns the expected parents for each node. """ assert_array_equal(parents[0], np.array([])) assert_array_equal(parents[1], np.array([0])) assert_array_equal(parents[2], np.array([0])) assert_array_equal(parents[3], np.array([1, 2]))
def test_parents(self): """ FUNCTION: parents, in graph.py. """ """Use the function to determine the parents of each node""" parents = [] for i in range(0, self.n): parents.append(graph.parents(self.dag, i)) """ Assert that the function returns the expected parents for each node. """ assert parents[0] == [] assert parents[1] == [0] assert parents[2] == [0] assert parents[3] == [1, 2]
def test_parents(self): """ FUNCTION: parents, in graph.py. """ """Use the function to determine the parents of each node""" parents = [] for i in range(0, self.n): parents.append(graph.parents(self.dag, i)) """ Assert that the function returns the expected parents for each node. """ assert_array_equal(parents[0],np.array([])) assert_array_equal(parents[1],np.array([0])) assert_array_equal(parents[2],np.array([0])) assert_array_equal(parents[3],np.array([1,2]))
def __init__(self, node_id, node_sizes, dag, CPT=None): """ Initializes the CPD. Parameters ---------- node_id: Int The index of the node this CPD is assigned to. node_size: Numpy array A list of the sizes of the nodes in Bayesian network that this CPD is part of. DAG: Numpy matrix An adjacency matrix representing the directed acyclic graph that from the bayesian network this CPD is part of. """ """Set the data members""" self.ess = None """Determine the nodes parents""" parents = graph.parents(dag, node_id) self.parents = parents[:] """ The domain sizes of the CPT can be determined by the number of values each node forming the CPT can assume. For instance, the CPT for P(a|b,c) is formed by the 3 discrete nodes a, b and c. The CPT will therefore have 3 dimensions. The size of each dimension is determined by the number of values each variable, a, b and c, can assume. If a and b can assume 1 of 2 possible values, and c can assume 1 of 4 possible values then dimension 1 and 2 are both of size 2, and dimension 3 is of size 4. Therefore, this CPT would be a 3-D array, with dimensions (2, 2, 4). """ parents.insert(0, node_id) self.fam_size = node_sizes[0, parents] if CPT != None: """If a CPT was defined, assign it to the CPD""" self.CPT = CPT else: """ If no CPT was defined, create an array of ones of the correct size. """ self.CPT = np.ones((self.fam_size))