-
-
Notifications
You must be signed in to change notification settings - Fork 195
Fix lbeta for large arguments #1612
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
bbbales2
merged 28 commits into
stan-dev:develop
from
martinmodrak:bugfix/1611-lbeta-large-arguments
Jan 28, 2020
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
4dce6ad
Failing test
martinmodrak 2fedf57
Fixes #1611
martinmodrak b0394a4
Test against derivatives, identitites at around cutoff for Stirling
martinmodrak c89f18c
[Jenkins] auto-formatting by clang-format version 5.0.0-3~16.04.1 (ta…
stan-buildbot 5ae5f93
Fixing lint errors
martinmodrak 10bbde3
Lint errors
martinmodrak e67e7a2
Revert expect_near_rel.hpp
martinmodrak b74c0bc
Line end
martinmodrak 6b87d37
Format and comment improvements as suggested in review.
martinmodrak 4157942
[Jenkins] auto-formatting by clang-format version 5.0.0-3~16.04.1 (ta…
stan-buildbot c74b152
Fixed headers
martinmodrak abe6d58
Removed problematic constexpr
martinmodrak 0a93fdc
Tighten test accuracy, use more terms of Stirling series in lgamma_st…
martinmodrak 6778e62
Merge commit '45dce152f1fdb6fc079218d76b611d2664bf305e' into HEAD
yashikno 571e727
[Jenkins] auto-formatting by clang-format version 5.0.0-3~16.04.1 (ta…
stan-buildbot ab16ea5
Ceased using expect_near_rel
martinmodrak 06cf322
[Jenkins] auto-formatting by clang-format version 5.0.2-svn328729-1~e…
stan-buildbot 1ced285
Slightly relaxed test to pass on Linux
martinmodrak 1c96115
Suggestions from @mcol
martinmodrak bcf5d48
[Jenkins] auto-formatting by clang-format version 5.0.0-3~16.04.1 (ta…
stan-buildbot 95aa753
Further tweak to test tolerance
martinmodrak a0ae6c3
[Jenkins] auto-formatting by clang-format version 6.0.0 (tags/google/…
stan-buildbot 17f3680
Tweak test tolerance for Linux
martinmodrak 084ddfb
Merge remote-tracking branch 'stan-dev/develop' into bugfix/1611-lbet…
martinmodrak 9ce2093
Slightly better Mathematica code to generate tests
martinmodrak e731457
Merge remote-tracking branch 'origin/bugfix/1611-lbeta-large-argument…
martinmodrak c1cea9c
Merge remote-tracking branch 'stan-dev/develop' into bugfix/1611-lbet…
martinmodrak 71b983e
[Jenkins] auto-formatting by clang-format version 5.0.0-3~16.04.1 (ta…
stan-buildbot 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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#ifndef STAN_MATH_PRIM_FUN_LGAMMA_STIRLING_HPP | ||
#define STAN_MATH_PRIM_FUN_LGAMMA_STIRLING_HPP | ||
|
||
#include <stan/math/prim/meta.hpp> | ||
#include <stan/math/prim/fun/constants.hpp> | ||
#include <stan/math/prim/fun/lgamma.hpp> | ||
#include <cmath> | ||
martinmodrak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
namespace stan { | ||
namespace math { | ||
|
||
/** | ||
* Return the Stirling approximation to the lgamma function. | ||
* | ||
|
||
\f[ | ||
\mbox{lgamma_stirling}(x) = | ||
\frac{1}{2} \log(2\pi) + (x-\frac{1}{2})*\log(x) - x | ||
\f] | ||
|
||
* | ||
* @tparam T Type of value. | ||
* @param x value | ||
* @return Stirling's approximation to lgamma(x). | ||
*/ | ||
martinmodrak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
template <typename T> | ||
return_type_t<T> lgamma_stirling(const T x) { | ||
return HALF_LOG_TWO_PI + (x - 0.5) * log(x) - x; | ||
} | ||
|
||
} // namespace math | ||
} // namespace stan | ||
|
||
#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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#ifndef STAN_MATH_PRIM_FUN_LGAMMA_STIRLING_DIFF_HPP | ||
#define STAN_MATH_PRIM_FUN_LGAMMA_STIRLING_DIFF_HPP | ||
|
||
#include <stan/math/prim/meta.hpp> | ||
#include <stan/math/prim/err.hpp> | ||
#include <stan/math/prim/fun/constants.hpp> | ||
#include <stan/math/prim/fun/inv.hpp> | ||
#include <stan/math/prim/fun/lgamma.hpp> | ||
#include <stan/math/prim/fun/lgamma_stirling.hpp> | ||
#include <stan/math/prim/fun/square.hpp> | ||
#include <stan/math/prim/fun/value_of.hpp> | ||
#include <cmath> | ||
martinmodrak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
namespace stan { | ||
namespace math { | ||
|
||
constexpr double lgamma_stirling_diff_useful = 10; | ||
|
||
/** | ||
* Return the difference between log of the gamma function and its Stirling | ||
* approximation. | ||
* This is useful to stably compute log of ratios of gamma functions with large | ||
* arguments where the Stirling approximation allows for analytic solution | ||
* and the (small) differences can be added afterwards. | ||
* This is for example used in the implementation of lbeta. | ||
* | ||
* The function will return correct value for all arguments, but using it can | ||
* lead to a loss of precision when x < lgamma_stirling_diff_useful. | ||
* | ||
\f[ | ||
\mbox{lgamma_stirling_diff}(x) = | ||
\log(\Gamma(x)) - \frac{1}{2} \log(2\pi) + | ||
(x-\frac{1}{2})*\log(x) - x | ||
\f] | ||
|
||
* | ||
* @tparam T Type of value. | ||
* @param x value | ||
* @return Difference between lgamma(x) and its Stirling approximation. | ||
*/ | ||
template <typename T> | ||
return_type_t<T> lgamma_stirling_diff(const T x) { | ||
using T_ret = return_type_t<T>; | ||
|
||
if (is_nan(value_of_rec(x))) { | ||
return NOT_A_NUMBER; | ||
} | ||
check_nonnegative("lgamma_stirling_diff", "argument", x); | ||
|
||
if (x == 0) { | ||
return INFTY; | ||
} | ||
if (value_of(x) < lgamma_stirling_diff_useful) { | ||
return lgamma(x) - lgamma_stirling(x); | ||
} | ||
|
||
// Using the Stirling series as expressed in formula 5.11.1. at | ||
// https://dlmf.nist.gov/5.11 | ||
constexpr double stirling_series[]{ | ||
0.0833333333333333333333333, -0.00277777777777777777777778, | ||
0.000793650793650793650793651, -0.000595238095238095238095238, | ||
0.000841750841750841750841751, -0.00191752691752691752691753, | ||
0.00641025641025641025641026, -0.0295506535947712418300654}; | ||
|
||
constexpr int n_stirling_terms = 6; | ||
T_ret result(0.0); | ||
T_ret multiplier = inv(x); | ||
T_ret inv_x_squared = square(multiplier); | ||
for (int n = 0; n < n_stirling_terms; n++) { | ||
if (n > 0) { | ||
multiplier *= inv_x_squared; | ||
} | ||
result += stirling_series[n] * multiplier; | ||
} | ||
return result; | ||
} | ||
|
||
} // namespace math | ||
} // namespace stan | ||
|
||
#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
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.
Uh oh!
There was an error while loading. Please reload this page.