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>
45 const Eigen::VectorXd& x,
46 const Eigen::VectorXd& v,
47 double& fx,
48 Eigen::VectorXd& hvp) {
49 fx = f(x);
50
51 double epsilon = std::sqrt(EPSILON) * (1 + x.norm()) / v.norm();
52
53 Eigen::VectorXd v_eps = epsilon * v;
54
55 int d = x.size();
56 double tmp;
57 Eigen::VectorXd grad_forward(d);
58 gradient(f, x + v_eps, tmp, grad_forward);
59
60 Eigen::VectorXd grad_backward(d);
61 gradient(f, x - v_eps, tmp, grad_backward);
62
63 hvp.resize(d);
64 hvp = (grad_forward - grad_backward) / (2 * epsilon);
65}
66} // namespace internal
67} // namespace math
68} // namespace stan
69#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 ...