Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
84 commits
Select commit Hold shift + click to select a range
c35a499
read joint flag from trait.mcmc attribute
divine7022 Nov 11, 2026
1182f19
document joint attribute in load.posteriors
divine7022 Nov 11, 2026
e63c45c
return independent flag per pft
divine7022 Nov 11, 2026
3abde1c
document per pft independent flag
divine7022 Nov 11, 2026
cb720ed
accept per pft independent, match param.names by pft name
divine7022 Nov 11, 2026
3e9729a
document per pft independent in get_parameter_samples
divine7022 Nov 11, 2026
a222719
share joint row index across pfts, check row counts, fix random method
divine7022 Nov 11, 2026
71f8328
update param.names doc for joint rows
divine7022 Nov 11, 2026
86e33eb
test joint attribute and per pft flag
divine7022 Nov 11, 2026
9b945cc
test joint rows across pfts
divine7022 Nov 11, 2026
0b47b4c
add joint mcmc sampling to changelog
divine7022 Nov 11, 2026
775064e
add joint mcmc sampling to uncertainty news
divine7022 Nov 11, 2026
3ad58ff
preserve supplied joint draws in ensemble samples
divine7022 Nov 12, 2026
c8794ef
keep mcmc draws joint and distribution samples independent
divine7022 Nov 12, 2026
6aff196
separate posterior loading from sampling choices
divine7022 Nov 12, 2026
d1b3c28
return pft posterior data for sampling
divine7022 Nov 12, 2026
0974105
forward sampling choice through parameter wrapper
divine7022 Nov 12, 2026
eb2c06d
pass sampling choice through ensemble design generation
divine7022 Nov 12, 2026
994c4c1
keep oat quantiles separate from ensemble sampling
divine7022 Nov 12, 2026
0482e1f
pass sampling choice through configuration workflow
divine7022 Nov 12, 2026
cb24bfc
preserve independent sampling in standalone ed script
divine7022 Nov 12, 2026
1913cb7
document joint and marginal ensemble sampling
divine7022 Nov 12, 2026
365a9c3
document parameter sampling defaults and constraints
divine7022 Nov 12, 2026
14fa756
document posterior loader return values
divine7022 Nov 12, 2026
590e291
document posterior data loaded from paths
divine7022 Nov 12, 2026
bfe0e6f
document legacy posterior loader return values
divine7022 Nov 12, 2026
2fc3e43
document pft posterior loading contract
divine7022 Nov 12, 2026
0730c92
document parameter wrapper sampling options
divine7022 Nov 12, 2026
da613cf
document ensemble design sampling option
divine7022 Nov 12, 2026
c3c8008
document input preparation sampling option
divine7022 Nov 12, 2026
edbf8d7
document workflow sampling option
divine7022 Nov 12, 2026
b599948
test ensemble row sampling and named selectors
divine7022 Nov 12, 2026
014f108
test joint draws and marginal parameter sampling
divine7022 Nov 12, 2026
a21c940
test posterior loading preserves joint draws
divine7022 Nov 12, 2026
da4f3c3
test parameter wrapper sampling requests
divine7022 Nov 12, 2026
324e681
test ensemble design sampling requests
divine7022 Nov 12, 2026
781b749
test oat quantile sampling requests
divine7022 Nov 12, 2026
d251c9f
test workflow sampling requests and shared bundles
divine7022 Nov 12, 2026
aa5a43a
describe joint sampling behavior in uncertainty news
divine7022 Nov 12, 2026
8fda731
describe workflow sampling option in news
divine7022 Nov 12, 2026
e8febf1
record joint ensemble sampling changes
divine7022 Nov 12, 2026
ef81488
look up joint traits by pft name and stop on unequal lengths
divine7022 Nov 17, 2026
43e653e
default get_parameter_samples to joint draws
divine7022 Nov 17, 2026
710df13
restore get.parameter.samples loader flag
divine7022 Nov 17, 2026
e86de21
restore independent flag in load_pft_posteriors
divine7022 Nov 17, 2026
8ba1f6d
restore is.joint detection in load.posteriors
divine7022 Nov 17, 2026
f7b54e9
drop independent argument from generate_joint_ensemble_design
divine7022 Nov 17, 2026
0ddcfff
take independent from loader in oat design
divine7022 Nov 17, 2026
90bd23d
drop independent argument from run.write.configs
divine7022 Nov 17, 2026
2f76509
revert ed script sampling call
divine7022 Nov 17, 2026
f50c285
test joint lookup by name and unequal lengths
divine7022 Nov 17, 2026
ce340a0
test joint default and pft with nothing to sample
divine7022 Nov 17, 2026
5561187
sample ma output independently in modular chain test
divine7022 Nov 17, 2026
8140ce8
restore load.posteriors tests
divine7022 Nov 17, 2026
794b7c2
restore get.parameter.samples tests
divine7022 Nov 17, 2026
1519f54
restore joint ensemble design tests
divine7022 Nov 17, 2026
8243f43
restore oat design tests
divine7022 Nov 17, 2026
72c3cae
restore run.write.configs tests
divine7022 Nov 17, 2026
64c6aa4
regenerate get.ensemble.samples docs
divine7022 Nov 17, 2026
5d60a20
regenerate get_parameter_samples docs
divine7022 Nov 17, 2026
6163593
regenerate get.parameter.samples docs
divine7022 Nov 17, 2026
5a1f3c5
regenerate generate_joint_ensemble_design docs
divine7022 Nov 17, 2026
f53dc00
regenerate load.posteriors docs
divine7022 Nov 17, 2026
aad6cfd
regenerate load.posteriors.from.path docs
divine7022 Nov 17, 2026
3688ecd
regenerate load.posteriors.legacy docs
divine7022 Nov 17, 2026
58c0cae
regenerate load_pft_posteriors docs
divine7022 Nov 17, 2026
60c2ec6
regenerate run.write.configs docs
divine7022 Nov 17, 2026
c0022d0
regenerate prepare_input_designs docs
divine7022 Nov 17, 2026
055c0fc
describe joint sampling changes in uncertainty news
divine7022 Nov 17, 2026
07d29e0
drop workflow sampling option from news
divine7022 Nov 17, 2026
4829d2a
update changelog for joint sampling
divine7022 Nov 17, 2026
7133e00
merge develop
divine7022 Nov 24, 2026
274828e
revert modular chain test to develop
divine7022 Nov 24, 2026
c02bbc2
trim joint sampling news
divine7022 Nov 24, 2026
6a8eee2
trim joint sampling changelog
divine7022 Nov 24, 2026
c973003
Merge remote-tracking branch 'upstream/develop' into joint-mcmc-samples
divine7022 Nov 24, 2026
e31da10
add independent argument to get.ensemble.samples
divine7022 Nov 26, 2026
d18055e
pass independent through get_parameter_samples, rename trait.mcmc local
divine7022 Nov 26, 2026
eb5a30d
test joint default, param.names subset and length check
divine7022 Nov 26, 2026
3e20a45
test that independent does not keep mcmc rows, rename test helpers
divine7022 Nov 26, 2026
ee4031d
regenerate get.ensemble.samples docs
divine7022 Nov 26, 2026
fec00eb
regenerate get_parameter_samples docs
divine7022 Nov 26, 2026
611986a
news for independent argument
divine7022 Nov 26, 2026
1690dbb
changelog for independent argument
divine7022 Nov 26, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ For more information about this file see also [Keep a Changelog](http://keepacha
- Added statewide synthetic fertilization and compost amendment event workflows for CA ag parcels. Outputs share an ensemble naming so a downstream cleaner unions them into one fertilization event type for SIPNET.

### Fixed
- `PEcAn.uncertainty::get.ensemble.samples()` matches `param.names` to PFTs by name, not position, and errors when parameters sampled jointly differ in length instead of returning truncated or `NA` draws (#4238).
- `PEcAn.uncertainty::read.ensemble.output()` uses `NA` for missing variables, so each member’s result uses only its own output (#4204).
- Fixed broken meteorological data, flux network, and API documentation links in the PEcAn book and data.atmosphere package (#4071).
- `PEcAn.uncertainty::generate_joint_ensemble_design(sobol = TRUE)` now errors on looped inputs and quasi-random parameter methods, which split into dependent `X1` and `X2` halves and gave wrong Sobol indices (#4134).
Expand Down Expand Up @@ -81,6 +82,7 @@ For more information about this file see also [Keep a Changelog](http://keepacha

### Changed

- `PEcAn.uncertainty::get.ensemble.samples()` now samples parameters jointly by default: each ensemble member takes all parameters of a PFT from one randomly drawn row of that PFT's samples. Set the new argument `independent = TRUE` to sample every parameter independently with `method`, the old default (#4238).
- Sensitivity analysis runs are now written by `write.ensemble.configs`; `write.sa.configs` has been removed. The run design carries labels saying which parameter and quantile each run is, and the run ids, database entries and manifest rows are built from those.
- Added `ensemble_downscale()`, a refactored version of `SDA_downscale()`.
- `PEcAn.uncertainty::get.parameter.samples()`: replaced the `save_to_disk` flag (from #3860) with an `outdir` argument (default `settings$outdir`) controlling whether `samples.Rdata` is written; `outdir = NULL` skips the save. Existing callers are unaffected (@omkarrr2533, #4016)
Expand Down
2 changes: 2 additions & 0 deletions modules/uncertainty/NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# PEcAn.uncertainty 1.9.0.9000

* `get.ensemble.samples()` now samples parameters jointly by default: each ensemble member takes all parameters of a PFT from one randomly drawn row of that PFT's samples. Set the new argument `independent = TRUE` to sample every parameter independently with `method`, the old default (#4238).
* `get.ensemble.samples()` matches `param.names` to PFTs by name, not position, and errors when parameters sampled jointly differ in length instead of returning truncated or `NA` draws (#4238).
* `read.ensemble.output()` uses `NA` for missing variables, so each member’s result uses only its own output (#4204).
* `generate_joint_ensemble_design(sobol = TRUE)` now errors when an input is sampled by `"looping"` or parameters by `"halton"` or `"sobol"`. These split into dependent `X1` and `X2` halves and gave wrong Sobol indices (#4134).
* In `run.sensitivity.analysis()`, load `samples` once before the variables loop, avoiding repeated disk reads (#3859).
Expand Down
67 changes: 32 additions & 35 deletions modules/uncertainty/R/ensemble.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#' The model run is indexed first by model run, then by parameter.
#' The model run is indexed first by model run, then by parameter.
#'
#" @section Joint vs Independent Sampling:
#' By default, all parameters are sampled jointly by taking uniform random draws
#' from the row indices of the parameter matrix and using all the parameter
#' values from each selected row. This is probably what you want: It preserves
#' any existing correlations between parameters (e.g. in matrices built by MCMC
#' sampling), and still produces correctly uniform samples when the parameters
#' are uncorrelated.
#' Two arguments can change this behavior:
#' * To sample _some_ parameters of each PFT jointly but others independently,
#' list the parameters to be sampled jointly in `param.names`.
#' * To completely ignore existing correlations and sample _every_ parameter
#' independently in each PFT, set `independent = TRUE`. Only recommended if you
#' need a non-uniform sample and are confident the columns of the parameter
#' matrix were generated independently too (e.g. halton design when all params
#' were drawn from marginal distributions).
#'
#' @section Sampling Methods:
#' The `method` argument is used only for parameters sampled independently.
#' Joint sampling always uses "uniform".
#' Accepted fully random methods are "uniform" (or identically "random") and
#' "lhc" (latin hypercube permutation). Accepted quasi-random methods are
#' "halton" and "sobol"; both give deviates that are deterministic but well
#' equidistributed.
#' For small ensemble size with relatively large parameter number (e.g ensemble
#' size < 5 and # of parameters > 5) use methods other than halton.

#'
#' @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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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 independent = FALSE ... with method..." as in conflict with "...method used ... for parameters sampled independently." Is the following accurate and clear to other folks?

Suggested change
#' @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.
#' @param method the method used to generate the ensemble samples for any
#' parameters that are sampled independently. See details.
#' @param param.names list named by PFT of the parameters to sample jointly.
#' If NULL (default), treated as listing every parameter of each PFT.
#' Ignored when \code{independent} is TRUE.
#' @param independent logical: Ignore correlations between parameters and sample
#' independently from each parameter in each PFT? See details.

If we're iterating even more I'd consider renaming param.names to something like joint_params, but I recognize that's a long-preexisting argument and renaming it gracefully would be a can of worms.

#' @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")
Expand All @@ -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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -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]])
Expand Down
53 changes: 24 additions & 29 deletions modules/uncertainty/R/get_parameter_samples.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,25 @@
#'
#' @param pft_names character vector of PFT names
#' @param prior_distns_list list of data frames (one per PFT), each with
#' columns: distn, parama, paramb, n. Row names are trait names.
#' @param trait_mcmc_list list of trait MCMC results (one per PFT).
#' columns: distn, parama, paramb, n. Row names are parameter names.
#' @param trait_mcmc_list list of parameter MCMC results (one per PFT).
#' Each element should be a named list of \code{coda::mcmc.list} objects,
#' or NULL if no MCMC results exist for that PFT.
#' @param ensemble.size integer. Number of runs in model ensemble.
#' @param ens.sample.method character. One of "halton", "sobol", "torus",
#' "lhc", "uniform".
#' @param ens.sample.method character. One of "random", "uniform", "lhc",
#' "halton", "sobol".
#' @param sa_quantiles numeric vector of quantiles for sensitivity analysis,
#' or NULL to skip SA sampling.
#' @param do_ensemble logical. Whether to generate ensemble samples.
#' @param independent logical. TRUE means parameters were fitted individually
#' (standard MA) and can be resampled independently. FALSE means they were
#' fitted together (e.g. PDA) and correlations should be preserved.
#' @param independent logical. If FALSE (default), parameters with MCMC samples
#' are sampled jointly within each PFT, preserving their correlations (each
#' ensemble member takes one complete MCMC draw of a PFT). If TRUE, every
#' parameter is sampled independently using \code{ens.sample.method},
#' ignoring any joint structure.
#'
#' @return named list with five elements:
#' \describe{
#' \item{trait.samples}{Named list by PFT, then by trait. Each leaf is a
#' \item{trait.samples}{Named list by PFT, then by parameter. Each leaf is a
#' numeric vector of parameter samples.}
#' \item{sa.samples}{Sensitivity analysis samples (list), or empty list
#' if \code{sa_quantiles} is NULL.}
Expand Down Expand Up @@ -69,7 +71,7 @@ get_parameter_samples <- function(pft_names,
ens.sample.method = "uniform",
sa_quantiles = NULL,
do_ensemble = TRUE,
independent = TRUE) {
independent = FALSE) {

# ---- Input validation ----
if (is.list(pft_names)) {
Expand Down Expand Up @@ -103,25 +105,25 @@ get_parameter_samples <- function(pft_names,
for (i in seq_along(pft_names)) {
pft.name <- pft_names[i]
prior.distns <- prior_distns_list[[i]]
trait.mcmc <- if (!is.null(trait_mcmc_list)) trait_mcmc_list[[i]] else NULL
param.mcmc <- if (!is.null(trait_mcmc_list)) trait_mcmc_list[[i]] else NULL

# Determine which traits have priors defined
# Determine which parameters have priors defined
if (!is.null(prior.distns)) {
priors <- rownames(prior.distns)
} else {
priors <- NULL
}

# Determine sample count: use MCMC chain length if available, else 20000
if (!is.null(trait.mcmc) && length(trait.mcmc) > 0) {
param.names[[i]] <- names(trait.mcmc)
if (!is.null(param.mcmc) && length(param.mcmc) > 0) {
param.names[[i]] <- names(param.mcmc)
names(param.names)[i] <- pft.name

samples.num <- min(
sapply(trait.mcmc, function(x) nrow(as.matrix(x)))
sapply(param.mcmc, function(x) nrow(as.matrix(x)))
)

## Report which traits use MA results vs priors
## Report which parameters use MA results vs priors
if (length(param.names[[i]]) > 0) {
PEcAn.logger::logger.info(
"PFT", pft.name, "has MCMC samples for:\n",
Expand Down Expand Up @@ -149,11 +151,11 @@ get_parameter_samples <- function(pft_names,
# If no priors defined, fall back to MCMC param names
if (is.null(priors)) priors <- param.names[[i]]

PEcAn.logger::logger.info("using ", samples.num, " samples per trait")
PEcAn.logger::logger.info("using ", samples.num, " samples per parameter")

n_priors <- length(priors)
if (n_priors == 0) {
PEcAn.logger::logger.info("No traits to sample for PFT", pft.name)
PEcAn.logger::logger.info("No parameters to sample for PFT", pft.name)
next
}

Expand All @@ -168,11 +170,11 @@ get_parameter_samples <- function(pft_names,
q_samples <- matrix(q_samples, ncol = 1)
}

# Sample each trait: from MCMC if available, otherwise from prior
# Sample each parameter: from MCMC if available, otherwise from prior
for (prior in priors) {
if (prior %in% param.names[[i]]) {
# Extract MCMC samples across all chains
samples <- trait.mcmc[[prior]] |>
samples <- param.mcmc[[prior]] |>
purrr::map(\(x) x[, "beta.o"]) |>
unlist() |>
as.matrix()
Expand All @@ -188,14 +190,6 @@ get_parameter_samples <- function(pft_names,
}
}

# ---- Handle independence flag ----
# When samples are independent (fitted individually), set param.names to NULL.
# This is important downstream: when param.names is not NULL,
# get.ensemble.samples() preserves correlations between MCMC parameters.
if (independent) {
param.names <- NULL
}

# ---- Sensitivity analysis samples ----
if (!is.null(sa_quantiles)) {
# get.quantiles() expects a settings-style list with $quantile elements.
Expand Down Expand Up @@ -231,7 +225,8 @@ get_parameter_samples <- function(pft_names,
pft.samples = trait.samples,
env.samples = env.samples,
method = ens.sample.method,
param.names = param.names
param.names = param.names,
independent = independent
)
}
}
Expand All @@ -252,7 +247,7 @@ get_parameter_samples <- function(pft_names,
#' using the specified sampling method.
#'
#' @param n integer, number of samples
#' @param dim integer, number of dimensions (traits)
#' @param dim integer, number of dimensions (parameters)
#' @param method character, sampling method
#' @return matrix of dimension n x dim with values in [0, 1]
#' @noRd
Expand Down
17 changes: 10 additions & 7 deletions modules/uncertainty/man/get.ensemble.samples.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading