What’s the function signature need to look like for the first argument of stan::math::hessian?
I have a signature like:
template <typename T_x>
inline const return_type_t<T_x>
f2(const std::vector<T_x> &x)
and the compiler doesn’t like the first argument, gives: <unresolved overloaded function type>
.
How should I define const F &
, the first argument for stan::math::hessian?
Thanks!
This depends whether you are including math/fwd/functor/hessian.hpp
or math/mix/functor/hessian.hpp
, but both of their requirements are documented in the doxygen comment on the function.
For the mix method, you need a function which satisfies:
fvar<var> operator()(const Eigen::Matrix<fvar<var>, Eigen::Dynamic, 1>&)
I think your f2
would work if changed from using std::vectors to Eigen vectors
2 Likes
Not working, additional advice?
I’m looking in the test cases, there’s probably some instantiation there, or at least there should be.
Yeah there’s no tests for this?
develop/test/unit/math/mix/functor
The Stan Math Library is a C++ template library for automatic differentiation of any order using forward, reverse, and mixed modes. It includes a range of built-in functions for probabilistic mode...
The general autodiff framework uses the hessian
function and tests it against finite differences for nearly every function in the Math library:
* documentation for `expect_near`.
*
* @tparam F type of functor
* @param tols tolerances for test
* @param f functor to test
* @param x value to test
* @param fx expected value
* @param test_derivs `true` if derivatives should be tested
*/
template <typename F>
void test_hessian(const ad_tolerances& tols, const F& f,
const Eigen::VectorXd& x, double fx,
bool test_derivs = true) {
double fx_ad;
Eigen::VectorXd grad_ad;
Eigen::MatrixXd H_ad;
stan::math::hessian<F>(f, x, fx_ad, grad_ad, H_ad);
expect_near_rel("hessian val", fx, fx_ad, tols.hessian_val_);
if (!test_derivs || !is_finite(x) || !is_finite(fx))
return;
double fx_fd;
Not quite a unit test for the function itself, but if it was wrong this would certainly notice
1 Like
There’s also an example in the StanHeaders vignette: Using the Stan Math C++ Library
Under the Higher-order functions section
1 Like