def testIsotropicGaussianKernelHyperparameterLearning(self): hyper = array([1.5, 1.1]) gkernel = SVGaussianKernel_iso(hyper) # marginal likelihood and likelihood gradient # # in MATLAB: # [nlml dnlml] = gpr(log([1.5, 1.1])', 'covSEiso', # [.5, .1, .3; .9, 1.2, .1; .55, .234, .1; .234, .547, .675] , # [.5, 1, .5, 2]') margl, marglderiv = marginalLikelihood(gkernel, self.X, self.Y, len(hyper), True) self.assertAlmostEqual(margl, 7.514, 2) for v, t in zip(marglderiv, [11.4659, -10.0714]): self.assertAlmostEqual(v, t, 2) # compare partial derivatives with result from Rasmussen's code target0 = matrix('[0 .5543 .0321 .2018; .5543 0 .449 .4945; .0321 .449 0 .2527; .2018 .4945 .2527 0]') target1 = matrix('[2.42 1.769 2.3877 2.2087; 1.769 2.42 1.914 1.8533; 2.3877 1.914 2.42 2.1519; 2.2087 1.8533 2.1519 2.42]') pder0 = gkernel.derivative(self.X, 0) pder1 = gkernel.derivative(self.X, 1) for i, (target, pder) in enumerate([(target0, pder0), (target1, pder1)]): for j in xrange(4): self.assertAlmostEqual(target[i,j], pder[i,j], 2) # optimize the marginal likelihood over the log hyperparameters # using BFGS argmin = optimize.fmin_bfgs(nlml, log(hyper), dnlml, args=[SVGaussianKernel_iso, self.X, self.Y], disp=False) for d, t in zip(argmin, [-0.0893, 0.29]): self.assertAlmostEqual(d, t, 2)
def testARDGaussianKernelHyperparameterLearning(self): hyper = array([2., 2., .1]) # test derivatives target0 = matrix('[0 .0046 .0001 0; .0046 0 .0268 0; .0001 .0268 0 0; 0 0 0 0]') target1 = matrix('[0 .0345 .0006 0; .0345 0 .2044 0; .0006 .2044 0 0; 0 0 0 0]') target2 = matrix('[0 .4561 .54 .012; .4561 0 0 0; .54 .0 0 0; .012 0 0 0]') target3 = matrix('[2 .2281 .27 .0017; .2281 2 1.7528 0; .27 1.7528 2 0; .0017 0 0 2]') pder0 = GaussianKernel_ard(hyper).derivative(self.X, 0) pder1 = GaussianKernel_ard(hyper).derivative(self.X, 1) pder2 = GaussianKernel_ard(hyper).derivative(self.X, 2) # pder3 = GaussianKernel_ard(hyper).derivative(self.X, 3) epsilon = .0001 for i, (target, pder) in enumerate([(target0, pder0), (target1, pder1), (target2, pder2)]): for j in xrange(4): for k in xrange(4): if abs(target[j, k]-pder[j, k]) > epsilon: print '\nelement [%d, %d] of pder%d differs from expected by > %f' % (j, k, i, epsilon) print '\ntarget:' print target print '\npder:' print pder assert False # marginal likelihood and likelihood gradient gkernel = GaussianKernel_ard(hyper) margl, marglderiv = marginalLikelihood(gkernel, self.X, self.Y, len(hyper), useCholesky=True) self.assertAlmostEqual(margl, 5.8404, 2) for d, t in zip(marglderiv, [0.0039, 0.0302, -0.1733, -1.8089]): self.assertAlmostEqual(d, t, 2) # make sure we're getting the same results for inversion and cholesky imargl, imarglderiv = marginalLikelihood(gkernel, self.X, self.Y, len(hyper), useCholesky=False) self.assertAlmostEqual(margl, imargl, 2) for c, i in zip(marglderiv, imarglderiv): self.assertAlmostEqual(c, i, 2) # optimize the marginal likelihood over log hyperparameters using BFGS hyper = array([2., 2., .1, 1.]) argmin = optimize.fmin_bfgs(nlml, log(hyper), dnlml, args=[SVGaussianKernel_ard, self.X, self.Y], disp=False) for v, t in zip(argmin, [6.9714, 0.95405, -0.9769, 0.36469]): self.assertAlmostEqual(v, t, 2)
def testMaternKernelHyperparameterLearning(self): # we'll just confirm the likelihood & likelihood gradient, rather than # all the intermediate steps. you can use the Gaussian kernel examples # if you want to figure out the partial derivatives. hyper = array([1.5, 1.1]) mkernel3 = MaternKernel3(hyper) margl3, marglderiv3 = marginalLikelihood(mkernel3, self.X, self.Y, len(hyper)) self.assertAlmostEqual(margl3, 5.1827, 2) self.assertAlmostEqual(marglderiv3[0], 1.6947897766, 2) self.assertAlmostEqual(marglderiv3[1], -2.9350, 2)
def testMaternKernelHyperparameterLearning(self): # we'll just confirm the likelihood & likelihood gradient, rather than # all the intermediate steps. you can use the Gaussian kernel examples # if you want to figure out the partial derivatives. hyper = array([1.5, 1.1]) mkernel3 = MaternKernel3(hyper) margl3, marglderiv3 = marginalLikelihood(mkernel3, self.X, self.Y, len(hyper)) self.assertAlmostEqual(margl3, 5.1827, 2) self.assertAlmostEqual(marglderiv3[0], 1.6947897766, 2) self.assertAlmostEqual(marglderiv3[1], -2.9350, 2)
def testIsotropicGaussianKernelHyperparameterLearning(self): hyper = array([1.5, 1.1]) gkernel = SVGaussianKernel_iso(hyper) # marginal likelihood and likelihood gradient # # in MATLAB: # [nlml dnlml] = gpr(log([1.5, 1.1])', 'covSEiso', # [.5, .1, .3; .9, 1.2, .1; .55, .234, .1; .234, .547, .675] , # [.5, 1, .5, 2]') margl, marglderiv = marginalLikelihood(gkernel, self.X, self.Y, len(hyper), True) self.assertAlmostEqual(margl, 7.514, 2) for v, t in zip(marglderiv, [11.4659, -10.0714]): self.assertAlmostEqual(v, t, 2) # compare partial derivatives with result from Rasmussen's code target0 = matrix( '[0 .5543 .0321 .2018; .5543 0 .449 .4945; .0321 .449 0 .2527; .2018 .4945 .2527 0]' ) target1 = matrix( '[2.42 1.769 2.3877 2.2087; 1.769 2.42 1.914 1.8533; 2.3877 1.914 2.42 2.1519; 2.2087 1.8533 2.1519 2.42]' ) pder0 = gkernel.derivative(self.X, 0) pder1 = gkernel.derivative(self.X, 1) for i, (target, pder) in enumerate([(target0, pder0), (target1, pder1)]): for j in xrange(4): self.assertAlmostEqual(target[i, j], pder[i, j], 2) # optimize the marginal likelihood over the log hyperparameters # using BFGS argmin = optimize.fmin_bfgs( nlml, log(hyper), dnlml, args=[SVGaussianKernel_iso, self.X, self.Y], disp=False) for d, t in zip(argmin, [-0.0893, 0.29]): self.assertAlmostEqual(d, t, 2)
def testARDGaussianKernelHyperparameterLearning(self): hyper = array([2., 2., .1]) # test derivatives target0 = matrix( '[0 .0046 .0001 0; .0046 0 .0268 0; .0001 .0268 0 0; 0 0 0 0]') target1 = matrix( '[0 .0345 .0006 0; .0345 0 .2044 0; .0006 .2044 0 0; 0 0 0 0]') target2 = matrix( '[0 .4561 .54 .012; .4561 0 0 0; .54 .0 0 0; .012 0 0 0]') target3 = matrix( '[2 .2281 .27 .0017; .2281 2 1.7528 0; .27 1.7528 2 0; .0017 0 0 2]' ) pder0 = GaussianKernel_ard(hyper).derivative(self.X, 0) pder1 = GaussianKernel_ard(hyper).derivative(self.X, 1) pder2 = GaussianKernel_ard(hyper).derivative(self.X, 2) # pder3 = GaussianKernel_ard(hyper).derivative(self.X, 3) epsilon = .0001 for i, (target, pder) in enumerate([(target0, pder0), (target1, pder1), (target2, pder2)]): for j in xrange(4): for k in xrange(4): if abs(target[j, k] - pder[j, k]) > epsilon: print '\nelement [%d, %d] of pder%d differs from expected by > %f' % ( j, k, i, epsilon) print '\ntarget:' print target print '\npder:' print pder assert False # marginal likelihood and likelihood gradient gkernel = GaussianKernel_ard(hyper) margl, marglderiv = marginalLikelihood(gkernel, self.X, self.Y, len(hyper), useCholesky=True) self.assertAlmostEqual(margl, 5.8404, 2) for d, t in zip(marglderiv, [0.0039, 0.0302, -0.1733, -1.8089]): self.assertAlmostEqual(d, t, 2) # make sure we're getting the same results for inversion and cholesky imargl, imarglderiv = marginalLikelihood(gkernel, self.X, self.Y, len(hyper), useCholesky=False) self.assertAlmostEqual(margl, imargl, 2) for c, i in zip(marglderiv, imarglderiv): self.assertAlmostEqual(c, i, 2) # optimize the marginal likelihood over log hyperparameters using BFGS hyper = array([2., 2., .1, 1.]) argmin = optimize.fmin_bfgs( nlml, log(hyper), dnlml, args=[SVGaussianKernel_ard, self.X, self.Y], disp=False) for v, t in zip(argmin, [6.9714, 0.95405, -0.9769, 0.36469]): self.assertAlmostEqual(v, t, 2)