forcedotcom / distributions

Low-level primitives for collapsed Gibbs sampling in python and C++
BSD 3-Clause "New" or "Revised" License
33 stars 25 forks source link

sample_from_likelihoods fails when sample_unif01 returns exactly 1 #89

Closed dpfau closed 9 years ago

dpfau commented 9 years ago

Really this is an issue with std::uniform_real_distribution, but until that's fixed we should make sure this code is robust to its weird behavior. Very rarely (say one in 10^7 samples) std::uniform_real_distribution (0.0, 1.0) returns exactly 1.0, meaning sample_unif01 returns exactly 1.0. If the vector of likelihoods passed to sample_from_likelihoods ends with zero, then sample_from_likelihoods will return the length of the likelihood array, rather than the last nonzero element of the likelihood array. This can be fixed by replacing if (DIST_UNLIKELY(t < 0)) { on line 325 of distributions/random.hpp with if (DIST_UNLIKELY(t <= 0)) {

fritzo commented 9 years ago

fixed by https://github.com/forcedotcom/distributions/pull/90