1#ifndef STAN_MATH_MIX_FUNCTOR_FINITE_DIFF_GRAD_HESSIAN_HPP
2#define STAN_MATH_MIX_FUNCTOR_FINITE_DIFF_GRAD_HESSIAN_HPP
42 Eigen::MatrixXd& hess,
43 std::vector<Eigen::MatrixXd>& grad_hess_fx,
44 double epsilon = 1
e-04) {
48 Eigen::VectorXd x_temp(x);
49 Eigen::VectorXd grad_auto(d);
50 Eigen::MatrixXd hess_auto(d, d);
51 Eigen::MatrixXd hess_diff(d, d);
53 hessian(f, x, fx, grad_auto, hess);
54 for (
int i = 0; i < d; ++i) {
58 x_temp(i) = x(i) + 2.0 * epsilon;
59 hessian(f, x_temp, dummy_fx_eval, grad_auto, hess_auto);
60 hess_diff = -hess_auto;
62 x_temp(i) = x(i) + -2.0 * epsilon;
63 hessian(f, x_temp, dummy_fx_eval, grad_auto, hess_auto);
64 hess_diff += hess_auto;
66 x_temp(i) = x(i) + epsilon;
67 hessian(f, x_temp, dummy_fx_eval, grad_auto, hess_auto);
68 hess_diff += 8.0 * hess_auto;
70 x_temp(i) = x(i) + -epsilon;
71 hessian(f, x_temp, dummy_fx_eval, grad_auto, hess_auto);
72 hess_diff -= 8.0 * hess_auto;
75 hess_diff /= 12.0 * epsilon;
77 grad_hess_fx.push_back(hess_diff);
static constexpr double e()
Return the base of the natural logarithm.
void finite_diff_grad_hessian(const F &f, const Eigen::VectorXd &x, double &fx, Eigen::MatrixXd &hess, std::vector< Eigen::MatrixXd > &grad_hess_fx, double epsilon=1e-04)
Calculate the value and the gradient of the hessian of the specified function at the specified argume...
void hessian(const F &f, const Eigen::Matrix< T, Eigen::Dynamic, 1 > &x, T &fx, Eigen::Matrix< T, Eigen::Dynamic, 1 > &grad, Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &H)
Calculate the value, the gradient, and the Hessian, of the specified function at the specified argume...
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...