b-steve / acre

Acoustic spatial capture-recapture models
GNU General Public License v3.0
3 stars 1 forks source link

Replace existing bessel function with the in-built `besselI()` TMB function #14

Closed b-steve closed 2 months ago

b-steve commented 10 months ago

At the moment we use our own bessel function (top of acreTMB.h), which is used within the von-Mises PDF for observed bearings. My recollection is that models with observed bearings are surprisingly slow to fit, and I suspect the bessel function is the culprit.

TMB now has an in-built function besselI(). It would be worth redefining our von-Mises PDF to use this in-built function and compare model-fitting speed.

b-steve commented 10 months ago

Here's a TMB function for the von-Mises PDF I wrote for a different project, with output that matches CircStats::dvm():

template<class Type>
Type dvm_stable(const Type &theta, const Type &mu, const Type &kappa, const int &give_log){
  Type out;
  out = kappa*(cos(theta - mu) - 1) - log(2*M_PI) - log(besselI(kappa, Type(0))/exp(kappa));
  if (give_log == 0){
    out = exp(out);
  }
  return out;
}
JosephReps commented 2 months ago

@b-steve If I recall correctly, we compared these and it ended up being slower when using the TMB besselI().

Can run some tests again if needed, otherwise I think we can close this issue.

EDIT: I think we came to the conclusion that the TMB built-in was likely slower due to it being a more accurate approximation, however our less-accurate approximation works just fine.