| Stan Math Library 2.6.3 probability, sampling & optimization

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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

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