Repository navigation
Keep rows of joint MCMC samples together #4238
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
base: develop
Are you sure you want to change the base?
Changes from all commits
c35a499
1182f19
e63c45c
3abde1c
cb720ed
3e9729a
a222719
71f8328
86e33eb
9b945cc
0b47b4c
775064e
3ad58ff
c8794ef
6aff196
d1b3c28
0974105
eb2c06d
994c4c1
0482e1f
cb24bfc
1913cb7
365a9c3
14fa756
590e291
bfe0e6f
2fc3e43
0730c92
da613cf
c3c8008
edbf8d7
b599948
014f108
a21c940
da4f3c3
324e681
781b749
d251c9f
aa5a43a
8fda731
e8febf1
ef81488
43e653e
710df13
e86de21
8ba1f6d
f7b54e9
0ddcfff
90bd23d
2f76509
f50c285
ce340a0
5561187
8140ce8
794b7c2
1519f54
8243f43
72c3cae
64c6aa4
5d60a20
6163593
5a1f3c5
f53dc00
aad6cfd
3688ecd
58c0cae
60c2ec6
c0022d0
055c0fc
07d29e0
4829d2a
7133e00
274828e
c02bbc2
6a8eee2
c973003
e31da10
d18055e
eb5a30d
3e20a45
ee4031d
fec00eb
611986a
1690dbb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -77,23 +77,24 @@ read.ensemble.output <- function(ensemble.size, pecandir, outdir, start.year, en | |||||||||||||||||
|
|
||||||||||||||||||
| #' Get parameter values used in ensemble | ||||||||||||||||||
| #' | ||||||||||||||||||
| #' Returns a matrix of randomly or quasi-randomly sampled trait values | ||||||||||||||||||
| #' to be assigned to traits over several model runs. | ||||||||||||||||||
| #' given the number of model runs and a list of sample distributions for traits | ||||||||||||||||||
| #' The model run is indexed first by model run, then by trait | ||||||||||||||||||
| #' Returns a matrix of randomly or quasi-randomly sampled parameter values | ||||||||||||||||||
| #' to be assigned to parameters over several model runs. | ||||||||||||||||||
| #' given the number of model runs and a list of sample distributions for parameters | ||||||||||||||||||
| #' The model run is indexed first by model run, then by parameter. | ||||||||||||||||||
| #' | ||||||||||||||||||
| #' @param ensemble.size number of runs in model ensemble | ||||||||||||||||||
| #' @param pft.samples random samples from parameter distribution, e.g. from a MCMC chain | ||||||||||||||||||
| #' @param pft.samples list named by PFT of random samples from parameter distribution, e.g. from a MCMC chain | ||||||||||||||||||
| #' @param env.samples env samples | ||||||||||||||||||
| #' @param method the method used to generate the ensemble samples. Random generators: uniform, uniform with latin hypercube permutation. Quasi-random generators: halton, sobol, torus. Random generation draws random variates whereas quasi-random generation is deterministic but well equidistributed. Default is uniform. For small ensemble size with relatively large parameter number (e.g ensemble size < 5 and # of traits > 5) use methods other than halton. | ||||||||||||||||||
| #' @param param.names a list of parameter names that were fitted either by MA or PDA, important argument, if NULL parameters will be resampled independently | ||||||||||||||||||
| #' @param method the method used to generate the ensemble samples for parameters sampled independently. Random generators: random and uniform (the same uniform random draw), lhc (latin hypercube permutation). Quasi-random generators: halton, sobol. Random generation draws random variates whereas quasi-random generation is deterministic but well equidistributed. Default is random. For small ensemble size with relatively large parameter number (e.g ensemble size < 5 and # of parameters > 5) use methods other than halton. | ||||||||||||||||||
| #' @param param.names list named by PFT of the parameters that have joint samples, e.g. MCMC draws. If NULL (default), every parameter of each PFT. Used only when \code{independent = FALSE}, where these are drawn by row together and any others with \code{method}. | ||||||||||||||||||
| #' @param independent logical. If FALSE (default), parameters are sampled jointly within each PFT, preserving their correlations: each ensemble member takes all of them (or only those in \code{param.names}) from one row, drawn at random with replacement. If TRUE, every parameter is sampled independently using \code{method}, ignoring any joint structure. | ||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This behavior is much better than before! But I was still confused how these three args interact, especially because I kept scanning "...when
Suggested change
If we're iterating even more I'd consider renaming |
||||||||||||||||||
| #' @param ... Other arguments passed on to the sampling method | ||||||||||||||||||
| #' | ||||||||||||||||||
| #' @return named list of data.frames with sampled indices as an attribute | ||||||||||||||||||
| #' @export | ||||||||||||||||||
| #' @author David LeBauer, Istem Fer | ||||||||||||||||||
| get.ensemble.samples <- function( ensemble.size, pft.samples, env.samples, | ||||||||||||||||||
| method = "random", param.names = NULL, ...) { | ||||||||||||||||||
| method = "random", param.names = NULL, independent = FALSE, ...) { | ||||||||||||||||||
|
|
||||||||||||||||||
| # Define supported methods | ||||||||||||||||||
| supported_methods <- c("random", "uniform", "halton", "sobol", "lhc") | ||||||||||||||||||
|
|
@@ -108,6 +109,8 @@ get.ensemble.samples <- function( ensemble.size, pft.samples, env.samples, | |||||||||||||||||
| } else if (ensemble.size == 1) { | ||||||||||||||||||
| ans <- PEcAn.utils::get.sa.sample.list(pft.samples, env.samples, 0.5) | ||||||||||||||||||
| } else { | ||||||||||||||||||
| # resolved before `env` joins pft.samples, so env.samples are never drawn by row | ||||||||||||||||||
| if (is.null(param.names)) param.names <- lapply(pft.samples, names) | ||||||||||||||||||
|
Comment on lines
+112
to
+113
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't follow the comment here, but env.samples is about to be removed in #4229 -- will it still apply after that? |
||||||||||||||||||
| pft.samples[[length(pft.samples) + 1]] <- env.samples | ||||||||||||||||||
| names(pft.samples)[length(pft.samples)] <- "env" | ||||||||||||||||||
| pft2col <- NULL | ||||||||||||||||||
|
|
@@ -160,40 +163,34 @@ get.ensemble.samples <- function( ensemble.size, pft.samples, env.samples, | |||||||||||||||||
| ensemble.samples[[pft.i]] <- matrix(nrow = ensemble.size, ncol = length(pft.samples[[pft.i]])) | ||||||||||||||||||
| sampled.indices[[pft.i]] <- matrix(nrow = ensemble.size, ncol = length(pft.samples[[pft.i]])) | ||||||||||||||||||
|
|
||||||||||||||||||
| # meaning we want to keep MCMC samples together | ||||||||||||||||||
| if (length(pft.samples[[pft.i]]) > 0 && !is.null(param.names)) { | ||||||||||||||||||
| if (method == "halton") { | ||||||||||||||||||
| same.i <- floor(randtoolbox::halton(ensemble.size) * length(pft.samples[[pft.i]][[1]]))+1 | ||||||||||||||||||
| } else if (method == "sobol") { | ||||||||||||||||||
| same.i <- floor(randtoolbox::sobol(ensemble.size, scrambling = 3) * length(pft.samples[[pft.i]][[1]]))+1 | ||||||||||||||||||
| } else if (method == "torus") { | ||||||||||||||||||
| same.i <- floor(randtoolbox::torus(ensemble.size) * length(pft.samples[[pft.i]][[1]]))+1 | ||||||||||||||||||
| } else if (method == "lhc") { | ||||||||||||||||||
| same.i <- floor(c(PEcAn.emulator::lhc(t(matrix(0:1, ncol = 1, nrow = 2)), ensemble.size) * length(pft.samples[[pft.i]][[1]])))+1 | ||||||||||||||||||
| } else if (method == "uniform") { | ||||||||||||||||||
| same.i <- sample.int(length(pft.samples[[pft.i]][[1]]), ensemble.size) | ||||||||||||||||||
| } else if (method == "random") { | ||||||||||||||||||
| PEcAn.logger::logger.info("Using random row sampling for MCMC draws") | ||||||||||||||||||
| same.i <- sample(nrow(pft.samples[[pft.i]][[1]]), ensemble.size, replace = TRUE) | ||||||||||||||||||
| } else { | ||||||||||||||||||
| PEcAn.logger::logger.error("Sampling method %s is not recognized", method) | ||||||||||||||||||
| # TRUE for parameters sampled jointly: a member takes all of them from one row | ||||||||||||||||||
| by.row <- !independent & names(pft.samples[[pft.i]]) %in% param.names[[names(pft.samples)[pft.i]]] | ||||||||||||||||||
| if (any(by.row)) { | ||||||||||||||||||
| n.rows <- unique(lengths(pft.samples[[pft.i]][by.row])) | ||||||||||||||||||
| if (length(n.rows) > 1) { | ||||||||||||||||||
| PEcAn.logger::logger.severe( | ||||||||||||||||||
| "Parameters to sample jointly for PFT", names(pft.samples)[pft.i], | ||||||||||||||||||
| "differ in length:", toString(n.rows) | ||||||||||||||||||
| ) | ||||||||||||||||||
| } | ||||||||||||||||||
| # draws whole rows, so a space-filling design over [0, 1] does not apply | ||||||||||||||||||
| same.i <- sample.int(n.rows, ensemble.size, replace = TRUE) | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| for (trait.i in seq(pft.samples[[pft.i]])) { | ||||||||||||||||||
| for (param.i in seq(pft.samples[[pft.i]])) { | ||||||||||||||||||
| col.i <- col.i + 1 | ||||||||||||||||||
| if (names(pft.samples[[pft.i]])[trait.i] %in% param.names[[pft.i]]) { | ||||||||||||||||||
| ensemble.samples[[pft.i]][, trait.i] <- pft.samples[[pft.i]][[trait.i]][same.i] | ||||||||||||||||||
| sampled.indices[[pft.i]][, trait.i] <- same.i | ||||||||||||||||||
| if (by.row[param.i]) { | ||||||||||||||||||
| ensemble.samples[[pft.i]][, param.i] <- pft.samples[[pft.i]][[param.i]][same.i] | ||||||||||||||||||
| sampled.indices[[pft.i]][, param.i] <- same.i | ||||||||||||||||||
| } else { | ||||||||||||||||||
| # Extract original trait values | ||||||||||||||||||
| trait.values <- pft.samples[[pft.i]][[trait.i]] | ||||||||||||||||||
| sampled.values <- stats::quantile(trait.values, random.samples[, col.i]) | ||||||||||||||||||
| # Extract original parameter values | ||||||||||||||||||
| param.values <- pft.samples[[pft.i]][[param.i]] | ||||||||||||||||||
| sampled.values <- stats::quantile(param.values, random.samples[, col.i]) | ||||||||||||||||||
|
|
||||||||||||||||||
| ensemble.samples[[pft.i]][, trait.i] <- stats::quantile(pft.samples[[pft.i]][[trait.i]], | ||||||||||||||||||
| ensemble.samples[[pft.i]][, param.i] <- stats::quantile(pft.samples[[pft.i]][[param.i]], | ||||||||||||||||||
| random.samples[, col.i]) | ||||||||||||||||||
| sampled.indices[[pft.i]][, trait.i] <- sapply(sampled.values, | ||||||||||||||||||
| function(val) {which.min(abs(trait.values - val)) }) | ||||||||||||||||||
| sampled.indices[[pft.i]][, param.i] <- sapply(sampled.values, | ||||||||||||||||||
| function(val) {which.min(abs(param.values - val)) }) | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
| ensemble.samples[[pft.i]] <- as.data.frame(ensemble.samples[[pft.i]]) | ||||||||||||||||||
|
|
||||||||||||||||||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.