Automatic Differentiation
 
Loading...
Searching...
No Matches

◆ finite_diff_gradient()

template<typename F >
void stan::math::finite_diff_gradient ( const F &  f,
const Eigen::VectorXd &  x,
double &  fx,
Eigen::VectorXd &  grad_fx,
double  epsilon = 1e-03 
)

Calculate the value and the gradient of the specified function at the specified argument using finite difference.

The functor must implement

double operator()(const Eigen::Matrix<double, -1, 1>&) const;

If epsilon is chosen to be near the square root of the machine precision and the input vector elements are all roughly unit scale, and if the function has reasonable limits on variation, error should be on the order of epsilon^6.

The reference for the algorithm is:


Robert de Levie. 2009. An improved numerical approximation for the first derivative. Journal of Chemical Sciences 121(5), page 3.

Evaluating this function involves 6 calls to f for each dimension of the input.

Template Parameters
FType of function
Parameters
[in]fFunction
[in]xArgument to function
[out]fxFunction applied to argument
[out]grad_fxGradient of function at argument
[in]epsilonperturbation size

Definition at line 43 of file finite_diff_gradient.hpp.