-
-
Notifications
You must be signed in to change notification settings - Fork 26
Improve floating point accuracy #463
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 all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
d88bbaf
fix: preserve extreme GPD tail probabilities
avehtari 01f464a
fix: stabilize Pareto convergence rates
avehtari 2816f4c
fix: stabilize Pareto tail differences
avehtari 5cdf5ff
fix: stabilize MCSE standard deviation
avehtari a7aea6f
fix: stabilize dissent logarithms
avehtari 96d8a95
refactor: move new helper functions to misc.R
avehtari a779942
Add clarifying comment
avehtari d22e9a0
fix: distinguish small nonzero GPD shapes
avehtari 230707a
Merge branch 'improve-floating-point-accuracy' of github.com:stan-dev…
avehtari eecfde8
fix: return zero from exp_x_minus_exp_y for equal infinite inputs
avehtari 0542ad8
fix: propagate missing shape parameters through ps_convergence_rate
avehtari 61cdaf2
fix: reject out-of-range probabilities in qgeneralized_pareto
avehtari fab0f07
test: cover -Inf contracts for log_sum_exp, GPD endpoints and pareto …
avehtari 01205e8
docs: note that subsetting weighted draws renormalizes the weights
avehtari 88b8bb6
fix: reject weights that carry no probability mass
avehtari 8cc5acf
fix: error instead of returning NaN when normalizing zero weights
avehtari 8ee7f1e
fix: report degenerate PIT weight columns per variable instead of abo…
avehtari 7472fb8
refactor: skip the Pareto tail fit when the tail carries no weight
avehtari f242a2c
test: cover mixed finite and -Inf log weights in weight_draws
avehtari b6ef3aa
test: cover mixed finite and -Inf log weights in pit
avehtari 0c76a4c
test: cover mixed finite and -Inf log weights in pareto_pit
avehtari 60d0d9b
fix: shift log_sum_exp by the maximum instead of clamping it at zero
avehtari 700d18d
refactor: add misc-math to outsource helpers for numerical stability
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
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,41 @@ | ||
| # numerically stable version of log(sum(exp(x))) | ||
| log_sum_exp <- function(x) { | ||
| x <- as.numeric(x) | ||
| if (length(x) == 0) { | ||
| return(-Inf) | ||
| } | ||
| max <- max(x) | ||
| if (max == -Inf) { | ||
| res <- -Inf | ||
| } else if (max == Inf) { | ||
| res <- Inf | ||
| } else { | ||
| sum <- sum(exp(x - max)) | ||
| res <- max + log(sum) | ||
| } | ||
| res | ||
| } | ||
|
|
||
| # numerically stable version of exp(x) - exp(y) | ||
| exp_x_minus_exp_y <- function(x, y) { | ||
| out <- -exp(x) * expm1(y - x) | ||
| # equal infinite inputs give 0 * NaN above; which() drops the NA comparisons | ||
| # that NA or NaN inputs would produce | ||
| out[which(x == y)] <- 0 | ||
| out | ||
| } | ||
|
|
||
| # numerically stable version of log2(1 - x) | ||
| log2_one_minus <- function(x) { | ||
| log1p(-x) / log(2) | ||
| } | ||
|
|
||
| # numerically stable version of log(1 - exp(x)) for x <= 0 | ||
| log1m_exp <- function(x) { | ||
| log(-expm1(x)) | ||
| } | ||
|
|
||
| # numerically stable version of log(exp(x) / sum(exp(x))) | ||
| log_normalize <- function(x) { | ||
| x - log_sum_exp(x) | ||
| } |
|
florence-bockting marked this conversation as resolved.
|
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
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.