Commit 31e5a69
Widen the Cody branch past the erfc^2 underflow (Cody 1969)
The lcdf value branch used log(erfc(-s)) for scaled_diff in (-20, 0]. In
forward-over-reverse mode (fvar<fvar<var>>) that produces a NaN third
derivative: fvar's log rule forms x.d_ / x.val_ with x.val_ = erfc(|s|), and
once erfc(|s|)^2 leaves the normal range the quotient chain evaluates
0 * finite / 0.
Measured, not predicted:
erfc(|s|)^2 leaves the normal range at |s| = 18.7268381191
NaN window bisects to s in (-20.0000000000, -19.2103473480)
i.e. y in (-28.2842712475, -27.1675337575) at mu=0, sigma=1
Reachable ONLY in fvar<fvar<var>>, i.e. expect_ad's grad_hessian(); fvar<var>
and fvar<fvar<double>> are both clean, and nothing shows up as an inf in the
forward value slot. The existing y = -20*sqrt(2) test point sits exactly on
the lower edge and takes the Cody branch, which is why this was never caught.
Move the boundary to -12, handing s <= -12 to the Cody (1969) rational
approximation that already sits below it. That clears the underflow window
with 6.7 units of margin. Cody's relative error over the newly handed-over
range s in [-20, -12] measures at worst 9.593e-17 against a 60-digit mpmath
reference -- machine precision, and indistinguishable from the erfc branch it
replaces. The dense R-generated value tables in
prim/prob/{normal,std_normal}_cdf_log_test.cpp cover y in [-37.5, 10], which
spans the re-branched region, and no expectation moves.
P1 and P2 tests go green; P3 remains red.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>1 parent 7cccbe5 commit 31e5a69
4 files changed
Lines changed: 10 additions & 4 deletions
File tree
- stan/math
- opencl
- kernels/device_functions
- prim
- prim/prob
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
32 | | - | |
| 32 | + | |
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
33 | | - | |
| 33 | + | |
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
91 | 91 | | |
92 | 92 | | |
93 | 93 | | |
94 | | - | |
| 94 | + | |
95 | 95 | | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
96 | 99 | | |
97 | 100 | | |
98 | 101 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
71 | 71 | | |
72 | 72 | | |
73 | 73 | | |
74 | | - | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
75 | 78 | | |
76 | 79 | | |
77 | 80 | | |
| |||
0 commit comments