math icon indicating copy to clipboard operation
math copied to clipboard

Improve Bernoulli (LC)CDF Numerical Stability

Open andrjohns opened this issue 3 years ago • 0 comments

Description

The bernoulli_cdf function directly subtracts the probability parameter from 1, and then multiplicatively accumulates for vectorised inputs:

    const T_partials_return Pi = 1 - theta_vec.val(i);

    P *= Pi;

For small probability (theta) values, this subtraction from 1 will have reduced resolution. When this is not the case, the multiplicative accumulation then carries the risk of underflow.

By moving these calculations to the log scale (i.e., using log1m), the distribution functions will have better resolution across a range of inputs

Current Version:

v4.4.0

andrjohns avatar Jul 08 '22 02:07 andrjohns