Skip to content

add logspace_gamma to TMBΒ #354

@bbolker

Description

@bbolker

This is from glmmTMB; it's lgamma() with the argument specified on the log scale.

It may be worth looking at/migrating other functions from there (e.g. dbetabinom, dbetabinom_robust).

 namespace adaptive {
    template<class T>
    T logspace_gamma(const T &x) {
      /* Tradeoff: The smaller x the better approximation *but* the higher
         risk of psigamma() overflow */
      if (x < -150)
        return -x;
      else
        return lgamma(exp(x));
    }
  }
  TMB_BIND_ATOMIC(logspace_gamma, 1, adaptive::logspace_gamma(x[0]))
  template<class Type>
  Type logspace_gamma(Type x) {
    CppAD::vector<Type> args(2); // Last index reserved for derivative order
    args[0] = x;
    args[1] = 0;
    return logspace_gamma(args)[0];
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions