test_gpr.py 1.39 KB
Newer Older
Malthe Kjær Bisbo's avatar
Malthe Kjær Bisbo committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import numpy as np
import unittest
from gpr import gpr

class test_kernel(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        print('setupClass')

    @classmethod
    def tearDownClass(cls):
        print('teardownClass')

    def setUp(self):
        print('setUp')
        #self.kernel = gauss_kernel()
        self.kernel = double_gauss_kernel()

    def tearDown(self):
        print('tearDown\n')
    
    def test_jac(self):
        x1 = np.array([1.,2.,3.])
        x2 = np.array([1.5,2.,1.])
        x3 = np.array([3.,1.,1.])
        y = np.c_[x1,x2,x3].T
        np.testing.assert_almost_equal(self.kernel.kernel_jacobian(x1,x2),
                                       self.kernel.numerical_jacobian(x1,x2))
        np.testing.assert_almost_equal(self.kernel.kernel_jacobian(x1,y),
                                       self.kernel.numerical_jacobian(x1,y))
        np.testing.assert_almost_equal(self.kernel.kernel_jacobian(x2,y),
                                       self.kernel.numerical_jacobian(x2,y))

    def test_hyper_jac(self):
        x1 = np.array([1.,2.,3.])
        x2 = np.array([1.5,2.,1.])
        x3 = np.array([3.,1.,1.])
        X = np.c_[x1,x2,x3].T
        np.testing.assert_almost_equal(self.kernel.kernel_hyperparameter_gradient(X),
                                       self.kernel.numerical_hyperparameter_gradient(X))

if __name__ == '__main__':
    unittest.main()