Example #1
0
def relative_mean_difference(pmf, mu=None):
    test_obj_instance(pmf, Pmf)
    if mu is None:
        mu = mean(pmf)

    diff = Pmf()
    for v1, p1 in pmf.items():
        for v2, p2 in pmf.items():
            diff.increment(abs(v1 - v2), p1 * p2)

    return mean(diff) / mu
Example #2
0
    def test_plot_histogram(self):
        xs = [0, 1, 3, 4, 5]
        ys = [0, 1, 3, 4, 5]

        plot_histogram(Pmf(dict(zip(xs, ys))))
        filename = '../resources/test_plot_histogram_method'
        save_format(filename)
        self.assertTrue(do_file_exist(filename + '.eps'))
        os.remove(filename + '.eps')
class PmfTest(unittest.TestCase):
    
    def setUp(self):
        self.pmf = Pmf(d={0:4.0,1:8.0,2:12.0,3:16.0})
        
    def test_get_prob(self):
        self.assertEqual(self.pmf.get_prob(0), 0.1)
        
    def test_get_probs(self):
        probs = self.pmf.get_probs()
        for indx, value in enumerate(probs):
            probs[indx] = round(value,1)
        self.assertEqual(probs, [0.1,0.2,0.3,0.4])
        
    def test_random(self):
        pmf = Pmf(d={0:10})
        self.assertEqual(pmf.random(), 0)
        
        
    def test_get_pmf_of_logs(self):
        probs = self.pmf.get_pmf_of_logs().get_probs()
        for indx, value in enumerate(probs):
            probs[indx] = round(value,1)

        self.assertEqual(probs, [-1.4,-0.7,-0.3,0.0])
        
    def test_get_pmf_of_exp(self):
        probs = self.pmf.get_pmf_of_exp().get_probs()
        for indx, value in enumerate(probs):
            probs[indx] = round(value,1)
            
        self.assertEqual(probs, [0.1,0.2,0.3,0.4])
        
    def test_max_likelihood(self):
        max_value, max_prob = self.pmf.max_likelihood()
        self.assertEqual(max_value, 3.0)
        self.assertEqual(max_prob, 0.4)
        
    def test_min_likelihood(self):
        min_value, min_prob = self.pmf.min_likelihood()
        self.assertEqual(min_value, 0.0)
        self.assertEqual(min_prob, 0.0)
Example #4
0
class PmfTest(unittest.TestCase):
    def setUp(self):
        self.pmf = Pmf(d={0: 4.0, 1: 8.0, 2: 12.0, 3: 16.0})

    def test_get_prob(self):
        self.assertEqual(self.pmf.get_prob(0), 0.1)

    def test_get_probs(self):
        probs = self.pmf.get_probs()
        for indx, value in enumerate(probs):
            probs[indx] = round(value, 1)
        self.assertEqual(probs, [0.1, 0.2, 0.3, 0.4])

    def test_random(self):
        pmf = Pmf(d={0: 10})
        self.assertEqual(pmf.random(), 0)

    def test_get_pmf_of_logs(self):
        probs = self.pmf.get_pmf_of_logs().get_probs()
        for indx, value in enumerate(probs):
            probs[indx] = round(value, 1)

        self.assertEqual(probs, [-1.4, -0.7, -0.3, 0.0])

    def test_get_pmf_of_exp(self):
        probs = self.pmf.get_pmf_of_exp().get_probs()
        for indx, value in enumerate(probs):
            probs[indx] = round(value, 1)

        self.assertEqual(probs, [0.1, 0.2, 0.3, 0.4])

    def test_max_likelihood(self):
        max_value, max_prob = self.pmf.max_likelihood()
        self.assertEqual(max_value, 3.0)
        self.assertEqual(max_prob, 0.4)

    def test_min_likelihood(self):
        min_value, min_prob = self.pmf.min_likelihood()
        self.assertEqual(min_value, 0.0)
        self.assertEqual(min_prob, 0.0)
Example #5
0
 def test_make_transform(self):
     dct = {1: 2, 2: 2, 3: 3, 4: 1, 5: 4}
     old_pmf = Pmf(dct)
     new_pmf, options = make_transform(old_pmf)
     self.assertEqual(old_pmf, new_pmf)
     self.assertEqual(options, {'xscale': 'linear', 'yscale': 'linear'})
Example #6
0
 def test_make_cdf_from_pmf(self):
     dct = {1: 2, 2: 2, 3: 3, 4: 1, 5: 4}
     self.assertEqual(len(make_cdf_from_pmf(Pmf(dct))), 5)
Example #7
0
 def test_make_mixture_pmfs(self):
     dct = {1: 2, 2: 2, 3: 3, 4: 1, 5: 4}
     pmfs = {Pmf(dct): 0.1, Pmf(dct): 0.2, Pmf(dct): 0.3, Pmf(dct): 0.4}
     self.assertEqual(len(make_mixture_pmfs(pmfs)), 5)
Example #8
0
 def test_random(self):
     pmf = Pmf(d={0: 10})
     self.assertEqual(pmf.random(), 0)
Example #9
0
 def setUp(self):
     self.pmf = Pmf(d={0: 4.0, 1: 8.0, 2: 12.0, 3: 16.0})
 def test_random(self):
     pmf = Pmf(d={0:10})
     self.assertEqual(pmf.random(), 0)
 def setUp(self):
     self.pmf = Pmf(d={0:4.0,1:8.0,2:12.0,3:16.0})
Example #12
0
 def test_fit_line_pmf(self):
     pmf = Pmf(dict(zip(self.xs, self.ys)))
     self.assertEqual(self.least_squares.fit_line_pmf(), pmf)