def test_determine_observed(): """ FUNCTION: determine_observed, in general.py. """ """Create data required to test function""" evidence = [None, None, 1, 2, None, 3] """Execute function""" [hidd, obs] = general.determine_observed(evidence) """Assert the function executed correctly""" assert hidd == [0, 1, 4] assert obs == [2, 3, 5]
def update_ess(self, sample, expected_vals, node_id, model): """ Update the expected sufficient statistics for this CPD. Parameters ---------- sample: List A partially observed sample of the all the nodes in the model this CPD is part of. sample[i] = [] if node i in unobserved. expected_vals: marginal A marginal object containing the expected values for any unobserved nodes in this CPD. node_sizes: Array A list of the sizes of each node in the model. If sizes[2] = 10, then node 2 can assume 1 of 10 different states. """ """Determine which nodes were observed in this sample""" node_sizes = model.node_sizes_unobserved [hidden, observed] = general.determine_observed(sample) """If the entire domain of the CPD is hidden""" if general.issubset(np.array(expected_vals.domain), np.array(hidden)): """ If the entire domain of the CPD was unobserved in the last sample. Then the marginal over the CPD domain will be just the CPD's entire CPT. Therefore we can add this directly to the CPD's expected sufficient statistics. """ self.ess = self.ess + expected_vals.T.flatten() else: """ If any part of the CPD's domain was observed, the expected values for the observed domain has been marginalized out. Therefore we need to pump the marginal up to its correct dimensions based on the observed evidence, and place the observed values where the 'expected' values were for the observed nodes. """ expected_vals.add_ev_to_dmarginal(sample, node_sizes) """ Add the new values to the CPD's expected sufficient statistics. """ self.ess = self.ess + expected_vals.T.flatten()
def update_ess(self, sample, expected_vals, node_sizes): """ Update the expected sufficient statistics for this CPD. Parameters ---------- sample: List A partially observed sample of the all the nodes in the model this CPD is part of. sample[i] = [] if node i in unobserved. expected_vals: marginal A marginal object containing the expected values for any unobserved nodes in this CPD. node_sizes: Array A list of the sizes of each node in the model. If sizes[2] = 10, then node 2 can assume 1 of 10 different states. """ """Determine which nodes were observed in this sample""" [hidden, observed] = general.determine_observed(sample) """If the entire domain of the CPD is hidden""" if general.issubset(np.array(expected_vals.domain), np.array(hidden)): """ If the entire domain of the CPD was unobserved in the last sample. Then the marginal over the CPD domain will be just the CPD's entire CPT. Therefore we can add this directly to the CPD's expected sufficient statistics. """ self.ess = self.ess + expected_vals.T.flatten() else: """ If any part of the CPD's domain was observed, the expected values for the observed domain has been marginalized out. Therefore we need to pump the marginal up to its correct dimensions based on the observed evidence, and place the observed values where the 'expected' values were for the observed nodes. """ expected_vals.add_ev_to_dmarginal(sample, node_sizes) """ Add the new values to the CPD's expected sufficient statistics. """ self.ess = self.ess + expected_vals.T.flatten()