-
-
Notifications
You must be signed in to change notification settings - Fork 218
Improve numerical stability of normal quantile gradients #3139
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1e105c3
Improve numerical stability of normal quantile gradients
andrjohns a67a993
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot b713e58
Update vectorisation
andrjohns d7edcbd
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot 4f53279
Fix opencl headers
andrjohns 4943a41
apply_scalary_binary -> apply_scalar_unary
andrjohns 444e167
Merge commit 'ac977e4ed79bd593440b9afbc25e1bba905e9276' into HEAD
yashikno 9840a14
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot d2ec33c
Remove unnecessary const auto
andrjohns 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
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 | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,9 +3,9 @@ | |||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| #include <stan/math/rev/meta.hpp> | ||||||||||||||||||||||||||
| #include <stan/math/rev/core.hpp> | ||||||||||||||||||||||||||
| #include <stan/math/prim/fun/constants.hpp> | ||||||||||||||||||||||||||
| #include <stan/math/prim/fun/sign.hpp> | ||||||||||||||||||||||||||
| #include <stan/math/prim/prob/std_normal_log_qf.hpp> | ||||||||||||||||||||||||||
| #include <stan/math/prim/functor/apply_scalar_binary.hpp> | ||||||||||||||||||||||||||
| #include <stan/math/prim/fun/elt_multiply.hpp> | ||||||||||||||||||||||||||
| #include <cmath> | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| namespace stan { | ||||||||||||||||||||||||||
|
|
@@ -19,16 +19,14 @@ namespace math { | |||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||
| template <typename T, require_stan_scalar_or_eigen_t<T>* = nullptr> | ||||||||||||||||||||||||||
| inline auto std_normal_log_qf(const var_value<T>& log_p) { | ||||||||||||||||||||||||||
| return make_callback_var( | ||||||||||||||||||||||||||
| std_normal_log_qf(log_p.val()), [log_p](auto& vi) mutable { | ||||||||||||||||||||||||||
| auto vi_array = as_array_or_scalar(vi.val()); | ||||||||||||||||||||||||||
| auto vi_sign = sign(as_array_or_scalar(vi.adj())); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const auto& deriv = as_array_or_scalar(log_p).val() | ||||||||||||||||||||||||||
| + log(as_array_or_scalar(vi.adj()) * vi_sign) | ||||||||||||||||||||||||||
| - NEG_LOG_SQRT_TWO_PI + 0.5 * square(vi_array); | ||||||||||||||||||||||||||
| as_array_or_scalar(log_p).adj() += vi_sign * exp(deriv); | ||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
| const auto& arena_rtn = to_arena(std_normal_log_qf(log_p.val())); | ||||||||||||||||||||||||||
| return make_callback_var(arena_rtn, [log_p, arena_rtn](auto& vi) mutable { | ||||||||||||||||||||||||||
| auto deriv = apply_scalar_binary( | ||||||||||||||||||||||||||
| log_p.val(), arena_rtn, [](const auto& logp_val, const auto& rtn_val) { | ||||||||||||||||||||||||||
| return exp(logp_val - std_normal_lpdf(rtn_val)); | ||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of
Suggested change
Same thing for the other change. |
||||||||||||||||||||||||||
| log_p.adj() += elt_multiply(vi.adj(), deriv); | ||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| } // namespace math | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
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.
Uh oh!
There was an error while loading. Please reload this page.