Function hypergeometric_lpmf() considers the case n=N as invalid

Hi, I’ve been having trouble with a hypergeometric model, and I narrowed the problem down to this:

The function hypergeometric_lpmf(int n | int N, int a, int b) stops when n=N claiming that it should be n<N strictly. However, the case of sampling n=N successes in N trials should be valid.

Reproducible Steps:

library(rstan)
code <- "
  functions {
   real log_dhyper(int n, int N, int a, int b) {
      real lp;
      lp = hypergeometric_lpmf(n | N, a, b);
      return lp;
    }
  }
"
expose_stan_functions(stanc(model_code = code))
log_dhyper(10, 10, 50, 50)
#> Error in log_dhyper(10, 10, 50, 50): Exception: hypergeometric_lpmf: Draws parameter is 10, but must be greater than 10  (in 'unknown file name' at line 5)

Created on 2018-10-17 by the reprex package (v0.2.1)

I opened an issue in GH. But then I saw that you are using the forums to report possible bugs. I’m sorry, I don’t know which venue is more convenient for you.

GitHub is great for issues. We’d rather have people ask here first if they’re unsure about whether what they’re reporting is an issue or a question. We’ll fix this one—thanks.

I’m wondering if this bug was solved? I updated to rstan 2.19.2, but the reproducible steps still output an error as:

Error in log_dhyper(10, 10, 50, 50) :
Exception: hypergeometric_lpmf: Draws parameter is 10, but must be greater than 10 (in ‘unknown file name’ at line 5)

From the github activity, I gather that the issue is still open unfortunately.

1 Like

Thanks for replying! For anyone who has the same issue, the way I dealt with this is using lchoose().

hypergeometric_lpmf(n | N, a, b) is the same as lchoose(a, n) + lchoose(b, N-n) - lchoose(a+b, N)

2 Likes

This bug has been fixed, so a working version of hypergeometric_lpmf for n=N will appear in the next release.

5 Likes