1#ifndef STAN_MATH_REV_FUNCTOR_HESSIAN_HPP
2#define STAN_MATH_REV_FUNCTOR_HESSIAN_HPP
44 Eigen::VectorXd& grad_fx,
45 Eigen::MatrixXd& hess_fx) {
48 Eigen::VectorXd x_temp(x);
53 std::vector<Eigen::VectorXd> g_plus(d);
54 std::vector<Eigen::VectorXd> g_minus(d);
55 std::vector<double> epsilons(d);
61 for (
size_t i = 0; i < d; ++i) {
62 Eigen::VectorXd x_temp(x);
64 x_temp(i) += epsilons[i];
69 for (
size_t i = 0; i < d; ++i) {
70 Eigen::VectorXd x_temp(x);
71 x_temp(i) -= epsilons[i];
72 gradient(f, x_temp, tmp, g_minus[i]);
75 for (
int i = 0; i < d; ++i) {
76 for (
int j = i; j < d; ++j) {
77 hess_fx(j, i) = (g_plus[j](i) - g_minus[j](i)) / (4 * epsilons[j])
78 + (g_plus[i](j) - g_minus[i](j)) / (4 * epsilons[i]);
79 hess_fx(i, j) = hess_fx(j, i);
void finite_diff_hessian_auto(const F &f, const Eigen::VectorXd &x, double &fx, Eigen::VectorXd &grad_fx, Eigen::MatrixXd &hess_fx)
Calculate the value and the Hessian of the specified function at the specified argument using first-o...
void gradient(const F &f, const Eigen::Matrix< T, Eigen::Dynamic, 1 > &x, T &fx, Eigen::Matrix< T, Eigen::Dynamic, 1 > &grad_fx)
Calculate the value and the gradient of the specified function at the specified argument.
double finite_diff_stepsize(double u)
Return the stepsize for finite difference evaluations at the specified scalar.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...