finite_diff_grad_hessian.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_MIX_MAT_FUNCTOR_FINITE_DIFF_GRAD_HESSIAN_HPP
#define STAN_MATH_MIX_MAT_FUNCTOR_FINITE_DIFF_GRAD_HESSIAN_HPP
#include <stan/math/prim/mat/fun/Eigen.hpp>
#include <stan/math/rev/core.hpp>
#include <stan/math/mix/mat/functor/hessian.hpp>
#include <vector>
namespace stan {
namespace math {
template <typename F>
void
finite_diff_grad_hessian(const F& f,
const Eigen::Matrix<double, -1, 1>& x,
double& fx,
Eigen::Matrix<double, -1, -1>& hess,
std::vector<Eigen::Matrix<double, -1, -1> >&
grad_hess_fx,
double epsilon = 1e-04) {
using Eigen::Matrix;
using Eigen::Dynamic;
int d = x.size();
double dummy_fx_eval;
Matrix<double, Dynamic, 1> x_temp(x);
Matrix<double, Dynamic, 1> grad_auto(d);
Matrix<double, Dynamic, Dynamic> hess_auto(d, d);
Matrix<double, Dynamic, Dynamic> hess_diff(d, d);
hessian(f, x, fx, grad_auto, hess);
for (int i = 0; i < d; ++i) {
hess_diff.setZero();
x_temp(i) = x(i) + 2.0 * epsilon;
hessian(f, x_temp, dummy_fx_eval, grad_auto, hess_auto);
hess_diff = -hess_auto;
x_temp(i) = x(i) + -2.0 * epsilon;
hessian(f, x_temp, dummy_fx_eval, grad_auto, hess_auto);
hess_diff += hess_auto;
x_temp(i) = x(i) + epsilon;
hessian(f, x_temp, dummy_fx_eval, grad_auto, hess_auto);
hess_diff += 8.0 * hess_auto;
x_temp(i) = x(i) + -epsilon;
hessian(f, x_temp, dummy_fx_eval, grad_auto, hess_auto);
hess_diff -= 8.0 * hess_auto;
x_temp(i) = x(i);
hess_diff /= 12.0 * epsilon;
grad_hess_fx.push_back(hess_diff);
}
fx = f(x);
}
}
}
#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
stan::math::finite_diff_grad_hessian
void finite_diff_grad_hessian(const F &f, const Eigen::Matrix< double, -1, 1 > &x, double &fx, Eigen::Matrix< double, -1, -1 > &hess, std::vector< Eigen::Matrix< double, -1, -1 > > &grad_hess_fx, double epsilon=1e-04)
Calculate the value and the gradient of the hessian of the specified function at the specified argume...
Definition: finite_diff_grad_hessian.hpp:42
stan
Definition: log_sum_exp.hpp:8
hessian.hpp
Eigen.hpp
stan::math::e
double e()
Return the base of the natural logarithm.
Definition: constants.hpp:94

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