3030#include < boost/math/constants/constants.hpp>
3131#include < boost/math/distributions/normal.hpp>
3232#include < boost/math/special_functions/digamma.hpp>
33- #include < boost/math/special_functions/gamma.hpp>
3433
3534#include < algorithm>
3635#include < cmath>
@@ -285,6 +284,7 @@ void BICGain(maths_t::EDataType dataType,
285284 double mr = mean (dataType, mvr);
286285 double vr = std::max (variance (dataType, mvr), vmin);
287286
287+ bool haveGamma = distributions.haveGamma ();
288288 try {
289289 // Mixture of log-normals (method of moments)
290290 double sl = std::log (1.0 + vl / CTools::pow2 (ml + logNormalOffset));
@@ -299,7 +299,21 @@ void BICGain(maths_t::EDataType dataType,
299299
300300 double log2piv = std::log (boost::math::double_constants::two_pi * v);
301301 double log2pis = std::log (boost::math::double_constants::two_pi * s);
302- double loggn = boost::math::lgamma (a) - a * std::log (b);
302+
303+ double loggn = 0.0 ;
304+ double loggnl = 0.0 ;
305+ double loggnr = 0.0 ;
306+
307+ if (haveGamma && maths::CTools::lgamma (a, loggn, true ) &&
308+ maths::CTools::lgamma (al, loggnl, true ) &&
309+ maths::CTools::lgamma (ar, loggnr, true )) {
310+ loggn -= a * std::log (b);
311+ loggnl -= al * std::log (bl) + std::log (wl);
312+ loggnr -= ar * std::log (br) + std::log (wr);
313+ } else {
314+ haveGamma = false ;
315+ }
316+
303317 double log2pivl =
304318 std::log (boost::math::double_constants::two_pi * vl / CTools::pow2 (wl));
305319 double log2pivr =
@@ -308,8 +322,6 @@ void BICGain(maths_t::EDataType dataType,
308322 std::log (boost::math::double_constants::two_pi * sl / CTools::pow2 (wl));
309323 double log2pisr =
310324 std::log (boost::math::double_constants::two_pi * sr / CTools::pow2 (wr));
311- double loggnl = boost::math::lgamma (al) - al * std::log (bl) - std::log (wl);
312- double loggnr = boost::math::lgamma (ar) - ar * std::log (br) - std::log (wr);
313325
314326 for (std::size_t i = start; i < split; ++i) {
315327 double ni = CBasicStatistics::count (categories[i]);
@@ -372,15 +384,15 @@ void BICGain(maths_t::EDataType dataType,
372384 double ll1 =
373385 min (distributions.haveNormal () ? ll1n : boost::numeric::bounds<double >::highest (),
374386 distributions.haveLogNormal () ? ll1l : boost::numeric::bounds<double >::highest (),
375- distributions. haveGamma () ? ll1g : boost::numeric::bounds<double >::highest ()) +
387+ haveGamma ? ll1g : boost::numeric::bounds<double >::highest ()) +
376388 distributions.parameters () * logn;
377389 double ll2 =
378390 min (distributions.haveNormal () ? ll2nl : boost::numeric::bounds<double >::highest (),
379391 distributions.haveLogNormal () ? ll2ll : boost::numeric::bounds<double >::highest (),
380- distributions. haveGamma () ? ll2gl : boost::numeric::bounds<double >::highest ()) +
392+ haveGamma ? ll2gl : boost::numeric::bounds<double >::highest ()) +
381393 min (distributions.haveNormal () ? ll2nr : boost::numeric::bounds<double >::highest (),
382394 distributions.haveLogNormal () ? ll2lr : boost::numeric::bounds<double >::highest (),
383- distributions. haveGamma () ? ll2gr : boost::numeric::bounds<double >::highest ()) +
395+ haveGamma ? ll2gr : boost::numeric::bounds<double >::highest ()) +
384396 (2.0 * distributions.parameters () + 1.0 ) * logn;
385397
386398 LOG_TRACE (<< " BIC(1) = " << ll1 << " , BIC(2) = " << ll2);
0 commit comments