Example #1
0
    def test_dldtheta(self):
        OK = np.zeros(5)
        for i in range(5):
            self.a = 1.0 * rand()
            self.b = self.a + 10.0 * rand()
            self.mu = (self.a + 10 * rand()) / 2.
            self.s = 2.0 * rand() + 1.0
            self.p = Distributions.TruncatedGaussian({
                'a': self.a,
                'b': self.b,
                'mu': self.mu,
                'sigma': self.s
            })
            p = self.p.copy()
            p.primary = ['mu', 'sigma']
            dat = p.sample(100)

            def f(arr):
                p.array2primary(arr)
                return np.sum(p.loglik(dat))

            def df(arr):
                p.array2primary(arr)
                return np.sum(p.dldtheta(dat), axis=1)

            arr0 = p.primary2array()
            arr0 = abs(np.random.randn(len(arr0)))
            err = optimize.check_grad(f, df, arr0)
            if err < 1e-02:
                OK[i] = 1
        M = np.max(OK)
        self.assertTrue(
            M > 0.5, 'Gradient error %.4g is greater than %.4g' % (err, 1e-02))
Example #2
0
 def setUp(self):
     self.a = 1.0 * rand()
     self.b = self.a + 10.0 * rand()
     self.mu = (self.a + 10 * rand()) / 2.
     self.s = 2.0 * rand() + 1.0
     self.p = Distributions.TruncatedGaussian({
         'a': self.a,
         'b': self.b,
         'mu': self.mu,
         'sigma': self.s
     })