How to use a TMB distribution in a STAN code?

How could I use a distribution (pdf) or function of this distribution (fda) from TMB (TMB Documentation: R style probability distributions) in code in stan? For example, how would I call the function:

Type dbinom_robust (Type k,
type size,
Type logit_p,
int give_log = 0
)

inside the Stan code?

There’s no easy way to call external code from within a Stan program. But let’s look at the function and see if we can reimplement. The doc for dbinom_robust(k, size, logit_p) says:

Density of binomial distribution parameterized via logit(prob)
This version should be preferred when working on the logit scale as it is numerically stable for probabilities close to 0 or 1.

We’re in luck. This is already impelemented as binomial_logit_lpmf(k | size, logit_p), which returns the log of the probability mass function with the probability on the log odds (logit) scale. You can call it like this in a Stan program:

k ~ binomial_logit(size, logit_p)

For the full doc, see: 16.2 Binomial distribution, logit parameterization | Stan Functions Reference

In general, we suffix distributions with the scale of the parameter when it’s non-standard.

I found the name in TMB to be confusing because it is meant to indicate arithmetic stability, not statistical robustness.

1 Like