stats icon indicating copy to clipboard operation
stats copied to clipboard

qinvgamma returns nan

Open j-faria opened this issue 4 years ago • 1 comments

For some inputs (apparently close to 1), qinvgamma is returning nan. Example:

stats::qinvgamma(0.999551553841898, 2.0, 1.0)

Probably the result of overflow?

j-faria avatar Oct 08 '20 20:10 j-faria

I was able to fix the issue by tracing the nan back to the incomplete_gamma_inv function in GCEM. In the file gcem/include/gcem_incl/incomplete_gamma_inv.hpp changing

...
incomplete_gamma_inv_halley(const T ratio_val_1, const T ratio_val_2)
noexcept
{
    return( ratio_val_1 / max( T(0.8), min( T(1.2), T(1) - T(0.5)*ratio_val_1*ratio_val_2 ) ) );
}

to

...
incomplete_gamma_inv_halley(const T ratio_val_1, const T ratio_val_2)
noexcept
{
    return( ratio_val_1 / ( T(1) - T(0.5)*ratio_val_1*ratio_val_2 ) );
}

recovers the correct result. All tests pass, although I'm not completely sure why the safeguards were there in the first place.

j-faria avatar Oct 14 '20 13:10 j-faria