1 #ifndef STAN_MATH_PRIM_MAT_FUNCTOR_FINITE_DIFF_HESSIAN_HPP
2 #define STAN_MATH_PRIM_MAT_FUNCTOR_FINITE_DIFF_HESSIAN_HPP
13 const Eigen::Matrix<double, Eigen::Dynamic, 1>& x,
15 const double epsilon = 1
e-03) {
19 Matrix<double, Dynamic, 1> x_temp(x);
22 x_temp(lambda) = x(lambda) + 2.0 * epsilon;
25 x_temp(lambda) = x(lambda) + -2.0 * epsilon;
28 x_temp(lambda) = x(lambda) + epsilon;
29 grad += 8.0 * f(x_temp);
31 x_temp(lambda) = x(lambda) + -epsilon;
32 grad -= 8.0 * f(x_temp);
67 const Eigen::Matrix<double, -1, 1>& x,
69 Eigen::Matrix<double, -1, 1>& grad_fx,
70 Eigen::Matrix<double, -1, -1>& hess_fx,
71 const double epsilon = 1
e-03) {
77 Matrix<double, Dynamic, 1> x_temp(x);
82 for (
int i = 0; i < d; ++i) {
83 for (
int j = i; j < d; ++j) {
84 x_temp(i) += 2.0 * epsilon;
87 x_temp(i) = x(i) + -2.0 * epsilon;
89 x_temp(i) = x(i) + epsilon;
91 x_temp(i) = x(i) + -epsilon;
93 f_diff /= 12.0 * epsilon * 12.0 * epsilon;
97 x_temp(i) = x(i) + -2.0 * epsilon;
99 x_temp(i) = x(i) + epsilon;
100 f_diff += 16.0 * f(x_temp);
101 x_temp(i) = x(i) - epsilon;
102 f_diff += 16.0 * f(x_temp);
103 f_diff /= 12 * epsilon * epsilon;
108 hess_fx(j, i) = f_diff;
109 hess_fx(i, j) = hess_fx(j, i);
static void grad(vari *vi)
Compute the gradient for all variables starting from the specified root variable implementation.
double e()
Return the base of the natural logarithm.
void finite_diff_gradient(const F &f, const Eigen::Matrix< double,-1, 1 > &x, double &fx, Eigen::Matrix< double,-1, 1 > &grad_fx, const double epsilon=1e-03)
Calculate the value and the gradient of the specified function at the specified argument using finite...
void finite_diff_hessian(const F &f, const Eigen::Matrix< double,-1, 1 > &x, double &fx, Eigen::Matrix< double,-1, 1 > &grad_fx, Eigen::Matrix< double,-1,-1 > &hess_fx, const double epsilon=1e-03)
Calculate the value and the Hessian of the specified function at the specified argument using second-...
double finite_diff_hess_helper(const F &f, const Eigen::Matrix< double, Eigen::Dynamic, 1 > &x, const int lambda, const double epsilon=1e-03)