Exemplo n.º 1
0
 def testThreading2(self):
     a = sam.Sam(logProb1, [.5, .5], lowerBounds=[0., -np.inf])
     samples = a.run(100000,
                     np.random.rand(5, 2),
                     1000,
                     threads=5,
                     showProgress=False)
     for i in a.getAcceptance():
         self.assertGreaterEqual(i[0], 0.)
         self.assertLessEqual(i[0], 1.)
         self.assertGreaterEqual(i[1], 0.)
         self.assertLessEqual(i[1], 1.)
     with self.assertRaises(AttributeError):
         a.samples = np.ones(5)
     self.assertEqual(samples.shape[0], 5)
     self.assertEqual(samples.shape[1], 100000)
     self.assertEqual(samples.shape[2], 2)
     self.assertNotEqual(samples[0, -1, -1], samples[1, -1, -1])
     samples = np.concatenate([samples[0], samples[1]], axis=1)
     self.assertTrue((samples[:, 0] >= 0).all())
     self.assertAlmostEqual(samples[:, 0].mean(),
                            sam.gammaMean(20, 40),
                            delta=.01)
     self.assertAlmostEqual(samples[:, 0].std(),
                            sam.gammaStd(20, 40),
                            delta=.01)
     self.assertAlmostEqual(samples[:, 1].mean(), 5., delta=.1)
     self.assertAlmostEqual(samples[:, 1].std(), 1., delta=.1)
     for i in range(len(a.resultsLogProb)):
         self.assertAlmostEqual(a.resultsLogProb[i],
                                logProb1(a.results[i], None, False))
Exemplo n.º 2
0
 def testAdaptiveMetropolis(self):
     a = sam.Sam(logProb4, np.ones(2))
     a.addAdaptiveMetropolis(np.array([[1, .1], [.1, 1.]]) / 2., scaling=4.)
     samples = a.run(50000, 5 * np.ones(2), 1000, showProgress=False)
     self.assertAlmostEqual(samples[:, 0].mean(), 0., delta=.1)
     self.assertAlmostEqual(samples[:, 0].std(), 1., delta=.1)
     self.assertAlmostEqual(samples[:, 1].mean(), 0., delta=.1)
     self.assertAlmostEqual(samples[:, 1].std(), 1., delta=.1)
Exemplo n.º 3
0
 def testCorrelatedMetropolis(self):
     a = sam.Sam(logProb4, np.ones(2))
     a.addMetropolis(np.array([[1, .1], [.1, 1.]]) / 2.)
     samples = a.run(100000, 5 * np.ones(2), 1000, showProgress=False)
     self.assertAlmostEqual(samples[:, 0].mean(), 0., delta=.05)
     self.assertAlmostEqual(samples[:, 0].std(), 1., delta=.1)
     self.assertAlmostEqual(samples[:, 1].mean(), 0., delta=.05)
     self.assertAlmostEqual(samples[:, 1].std(), 1., delta=.1)
Exemplo n.º 4
0
 def test1DMetropolis(self):
     a = sam.Sam(logProb2, .5, 0., 1.)
     samples = a.run(100000, 1, showProgress=False)
     self.assertGreaterEqual(a.getAcceptance()[0], 0.)
     self.assertLessEqual(a.getAcceptance()[0], 1.)
     self.assertTrue((samples >= 0).all())
     self.assertTrue((samples <= 1).all())
     self.assertAlmostEqual(samples.mean(), sam.betaMean(15, 20), delta=.01)
     self.assertAlmostEqual(samples.std(), sam.betaStd(15, 20), delta=.01)
Exemplo n.º 5
0
 def testErrorHandling(self):
     a = sam.Sam(raisesLogProb, [.5, .5], [0., -np.inf])
     self.assertIsNone(a.results)
     self.assertIsNone(a.samples)
     self.assertRaises(AssertionError, a.getStats)
     self.assertRaises(AssertionError, a.summary)
     self.assertRaises(ValueError, a.run, 1000, [.5, .5])
     self.assertRaises(AttributeError, a.gradientDescent, [.5, .5])
     self.assertRaises(ValueError, a.simulatedAnnealing, [.5, .5])
     self.assertRaises(AssertionError, a.getSampler, 2)
     self.assertRaises(OverflowError, a.getSampler, -3)
     self.assertRaises(ValueError, sam.normalCDF, 1, 0, -1)
Exemplo n.º 6
0
 def test2DHMC(self):
     a = sam.Sam(logProb1, [1, 1], lowerBounds=[0., -np.inf])
     a.addHMC(10, .1)
     samples = a.run(50000, [.5, .5], 10, showProgress=False)
     self.assertTrue((samples[:, 0] >= 0).all())
     self.assertAlmostEqual(samples[:, 0].mean(),
                            sam.gammaMean(20, 40),
                            delta=.05)
     self.assertAlmostEqual(samples[:, 0].std(),
                            sam.gammaStd(20, 40),
                            delta=.05)
     self.assertAlmostEqual(samples[:, 1].mean(), 5., delta=.2)
     self.assertAlmostEqual(samples[:, 1].std(), 1., delta=.2)
