Automatic Differentiation
 
Loading...
Searching...
No Matches
finite_diff_hessian_times_vector_auto.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_FUNCTOR_HESSIAN_TIMES_VECTOR_HPP
2#define STAN_MATH_REV_FUNCTOR_HESSIAN_TIMES_VECTOR_HPP
3
9#include <cmath>
10
11namespace stan {
12namespace math {
13namespace internal {
14
43template <typename F>
44void finite_diff_hessian_times_vector_auto(const F& f, const Eigen::VectorXd& x,
45 const Eigen::VectorXd& v, double& fx,
46 Eigen::VectorXd& hvp) {
47 fx = f(x);
48
49 double epsilon = std::sqrt(EPSILON) * (1 + x.norm()) / v.norm();
50
51 Eigen::VectorXd v_eps = epsilon * v;
52
53 int d = x.size();
54 double tmp;
55 Eigen::VectorXd grad_forward(d);
56 gradient(f, x + v_eps, tmp, grad_forward);
57
58 Eigen::VectorXd grad_backward(d);
59 gradient(f, x - v_eps, tmp, grad_backward);
60
61 hvp.resize(d);
62 hvp = (grad_forward - grad_backward) / (2 * epsilon);
63}
64} // namespace internal
65} // namespace math
66} // namespace stan
67#endif
void finite_diff_hessian_times_vector_auto(const F &f, const Eigen::VectorXd &x, const Eigen::VectorXd &v, double &fx, Eigen::VectorXd &hvp)
Calculate the value and the product of the Hessian and the specified vector of the specified function...
static constexpr double EPSILON
Smallest positive value.
Definition constants.hpp:41
void gradient(const F &f, const Eigen::Matrix< T, Eigen::Dynamic, 1 > &x, T &fx, Eigen::Matrix< T, Eigen::Dynamic, 1 > &grad_fx)
Calculate the value and the gradient of the specified function at the specified argument.
Definition gradient.hpp:40
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...