def learn_params_mle(self, samples): """ Maximum liklihood estimation (MLE) parameter learing for a BNET. Parameters ---------- samples: List A list of fully observed samples for the spanning the total domain of this BNET. Where samples[i][n] is the i'th sample for node n. """ """Convert the samples list to an array""" samples = np.array(samples) """If the CPD's have not yet been initialized""" if len(self.cpds) == 0: """For every node in the BNET""" for i in range(0, self.num_nodes): """Create a blank CPD for the node""" family = graph.family(self.model_graph, i) self.cpds.append(cpds.tabular_CPD(i, self.node_sizes, \ self.model_graph)) """Get the samples within this nodes CPDs domain""" local_samples = samples[:, family] """Learn the node parameters""" if len(local_samples.tolist()) != 0: self.cpds[i].learn_params_mle(local_samples) else: """For every node in the BNET""" for i in range(0, self.num_nodes): """Get the samples within this nodes CPDs domain""" family = graph.family(self.model_graph, i) local_samples = samples[:, family] """Learn the node parameters""" if len(local_samples.tolist()) != 0: self.cpds[i].learn_params_mle(local_samples)
def learn_params_mle(self, samples): """ Maximum liklihood estimation (MLE) parameter learing for a BNET. Parameters ---------- samples: List A list of fully observed samples for the spanning the total domain of this BNET. Where samples[i][n] is the i'th sample for node n. """ """Convert the samples list to an array""" samples = np.array(samples) if len(self.node_cpds) == 0: self.init_cpds() """For every node in the BNET""" for i in range(0, self.num_nodes): """Get the samples within this nodes CPDs domain""" family = graph.family(self.adj_mat, i) local_samples = samples[:, family] """Learn the node parameters""" if len(local_samples.tolist()) != 0: self.node_cpds[i].learn_params_mle(local_samples) else: """For every node in the BNET""" for i in range(0, self.num_nodes): """Get the samples within this nodes CPDs domain""" family = graph.family(self.adj_mat, i) local_samples = samples[:, family] """Learn the node parameters""" if len(local_samples.tolist()) != 0: self.node_cpds[i].learn_params_mle(local_samples)
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 learn_params_EM(self, samples, max_iter=10, thresh=np.exp(-4), \ exact=True, inf_max_iter=10): """ EM algorithm parameter learing for a BNET, accepts partially observed samples. Parameters ---------- samples: List A list of partially observed samples for the spanning the total domain of this BNET. Where samples[i][n] is the i'th sample for node n. samples[i][n] can be [] if node n was not observed in the i'th sample. """ """If the CPDs have not yet been defined, then create them""" if len(self.cpds) == 0: """For every node in the BNET""" for i in range(0, self.num_nodes): """Create a blank CPD for the node""" family = graph.family(self.model_graph, i) self.cpds.append(cpds.tabular_CPD(i, self.node_sizes, \ self.model_graph)) else: """If they have been defined, reset the CPT's""" for i in range(0, self.num_nodes): self.cpds[i].CPT = np.ones(self.cpds[i].CPT.shape) """Create data used in the EM algorithm""" loglik = 0 prev_loglik = -1*np.Inf converged = False num_iter = 0 """Init the training inference engine for the new BNET""" self.init_inference_engine(exact, inf_max_iter) while ((not converged) and (num_iter < max_iter)): """Perform an EM iteration and gain the new log likelihood""" loglik = self.EM_step(samples) """Check for convergence""" delta_loglik = np.abs(loglik - prev_loglik) avg_loglik = np.nan_to_num((np.abs(loglik) + \ np.abs(prev_loglik))/2) if (delta_loglik / avg_loglik) < thresh: """Algorithm has converged""" break prev_loglik = loglik """Increase the iteration counter""" num_iter = num_iter + 1
def test_family(self): """ FUNCTION: family, in graph.py. """ """Use the function to determine the family of each node""" family = [] for i in range(0, self.n): family.append(graph.family(self.dag, i)) """ Assert that the function returns the expected family for each node. """ assert family[0] == [0] assert family[1] == [0, 1] assert family[2] == [0, 2] assert family[3] == [1, 2, 3]
def test_family(self): """ FUNCTION: family, in graph.py. """ """Use the function to determine the family of each node""" family = [] for i in range(0, self.n): family.append(graph.family(self.dag, i)) """ Assert that the function returns the expected family for each node. """ print family assert_array_equal(family[0], np.array([0])) assert_array_equal(family[1], np.array([0, 1])) assert_array_equal(family[2], np.array([0, 2])) assert_array_equal(family[3], np.array([1, 2, 3]))
def test_family(self): """ FUNCTION: family, in graph.py. """ """Use the function to determine the family of each node""" family = [] for i in range(0, self.n): family.append(graph.family(self.dag, i)) """ Assert that the function returns the expected family for each node. """ print family assert_array_equal(family[0], np.array([0])) assert_array_equal(family[1], np.array([0,1])) assert_array_equal(family[2], np.array([0,2])) assert_array_equal(family[3], np.array([1,2,3]))