-
-
Notifications
You must be signed in to change notification settings - Fork 25
Add pit functions #395
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
paul-buerkner
merged 24 commits into
stan-dev:master
from
TeemuSailynoja:add_pit_functions
Mar 28, 2025
Merged
Add pit functions #395
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
bf7ac8c
default method for PIT and LOO-PIT
TeemuSailynoja d9b02be
Add validation for loo_weights.
TeemuSailynoja 359f6b1
Check PIT bounds only once.
TeemuSailynoja 72a3a30
Normalize loo_weights in pit.default. Add tests for internals and pit…
TeemuSailynoja 75d2592
Handle PIT = 0 without warnings. Add documentation for input parameters.
TeemuSailynoja efc37d6
default method for PIT and LOO-PIT
TeemuSailynoja 0258555
Add validation for loo_weights.
TeemuSailynoja 48dc3a6
Normalize loo_weights in pit.default. Add tests for internals and pit…
TeemuSailynoja f43023e
Handle PIT = 0 without warnings. Add documentation for input parameters.
TeemuSailynoja b2ffb7e
Merge remote-tracking branch 'refs/remotes/origin/add_pit_functions' …
TeemuSailynoja 8f2c361
Implement pit for draws and rvar. Add unit tests.
TeemuSailynoja ab21007
Make PIT description short and add myself as contributor.
TeemuSailynoja c00b326
typo1 R/pit.R
TeemuSailynoja 2537986
change \(x) to function(x) for backewards compatibility.
TeemuSailynoja f58e347
Merge branch 'stan-dev:master' into add_pit_functions
TeemuSailynoja 7acb46f
fix typo in test-pit.R
TeemuSailynoja 985a6c5
Update documentation of pit(), use validate_weights, log = FALSE by d…
TeemuSailynoja 203fdb3
make x optional in validate_y
TeemuSailynoja 9787124
fix rvar and draws checking
TeemuSailynoja 69e3560
Add tolerance to warning of pit over 1. Always clip pit at 1.
TeemuSailynoja af8f26a
remove test for warning when pit over tolerance. Can't reproduce.
TeemuSailynoja f33a216
fix example in pit
TeemuSailynoja f8a9894
Update NEWS.md
TeemuSailynoja 006ef16
Update NEWS.md
TeemuSailynoja 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,179 @@ | ||
| #' Probability integral transform | ||
| #' | ||
| #' Probability integral transform (PIT). LOO-PIT is given by a weighted sample. | ||
| #' | ||
| #' @name pit | ||
| #' | ||
| #' @param x (draws) A [`draws_matrix`] object or one coercible to a | ||
| #' `draws_matrix` object, or an [`rvar`] object. | ||
| #' | ||
| #' @param y (observations) A 1D vector, or an array of dim(x), if x is `rvar`. | ||
| #' Each element of `y` corresponds to a variable in `x`. | ||
| #' | ||
| #' @param weights A matrix of weights for each draw and variable. `weights` | ||
| #' should have one column per variable in `x`, and `ndraws(x)` rows. | ||
| #' | ||
| #' @param log (logical) Are the weights passed already on the log scale? The | ||
| #' default is `FALSE`, that is, expecting `weights` to be on the standard | ||
| #' (non-log) scale. | ||
| #' | ||
| #' @template args-methods-dots | ||
| #' | ||
| #' @details The `pit()` function computes the probability integral transform of | ||
| #' `y` using the empirical cumulative distribution computed from the samples | ||
| #' in `x`. For continuous valued `y` and `x`, the PIT for the elements of `y` | ||
| #' is computed as the empirical cumulative distribution value: | ||
| #' | ||
| #' PIT(y_i) = Pr(x_i < y_i), | ||
| #' | ||
| #' where x_i, is the corresponding set of draws in `x`. For `draws` objects, | ||
| #' this corresponds to the draws of the *i*th variable, and for `rvar` | ||
| #' the elements of `y` and `x` are matched. | ||
| #' | ||
| #' The draws in `x` can further be provided (log-)weights in | ||
| # `weights`, which enables for example the computation of LOO-PITs. | ||
| #' | ||
| #' If `y` and `x` are discrete, randomisation is used to obtain continuous PIT | ||
| #' values. (see, e.g., Czado, C., Gneiting, T., Held, L.: Predictive model | ||
| #' assessment for count data. Biometrics 65(4), 1254–1261 (2009).) | ||
| #' | ||
| #' @return A numeric vector of length `length(y)` containing the PIT values, or | ||
| #' an array of shape `dim(y)`, if `x` is an `rvar`. | ||
|
|
||
| #' @examples | ||
| #' # PIT for a draws object | ||
| #' x <- example_draws() | ||
| #' # Create a vector of observations | ||
| #' y <- rnorm(nvariables(x), 5, 5) | ||
| #' pit(x, y) | ||
| #' | ||
| #' # Compute weighted PIT (for example LOO-PIT) | ||
| #' weights <- matrix(runif(length(x)), ncol = nvariables(x)) | ||
| #' | ||
| #' pit(x, y, weights) | ||
| #' | ||
| #' # PIT for an rvar | ||
| #' x <- rvar(example_draws()) | ||
| #' # Create an array of observations with the same dimensions as x. | ||
| #' y_arr <- array(rnorm(length(x), 5, 5), dim = dim(x)) | ||
| #' pit(x, y_arr) | ||
| #' | ||
| NULL | ||
|
|
||
| #' @rdname pit | ||
| #' @export | ||
| pit <- function(x, y, ...) UseMethod("pit") | ||
|
|
||
| #' @rdname pit | ||
| #' @export | ||
| pit.default <- function(x, y, weights = NULL, log = FALSE, ...) { | ||
| x <- as_draws_matrix(x) | ||
| if (!is.null(weights)) { | ||
| weights <- as_draws_matrix(weights) | ||
| } | ||
| pit(x, y, weights, log) | ||
| } | ||
|
|
||
| #' @rdname pit | ||
| #' @export | ||
| pit.draws_matrix <- function(x, y, weights = NULL, log = FALSE, ...) { | ||
| y <- validate_y(y, x) | ||
| if (!is.null(weights)) { | ||
| weights <- sapply(seq_len(nvariables(x)), \(var_idx) { | ||
| validate_weights(weights[, var_idx], x[, var_idx], log) | ||
| }) | ||
| weights <- normalize_log_weights(weights) | ||
| } | ||
| pit <- vapply(seq_len(ncol(x)), function(j) { | ||
| sel_min <- x[, j] < y[j] | ||
| if (!any(sel_min)) { | ||
| pit <- 0 | ||
| } else { | ||
| if (is.null(weights)) { | ||
| pit <- mean(sel_min) | ||
| } else { | ||
| pit <- exp(log_sum_exp(weights[sel_min, j])) | ||
| } | ||
| } | ||
|
|
||
| sel_sup <- x[, j] == y[j] | ||
| if (any(sel_sup)) { | ||
| # randomized PIT for discrete y (see, e.g., Czado, C., Gneiting, T., | ||
| # Held, L.: Predictive model assessment for count data. | ||
| # Biometrics 65(4), 1254–1261 (2009).) | ||
| if (is.null(weights)) { | ||
| pit_sup <- pit + mean(sel_sup) | ||
| } else { | ||
| pit_sup <- pit + exp(log_sum_exp(weights[sel_sup, j])) | ||
| } | ||
|
|
||
| pit <- runif(1, pit, pit_sup) | ||
| } | ||
| pit | ||
| }, FUN.VALUE = 1.0) | ||
|
|
||
| if (any(pit > 1 + 1e-10)) { | ||
| warning_no_call( | ||
|
paul-buerkner marked this conversation as resolved.
|
||
| paste( | ||
| "Some PIT values larger than 1. ", | ||
| "This is usually due to numerical inaccuracies. ", | ||
| "Largest value: ", | ||
| max(pit), | ||
| "\nRounding PIT > 1 to 1.", | ||
| sep = "" | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| setNames(pmin(1, pit), variables(x)) | ||
| } | ||
|
|
||
| #' @rdname pit | ||
| #' @export | ||
| pit.rvar <- function(x, y, weights = NULL, log = FALSE, ...) { | ||
| y <- validate_y(y, x) | ||
| if (is.null(weights)) { | ||
| out <- array( | ||
| runif(length(y), Pr(x < y), Pr(x <= y)), | ||
| dim(x), | ||
| dimnames(x) | ||
| ) | ||
| } else { | ||
| out <- array( | ||
| data = pit( | ||
| x = as_draws_matrix(c(x)), | ||
| y = c(y), | ||
| weights = weights, | ||
| log = log | ||
| ), | ||
| dim = dim(x), | ||
| dimnames = dimnames(x) | ||
| ) | ||
| } | ||
| out | ||
| } | ||
|
|
||
| # internal ---------------------------------------------------------------- | ||
|
|
||
| validate_y <- function(y, x = NULL) { | ||
| if (!is.numeric(y)) { | ||
| stop_no_call("`y` must be numeric.") | ||
| } | ||
| if (anyNA(y)) { | ||
| stop_no_call("NAs not allowed in `y`.") | ||
| } | ||
| if (is_rvar(x)) { | ||
| if (length(x) != length(y) || any(dim(y) != dim(x))) { | ||
| stop_no_call("`dim(y)` must match `dim(x)`.") | ||
| } | ||
| } else if (is_draws(x)) { | ||
| if (!is.vector(y, mode = "numeric") || length(y) != nvariables(x)) { | ||
| stop_no_call("`y` must be a vector of length `nvariables(x)`.") | ||
| } | ||
| } | ||
| y | ||
| } | ||
|
|
||
| normalize_log_weights <- function(log_weights) { | ||
| apply(log_weights, 2, function(col) col - log_sum_exp(col)) | ||
| } | ||
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.
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.