hessian.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_MIX_MAT_FUNCTOR_HESSIAN_HPP
#define STAN_MATH_MIX_MAT_FUNCTOR_HESSIAN_HPP
#include <stan/math/fwd/core.hpp>
#include <stan/math/prim/mat/fun/Eigen.hpp>
#include <stan/math/rev/core.hpp>
#include <stdexcept>
#include <vector>
namespace stan {
namespace math {
template <typename F>
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) {
H.resize(x.size(), x.size());
grad.resize(x.size());
try {
for (int i = 0; i < x.size(); ++i) {
start_nested();
Eigen::Matrix<fvar<var>, Eigen::Dynamic, 1> x_fvar(x.size());
for (int j = 0; j < x.size(); ++j)
x_fvar(j) = fvar<var>(x(j), i == j);
fvar<var> fx_fvar = f(x_fvar);
grad(i) = fx_fvar.d_.val();
if (i == 0) fx = fx_fvar.val_.val();
stan::math::grad(fx_fvar.d_.vi_);
for (int j = 0; j < x.size(); ++j)
H(i, j) = x_fvar(j).val_.adj();
recover_memory_nested();
}
} catch (const std::exception& e) {
recover_memory_nested();
throw;
}
}
// time O(N^3); space O(N^2)
template <typename T, typename F>
void
hessian(const F& f,
const Eigen::Matrix<T, Eigen::Dynamic, 1>& x,
T& fx,
Eigen::Matrix<T, Eigen::Dynamic, 1>& grad,
Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& H) {
H.resize(x.size(), x.size());
grad.resize(x.size());
Eigen::Matrix<fvar<fvar<T> >, Eigen::Dynamic, 1> x_fvar(x.size());
for (int i = 0; i < x.size(); ++i) {
for (int j = i; j < x.size(); ++j) {
for (int k = 0; k < x.size(); ++k)
x_fvar(k) = fvar<fvar<T> >(fvar<T>(x(k), j == k),
fvar<T>(i == k, 0));
fvar<fvar<T> > fx_fvar = f(x_fvar);
if (j == 0)
fx = fx_fvar.val_.val_;
if (i == j)
grad(i) = fx_fvar.d_.val_;
H(i, j) = fx_fvar.d_.d_;
H(j, i) = H(i, j);
}
}
}
}
}
#endif
stan::math::hessian
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
core.hpp
core.hpp
stan::math::fvar::d_
T d_
Definition: fvar.hpp:16
stan
Definition: log_sum_exp.hpp:8
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
stan::math::fvar::val_
T val_
Definition: fvar.hpp:15
stan::math::var::vi_
vari * vi_
Pointer to the implementation of this variable.
Definition: var.hpp:42
Eigen.hpp
stan::math::e
double e()
Return the base of the natural logarithm.
Definition: constants.hpp:94
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:20
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::var::val
double val() const
Return the value of this variable.
Definition: var.hpp:230
stan::math::fvar
Definition: fvar.hpp:14

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