-
-
Notifications
You must be signed in to change notification settings - Fork 220
More stable logistic_lpdf/lcdf/lccdf computation #3398
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
avehtari
wants to merge
10
commits into
develop
Choose a base branch
from
improve-logistic_lpdf-derivative-stability
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7bf5028
Stabilize logistic location gradients
avehtari 777a274
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot 6c71ece
Stabilize logistic log-CDF tails
avehtari 99cbc2c
Merge branch 'improve-logistic_lpdf-derivative-stability' of github.c…
avehtari d8f3796
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot fac79ff
Stabilize logistic_cdf tails and derive all logistic_lpdf gradients f…
avehtari 18321ef
Merge branch 'improve-logistic_lpdf-derivative-stability' of github.c…
avehtari 4b0034b
Cut comments
avehtari 6f54a4c
Keep the logistic tail derivative on the log scale where inv_logit un…
avehtari 448dcc1
Guard the OpenCL logistic tail derivative the same way as the CPU
avehtari File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
43 changes: 43 additions & 0 deletions
43
stan/math/opencl/kernels/device_functions/logistic_tail_deriv.hpp
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| #ifndef STAN_MATH_OPENCL_KERNELS_DEVICE_FUNCTIONS_LOGISTIC_TAIL_DERIV_HPP | ||
| #define STAN_MATH_OPENCL_KERNELS_DEVICE_FUNCTIONS_LOGISTIC_TAIL_DERIV_HPP | ||
| #ifdef STAN_OPENCL | ||
|
|
||
| #include <stan/math/opencl/stringify.hpp> | ||
| #include <string> | ||
|
|
||
| namespace stan { | ||
| namespace math { | ||
| namespace opencl_kernels { | ||
|
|
||
| // \cond | ||
| static constexpr const char* logistic_tail_deriv_device_function | ||
| = "\n" | ||
| "#ifndef STAN_MATH_OPENCL_KERNELS_DEVICE_FUNCTIONS_LOGISTIC_TAIL_DERIV\n" | ||
| "#define " | ||
| "STAN_MATH_OPENCL_KERNELS_DEVICE_FUNCTIONS_LOGISTIC_TAIL_" | ||
| "DERIV\n" STRINGIFY( | ||
| // \endcond | ||
| /** \ingroup opencl_kernels | ||
| * | ||
| * Return inv_logit(-x) / sigma. | ||
| * | ||
| * @param x scaled difference | ||
| * @param sigma scale | ||
| * @return inv_logit(-x) / sigma | ||
| */ | ||
| double logistic_tail_deriv(double x, double sigma) { | ||
| if (x > 700.0) { | ||
| return exp(log1m_inv_logit(x) - log(sigma)); | ||
| } | ||
| return inv_logit(-x) / sigma; | ||
| } | ||
| // \cond | ||
| ) "\n#endif\n"; // NOLINT | ||
| // \endcond | ||
|
|
||
| } // namespace opencl_kernels | ||
| } // namespace math | ||
| } // namespace stan | ||
|
|
||
| #endif | ||
| #endif |
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
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
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
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
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can use the same log-calculations as the
lcdf(see below), accumulating on the log scale and exponentiating the result (for the derivatives as well)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Claude assisted answer:
Measured against a
long doublereference,exp(Σ log inv_logit)is 749 ulp at N=100, z=−0.5 against 3.0 for the product, and 641 vs 1.0 at N=10, z=−70 —expamplifies the summed log by|log P|·eps. It also doesn't extend the range: both forms underflow at the same points, including for a single observation at z = −745.2.inv_logithas no cancellation to avoid, unlike the difference inexp_mod_normal_cdfwhere the log scale genuinely buys something.The rescaling
partials *= cdfdoes drop the case where the cdf underflows butcdf × ∂log F/∂θis representable. That's the same σ < 1e-16 window as the tail fix, and there the cdf returns exactly 0, sologistic_lcdfis the right function. If you want this to be changes, it needs sign tracking to exponentiate each partial separately.