Stan Math Library  2.20.0
reverse mode automatic differentiation
finite_diff_hessian_auto.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUNCTOR_FINITE_DIFF_HESSIAN_AUTO_HPP
2 #define STAN_MATH_PRIM_MAT_FUNCTOR_FINITE_DIFF_HESSIAN_AUTO_HPP
3 
8 
9 namespace stan {
10 namespace math {
11 
41 template <typename F>
42 void finite_diff_hessian_auto(const F& f, const Eigen::VectorXd& x, double& fx,
43  Eigen::VectorXd& grad_fx,
44  Eigen::MatrixXd& hess_fx) {
45  int d = x.size();
46 
47  Eigen::VectorXd x_temp(x);
48  hess_fx.resize(d, d);
49 
50  finite_diff_gradient_auto(f, x, fx, grad_fx);
51  double f_diff = 0;
52  for (int i = 0; i < d; ++i) {
53  for (int j = i; j < d; ++j) {
54  double epsilon = finite_diff_stepsize(x(i));
55  x_temp(i) += 2 * epsilon;
56  if (i != j) {
57  f_diff = -finite_diff_hessian_helper(f, x_temp, j, epsilon);
58  x_temp(i) = x(i) + -2 * epsilon;
59  f_diff += finite_diff_hessian_helper(f, x_temp, j, epsilon);
60  x_temp(i) = x(i) + epsilon;
61  f_diff += 8 * finite_diff_hessian_helper(f, x_temp, j, epsilon);
62  x_temp(i) = x(i) + -epsilon;
63  f_diff -= 8 * finite_diff_hessian_helper(f, x_temp, j, epsilon);
64  f_diff /= 12 * epsilon * 12 * epsilon;
65  } else {
66  f_diff = -f(x_temp);
67  f_diff -= 30 * fx;
68  x_temp(i) = x(i) + -2 * epsilon;
69  f_diff -= f(x_temp);
70  x_temp(i) = x(i) + epsilon;
71  f_diff += 16 * f(x_temp);
72  x_temp(i) = x(i) - epsilon;
73  f_diff += 16 * f(x_temp);
74  f_diff /= 12 * epsilon * epsilon;
75  }
76  x_temp(i) = x(i);
77  hess_fx(j, i) = f_diff;
78  hess_fx(i, j) = hess_fx(j, i);
79  }
80  }
81 }
82 } // namespace math
83 } // namespace stan
84 #endif
void finite_diff_gradient_auto(const F &f, const Eigen::VectorXd &x, double &fx, Eigen::VectorXd &grad_fx)
Calculate the value and the gradient of the specified function at the specified argument using finite...
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 second-...
double finite_diff_hessian_helper(const F &f, const Eigen::VectorXd &x, int i, double epsilon=1e-03)
Return the subcalculation required by finite_diff_hessian and finite_diff_hessian_auto.
double finite_diff_stepsize(double u)
Return the stepsize for finite difference evaluations at the specified scalar.

     [ Stan Home Page ] © 2011–2018, Stan Development Team.