def test_hess_fun1_fd(self): for test_params in self.params: #hetrue = 0 hetrue = self.hesstrue(test_params) if hetrue is not None: #Hessian does not work for 2d return of fun fun = self.fun() #default works, epsilon 1e-6 or 1e-8 is not precise enough hefd = numdiff.approx_hess1( test_params, fun, #epsilon=1e-8, # TODO: should be kwds args=self.args) assert_almost_equal(hetrue, hefd, decimal=DEC3) #TODO: I reduced precision to DEC3 from DEC4 because of # TestDerivativeFun hefd = numdiff.approx_hess2( test_params, fun, #epsilon=1e-8, # TODO: should be kwds args=self.args) assert_almost_equal(hetrue, hefd, decimal=DEC3) hefd = numdiff.approx_hess3( test_params, fun, #epsilon=1e-8, # TODO: should be kwds args=self.args) assert_almost_equal(hetrue, hefd, decimal=DEC3)
def hessian(self, params): '''Hessian of loglikelihood calculated by numerical differentiation Note: import requires statsmodels master ''' from statsmodels.tools.numdiff import approx_hess1 # need options for hess (epsilon) return approx_hess1(params, self.loglike)
def test_hess(self): for test_params in self.params: he = self.mod.hessian(test_params) hefd = numdiff.approx_fprime_cs(test_params, self.mod.score) assert_almost_equal(he, hefd, decimal=DEC8) #NOTE: notice the accuracy below assert_almost_equal(he, hefd, decimal=7) hefd = numdiff.approx_fprime(test_params, self.mod.score, centered=True) assert_allclose(he, hefd, rtol=1e-9) hefd = numdiff.approx_fprime(test_params, self.mod.score, centered=False) assert_almost_equal(he, hefd, decimal=4) hescs = numdiff.approx_fprime_cs(test_params.ravel(), self.mod.score) assert_allclose(he, hescs, rtol=1e-13) hecs = numdiff.approx_hess_cs(test_params.ravel(), self.mod.loglike) assert_allclose(he, hecs, rtol=1e-9) #NOTE: Look at the lack of precision - default epsilon not always #best grad = self.mod.score(test_params) hecs, gradcs = numdiff.approx_hess1(test_params, self.mod.loglike, 1e-6, return_grad=True) assert_almost_equal(he, hecs, decimal=1) assert_almost_equal(grad, gradcs, decimal=1) hecs, gradcs = numdiff.approx_hess2(test_params, self.mod.loglike, 1e-4, return_grad=True) assert_almost_equal(he, hecs, decimal=3) assert_almost_equal(grad, gradcs, decimal=1) hecs = numdiff.approx_hess3(test_params, self.mod.loglike, 1e-5) assert_almost_equal(he, hecs, decimal=4)
def test_hess_fun1_fd(self): for test_params in self.params: #hetrue = 0 hetrue = self.hesstrue(test_params) if not hetrue is None: #Hessian doesn't work for 2d return of fun fun = self.fun() #default works, epsilon 1e-6 or 1e-8 is not precise enough hefd = numdiff.approx_hess1(test_params, fun, #epsilon=1e-8, # TODO: should be kwds args=self.args) assert_almost_equal(hetrue, hefd, decimal=DEC3) #TODO: I reduced precision to DEC3 from DEC4 because of # TestDerivativeFun hefd = numdiff.approx_hess2(test_params, fun, #epsilon=1e-8, # TODO: should be kwds args=self.args) assert_almost_equal(hetrue, hefd, decimal=DEC3) hefd = numdiff.approx_hess3(test_params, fun, #epsilon=1e-8, # TODO: should be kwds args=self.args) assert_almost_equal(hetrue, hefd, decimal=DEC3)
def _approx_hess1_backward(x, f, epsilon=None, args=(), kwargs=None): n = len(x) epsilon = -np.abs(_get_epsilon(x, 3, epsilon, n)) return approx_hess1(x, f, epsilon, args, kwargs, centered=False)
def _approx_hess1_backward(x, f, epsilon=None, args=(), kwargs=None): n = len(x) kwargs = {} if kwargs is None else kwargs epsilon = -np.abs(_get_epsilon(x, 3, epsilon, n)) return approx_hess1(x, f, epsilon, args, kwargs)