Skip to content

Commit fa4bc97

Browse files
authored
Merge pull request #219 from stan-dev/use-posterior-if-available
use posterior::autocovariance if available
2 parents 58fe5be + f32418c commit fa4bc97

2 files changed

Lines changed: 8 additions & 9 deletions

File tree

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Package: loo
22
Type: Package
33
Title: Efficient Leave-One-Out Cross-Validation and WAIC for Bayesian Models
44
Version: 2.6.0
5-
Date: 2023-03-29
5+
Date: 2023-03-30
66
Authors@R: c(person("Aki", "Vehtari", email = "Aki.Vehtari@aalto.fi", role = c("aut")),
77
person("Jonah", "Gabry", email = "jsg2201@columbia.edu", role = c("cre", "aut")),
88
person("Mans", "Magnusson", role = c("aut")),
@@ -43,6 +43,7 @@ Suggests:
4343
ggplot2,
4444
graphics,
4545
knitr,
46+
posterior,
4647
rmarkdown,
4748
rstan,
4849
rstanarm (>= 2.19.0),

R/effective_sample_sizes.R

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -199,17 +199,14 @@ psis_n_eff.matrix <- function(w, r_eff = NULL, ...) {
199199
#' @return MCMC effective sample size based on RStan's calculation.
200200
#'
201201
ess_rfun <- function(sims) {
202-
# Compute the effective sample size for samples of several chains
203-
# for one parameter; see the C++ code of function
204-
# effective_sample_size in chains.cpp
205-
#
206-
# Args:
207-
# sims: a 2-d array _without_ warmup samples (# iter * # chains)
208-
#
209202
if (is.vector(sims)) dim(sims) <- c(length(sims), 1)
210203
chains <- ncol(sims)
211204
n_samples <- nrow(sims)
212-
acov <- lapply(1:chains, FUN = function(i) autocovariance(sims[,i]))
205+
if (requireNamespace("posterior", quietly = TRUE)) {
206+
acov <- lapply(1:chains, FUN = function(i) posterior::autocovariance(sims[,i]))
207+
} else {
208+
acov <- lapply(1:chains, FUN = function(i) autocovariance(sims[,i]))
209+
}
213210
acov <- do.call(cbind, acov)
214211
chain_mean <- colMeans(sims)
215212
mean_var <- mean(acov[1,]) * n_samples / (n_samples - 1)
@@ -276,6 +273,7 @@ fft_next_good_size <- function(N) {
276273
}
277274
}
278275

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

0 commit comments

Comments
 (0)