Skip to content
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

Adaptive thinning for testing distributions #735

Closed
njtierney opened this issue Oct 31, 2024 · 1 comment · Fixed by #742
Closed

Adaptive thinning for testing distributions #735

njtierney opened this issue Oct 31, 2024 · 1 comment · Fixed by #742

Comments

@njtierney
Copy link
Collaborator

  • The KS test for equivalence of mcmc and iid samples is sensitive specifically to correlation in the MCMC samples, i.e., it assumes that there are n independent samples and the test statistic biases the p-value (because the samples are non-independent).
  • An ad hoc way of doing this is just to thin to reduce correlation
  • It would be more stable to estimate the amount of correlation and then thin appropriately
@goldingn
Copy link
Member

Here's code to find a reasonable thinning rate for an mcmclist object:

# find a thinning rate that sufficiently reduces autocorrelation in the samples
find_thinning <- function(draws, max_thin = 100, autocorr_threshold = 0.01) {
  autocorr_list <- coda::autocorr(draws, lags = seq_len(max_thin))
  autocorrs <- do.call(cbind, autocorr_list)
  mean_autocorr <- rowMeans(autocorrs)
  smallest_thin <- which(mean_autocorr < autocorr_threshold)[1]
  if (is.na(smallest_thin)) {
    smallest_thin <- max_thin
    warning("could not find any thinning value that reduces mean autocorrelation to below the threshold, using the maximum thinning amount",
            call. = FALSE)
  }
  smallest_thin
}

# # example
# x <- normal(0, 1)
# m <- model(x)
# draws <- mcmc(m)
# find_thinning(draws)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Development

Successfully merging a pull request may close this issue.

2 participants