Two quick questions on "dbern"

223 views
Skip to first unread message

Luca

unread,
Nov 29, 2021, 3:33:57 AM11/29/21
to nimble-users
Dear Nimble devs and users,

Two quick questions on the Bernoulli distribution "dbern"

1. The manual and cheatsheet say that "dbern(prob)" is available. However, if at the command prompt I try (after loading Nimble)
> dbern(1, prob=0.5)
I get an error message: 'Error in dbern(1, prob = 0.5) : could not find function "dbern"'. Is this distribution only available within Nimble models?


2. (If "dbern" exists) Is there any computational advantage in using dbern(prob=p) over dcat(prob=c(1-p,p))? Will conjugaty with the Beta be recognized for dcat? Or should I then use a Dirichlet (with 2-dimensional alpha) instead of Beta? Would this be computationally more expensive then?

Cheers!
Luca

Luca

unread,
Nov 29, 2021, 6:43:20 AM11/29/21
to nimble-users
I'm sorry for asking those questions without exploring possible answers by myself first. I've done that now, and below are my own answers, maybe of use to other Nimble users too. Please let me know if I'm wrong on any of them:


1. The Bernoulli "dbern" is available within nimbleCode, but not outside, eg at the command prompt.


2. One can use (a) dbern and the conjugate dbeta, or *equivalently in theory* (b) dcat and the conjugate ddirch (in two dimensions). The two implementations are different though, because (a) works with one parameter p, whereas (b) works with two correlated parameters p[1:2] satistfying sum(p)=1.

For this reason it is not really possible to use dcat as the likelihood and dbeta as the conjugate prior, since one would have to change a 1D parameter into a 2D one in between.

Implementation (b) proved to be slower, taking roughly 135% of the time of implementation (a).

Cheers!
Luca

Adam Smith

unread,
Dec 8, 2025, 12:34:00 PM (6 days ago) Dec 8
to nimble-users
Just wanted to add that you can also get the same error if you specify the result of a call to dbern() as a deterministic node; i.e., y[i] <- dbern(psi[i]), as opposed to  declaring it as a stochastic node, y[i] ~ dbern(psi[i]).

Chris Paciorek

unread,
Dec 8, 2025, 7:48:57 PM (6 days ago) Dec 8
to Adam Smith, nimble-users
Yes, all of these comments look correct.

`dbern` is of course equivalent to `dbinom` with `size=1`. I believe we provided `dbern` for stochastic model declarations as a helpful alternative and to match BUGS and JAGS. But we didn't extend that support for other uses given one could use `dbinom`. The set of distributions allowed in nimbleFunction code is here.

As far as conjugacy, we basically have a lookup table of conjugate pairs and we don't handle mixing and matching dcat+dbeta, in part because the checking for conjugacy is more complicated for that.

-chris

--
You received this message because you are subscribed to the Google Groups "nimble-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nimble-users...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/nimble-users/e7505b0c-6323-4118-8b69-5b517ee84d6fn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages