Conversation
avehtari
marked this pull request as draft
September 14, 2026 20:41
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR and PR text were assisted by Sol and Herbie
Summary
exp_mod_normal_lpdf,cdf,lcdf, andlccdfvalues and gradients in normal tails,cancellation regions, and the small-
lambdalimit.CDF scale-gradient signs.
Motivation
The existing CDF family subtracts nearly equal normal-tail terms. This can produce substantially
wrong finite values and near-zero gradients at moderate normalized parameters. For example:
lcdf:-0.62114instead of-0.65315, withd_y ≈ -1.5e-14instead of0.76490.cdf:0.53733instead of0.52041, withd_y ≈ -8e-15instead of0.39806.lccdf:-22.44429instead of-22.00452, withd_y ≈ -9e-14instead of-6.33910.The LPDF separately evaluates
log(erfc(r))andexp(-r^2) / erfc(r). Atr = 27.25, the currentimplementation returns
-Infand an infinite gradient, although the finite values are approximately-709.09642and37.56323.The regression tests have specific failing examples.
Herbie analysis
Herbie was run on scalar value and analytic-gradient expressions copied from the current
implementation, using fixed seeds and targeted central, tail, and cancellation domains. Across three
seeds, it identified repeatable improvements of approximately:
Herbie consistently exposed the cancellation-prone normal terms and suggested
erfc- andFMA-based rewrites. Its output was used for triage only. The final implementation uses manually
derived log-space identities, asymptotic expansions, and independently evaluated regression
references.
Helper rationale
The repair repeatedly needs stable
log(Phi(x)), the inverse Mills ratiophi(x) / Phi(x), andpositive-tail
erfcxevaluation. These operations are implemented in an internalnormal_tail_utils.hpphelper. Exp-mod-normal-specific log differences, log sums, small-parameterexpansions, and normalized derivatives remain private to
exp_mod_normal_utils.hpp.The same normal-tail pair is required by
skew_normal_lpdfand is directly applicable to lognormalLCDF/LCCDF. Keeping it distribution-independent avoids duplicating sensitive tail approximations
without adding a public API or refactoring the existing normal CDF implementations.
Tests
Regression tests are included
Side Effects
Helper functions will make couple more PRs smaller
Release notes
Checklist
Copyright holder: Aki Vehtari
The copyright holder is typically you or your assignee, such as a university or company. By submitting this pull request, the copyright holder is agreeing to the license the submitted work under the following licenses:
- Code: BSD 3-clause (https://opensource.org/licenses/BSD-3-Clause)
- Documentation: CC-BY 4.0 (https://creativecommons.org/licenses/by/4.0/)
the basic tests are passing
./runTests.py test/unit)make test-headers)make test-math-dependencies)make cpplint)the code is written in idiomatic C++ and changes are documented in the doxygen
the new changes are tested