Beispiel #1
0
    def test_beta_prior(self):

        # Test input parameters
        self.assertRaises(ValueError, pints.BetaLogPrior, 0, 0)
        self.assertRaises(ValueError, pints.BetaLogPrior, 2, -2)
        self.assertRaises(ValueError, pints.BetaLogPrior, -2, 2)

        p1 = pints.BetaLogPrior(0.123, 2.34)
        p2 = pints.BetaLogPrior(3.45, 4.56)

        points = [-2., 0.001, 0.1, 0.3, 0.5, 0.7, 0.9, 0.999, 2.]

        # Test means
        self.assertAlmostEqual(p1.mean(), 0.04993909866017051)
        self.assertAlmostEqual(p2.mean(), 0.4307116104868914)

        # Test n_parameters
        self.assertEqual(p1.n_parameters(), 1)

        # Test specific points
        for point in points:
            to_test = [point]
            self.assertAlmostEqual(scipy.stats.beta.logpdf(
                to_test[0], 0.123, 2.34),
                                   p1(to_test),
                                   places=9)
            self.assertAlmostEqual(scipy.stats.beta.logpdf(
                to_test[0], 3.45, 4.56),
                                   p2(to_test),
                                   places=9)

        # Test derivatives
        p1_derivs = [
            0., -878.341341341342, -10.25888888888889, -4.837619047619048,
            -4.434, -5.719523809523809, -14.37444444444445, -1340.877877877876,
            0.
        ]

        p2_derivs = [
            0., 2446.436436436437, 20.54444444444445, 3.080952380952382,
            -2.219999999999999, -8.36666666666666, -32.87777777777778,
            -3557.547547547544, 0.
        ]

        for point, deriv in zip(points, p1_derivs):
            calc_val, calc_deriv = p1.evaluateS1([point])
            self.assertAlmostEqual(calc_deriv[0], deriv)

        for point, deriv in zip(points, p2_derivs):
            calc_val, calc_deriv = p2.evaluateS1([point])
            self.assertAlmostEqual(calc_deriv[0], deriv)

        # Test pathological edge cases
        p3 = pints.BetaLogPrior(1.0, 1.0)
        calc_val, calc_deriv = p3.evaluateS1([0.0])
        self.assertAlmostEqual(calc_deriv[0], 0.0)

        calc_val, calc_deriv = p3.evaluateS1([1.0])
        self.assertAlmostEqual(calc_deriv[0], 0.0)
Beispiel #2
0
    def test_beta_prior_sampling(self):
        p1 = pints.BetaLogPrior(0.123, 2.34)
        self.assertEqual(len(p1.sample()), 1)

        n = 100
        samples1 = p1.sample(n)
        self.assertEqual(len(samples1), n)

        n = 10000
        p1 = pints.BetaLogPrior(100, 100)
        samples = p1.sample(n)
        self.assertTrue(np.abs(np.mean(samples) - 0.5) < 0.01)

        p1 = pints.BetaLogPrior(20, 30)
        samples = p1.sample(n)
        self.assertTrue(np.abs(np.mean(samples) - 0.4) < 0.01)
Beispiel #3
0
    def test_beta_prior_sampling(self):
        # Just returns samples from the numpy beta distribution so no utility
        # in verifying shape params - just check it's working as expected
        p1 = pints.BetaLogPrior(0.123, 2.34)
        self.assertEqual(len(p1.sample()), 1)

        n = 100
        samples1 = p1.sample(n)
        self.assertEqual(len(samples1), n)
Beispiel #4
0
    def test_beta_prior(self):

        # Test input parameters
        self.assertRaises(ValueError, pints.BetaLogPrior, 0, 0)
        self.assertRaises(ValueError, pints.BetaLogPrior, 2, -2)
        self.assertRaises(ValueError, pints.BetaLogPrior, -2, 2)

        p1 = pints.BetaLogPrior(0.123, 2.34)
        p2 = pints.BetaLogPrior(3.45, 4.56)

        points = [-2., 0.001, 0.1, 0.3, 0.5, 0.7, 0.9, 0.999, 2.]

        # Test means
        self.assertAlmostEqual(p1.mean(), 0.04993909866017051)
        self.assertAlmostEqual(p2.mean(), 0.4307116104868914)

        # Test CDFs
        self.assertAlmostEqual(p1.cdf(0.2), 0.9161569551360381)
        self.assertAlmostEqual(p1.cdf(0.5), 0.982186394491929)
        self.assertAlmostEqual(p2.cdf(0.5), 0.6606214580849932)
        self.assertAlmostEqual(p2.cdf(0.05), 0.001056893325194372)

        # Test inverse-CDFs
        self.assertAlmostEqual(p1.icdf(0.9), 0.1662966999241491)
        self.assertAlmostEqual(p1.icdf(0.99), 0.593317052562366)
        self.assertAlmostEqual(p2.icdf(0.3), 0.33292896683293627)
        self.assertAlmostEqual(p2.icdf(0.9), 0.6538975170733259)

        # Test n_parameters
        self.assertEqual(p1.n_parameters(), 1)

        # Test specific points
        for point in points:
            to_test = [point]
            self.assertAlmostEqual(
                scipy.stats.beta.logpdf(to_test[0], 0.123, 2.34),
                p1(to_test),
                places=9)
            self.assertAlmostEqual(
                scipy.stats.beta.logpdf(to_test[0], 3.45, 4.56),
                p2(to_test),
                places=9)

        # Test derivatives
        p1_derivs = [0., -878.341341341342, -10.25888888888889,
                     -4.837619047619048,
                     -4.434, -5.719523809523809, -14.37444444444445,
                     -1340.877877877876,
                     0.]

        p2_derivs = [0., 2446.436436436437, 20.54444444444445,
                     3.080952380952382,
                     -2.219999999999999, -8.36666666666666, -32.87777777777778,
                     -3557.547547547544, 0.]

        for point, deriv in zip(points, p1_derivs):
            calc_val, calc_deriv = p1.evaluateS1([point])
            self.assertAlmostEqual(calc_deriv[0], deriv)

        for point, deriv in zip(points, p2_derivs):
            calc_val, calc_deriv = p2.evaluateS1([point])
            self.assertAlmostEqual(calc_deriv[0], deriv)

        # Test pathological edge cases
        p3 = pints.BetaLogPrior(1.0, 1.0)
        calc_val, calc_deriv = p3.evaluateS1([0.0])
        self.assertAlmostEqual(calc_deriv[0], 0.0)

        calc_val, calc_deriv = p3.evaluateS1([1.0])
        self.assertAlmostEqual(calc_deriv[0], 0.0)