1 #ifndef STAN_MATH_PRIM_MAT_FUNCTOR_FINITE_DIFF_HESSIAN_HPP
2 #define STAN_MATH_PRIM_MAT_FUNCTOR_FINITE_DIFF_HESSIAN_HPP
14 const Eigen::Matrix<double, Eigen::Dynamic, 1>& x,
16 const double epsilon = 1
e-03) {
20 Matrix<double, Dynamic, 1> x_temp(x);
23 x_temp(lambda) = x(lambda) + 2.0 * epsilon;
26 x_temp(lambda) = x(lambda) + -2.0 * epsilon;
29 x_temp(lambda) = x(lambda) + epsilon;
30 grad += 8.0 * f(x_temp);
32 x_temp(lambda) = x(lambda) + -epsilon;
33 grad -= 8.0 * f(x_temp);
68 const Eigen::Matrix<double, -1, 1>& x,
70 Eigen::Matrix<double, -1, 1>& grad_fx,
71 Eigen::Matrix<double, -1, -1>& hess_fx,
72 const double epsilon = 1
e-03) {
78 Matrix<double, Dynamic, 1> x_temp(x);
83 for (
int i = 0; i < d; ++i) {
84 for (
int j = i; j < d; ++j) {
85 x_temp(i) += 2.0 * epsilon;
88 x_temp(i) = x(i) + -2.0 * epsilon;
90 x_temp(i) = x(i) + epsilon;
92 x_temp(i) = x(i) + -epsilon;
94 f_diff /= 12.0 * epsilon * 12.0 * epsilon;
98 x_temp(i) = x(i) + -2.0 * epsilon;
100 x_temp(i) = x(i) + epsilon;
101 f_diff += 16.0 * f(x_temp);
102 x_temp(i) = x(i) - epsilon;
103 f_diff += 16.0 * f(x_temp);
104 f_diff /= 12 * epsilon * epsilon;
109 hess_fx(j, i) = f_diff;
110 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)