| tulpa_hyper_grid | R Documentation |
Generic driver for nested-Laplace-style outer integration over a small
hyperparameter block. The user supplies per-axis specs (values + optional
log-prior + log-scale / bounds / refinable metadata) and an inner_fit
callback that, at every hyperparameter cell, returns the inner log marginal
and optionally the fixed-effect posterior mode + marginal covariance.
The driver builds the Cartesian outer grid, normalises the log-marginals to
integration weights, reports per-axis posterior moments and weighted
quantiles, and (when the inner fit supplies them) law-of-total-covariance
fixed-effect posterior + mixture draws.
This factors the per-family outer-grid plumbing in tulpa_nested_laplace()
/ tulpa_nested_laplace_joint() / tulpa_re_cov_nested() into one
callback-driven entry point: downstream consumers (occupancy / N-mixture /
cover hurdle families in tulpaObs, custom user families) drop in their own
per-cell inner fit and get the outer integration for free.
tulpa_hyper_grid(
hyper_specs,
inner_fit,
combine = c("law_of_total_cov", "weighted_mean_only", "none"),
n_draws = 2000L,
seed = NULL,
beta_names = NULL,
control = list()
)
hyper_specs |
A list of axis specs. Each spec is either a
|
inner_fit |
|
combine |
How to pool per-cell fixed-effect posteriors into a single posterior over the betas. One of:
|
n_draws |
Number of posterior draws of the fixed effects to
synthesise from the cell mixture (default 2000). Used only when
|
seed |
Optional integer seed for the draw synthesis. |
beta_names |
Optional character vector naming the fixed-effect
coordinates. When |
control |
Optional list of refinement / tuning knobs:
|
A list of class c("tulpa_fit", "list") with:
theta_grid – numeric matrix [n_cells x n_axes] of outer-grid
hyperparameter values; columns named after the axes.
theta_names – character vector of axis names.
log_marginal – numeric [n_cells]; per-cell log integrand
inner$log_marginal + log_prior(theta_cell). Non-finite / failed cells
are -Inf.
log_prior – numeric [n_cells]; per-cell log-prior contribution
(the sum across axes of axis$log_prior(theta_cell[axis])). 0 when
all axes have log_prior = NULL.
weights – numeric [n_cells] summing to 1 (or all NA with a
warning when no cell carries finite mass).
theta_mean, theta_sd – named numeric vectors; weighted posterior
mean and SD per axis. Each SD comes from the estimator its axis's own
resolution calls for – the weighted spread of the axis marginal where
it is resolved, the 3-point parabola at the modal node where the
marginal has collapsed onto too few nodes to have a spread.
theta_sd_source, theta_sd_ess, theta_sd_stencil_declined –
per axis, which estimator produced the SD, the quadrature effective
sample size that decided it, and (where the parabola was wanted and
could not be formed) why it declined.
theta_median, theta_ci_lo, theta_ci_hi – named numeric vectors;
weighted-quantile median and 2.5 / 97.5\
(the recommended summary for right-skewed scale-like axes).
beta, beta_cov, draws – fixed-effect posterior, present per
combine:
combine = "law_of_total_cov": weighted mean, total covariance
(E[V] + V[E]), [n_draws x p] mixture draws.
combine = "weighted_mean_only": weighted mean only; beta_cov
and draws are NULL.
combine = "none": all three are NULL.
means, param_names, process_info, n_samples, n_params,
N – the tulpa_fit accessor surface, populated when a
fixed-effect posterior is assembled.
hyper_specs – echoed list of hyper_axis_spec objects (the
normalised form).
combine, n_failed, n_grid, refining_axis – diagnostic fields.
adaptive_grid_info, var_of_means_consistency_info – present when
the corresponding refinement pass fired.
hyper_axis_spec() for the axis-spec constructor;
tulpa_nested_laplace_joint() for the family-specific outer-grid
driver that this helper generalises.
# Integrate a Gaussian linear model's residual SD: inner_fit(hypers) returns
# the per-cell log marginal and the conditional fixed-effect posterior, and
# the axis spec names the grid. tulpa_nested_laplace() is the packaged driver
# built on this.
set.seed(1)
n <- 40L; X <- cbind(1, rnorm(n)); y <- as.numeric(X %*% c(0.5, -1) + rnorm(n))
XtX <- crossprod(X); Xty <- crossprod(X, y)
inner_fit <- function(hypers) {
sigma <- as.numeric(hypers["sigma"])
V <- chol2inv(chol(XtX / sigma^2 + diag(1e-4, 2)))
mu <- as.numeric(V %*% (Xty / sigma^2))
list(log_marginal = -n * log(sigma) - 0.5 * sum((y - X %*% mu)^2) / sigma^2 +
0.5 * as.numeric(determinant(V)$modulus),
beta_mean = stats::setNames(mu, c("b0", "b1")), beta_cov = V)
}
specs <- list(hyper_axis_spec("sigma", grid = exp(seq(log(0.5), log(2),
length.out = 9)),
log_scale = TRUE, bounds = c(0, Inf)))
res <- tulpa_hyper_grid(specs, inner_fit, n_draws = 500L, seed = 1L)
res$theta_median
res$beta
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.