# Problem computing the Hessian

**URL:** <https://discourse.mc-stan.org/t/problem-computing-the-hessian/4180>\
**Category:** Modeling\
**Created:** [May 13, 2018, 3:11pm UTC](https://discourse.mc-stan.org/t/problem-computing-the-hessian/4180 "2018-05-13T15:11:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![johne](https://avatars.discourse-cdn.com/v4/letter/j/f1d935/32.png) [@johne](https://discourse.mc-stan.org/u/johne)\
**Post date:** [May 13, 2018, 3:11pm UTC](https://discourse.mc-stan.org/t/problem-computing-the-hessian/4180/1 "2018-05-13T15:11:30Z")

</div>

Hi guys,

Im new to stan::math, im trying to build a stat model which requires me to calculate the hessian of some product of gaussian cdf with a dot product of param and data as a function argument.

Im testing with calculating the hessian of just one gaussian cdf e.g.: {\bigtriangledown}^2 \Phi(\vec{ \theta } \cdot \vec{x}), where \vec{\theta} is the param and \vec{x} is the data vector.

Im getting an error as shown in the pic:

 ![error](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/2/2f5ab2ee4a457cc4ee8b5ed3c56cd779dff717fe.jpg)

It says that it has no matching for call to dot product. However, the dot product with the above arugments is well defined according to the documentation:  
fvar stan::math::dot\_product ( const Eigen::Matrix\< fvar\< T \>, R1, C1 \> & v1,const Eigen::Matrix\<double, R2, C2 \> & v2 )

The code for producing this error is shown below:

#include \<Rcpp.h\>  
#include \<stan/math.hpp\>  
#include \<RcppEigen.h\>

using namespace Rcpp;  
using namespace Eigen;  
using namespace std;

typedef Eigen::Matrix \<double, Dynamic,Dynamic\> MatrixXd;  
typedef Eigen::Matrix \<double,Dynamic,1\> VectorXd;  
typedef Eigen::Matrix \<double,1,Dynamic\> RowVectorXd;

struct f {  
const Eigen::Matrix\<double, Eigen::Dynamic, 1\> y\_;  
f(const Eigen::Matrix\<double, Eigen::Dynamic, 1\>& y) : y\_(y) { }  
template   
T operator()(const Eigen::Matrix\<T, Dynamic, 1\>& theta) const {  
T ret = 0;  
ret += stan::math::Phi(stan::math::dot\_product(theta,y\_));

```
return ret;

```

}  
};

// [[Rcpp::export]]  
NumericMatrix getHess(stan::math::vector\_d v\_){

stan::math::vector\_d v = v\_;  
int len = v\_.size();

MatrixXd zero = MatrixXd::Constant(len,2,0);  
stan::math::vector\_d theta = zero;

stan::math::vector\_d grad(len);  
stan::math::matrix\_d hess = MatrixXd::Constant(len,len,0);

double fx;

stan::math::hessian(f(v),theta,fx,grad,hess);

stan::math::recover\_memory();  
stan::math::ChainableStack::memalloc\_.free\_all();  
stan::math::set\_zero\_all\_adjoints();

return wrap(hess);  
}

I have no problem with using the same functor f to calculate the gradient using stan::math::gradient() nor the numerical hessian using stan::math::finite\_diff\_hessian. What is the problem here?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [May 13, 2018, 4:07pm UTC](https://discourse.mc-stan.org/t/problem-computing-the-hessian/4180/2 "2018-05-13T16:07:57Z")

</div>

For me, it at least compiles if you start with

```
#include <Rcpp.h>
// [[Rcpp::depends(StanHeaders)]]
#include <stan/math.hpp>
#include <stan/math/mix/mat.hpp>
// [[Rcpp::depends(RcppEigen)]]
#include <RcppEigen.h>

```

and add `<typename T>` after `template` in your struct. See attachment.  
[johne.txt](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/a/a18bd9ed4289fbb73bc6e81c9980f06c5aa3e4bb.txt) (1.2 KB)
