| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

finite_diff_hessian.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUNCTOR_FINITE_DIFF_HESSIAN_HPP
2 #define STAN_MATH_PRIM_MAT_FUNCTOR_FINITE_DIFF_HESSIAN_HPP
3
4 #include <stan/math/prim/mat/fun/Eigen.hpp>
5 #include <stan/math/prim/mat/functor/finite_diff_gradient.hpp>
6
7 namespace stan {
8 namespace math {
9
10 template <typename F>
11 double
12 finite_diff_hess_helper(const F& f,
13 const Eigen::Matrix<double, Eigen::Dynamic, 1>& x,
14 const int lambda,
15 const double epsilon = 1e-03) {
16 using Eigen::Matrix;
17 using Eigen::Dynamic;
18
19 Matrix<double, Dynamic, 1> x_temp(x);
20
21 double grad = 0.0;
22 x_temp(lambda) = x(lambda) + 2.0 * epsilon;
23 grad = -f(x_temp);
24
25 x_temp(lambda) = x(lambda) + -2.0 * epsilon;
26 grad += f(x_temp);
27
28 x_temp(lambda) = x(lambda) + epsilon;
29 grad += 8.0 * f(x_temp);
30
31 x_temp(lambda) = x(lambda) + -epsilon;
32 grad -= 8.0 * f(x_temp);
33
34 return grad;
35 }
36
64 template <typename F>
65 void
66 finite_diff_hessian(const F& f,
67 const Eigen::Matrix<double, -1, 1>& x,
68 double& fx,
69 Eigen::Matrix<double, -1, 1>& grad_fx,
70 Eigen::Matrix<double, -1, -1>& hess_fx,
71 const double epsilon = 1e-03) {
72 using Eigen::Matrix;
73 using Eigen::Dynamic;
74
75 int d = x.size();
76
77 Matrix<double, Dynamic, 1> x_temp(x);
78 hess_fx.resize(d, d);
79
80 finite_diff_gradient(f, x, fx, grad_fx);
81 double f_diff(0.0);
82 for (int i = 0; i < d; ++i) {
83 for (int j = i; j < d; ++j) {
84 x_temp(i) += 2.0 * epsilon;
85 if (i != j) {
86 f_diff = -finite_diff_hess_helper(f, x_temp, j);
87 x_temp(i) = x(i) + -2.0 * epsilon;
88 f_diff += finite_diff_hess_helper(f, x_temp, j);
89 x_temp(i) = x(i) + epsilon;
90 f_diff += 8.0 * finite_diff_hess_helper(f, x_temp, j);
91 x_temp(i) = x(i) + -epsilon;
92 f_diff -= 8.0 * finite_diff_hess_helper(f, x_temp, j);
93 f_diff /= 12.0 * epsilon * 12.0 * epsilon;
94 } else {
95 f_diff = -f(x_temp);
96 f_diff -= 30 * fx;
97 x_temp(i) = x(i) + -2.0 * epsilon;
98 f_diff -= f(x_temp);
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;
104 }
105
106 x_temp(i) = x(i);
107
108 hess_fx(j, i) = f_diff;
109 hess_fx(i, j) = hess_fx(j, i);
110 }
111 }
112 }
113 }
114 }
115 #endif
stan
Definition: log_sum_exp.hpp:8
finite_diff_gradient.hpp
stan::math::grad
static void grad(vari *vi)
Compute the gradient for all variables starting from the specified root variable implementation.
Definition: grad.hpp:30
Eigen.hpp
stan::math::e
double e()
Return the base of the natural logarithm.
Definition: constants.hpp:94
stan::math::finite_diff_gradient
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...
Definition: finite_diff_gradient.hpp:38
stan::math::finite_diff_hessian
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-...
Definition: finite_diff_hessian.hpp:66
stan::math::finite_diff_hess_helper
double finite_diff_hess_helper(const F &f, const Eigen::Matrix< double, Eigen::Dynamic, 1 > &x, const int lambda, const double epsilon=1e-03)
Definition: finite_diff_hessian.hpp:12

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