def test_student_t_prior_sampling(self): p1 = pints.StudentTLogPrior(0, 1000, 1) self.assertEqual(len(p1.sample()), 1) n = 10000 samples1 = p1.sample(n) self.assertEqual(len(samples1), n) self.assertTrue(np.absolute(np.mean(samples1)) < 2) p2 = pints.StudentTLogPrior(0, 1, 1) samples2 = p2.sample(n) self.assertGreater(np.var(samples2), np.var(samples1)) p3 = pints.StudentTLogPrior(0, 1000, 1000) samples3 = p3.sample(n) self.assertGreater(np.var(samples3), np.var(samples1)) p4 = pints.StudentTLogPrior(1000, 1000, 1) samples4 = p4.sample(n) self.assertGreater(np.mean(samples4), np.mean(samples1))
def test_student_t_prior_cdf_icdf(self): p1 = pints.StudentTLogPrior(4.4, 1.3, 3.0) self.assertAlmostEqual(p1.cdf(-3.4), 0.09239348006197708) self.assertAlmostEqual(p1.icdf(0.67), 6.060216885291837)
def test_student_t_prior(self): # Test two specific function values p1 = pints.StudentTLogPrior(0, 2, 10) self.assertEqual(p1([0]), -3.342305863833964) p2 = pints.StudentTLogPrior(10, 5, 10) self.assertEqual(p2([10]), -3.27120468204877) # Test exceptions self.assertRaises(ValueError, pints.StudentTLogPrior, 0, 0, 10) self.assertRaises(ValueError, pints.StudentTLogPrior, 0, -1, 10) self.assertRaises(ValueError, pints.StudentTLogPrior, 0, 1, 0) self.assertRaises(ValueError, pints.StudentTLogPrior, 0, 1, -1) loc1 = 0 df1 = 2 scale1 = 10 loc2 = 10 df2 = 5 scale2 = 8 p1 = pints.StudentTLogPrior(loc1, df1, scale1) p2 = pints.StudentTLogPrior(loc2, df2, scale2) p3 = pints.StudentTLogPrior(1.23, 1, 2.34) # Test means self.assertAlmostEqual(p1.mean(), 0.) self.assertAlmostEqual(p2.mean(), 10.) self.assertTrue(np.isnan(p3.mean())) # Test other function calls self.assertEqual(p1.n_parameters(), 1) self.assertEqual(p2.n_parameters(), 1) points = [-5., -2., 0., 1., 8.91011] # Test specific points for point in points: to_test = [point] self.assertAlmostEqual( scipy.stats.t.logpdf(to_test[0], df=df1, loc=loc1, scale=scale1), p1(to_test), places=9) self.assertAlmostEqual( scipy.stats.t.logpdf(to_test[0], df=df2, loc=loc2, scale=scale2), p2(to_test), places=9) # Test derivatives p1_derivs = [0.06666666666666668, 0.02941176470588236, 0., -0.01492537313432837, -0.0956738760845951] p2_derivs = [0.1651376146788991, 0.1551724137931035, 0.1428571428571429, 0.1346633416458853, 0.02035986041216401] 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)