Skip to content

Commit

Permalink
Merge pull request #219 from stan-dev/use-posterior-if-available
Browse files Browse the repository at this point in the history
use posterior::autocovariance if available
  • Loading branch information
jgabry authored Mar 31, 2023
2 parents 58fe5be + f32418c commit fa4bc97
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Package: loo
Type: Package
Title: Efficient Leave-One-Out Cross-Validation and WAIC for Bayesian Models
Version: 2.6.0
Date: 2023-03-29
Date: 2023-03-30
Authors@R: c(person("Aki", "Vehtari", email = "[email protected]", role = c("aut")),
person("Jonah", "Gabry", email = "[email protected]", role = c("cre", "aut")),
person("Mans", "Magnusson", role = c("aut")),
Expand Down Expand Up @@ -43,6 +43,7 @@ Suggests:
ggplot2,
graphics,
knitr,
posterior,
rmarkdown,
rstan,
rstanarm (>= 2.19.0),
Expand Down
14 changes: 6 additions & 8 deletions R/effective_sample_sizes.R
Original file line number Diff line number Diff line change
Expand Up @@ -199,17 +199,14 @@ psis_n_eff.matrix <- function(w, r_eff = NULL, ...) {
#' @return MCMC effective sample size based on RStan's calculation.
#'
ess_rfun <- function(sims) {
# Compute the effective sample size for samples of several chains
# for one parameter; see the C++ code of function
# effective_sample_size in chains.cpp
#
# Args:
# sims: a 2-d array _without_ warmup samples (# iter * # chains)
#
if (is.vector(sims)) dim(sims) <- c(length(sims), 1)
chains <- ncol(sims)
n_samples <- nrow(sims)
acov <- lapply(1:chains, FUN = function(i) autocovariance(sims[,i]))
if (requireNamespace("posterior", quietly = TRUE)) {
acov <- lapply(1:chains, FUN = function(i) posterior::autocovariance(sims[,i]))
} else {
acov <- lapply(1:chains, FUN = function(i) autocovariance(sims[,i]))
}
acov <- do.call(cbind, acov)
chain_mean <- colMeans(sims)
mean_var <- mean(acov[1,]) * n_samples / (n_samples - 1)
Expand Down Expand Up @@ -276,6 +273,7 @@ fft_next_good_size <- function(N) {
}
}

# autocovariance function to use if posterior::autocovariance is not available
autocovariance <- function(y) {
# Compute autocovariance estimates for every lag for the specified
# input sequence using a fast Fourier transform approach.
Expand Down

0 comments on commit fa4bc97

Please sign in to comment.