Exemplo n.º 7
0
    def testModelSelection(self):
        # This is a roundabout way to test them, but it does work

        def rightModel(x):
            return sam.normalLogPDF(x, 0, 1.)

        def wrongModel(x):
            return sam.normalLogPDF(x, 0, 2.)

        def flatPrior(x):
            return 0.

        a = sam.Sam(rightModel, .5)
        a.run(100000, .5, showProgress=False)
        b = sam.Sam(wrongModel, .5)
        b.run(100000, .5, showProgress=False)
        assert not any(np.isnan(a.resultsLogProb))
        assert not any(np.isnan(b.resultsLogProb))

        # DIC
        right = a.getDIC(flatPrior)
        wrong = b.getDIC(flatPrior)
        self.assertLessEqual(right, wrong)
        self.assertAlmostEqual(right, 3., delta=.2)
        self.assertAlmostEqual(wrong, 4.4, delta=.2)
        # AIC
        right = a.getAIC(flatPrior)
        wrong = b.getAIC(flatPrior)
        self.assertLessEqual(right, wrong)
        self.assertAlmostEqual(right, 3.837, delta=.01)
        self.assertAlmostEqual(wrong, 5.224, delta=.01)
        # BIC
        right = a.getBIC(flatPrior, 1000)
        wrong = b.getBIC(flatPrior, 1000)
        self.assertLessEqual(right, wrong)
        self.assertAlmostEqual(right, 8.74, delta=.01)
        self.assertAlmostEqual(wrong, 10.13, delta=.01)
        return
Exemplo n.º 8
0
 def testRunningStats(self):
     a = sam.Sam(logProb3, [.5, .5],
                 lowerBounds=[0., -np.inf],
                 upperBounds=[1., np.inf])
     a.addMetropolis()
     samples = a.run(100000, [.5, .5],
                     1000,
                     recordStop=0,
                     collectStats=True,
                     showProgress=False)
     self.assertEqual(samples.size, 0)
     self.assertAlmostEqual(a.getStats()[0][0],
                            sam.betaMean(20, 40),
                            delta=.01)
     self.assertAlmostEqual(a.getStats()[1][0],
                            sam.betaStd(20, 40),
                            delta=.01)
     self.assertAlmostEqual(a.getStats()[0][1], 5, delta=.1)
     self.assertAlmostEqual(a.getStats()[1][1], 1, delta=.1)
Exemplo n.º 9
0
 def test2DMetropolis(self):
     a = sam.Sam(logProb1, [.5, .5], [0., -np.inf])
     samples = a.run(100000, [.5, .5], 1000, showProgress=False)
     self.assertGreaterEqual(a.getAcceptance()[0], 0.)
     self.assertLessEqual(a.getAcceptance()[0], 1.)
     self.assertGreaterEqual(a.getAcceptance()[1], 0.)
     self.assertLessEqual(a.getAcceptance()[1], 1.)
     self.assertTrue((samples[:, 0] >= 0).all())
     self.assertAlmostEqual(samples[:, 0].mean(),
                            sam.gammaMean(20, 40),
                            delta=.01)
     self.assertAlmostEqual(samples[:, 0].std(),
                            sam.gammaStd(20, 40),
                            delta=.01)
     self.assertAlmostEqual(samples[:, 1].mean(), 5., delta=.1)
     self.assertAlmostEqual(samples[:, 1].std(), 1., delta=.1)
     for i in range(50000):
         self.assertAlmostEqual(a.samplesLogProb[i],
                                logProb1(a.samples[i], None, False))
Exemplo n.º 10
0
 def testGetCovar(self):
     a = sam.Sam(logProb4, np.ones(2))
     a.addMetropolis()
     c = a.getProposalCov()
     for i, j in zip(c.flatten(), [1, 0., 0., 1]):
         self.assertAlmostEqual(i, j)
     a.clearSamplers()
     a.addMetropolis(np.array([[1, .1], [.1, 1.]]) / 2.)
     c = a.getProposalCov(0)
     for i, j in zip(c.flatten(), np.array([1, .1, .1, 1]) / 2.):
         self.assertAlmostEqual(i, j)
     a.clearSamplers()
     a.addHMC(10, .1)
     c = a.getProposalCov()
     for i, j in zip(c.flatten(), [1, 0., 0., 1]):
         self.assertAlmostEqual(i, j)
     a.clearSamplers()
     a.addAdaptiveMetropolis(np.array([[1, .1], [.1, 1.]]) / 2.)
     c = a.getProposalCov(0)
     # The covariance output is the sample covariance, which should be 0
     for i, j in zip(c.flatten(), [0, 0, 0, 0.]):
         self.assertAlmostEqual(i, j)
Exemplo n.º 11
0
 def testExceptionsRaised(self):
     a = sam.Sam(None, np.ones(1))
     with self.assertRaises(RuntimeError):
         a(np.ones(1))
Exemplo n.º 12
0
 def test2DGradientDescent(self):
     a = sam.Sam(logProb1, [.5, .5], lowerBounds=[0., -np.inf])
     posteriorMax = a.gradientDescent([.5, .5], step=.05)
     self.assertAlmostEqual(posteriorMax[0], 19. / 40., delta=1e-4)
     self.assertAlmostEqual(posteriorMax[1], 5., delta=1e-4)
Exemplo n.º 13
0
 def testSummary(self):
     a = sam.Sam(logProb2, .5, 0., 1.)
     with self.assertRaises(AssertionError):
         a.summary()
     a.run(100000, .5, showProgress=False)
     self.assertGreaterEqual(len(a.summary(None, True)), 0)