-
-
Notifications
You must be signed in to change notification settings - Fork 190
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
Improve numerical stability of normal quantile gradients #3139
base: develop
Are you sure you want to change the base?
Conversation
Jenkins Console Log Machine informationNo LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 20.04.3 LTS Release: 20.04 Codename: focalCPU: G++: Clang: |
Jenkins Console Log Machine informationNo LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 20.04.3 LTS Release: 20.04 Codename: focalCPU: G++: Clang: |
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.
I'm fine with this as is, though I have a suggestion that I think is simpler and should work
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)); | ||
}); |
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.
Instead of apply_scalar_binary
here how do you feel about just doing the std_normal_lpdf
on the return val? Since then the rest can use standard vectorization from Eigen.
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)); | |
}); | |
if constexpr (is_eigen<decltype(arena_rtn)>::value) { | |
auto derive = exp(log_p.val() - arena_rtn.unaryExpr([](auto x) { | |
return std_normal_lpdf(x);})); | |
log_p.adj() += elt_multiply(vi.adj(), deriv); | |
} else { | |
auto derive = exp(log_p.val() - std_normal_lpdf(arena_rtn)); | |
log_p.adj() += vi.adj() * deriv; | |
} |
Same thing for the other change.
Summary
This PR updates the gradient calculations for the normal quantile functions (
inv_Phi
andstd_normal_log_qf
) to use the existing (numerically stable) standard normal density functions.As:
Tests
N/A - tests should still pass
Side Effects
Improved numerical stability for gradients
Release notes
Improved the numerical stability of the gradient calculations for
inv_Phi
andstd_normal_log_qf
Checklist
Copyright holder: Andrew Johnson
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 doxygen
)make cpplint
)the code is written in idiomatic C++ and changes are documented in the doxygen
the new changes are tested