math icon indicating copy to clipboard operation
math copied to clipboard

stable log cdfs for normal

Open bob-carpenter opened this issue 10 months ago • 2 comments

Stan's log cdfs just take the log of the linear cdf, so they're prone to underflow to -infinity after the cdc underflows to 0 or rounds to 1.

The R implementation of log cdfs is more stable, e.g.,

> pnorm(-50, log=TRUE)
[1] -1254.831

We should be able to borrow an implementation from R or from SciPy.

bob-carpenter avatar Feb 21 '25 17:02 bob-carpenter

Here is the source code for pnorm https://github.com/wch/r-source/blob/trunk/src/nmath/pnorm.c#L90

SteveBronder avatar Feb 21 '25 19:02 SteveBronder

The standard-normal lcdf already uses that algorithm I believe

andrjohns avatar Feb 23 '25 09:02 andrjohns