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

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