def test_hessian(self):
"""Tests hessian calculation
"""
indep = np.arange(10)
resp_mat = np.array([(1.02 * 2 * indep)**2 + 1 * indep -
(2 * indep)**2 - indep,
(2 * indep)**2 + 1.02 * indep -
(2 * indep)**2 - indep])
inp_mat = np.array([[0.02 * 2, 0], [0, 0.02]])
init_sens = np.linalg.lstsq(inp_mat, resp_mat)[0].T
resp_mat = np.array([(1.02**2 * 2 * indep)**2 + indep -
(1.02 * 2 * indep)**2 - indep,
(1.02 * 2 * indep)**2 + 1.02 * indep -
(1.02 * 2 * indep)**2 - indep])
inp_mat = np.array([[0.02 * 1.02 * 2, 0], [0, 0.02]])
step1_sens = np.linalg.lstsq(inp_mat, resp_mat)[0].T
resp_mat = np.array([(1.02 * 2 * indep)**2 + 1.02 * indep -
(2 * indep)**2 - 1.02 * indep,
(2 * indep)**2 + 1.02**2 * indep -
(2 * indep)**2 - 1.02 * indep])
inp_mat = np.array([[0.02 * 2, 0], [0, 0.02 * 1.02]])
step2_sens = np.linalg.lstsq(inp_mat, resp_mat)[0].T
true_hess = np.zeros((2, 10, 2))
true_hess[0, :, :] = (step1_sens - init_sens) / (0.02 * 2.0)
true_hess[1, :, :] = (step2_sens - init_sens) / (0.02 * 1.0)
hessian = self.bayes.get_hessian()
npt.assert_array_almost_equal(hessian['simple'], true_hess,
decimal=8,
err_msg='Hessian not as predicted')
评论列表
文章目录