def test_pmf_expected_values(): animals = Variable(['cat', 'dog', 'cat', 'mouse', 'dog', 'cat', 'cat', 'dog']) PrAnimals = PMF(animals) assert almostEqual(1.0, PrAnimals.expected_value(lambda v, p: 1)) # Test calculations of base-e entropy and base-2 entropy. assert almostEqual(0.97431475, (-1) * PrAnimals.expected_value(lambda v, p: math.log(p))) assert almostEqual(1.40563906, (-1) * PrAnimals.expected_value(lambda v, p: math.log2(p))) # Expected word length. assert PrAnimals.expected_value(lambda v, p: len(v)) == 3.25
def get_joint_entropy_term(self, *variables): jht_key = self.create_flat_variable_set(*variables) if len(jht_key) == 0: return 0 self.JHT_reads += 1 try: H = self.JHT[jht_key] except KeyError: self.JHT_misses += 1 joint_variables = self.datasetmatrix.get_variables('X', jht_key) pmf = PMF(joint_variables) H = - pmf.expected_value(lambda v, p: math.log(p)) self.JHT[jht_key] = H if self.DoF_calculator.requires_pmfs: self.DoF_calculator.set_context_pmfs(pmf, None, None, None) return H