| 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
hessian.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_MIX_MAT_FUNCTOR_HESSIAN_HPP
2 #define STAN_MATH_MIX_MAT_FUNCTOR_HESSIAN_HPP
3
4 #include <stan/math/fwd/core.hpp>
5 #include <stan/math/prim/mat/fun/Eigen.hpp>
6 #include <stan/math/rev/core.hpp>
7 #include <vector>
8
9 namespace stan {
10
11 namespace math {
12
13 using Eigen::Dynamic;
14
44 template <typename F>
45 void
46 hessian(const F& f,
47 const Eigen::Matrix<double, Dynamic, 1>& x,
48 double& fx,
49 Eigen::Matrix<double, Dynamic, 1>& grad,
50 Eigen::Matrix<double, Dynamic, Dynamic>& H) {
51 H.resize(x.size(), x.size());
52 grad.resize(x.size());
53 try {
54 for (int i = 0; i < x.size(); ++i) {
55 start_nested();
56 Eigen::Matrix<fvar<var>, Dynamic, 1> x_fvar(x.size());
57 for (int j = 0; j < x.size(); ++j)
58 x_fvar(j) = fvar<var>(x(j), i == j);
59 fvar<var> fx_fvar = f(x_fvar);
60 grad(i) = fx_fvar.d_.val();
61 if (i == 0) fx = fx_fvar.val_.val();
62 stan::math::grad(fx_fvar.d_.vi_);
63 for (int j = 0; j < x.size(); ++j)
64 H(i, j) = x_fvar(j).val_.adj();
65 stan::math::recover_memory_nested();
66 }
67 } catch (const std::exception& e) {
68 stan::math::recover_memory_nested();
69 throw;
70 }
71 }
72 // time O(N^3); space O(N^2)
73 template <typename T, typename F>
74 void
75 hessian(const F& f,
76 const Eigen::Matrix<T, Dynamic, 1>& x,
77 T& fx,
78 Eigen::Matrix<T, Dynamic, 1>& grad,
79 Eigen::Matrix<T, Dynamic, Dynamic>& H) {
80 H.resize(x.size(), x.size());
81 grad.resize(x.size());
82 Eigen::Matrix<fvar<fvar<T> >, Dynamic, 1> x_fvar(x.size());
83 for (int i = 0; i < x.size(); ++i) {
84 for (int j = i; j < x.size(); ++j) {
85 for (int k = 0; k < x.size(); ++k)
86 x_fvar(k) = fvar<fvar<T> >(fvar<T>(x(k), j == k),
87 fvar<T>(i == k, 0));
88 fvar<fvar<T> > fx_fvar = f(x_fvar);
89 if (j == 0)
90 fx = fx_fvar.val_.val_;
91 if (i == j)
92 grad(i) = fx_fvar.d_.val_;
93 H(i, j) = fx_fvar.d_.d_;
94 H(j, i) = H(i, j);
95 }
96 }
97 }
98
99 } // namespace math
100 } // namespace stan
101 #endif
core.hpp
core.hpp
stan::math::fvar::d_
T d_
Definition: fvar.hpp:15
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
stan::math::fvar::val_
T val_
Definition: fvar.hpp:14
stan::math::var::vi_
vari * vi_
Pointer to the implementation of this variable.
Definition: var.hpp:44
Eigen.hpp
stan::math::e
double e()
Return the base of the natural logarithm.
Definition: constants.hpp:95
stan::math::hessian
void hessian(const F &f, const Eigen::Matrix< double, Dynamic, 1 > &x, double &fx, Eigen::Matrix< double, Dynamic, 1 > &grad, Eigen::Matrix< double, Dynamic, Dynamic > &H)
Calculate the value, the gradient, and the Hessian, of the specified function at the specified argume...
Definition: hessian.hpp:46
stan::math::recover_memory_nested
static void recover_memory_nested()
Recover only the memory used for the top nested call.
Definition: recover_memory_nested.hpp:21
stan::math::var::val
double val() const
Return the value of this variable.
Definition: var.hpp:234
stan::math::start_nested
static void start_nested()
Record the current position so that recover_memory_nested() can find it.
Definition: start_nested.hpp:13
stan::math::fvar
Definition: fvar.hpp:13

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