예제 #1
0
 def _logp(self, x):
     """ Compute log pdf of model """
     dist = Normal(x, self.var)
     val = self.prior.logp(x)
     for i in range(len(self.data)):
         val = val + dist.logp(self.data[i])
     return val
예제 #2
0
 def _grad_logp(self, x):
     """ Computes gradient of log pdf of model """
     dist = Normal(x, self.var)
     val = self.prior.grad_logp(x)
     for i in range(len(self.data)):
         val = val - dist.grad_logp(self.data[i])
     return val
예제 #3
0
 def _pdf(self, x):
     """ Computes pdf of model """
     dist = Normal(x, self.var)
     val = self.prior.pdf(x)
     for i in range(len(self.data)):
         val = val * dist.pdf(self.data[i])
     return val
예제 #4
0
 def setUp(self):
     """ Sets up unit tests. """
     self.size = 1000
     self.threshold = 0.01
     self.data = np.random.normal(5, 2, 1000)
     self.m = 0
     self.tau = 4
     self.prior = Normal(self.m, self.tau)
     self.var = 2
예제 #5
0
 def test_nuts_sampling_normal(self):
     """ Tests NUTS sampling from Normal distribution """
     self.report('Test NUTS sampling of Normal Distribution.')
     mean = 11
     variance = 3
     dist = Normal(mean, variance)
     samples = dist.draw_samples('nuts', self.size, np.array([0.1]), 10)
     mean_r = self._compute_mean(samples)
     var_r = self._compute_variance(samples)
     self._check_sample_sizes(samples)
     assert abs(mean - mean_r) <= self.threshold
     assert abs(variance - var_r) <= self.threshold
     self.report('%s :: test result: mean=%0.3f, variance=%0.3f'\
                  %(str(dist), mean_r, var_r), 'test_result')
예제 #6
0
 def test_metropolis_normal(self):
     """ Tests metropolis sampling from Normal distribution """
     self.report('Test metropolis sampling from Normal Distribution.')
     mean = 11
     var = 3
     dist = Normal(mean, var)
     samples = dist.draw_samples('metropolis', self.size, np.array([11]))
     mean_r = self._compute_mean(samples)
     var_r = self._compute_variance(samples)
     self._check_sample_sizes(samples)
     assert abs(mean - mean_r) <= self.threshold
     assert abs(var - var_r) <= self.threshold
     self.report('%s :: test result: mean=%0.3f, variance=%0.3f'\
                  %(str(dist), mean_r, var_r), 'test_result')
예제 #7
0
 def test_rand_sampling_normal(self):
     """ Tests random sampling from Normal distribution """
     self.report('Test random sampling of Normal Distribution.')
     mean = 0
     variance = 1
     dist = Normal(mean, variance)
     samples = dist.draw_samples('random', self.size)
     mean_r = self._compute_mean(samples)
     var_r = self._compute_variance(samples)
     self._check_sample_sizes(samples)
     assert abs(mean - mean_r) <= self.threshold
     assert abs(variance - var_r) <= self.threshold
     self.report('%s :: test result: mean=%0.3f, variance=%0.3f'%\
                 (str(dist), mean_r, var_r), 'test_result')
예제 #8
0
 def test_rand_sampling(self):
     """ Tests random sampling from Joint distribution """
     self.report('Test random sampling of Joint Distribution.')
     normal_rv = Normal(0, 1)
     exp_rv = Exponential(2)
     uniform_rv = ContinuousUniform(-5, 5)
     dist = JointDistribution([normal_rv, exp_rv, uniform_rv])
     samples = dist.draw_samples('random', self.size)
     for i, rv in enumerate(dist.get_rv_s()):
         self._check_sample_sizes(samples[i])
         assert abs(rv.get_mean() -
                    self._compute_mean(samples[i])) <= self.threshold
         assert abs(rv.get_variance() -
                    self._compute_variance(samples[i])) <= self.threshold
예제 #9
0
class PosteriorTestCase(BaseTestClass):
    """ Unit test for posterior sampling with Gaussian likelihood and Gaussian prior"""
    def setUp(self):
        """ Sets up unit tests. """
        self.size = 1000
        self.threshold = 0.01
        self.data = np.random.normal(5, 2, 1000)
        self.m = 0
        self.tau = 4
        self.prior = Normal(self.m, self.tau)
        self.var = 2

    def _check_sample_sizes(self, samples):
        """ Compares the sample sizes with the size parameter"""
        assert self.size == len(samples)

    def _compute_mean(self, samples):
        """ Computes Mean """
        return np.mean(samples)

    def _compute_variance(self, samples):
        """ Computes Variance """
        return np.var(samples)

    def _pdf(self, x):
        """ Computes pdf of model """
        dist = Normal(x, self.var)
        val = self.prior.pdf(x)
        for i in range(len(self.data)):
            val = val * dist.pdf(self.data[i])
        return val

    def _logp(self, x):
        """ Compute log pdf of model """
        dist = Normal(x, self.var)
        val = self.prior.logp(x)
        for i in range(len(self.data)):
            val = val + dist.logp(self.data[i])
        return val

    def _grad_logp(self, x):
        """ Computes gradient of log pdf of model """
        dist = Normal(x, self.var)
        val = self.prior.grad_logp(x)
        for i in range(len(self.data)):
            val = val - dist.grad_logp(self.data[i])
        return val

    def test_posterior_NUTS(self):
        """ Tests posterior estimation using NUTS sampling """
        self.report(
            'Test posterior estimation with Gaussian likelihood and Gaussian prior '
            'using NUTS sampling')

        model_a = Model(self._pdf, self._logp, self._grad_logp)
        samples = model_a.draw_samples('nuts', self.size, np.array([3]), 10)

        avg = 0
        for i in range(len(self.data)):
            avg = avg + self.data[i]
        avg = avg / len(self.data)

        # Map estimation
        val = self.var / len(self.data)
        mean_a = (self.tau * avg) / (self.tau +
                                     val) + self.m * (val / (self.tau + val))
        var_a = (val * self.tau) / (self.tau + val)

        mean_r = self._compute_mean(samples)
        var_r = self._compute_variance(samples)

        self._check_sample_sizes(samples)
        assert abs(mean_a - mean_r) <= self.threshold
        assert abs(var_a - var_r) <= self.threshold
        self.report(
            'Map Estimation: mean=%0.3f, variance=%0.3f :: test result: mean=%0.3f, '
            'variance=%0.3f' % (mean_a, var_a, mean_r, var_r), 'test_result')

    def test_posterior_Slice(self):
        """ Tests posterior estimation using Slice sampling """
        self.report(
            'Test posterior estimation with Gaussian likelihood and Gaussian prior '
            'using Slice sampling')

        model_a = Model(self._pdf, self._logp, self._grad_logp)
        samples = model_a.draw_samples('slice', self.size, np.array([4.5]))

        avg = 0
        for i in range(len(self.data)):
            avg = avg + self.data[i]
        avg = avg / len(self.data)

        # Map estimation
        val = self.var / len(self.data)
        mean_a = (self.tau * avg) / (self.tau +
                                     val) + self.m * (val / (self.tau + val))
        var_a = (val * self.tau) / (self.tau + val)

        mean_r = self._compute_mean(samples)
        var_r = self._compute_variance(samples)

        self._check_sample_sizes(samples)
        assert abs(mean_a - mean_r) <= self.threshold
        assert abs(var_a - var_r) <= self.threshold
        self.report(
            'Map Estimation: mean=%0.3f, variance=%0.3f :: test result: mean=%0.3f, '
            'variance=%0.3f' % (mean_a, var_a, mean_r, var_r), 'test_result')