-
Notifications
You must be signed in to change notification settings - Fork 0
Part IV Function Reference 5
← Part IV — Exhaustive Function Reference (4/7) · gdpar Wiki Home · Part IV — Exhaustive Function Reference (6/7) →
gdpar_geom_target(object = NULL, log_prob = NULL, grad_log_prob = NULL, dim = NULL, hessian = NULL, param_names = NULL, data = NULL, simulate = NULL, score = NULL)
Purpose
Constructs a target object that wraps a posterior's log-density and its gradient so the Block RG geometric sampling engine (gdpar_geom_hmc) can evaluate them on the unconstrained scale. Acts as a three-way adapter (decision A): density work is delegated to a compiled cmdstan model, an R closure, or a gdpar_geometry_suite() instance, while the integrator itself runs in pure R.
Arguments
| Argument | Type | Meaning |
|---|---|---|
object |
NULL, a list (suite instance), or a cmdstanr fit/model (CmdStanFit, CmdStanMCMC, CmdStanModel) |
Optional. If supplied the closure arguments are ignored. A suite instance must carry $log_prob, $grad_log_prob, and optionally $dim. A cmdstan object must have been compiled with compile_model_methods = TRUE exposing $grad_log_prob. |
log_prob |
function(theta) or NULL
|
Function of an unconstrained parameter vector returning the log-posterior density. Used only when object is NULL. |
grad_log_prob |
function(theta) or NULL
|
Function returning the gradient of log_prob at theta (unconstrained scale). Used only when object is NULL. |
dim |
positive integer scalar or NULL
|
Dimension of the unconstrained parameter vector. Required for the closure form and for a cmdstan object whose unconstrained dimension cannot always be inferred automatically. |
hessian |
function(theta) or NULL
|
Optional function returning the Hessian matrix of log_prob. Used by the Riemannian level (RG.3). For a cmdstan object the built-in $hessian method is used when available. |
param_names |
character vector or NULL
|
Optional names for the parameters. Falls back to paste0("theta[", seq_len(dim), "]"). |
data |
list or NULL
|
Optional data list forwarded to a one-iteration sampling run when object is an uninstantiated CmdStanModel (needed only to expose $log_prob / $grad_log_prob). |
simulate |
function(theta) or NULL
|
Optional generative function returning one synthetic data set drawn from the model at theta. Supplying both simulate and score makes the target a generative target consumed by gdpar_geom_fisher_simulator. |
score |
function(theta, y) or NULL
|
Optional function returning the gradient of the model's log-likelihood of a data set y at theta on the unconstrained scale (the per-draw score whose outer product is the expected Fisher information). |
Logic / Algorithm
-
Input validation:
simulateandscore, if non-NULL, must be functions; otherwisegdpar_abortwith classgdpar_input_error. -
Suite-instance path (
objectis a list with$log_proband$grad_log_probfunctions): Constructs the target via.gdpar_geom_target_obj, mergingdim,param_names,simulate,scorefromobjectwhere the user-supplied arguments areNULL. Backend is"closure". -
cmdstan path (
objectis aCmdStanFit/CmdStanMCMC/CmdStanModelor has$grad_log_prob): Delegates to.gdpar_geom_target_cmdstan. -
Unrecognised
object:gdpar_abort. -
Closure path (
objectisNULL): Bothlog_probandgrad_log_probmust be functions anddimmust be supplied;dimis asserted to be a count. Constructs via.gdpar_geom_target_objwith backend"closure".
Returns
A list of class c("gdpar_geom_target", "list") with elements:
| Element | Type | Description |
|---|---|---|
log_prob |
function(theta) |
Log-posterior on the unconstrained scale. |
grad_log_prob |
function(theta) |
Gradient of log_prob. |
hessian |
function(theta) or NULL
|
Hessian of log_prob when available. |
dim |
integer | Dimension of the unconstrained parameter space. |
param_names |
character vector | Parameter names. |
backend |
"closure" or "cmdstan"
|
Which backend performs density evaluations. |
simulate |
function(theta) or NULL
|
Generative simulator if provided. |
score |
function(theta, y) or NULL
|
Score function if provided. |
Notes
- The function operates on the unconstrained scale (the scale on which Hamiltonian dynamics act and cmdstan's
$grad_log_probis defined). - When
objectis aCmdStanModel(not yet sampled), a throwaway one-chain, one-warmup-iteration, one-sampling-iteration fit is performed to expose the methods; the data are forwarded viadata. - The
%||%null-coalescing operator is used throughout to prefer user-supplied values over object-carried defaults. - Errors are raised via
gdpar_abortwith class"gdpar_input_error".
.gdpar_geom_target_obj(log_prob, grad_log_prob, hessian, dim, param_names, backend, simulate = NULL, score = NULL)
Purpose
Internal constructor that assembles the raw list with class c("gdpar_geom_target", "list") from already-validated components. Called by gdpar_geom_target for both the closure and suite-instance paths and by .gdpar_geom_target_cmdstan for the cmdstan path.
Arguments
| Argument | Type | Meaning |
|---|---|---|
log_prob |
function(theta) |
Log-posterior (unconstrained scale). |
grad_log_prob |
function(theta) |
Gradient of the log-posterior. |
hessian |
function(theta) or NULL
|
Hessian of the log-posterior (optional). |
dim |
integer | Unconstrained parameter dimension. Coerced to integer. |
param_names |
character vector or NULL
|
Names; defaults to paste0("theta[", seq_len(dim), "]") when NULL. |
backend |
"closure" or "cmdstan"
|
Label indicating the density backend. |
simulate |
function(theta) or NULL
|
Optional generative simulator. |
score |
function(theta, y) or NULL
|
Optional score function. |
Returns
A list of class c("gdpar_geom_target", "list") whose elements are the arguments stored directly (log_prob, grad_log_prob, hessian, dim, param_names, backend, simulate, score).
Notes
- No input validation is performed; callers are responsible for passing valid arguments.
-
param_namesfalls back via%||%to sequential"theta[i]"labels. -
dimis coerced withas.integer.
Purpose
Internal helper that wraps a cmdstanr fit or model so that its $log_prob, $grad_log_prob, and optionally $hessian methods are exposed as plain R closures taking an unconstrained parameter vector.
Arguments
| Argument | Type | Meaning |
|---|---|---|
object |
cmdstanr object (CmdStanModel, CmdStanFit, CmdStanMCMC) |
The compiled cmdstan object whose methods are to be wrapped. |
dim |
integer or NULL
|
Unconstrained parameter dimension. Required; the code aborts if NULL. |
data |
list or NULL
|
Data list forwarded to a one-iteration sampling run when object is a CmdStanModel. |
param_names |
character vector or NULL
|
Optional parameter names. |
simulate |
function(theta) or NULL
|
Optional generative simulator. |
score |
function(theta, y) or NULL
|
Optional score function. |
Logic / Algorithm
- If
objectis aCmdStanModel, a minimal fit is obtained:object$sample(data = data, chains = 1, iter_warmup = 1, iter_sampling = 1, refresh = 0, show_messages = FALSE, show_exceptions = FALSE). This requirescmdstanr(suggested dependency check viarequire_suggested). - If the resulting
fitdoes not expose$grad_log_prob, an error is raised instructing the user to compile withcompile_model_methods = TRUE. - If
dimisNULL, an error is raised. - Three closures are built:
-
lp(theta)callsfit$log_prob(unconstrained_variables = theta). -
gl(theta)callsfit$grad_log_prob(unconstrained_variables = theta), coerced withas.numeric. -
he(theta)(whenfit$hessianexists) callsfit$hessian(unconstrained_variables = theta)$hessian; otherwiseNULL.
-
- Delegates to
.gdpar_geom_target_objwith backend"cmdstan".
Returns
A gdpar_geom_target list (same structure as .gdpar_geom_target_obj).
Notes
- The throwaway fit is never used for inference; it exists solely to expose the model methods.
- The
as.numericcoercion on the gradient ensures a plain numeric vector regardless of cmdstanr's internal return type. - Errors raised:
"gdpar_input_error"if$grad_log_probis missing, or ifdimisNULL.
Purpose
S3 print method for gdpar_geom_target objects. Provides a concise summary of the target's backend, dimension, Hessian availability, and generative status.
Arguments
| Argument | Type | Meaning |
|---|---|---|
x |
gdpar_geom_target |
The target object to print. |
... |
— | Unused; present for S3 method compatibility. |
Returns
Invisibly returns x (standard print-method convention).
Side Effects Prints two lines to the console:
<gdpar_geom_target> backend: <backend> | dim: <dim>
hessian available: <TRUE/FALSE> | generative: <TRUE/FALSE>
The generative flag is TRUE when both x$simulate and x$score are non-NULL.
Notes
- No validation is performed on
x.
Purpose Constructs the level-0/1 Euclidean (position-independent) metric for the Block RG geometry hierarchy. With the identity matrix (default) it is the standard diagonal Euclidean metric; with a user-supplied symmetric positive-definite matrix or diagonal variance vector it becomes a constant dense preconditioner — the remedy for anisotropic posteriors (straight canyons).
Arguments
| Argument | Type | Meaning |
|---|---|---|
dim |
positive integer or NULL
|
Dimension. Required when M is NULL. |
M |
symmetric positive-definite dim × dim matrix, length-dim positive numeric vector, or NULL
|
Mass matrix. A vector is interpreted as diagonal entries and expanded to diag(M). Defaults to the dim × dim identity. |
Logic / Algorithm
- If
MisNULL,dimmust be supplied and is asserted to be a count.Mis set todiag(dim). - If
Mis a vector (not a matrix), its entries are validated as finite and positive, thenM <- diag(M). -
Mis coerced to a matrix and checked for squareness. - Cholesky decomposition
ch <- chol(M)is attempted; failure raises an error (not symmetric positive-definite). - Derived quantities are computed once:
-
Minv <- chol2inv(ch)— the inverse$M^{-1}$ . -
L <- t(ch)— the lower Cholesky factor$\mathbf{L}$ such that$M = \mathbf{L}\mathbf{L}^\top$ ; momenta are drawn as$\mathbf{p} = \mathbf{L}\mathbf{z}$ with$\mathbf{z} \sim \mathcal{N}(0, I)$ . -
ld <- 2 * sum(log(diag(ch)))—$\log\det M$ .
-
Mathematics
Returns
A list of class c("gdpar_geom_metric", "list") with:
| Element | Type | Description |
|---|---|---|
position_dependent |
FALSE |
Euclidean metric is constant. |
dim |
integer | Dimension |
mass |
function(theta) → matrix |
Returns theta). |
inv_mass |
function(theta) → matrix |
Returns theta). |
chol_mass |
function(theta) → matrix |
Returns lower Cholesky factor theta). |
logdet |
function(theta) → scalar |
Returns theta). |
Notes
- All four accessor functions accept
thetafor interface compatibility with the Riemannian metric (which is position-dependent) but ignore it entirely. - The identity metric (
M = NULL, nodimwith a vector) is the default for Euclidean HMC. - Errors:
"gdpar_input_error"if neitherdimnorMis supplied; ifMis not square; ifMhas non-finite or non-positive diagonal entries (vector form); ifMis not symmetric positive-definite (Cholesky fails).
gdpar_geom_metric_riemannian(target, curvature, fisher = NULL, dfisher = NULL, alpha = 1e6, floor = 1e-8, fd_step = 1e-5)
Purpose
Constructs the level-3 Riemannian (position-dependent) metric of the Block RG geometry hierarchy: a mass matrix
Arguments
| Argument | Type | Meaning |
|---|---|---|
target |
gdpar_geom_target or object accepted by gdpar_geom_target
|
Provides the Hessian/gradient for SoftAbs and the parameter dimension. |
curvature |
"fisher" or "softabs"
|
Curvature source selector. |
fisher |
function(theta) → matrix or NULL
|
For curvature = "fisher": returns the theta. |
dfisher |
function(theta) → list or NULL
|
For curvature = "fisher": returns a length-$d$ list whose NULL, derivatives are finite-differenced from fisher. |
alpha |
positive scalar (default 1e6) |
SoftAbs softening parameter |
floor |
positive scalar (default 1e-8) |
Minimum eigenvalue imposed on |
fd_step |
positive scalar (default 1e-5) |
Finite-difference step size for: (a) the Hessian in the SoftAbs path when target$hessian is NULL; (b) the metric derivative |
Mathematics
SoftAbs regularisation. Given the Hessian
where floor is applied to the eigenvalues.
Fisher metric. dfisher or computed by central finite differences:
Returns
A list of class c("gdpar_geom_metric", "list") with:
| Element | Type | Description |
|---|---|---|
position_dependent |
TRUE |
Riemannian metric varies with |
dim |
integer | Parameter dimension |
mass |
function(theta) → matrix |
|
inv_mass |
function(theta) → matrix |
|
chol_mass |
function(theta) → matrix |
Lower Cholesky factor of |
logdet |
function(theta) → scalar |
|
dmass |
function(theta) → list |
Length-$d$ list of |
Notes
- The metric is a preconditioner, not part of the target density; the Metropolis correction with the exact log-density ensures correctness regardless of metric quality.
- The Riemannian interface is a strict superset of the Euclidean interface (it adds
dmass), sogdpar_geom_hmccan dispatch uniformly. - SoftAbs requires the Hessian: it uses
target$hessianif available, otherwise finite-differences it fromtarget$grad_log_probwith stepfd_step. - The
fishersource is the primary, maximally robust choice; its learned amortisation across model families is a separate sub-phase. - Errors follow
gdpar_abortwith class"gdpar_input_error".
gdpar_geom_metric_riemannian(target, curvature = c("fisher", "softabs"), fisher = NULL, dfisher = NULL, alpha = 1e6, floor = 1e-8, fd_step = 1e-4)
Purpose
Constructs a position-dependent Riemannian metric (mass matrix) for a target distribution using either the exact expected Fisher information matrix (if available and closed-form) or the SoftAbs transform of the Hessian of the negative log-probability. This is the primary factory for Capa 1 metrics in the Block RG hierarchy.
Arguments
-
target: Agdpar_geom_targetobject or an object coercible to it viagdpar_geom_target(). Must provide at leastdim,grad_log_prob, and optionallyhessian. -
curvature: Character string, either"fisher"or"softabs", selecting the curvature estimator. -
fisher: A functiontheta -> matrixreturning the expected Fisher information matrix attheta. Required whencurvature = "fisher". -
dfisher: Optional functiontheta -> list of matricesreturning the partial derivatives of the Fisher matrix with respect to each component oftheta. IfNULLandcurvature = "fisher", derivatives are computed via finite differences. -
alpha: Positive scalar controlling the SoftAbs softening parameter (larger values yield a sharper approximation of the absolute eigenvalue). Used only forcurvature = "softabs". -
floor: Non-negative scalar; eigenvalues of the mass matrix are floored to this value to ensure positive definiteness. -
fd_step: Positive scalar step size for central finite-difference approximations of gradients and Hessians.
Mathematics
-
Fisher case:
$M(\theta) = \text{floor\_spd}(\text{fisher}(\theta))$ , where$\text{floor\_spd}$ projects onto the SPD cone. -
SoftAbs case: Let
$H_U(\theta)$ be the Hessian of$-\log p(\theta)$ . The mass matrix is$M(\theta) = Q \cdot \text{diag}\bigl(\lambda_i \coth(\alpha \lambda_i)\bigr) \cdot Q^\top$ , where$H_U = Q \Lambda Q^\top$ is the eigendecomposition, with eigenvalues$\lambda_i$ floored tofloor. Derivatives use the Daleckii–Krein formula.
Returns
A list of class gdpar_geom_metric (with position_dependent = TRUE) containing functions:
-
mass(theta): the mass matrix$M(\theta)$ . -
inv_mass(theta):$M(\theta)^{-1}$ . -
chol_mass(theta): the upper Cholesky factor of$M(\theta)$ . -
logdet(theta):$\log\det M(\theta)$ . -
dmass(theta): a list of matrices$\partial M / \partial \theta_k$ .
Also includes metadata fields: dim, metric_kind ("fisher" or "softabs"), alpha.
Notes
- Raises an error of class
"gdpar_input_error"ifcurvature = "fisher"andfisheris not a function. - For the SoftAbs case, if the target does not expose a
hessianmethod, the Hessian is approximated by central finite differences of the gradient (symmetrized). - The returned object uses internal caching to reuse the Cholesky factorization across calls for the same
theta(optimization for the implicit leapfrog integrator).
Purpose
Internal helper that assembles a gdpar_geom_metric object from supplied mass and derivative functions, incorporating a caching mechanism for the mass matrix, its Cholesky factor, and inverse at the last queried position. This avoids redundant factorizations during fixed-point iterations of the implicit leapfrog.
Arguments
-
mass_fn: A functiontheta -> matrixreturning the mass matrix$M(\theta)$ . -
dmass_fn: A functiontheta -> list of matricesreturning$\partial M(\theta)/\partial \theta_k$ . -
d: Integer, the dimension of the parameter space. -
kind: Character string indicating the metric type (e.g.,"fisher","softabs","gp_fisher"). -
alpha: Numeric scalar; the SoftAbs smoothing parameter (used for metadata).
Returns
A list of class gdpar_geom_metric with the standard interface (mass, inv_mass, chol_mass, logdet, dmass), plus fields position_dependent = TRUE, dim, metric_kind, alpha. The functions mass, inv_mass, chol_mass, and logdet are backed by a cache that stores the last evaluated theta, its mass matrix, Cholesky factor, inverse, and log-determinant.
Notes
- The cache is implemented via an environment (
new.env(parent = emptyenv())). - The
ensurefunction updates the cache only if the inputthetadiffers from the cached one (usingidentical).
Purpose
Computes the lower Cholesky factor of a symmetric matrix, with symmetrization and fallback jitter to handle numerically near-singular but valid metric matrices.
Arguments
-
M: A square numeric matrix (assumed symmetric or nearly symmetric).
Mathematics
- Symmetrize:
$M \leftarrow (M + M^\top)/2$ . - Attempt Cholesky decomposition
$M = L L^\top$ . - If it fails, add a jitter
$jI$ to the diagonal, where$j = 10^{-8} \cdot \max(|\mathrm{diag}(M)|, 1)$ , and recompute.
Returns
The lower Cholesky factor
Notes
- The jitter ensures the matrix becomes strictly positive definite, preserving SPD structure for metrics that are theoretically SPD but numerically borderline.
Purpose
Projects a symmetric matrix onto the symmetric positive-definite (SPD) cone by flooring its eigenvalues.
Arguments
-
M: A square numeric matrix (symmetric). -
floor: Non-negative scalar; the minimum allowed eigenvalue.
Mathematics
- Symmetrize:
$M \leftarrow (M + M^\top)/2$ . - Eigendecomposition:
$M = Q \Lambda Q^\top$ . - Floor eigenvalues:
$\Lambda' = \max(\Lambda, \text{floor})$ . - Reconstruct:
$M' = Q \Lambda' Q^\top$ .
Returns
The projected matrix floor.
Notes
- This operation is idempotent if
Mis already SPD with eigenvalues ≥floor.
Purpose
Evaluates the SoftAbs transform
Arguments
-
lambda: Numeric vector of eigenvalues. -
alpha: Positive scalar.
Mathematics
Let
$$ f(\lambda) = \frac{1}{\alpha} \times \begin{cases} \frac{u}{\tanh u} & \text{if } |u| \ge 10^{-4}, \ 1 + \frac{u^2}{3} & \text{if } |u| < 10^{-4} \end{cases} $$
The near-zero expansion uses the series of
Returns
A numeric vector of the same length as lambda containing
Notes
- The threshold
$10^{-4}$ is chosen to avoid loss of precision in the direct formula.
Purpose
Computes the derivative of the SoftAbs transform
Arguments
-
lambda: Numeric vector of eigenvalues. -
alpha: Positive scalar.
Mathematics
Let
$$ f'(\lambda) = \frac{1}{\alpha} \times \begin{cases} \frac{1}{\tanh u} - \frac{u}{\sinh^2 u} & \text{if } |u| \ge 10^{-3}, \ \frac{2}{3} u & \text{if } |u| < 10^{-3} \end{cases} $$
The near-zero expansion uses the Taylor series
Returns
A numeric vector of derivatives, same length as lambda.
Notes
- The threshold
$10^{-3}$ balances accuracy and stability; the linear approximation is valid for small$u$ .
Purpose
Constructs the SoftAbs mass matrix from the Hessian of the negative log-probability.
Arguments
-
HU: Square matrix, the Hessian of$-\log p(\theta)$ (symmetric). -
alpha: Positive scalar for SoftAbs. -
floor: Non-negative scalar for eigenvalue flooring.
Mathematics
- Symmetrize
$HU$ . - Eigendecomposition:
$HU = Q \Lambda Q^\top$ . - Transform eigenvalues:
$\tilde{\lambda} = \max(f(\lambda), \text{floor})$ , where$f$ is the SoftAbs function. - Reconstruct:
$M = Q \mathrm{diag}(\tilde{\lambda}) Q^\top$ .
Returns
The SPD mass matrix
Notes
- The flooring is applied after the SoftAbs transform to ensure positive definiteness even for zero or negative eigenvalues of
$HU$ .
Purpose
Computes the derivative of the SoftAbs mass matrix with respect to each component of
Arguments
-
theta: Numeric vector, current parameter position. -
hess_lp_fn: Function returning the Hessian of the log-probability (i.e.,$-\text{Hessian of } \log p$ ishess_lp_fn(theta)). -
alpha: Positive scalar for SoftAbs. -
h: Positive scalar finite-difference step.
Mathematics
- Compute
$HU = -\text{hess\_lp\_fn}(\theta)$ and its eigendecomposition$HU = Q \Lambda Q^\top$ . - Compute the matrix of divided differences
$R$ :
$R_{ij} = \begin{cases} \frac{f(\lambda_i) - f(\lambda_j)}{\lambda_i - \lambda_j} & \text{if } |\lambda_i - \lambda_j| > 10^{-8}, \ f'((\lambda_i + \lambda_j)/2) & \text{otherwise}. \end{cases}$ - For each component
$k = 1,\dots,d$ :- Finite-difference the Hessian:
$dHU_k = \frac{HU(\theta + h e_k) - HU(\theta - h e_k)}{4h} + \text{symmetric part}$ ? Wait, the code does:This is a central difference of the symmetrized Hessian.HUp <- -hess_lp_fn(theta + e); HUm <- -hess_lp_fn(theta - e) dHU <- ((HUp + t(HUp)) - (HUm + t(HUm))) / (4 * h)
- Compute
$A = Q^\top dHU_k Q$ . - Then
$\frac{\partial M}{\partial \theta_k} = Q (R \odot A) Q^\top$ , where$\odot$ is the Hadamard (element-wise) product.
- Finite-difference the Hessian:
Returns
A list of length
Notes
- The divided-difference matrix
$R$ accounts for repeated eigenvalues; the derivative at a repeated eigenvalue is taken as$f'(\lambda)$ (the derivative, which is well-defined). - The finite-difference step
his applied symmetrically to reduce bias.
Purpose
Returns a function that computes the Hessian of the log-probability, either analytically (if the target exposes a hessian method) or via central finite differences of the gradient.
Arguments
-
target: Agdpar_geom_targetobject (or similar) that must have agrad_log_probmethod and optionally ahessianmethod. -
h: Positive scalar finite-difference step.
Mathematics
If no analytic Hessian is available, the Hessian
$$ H_{:,k} \approx \frac{\nabla \log p(\theta + h e_k) - \nabla \log p(\theta - h e_k)}{2h}, $$
and then symmetrized:
Returns
A function theta -> matrix returning the Hessian of the log-probability at theta.
Notes
- The returned function is used by the SoftAbs machinery to obtain
$HU = -\text{Hessian}$ .
Purpose
Approximates the derivative of a matrix-valued mass function by central finite differences.
Arguments
-
mass_fn: A functiontheta -> matrixreturning the mass matrix. -
theta: Numeric vector, current position. -
h: Positive scalar finite-difference step.
Mathematics
For each component
$$ \frac{\partial M}{\partial \theta_k} \approx \frac{\text{mass_fn}(\theta + h e_k) - \text{mass_fn}(\theta - h e_k)}{2h}. $$
Returns
A list of matrices, one per component of theta, representing the partial derivatives of the mass matrix.
Notes
- Used when an analytic derivative is not provided (e.g., for the Fisher case when
dfisherisNULL).
Purpose
Generates the row and column indices of the lower-triangular part of a
Arguments
-
d: Integer, the dimension of the matrix.
Returns
A matrix with two columns and (row, col) index of an element in the lower triangle (including diagonal), ordered column by column (i.e., all indices for column 1, then column 2, etc.).
Notes
- The order is consistent with R's
lower.tri(..., diag = TRUE)and the column-major storage order.
Purpose
Computes the log-Cholesky parametrisation of an SPD matrix
Arguments
-
M: A symmetric positive-definite matrix. -
idx: A matrix of lower-triangular indices as returned by.gdpar_geom_tri_index(d).
Mathematics
- Symmetrize
$M$ and compute its lower Cholesky factor:$M = L L^\top$ . - Define
$\psi$ such that for each index$(i,j)$ inidx:- If
$i = j$ (diagonal),$\psi_{\text{idx}} = \log(L_{ii})$ . - If
$i > j$ (off-diagonal),$\psi_{\text{idx}} = L_{ij}$ .
- If
Returns
A list with components:
-
L: The lower Cholesky factor (a lower-triangular matrix). -
psi: Numeric vector of length$d(d+1)/2$ containing the log-Cholesky parameters.
Notes
- This parametrisation ensures that any
$\psi \in \mathbb{R}^{d(d+1)/2}$ maps to a unique SPD matrix$M = L L^\top$ with$L_{ii} = \exp(\psi_{ii}) > 0$ , making the map smooth and surjective onto the SPD cone.
Purpose
Reconstructs the lower Cholesky factor
Arguments
-
psi: Numeric vector of length$d(d+1)/2$ , the log-Cholesky parameters. -
idx: Matrix of lower-triangular indices (from.gdpar_geom_tri_index(d)). -
d: Integer, the dimension.
Mathematics
For each index idx:
- If
$i = j$ (diagonal),$L_{ij} = \exp(\psi_{\text{idx}})$ . - If
$i > j$ (off-diagonal),$L_{ij} = \psi_{\text{idx}}$ .
All other entries of$L$ are zero.
Returns
A lower-triangular matrix
Purpose
Computes the derivative of the mass matrix
Arguments
-
L: The current lower Cholesky factor (lower-triangular matrix). -
dpsi: Numeric vector of the same length aspsi, representing$d\psi$ . -
idx: Matrix of lower-triangular indices. -
d: Integer, the dimension.
Mathematics
First, construct
- For diagonal entries (
$i = j$ ):$dL_{ii} = L_{ii} \cdot d\psi_{ii}$ (since$\psi_{ii} = \log L_{ii}$ , so$dL_{ii}/L_{ii} = d\psi_{ii}$ ). - For off-diagonal entries (
$i > j$ ):$dL_{ij} = d\psi_{ij}$ .
Then, the derivative of
$$ dM = dL \cdot L^\top + L \cdot dL^\top. $$
Returns
A symmetric matrix
Purpose
Computes the derivative of the log-Cholesky parameters .gdpar_geom_dM_from_dpsi.
Arguments
-
L: The current lower Cholesky factor (lower-triangular matrix). -
dM: A symmetric matrix representing$dM$ . -
idx: Matrix of lower-triangular indices. -
d: Integer, the dimension.
Mathematics
- Compute
$P = L^{-1} dM L^{-\top}$ (symmetric). - Define
$\Phi$ as the lower-triangular part of$P$ with the diagonal halved:-
$\Phi_{ij} = P_{ij}$ for$i > j$ , -
$\Phi_{ii} = P_{ii}/2$ , -
$\Phi_{ij} = 0$ for$i < j$ .
-
- Compute
$dL = L \Phi$ . - Extract
$d\psi$ from$dL$ :- For diagonal entries:
$d\psi_{ii} = dL_{ii} / L_{ii}$ . - For off-diagonal entries:
$d\psi_{ij} = dL_{ij}$ .
- For diagonal entries:
Returns
A numeric vector
Notes
- This function is the differential of the inverse map from
$M$ to$\psi$ , useful for backpropagation or sensitivity analysis in the log-Cholesky space.
gdpar_geom_metric_gp_fisher(target, fisher, sites, weights = NULL, lengthscale = NULL, nugget = 1e-6, alpha = 1e6, floor = 1e-8, fd_step = 1e-4)
Purpose
Constructs a learned Gaussian-process (GP) Riemannian metric that serves as a surrogate for the expected Fisher information matrix. The GP models the residual between the SoftAbs curvature (the always-available Capa 1 metric) and the true Fisher information, in log-Cholesky parametrisation. This is the Capa 2 metric for the general case where the Fisher has no closed form.
Arguments
-
target: Agdpar_geom_targetobject (or coercible) providing the SoftAbs mean function (via its Hessian/gradient) and the dimension. -
fisher: A functiontheta -> matrixthat returns the expected Fisher information matrix attheta(the ground truth for training). -
sites: An$m \times d$ matrix of reservoir positions (one per row) where the Fisher information is evaluated to train the GP. -
weights: Optional positive numeric vector of length$m$ ; importance weights for the sites (e.g., to correct for biased reservoir sampling). Defaults to equal weights. -
lengthscale: Optional positive scalar; the RBF kernel lengthscale on standardised inputs. IfNULL, defaults to the median pairwise Euclidean distance among the standardised sites. -
nugget: Non-negative scalar; the GP observation noise variance. Small values interpolate the Fisher at sites; larger values smooth it. -
alpha,floor,fd_step: Parameters passed to the SoftAbs machinery for the mean function (same as ingdpar_geom_metric_riemannian).
Mathematics
The surrogate models the log-Cholesky parameters
$$ \psi(\theta) = \psi_{\text{SoftAbs}}(\theta) + \delta(\theta), $$
where
$$ k(\theta, \theta') = \sigma^2 \exp\left( -\frac{|\theta - \theta'|^2}{2 \ell^2} \right) + \text{nugget} \cdot \delta_{\theta,\theta'}. $$
The GP is trained on the residuals
Returns
A list of class gdpar_geom_metric (with position_dependent = TRUE, metric_kind = "gp_fisher") containing the standard metric interface:
-
mass(theta),inv_mass(theta),chol_mass(theta),logdet(theta),dmass(theta).
Additionally: -
novelty(theta): A function returning the predictive standard deviation of$\psi$ attheta(a measure of epistemic uncertainty; higher values indicate out-of-distribution regions). -
n_sites: Integer, the number of training sites. -
lengthscale: The lengthscale used (may be the computed median if not provided).
Notes
- The metric degrades gracefully to the SoftAbs mean when far from the reservoir because the kernel decays to zero.
- The sampler remains exact because the metric is only a preconditioner; the Metropolis correction uses the exact target density.
- The derivative
dmass(theta)is computed analytically: the GP derivative plus the Daleckii–Krein derivative of the SoftAbs mean, all pushed through the log-Cholesky map. - The GP is implemented using exact (non-sparse) inference; for large reservoirs, this may be computationally expensive.
- The function performs input standardisation (zero mean, unit variance) internally before applying the kernel.
gdpar_geom_metric_gp_fisher(target, fisher, sites, weights = NULL, lengthscale = NULL, nugget = 1e-6, alpha = 1e6, floor = 1e-8, fd_step = 1e-4)
Purpose Constructs a learned, position-dependent Riemannian metric whose mass matrix is a Gaussian-process surrogate of the expected Fisher information (in log-Cholesky coordinates). The GP's mean function is the SoftAbs approximation to the negative log-posterior Hessian (the deterministic "cold-start" curvature), and the process learns the smooth residual between the true expected Fisher and that SoftAbs mean evaluated at a set of reservoir sites. This implements the highest fidelity level of the Block RG hierarchy, where the Fisher information is approximated point-wise via an RBF interpolant over the log-Cholesky parameterisation of SPD matrices.
Arguments
-
target— Agdpar_geom_targetobject (or any object accepted by its constructor). Provideslog_prob,grad_log_prob, anddim. Converted internally if needed. -
fisher— A functionfisher(theta)returning thedim × dimexpected Fisher information matrix at positiontheta. Must be supplied by the caller (e.g. the output ofgdpar_geom_fisher_simulatoror a closed-form evaluator). -
sites— A numeric matrix withdcolumns, each row a reservoir site where the Fisher and SoftAbs are evaluated to train the GP. If a bare numeric vector of lengthdis passed it is reshaped into a single-row matrix. Must have at least two rows. -
weights— Optional positive numeric vector of lengthm = nrow(sites). Importance weights for the reservoir sites; when non-NULL, the nugget noise at each site is scaled asnugget / (w / mean(w)), implementing importance-weighted GP regression (inverse-variance weighting where higher-weight sites receive less noise). -
lengthscale— Positive scalar length-scale of the RBF kernel. IfNULL, set automatically by the median-distance heuristic: compute pairwise Euclidean distances in the standardised site matrix, take the median of strictly positive distances; if that fails, default tosqrt(d). -
nugget— Non-negative scalar; the base diagonal noise added to the kernel matrix. Ensures numerical positive-definiteness ofK + noise. -
alpha— Non-negative scalar SoftAbs hyperparameter controlling the smoothness of the absolute-value approximation to eigenvalues (larger = sharper). Passed to.gdpar_geom_softabs_mass. -
floor— Non-negative scalar; minimum eigenvalue floor applied inside the SoftAbs mass computation. -
fd_step— Positive scalar finite-difference step for the Hessian approximation and for the derivative of the SoftAbs mass matrix. Passed to.gdpar_geom_hessian_fnand.gdpar_geom_softabs_dmass.
Mathematics
The function maps each site
The RBF (squared-exponential) kernel is
with
At every site
The GP target is the residual
At a new position
where
For derivatives, the GP correction to the log-Cholesky differential is
where
The novelty (predictive standard deviation at a test point, without the noise term) is
Returns A list of class c("gdpar_geom_metric", "list") with position_dependent = TRUE and metric_kind = "gp_fisher". Contains closures mass(theta), inv_mass(theta), chol_mass(theta), logdet(theta), dmass(theta) (returning a length-d list of SPD matrices), and novelty(theta). Also exposes dim, alpha, n_sites, lengthscale.
Notes
- All mutable per-position state is cached in an internal environment
st; repeated calls at the samethetaskip recomputation (guarded byidentical). - The SoftAbs mean serves as a structural control variate: it is deterministic and carries the bulk of the curvature; the GP learns only the smooth residual, so few reservoir sites suffice.
-
dmasscomputes the derivative of the SoftAbs mean analytically via.gdpar_geom_softabs_dmass(cached on first derivative call) and adds the GP correction analytically. - Input validation raises errors of class
"gdpar_input_error"viagdpar_abortfor malformed inputs. - The log-Cholesky index set
.gdpar_geom_tri_index(d)produces$q$ upper-triangular entries of the$d \times d$ factor.
gdpar_geom_reservoir(target, n_sites = 50L, metric = NULL, epsilon = 0.25, L = 15L, n_warmup = 200L, init = NULL, seed = NULL)
Purpose Runs a short HMC warmup to collect a reservoir of positions that are later used as the sites argument of gdpar_geom_metric_gp_fisher. This is the first phase of the two-phase, decoupled-archivist design (ORPHEUS-PIMC-A section 16): a cheap Euclidean (or SoftAbs) warmup provides geometrically representative sites at which the expensive expected Fisher is evaluated.
Arguments
-
target— Agdpar_geom_target(or any object accepted by its constructor). Converted internally. -
n_sites— Integer: the number of retained post-warmup draws to return (the reservoir size). Must be a positive integer (validated byassert_count). -
metric— Optional metric object for the warmup HMC run; defaults to the Euclidean identity insidegdpar_geom_hmc. Agdpar_geom_metric_riemannianSoftAbs metric is recommended for curved targets. -
epsilon— Positive scalar: leapfrog step size for the warmup run. -
L— Positive integer: trajectory length (number of leapfrog steps) for the warmup run. -
n_warmup— Non-negative integer: number of discarded warmup iterations before retaining draws. -
init— Optional numeric vector of lengthdimgiving the initial position; defaults to zeros insidegdpar_geom_hmc. -
seed— Optional integer seed for reproducibility of the HMC run.
Mathematics None beyond the underlying HMC sampler.
Returns A numeric matrix of dimension n_sites × dim, the retained reservoir positions.
Notes
- The function is a thin wrapper: it calls
gdpar_geom_hmcwithn_iter = n_sitesand returnsfit$draws. - The reservoir design decouples the (potentially expensive) Fisher evaluation from the sampling run, allowing the surrogate to be trained offline.
Purpose Derives a deterministic, non-negative integer key from a position vector so that simulation-based evaluations at gdpar_geom_fisher_simulator to reseed the RNG before each expected-Fisher evaluation.
Arguments
-
theta— A numeric vector (the position). Non-finite entries are replaced with zero.
Mathematics
The primes 1009 and 7919 are chosen to decorrelate nearby positions while keeping the computation cheap. The modulus 2147483646 =
Returns A single integer in set.seed.
Notes
- Non-finite entries of
thetaare silently zeroed before the computation. - The key is designed to be a fast hash, not a cryptographic hash; collisions are possible but benign (they merely produce correlated simulations, which the GP surrogate absorbs).
Purpose Constructs a deterministic, simulation-based estimator of the expected Fisher information matrix for generative models where no closed-form expression exists. The returned function is suitable as the fisher argument of gdpar_geom_metric_gp_fisher or gdpar_geom_metric_subriemannian.
Arguments
-
target— Agdpar_geom_target(or any object accepted by its constructor) that must carry bothsimulate(theta)(draws one synthetic data set from the model at$\theta$ ) andscore(theta, y)(returns the gradient of the log-likelihood of$y$ at$\theta$ ). If either is missing, an error is raised. -
n_sim— Positive integer: the number of simulated data sets averaged per evaluation. Should comfortably exceeddimto ensure the raw estimate is non-singular before the eigenvalue floor is applied. -
seed— Positive integer: the base seed combined with the position key to produce the sub-seed for each evaluation. -
floor— Non-negative scalar: minimum eigenvalue imposed on the averaged matrix to guarantee strict positive-definiteness (required downstream by the log-Cholesky map).
Mathematics
The expected Fisher information is
where
This is positive semi-definite by construction (sum of PSD rank-one terms) and unbiased. The eigenvalue floor is applied as
via .gdpar_geom_floor_spd.
The sub-seed for reproducibility is
Returns A closure fisher(theta) returning a dim × dim symmetric strictly-positive-definite matrix (the estimated expected Fisher), with attribute "n_sim" recording the number of simulations.
Notes
- Each call saves and restores the global RNG state (
.Random.seed) so that evaluations are independent of call order — the position key makes the function deterministic in$\theta$ . - The docstring explicitly notes that antithetic simulation would give no variance reduction for location families because the score is odd in the centred data, hence
$(-s)(-s)^\top = ss^\top$ . - Input validation raises
"gdpar_input_error"ifsimulateorscoreare missing from the target, or if the score output is not a finite vector of lengthdim.
Purpose Performs the exact, closed-form harmonic (Gaussian) flow of the reference quadratic used as the middle step of the sub-Riemannian Strang integrator. This is the exact symplectic rotation in each eigenmode, replacing the leapfrog step for the stiff wall modes.
Arguments
-
theta— Numeric vector of length$d$ : the current position. -
p— Numeric vector of length$d$ : the current momentum. -
ref— Numeric vector of length$d$ : the reference position$\theta^\ast$ . -
U— Orthogonal$d \times d$ matrix: the eigenvectors of the reference Fisher (columns are eigenvectors), defining the modal coordinates$z = U^\top(\theta - \text{ref})$ ,$q = U^\top p$ . -
omega— Numeric vector of length$d$ : the modal frequencies$\omega_i = \sqrt{w_i \lambda_i}$ where$w_i$ is the verticality weight and$\lambda_i$ the Fisher eigenvalue. -
t— Scalar: the flow time (half the leapfrog step, or the full step for the exact flow).
Mathematics
Each mode
For
The implementation uses ifelse(drift, ...) element-wise to blend between the two regimes without branching.
In global coordinates:
Returns A named list with components theta (numeric vector, the advanced position) and p (numeric vector, the advanced momentum).
Notes
- The flow is exact and reversible for any time step
$t$ ; there is no step-size penalty from stiff (high-$\omega$) modes, which is the core advantage of the sub-Riemannian integrator. - The
ifelseon thedriftmask ensures numerical stability as$\omega_i \to 0$ :$\sin(\omega t)/\omega \to t$ and$\omega\sin(\omega t) \to 0$ .
gdpar_geom_metric_subriemannian(target, fisher, reference = NULL, tau = NULL, softness = 1, floor = 1e-8)
Purpose Constructs the level-5 geometry of the Block RG hierarchy: a sub-Riemannian metric for quasi-deterministic posteriors where the typical set contracts onto a lower-dimensional manifold. The expected Fisher information grows without bound along stiff "wall" directions while soft "floor" directions carry genuine variation. The metric equips only a distribution
Arguments
-
target— Agdpar_geom_target(or any object accepted by its constructor). Converted internally. -
fisher— A functionfisher(theta)returning thedim × dimexpected Fisher information matrix at positiontheta. For models without a closed form, usegdpar_geom_fisher_simulator. Evaluated once at the reference position. -
reference— Optional numeric vector of lengthdimat which the Fisher and the reference quadratic are evaluated. Defaults to the zero vector. -
tau— Positive scalar: the eigenvalue threshold of the verticality filter (directions with eigenvalue$\lambda \gg \tau$ are walls). Defaults to the smallest eigenvalue of the (floored) Fisher at the reference, so that the leapfrog residual curvature is capped at the floor scale and the step is never limited by stiff walls. -
softness— Positive scalar: the logistic width of the verticality filter in log-eigenvalue space. Small values approximate a hard floor/wall split; the default 1 keeps the blend smooth. -
floor— Non-negative scalar: minimum eigenvalue imposed on the Fisher before the filter is applied, so genuinely flat directions remain well-defined.
Mathematics
At the reference position
A continuous verticality filter assigns each eigendirection a weight in
where softness parameter. Directions with
The wall curvature matrix is
with modal frequencies
The integrator uses a Strang splitting with a Euclidean kinetic energy
- Half momentum kick:
$p \leftarrow p + \frac{\epsilon}{2},\nabla U_{\text{rest}}(\theta)$ where$U_{\text{rest}} = -\log p(\theta) - \frac{1}{2}(\theta - \theta^\ast)^\top A,(\theta - \theta^\ast)$ . - Exact harmonic flow of the reference quadratic for time
$\epsilon$ :.gdpar_geom_subriemann_flow. - Second half momentum kick.
The residual gradient is
Returns A list of class c("gdpar_geom_metric", "list") with position_dependent = FALSE and metric_kind = "sub_riemannian". The mass functions (mass, inv_mass, chol_mass, logdet) all return the identity (the kinetic energy is Euclidean). An integrator closure is provided for consumption by gdpar_geom_hmc. Diagnostic fields include: reference, eigenvalues, verticality (the frequencies (the n_walls (count of directions with tau, softness, and suggested_epsilon (a leapfrog step matched to the floor scale).
Notes
- The Metropolis correction inside
gdpar_geom_hmcuses the exact target log-density, so the sampler is exact regardless of how coarse the Gaussian wall approximation is — only efficiency depends on it. - The
suggested_epsilonexploits the fact that the exact wall integration imposes no step-size limit; the step is set solely by the gentle floor curvature, which is the source of the speed-up over Euclidean HMC (whose step is bottlenecked by the stiffest wall eigenvalue). - The scheme is symplectic and time-reversible by construction (split HMC with a Gaussian part; Shahbaba et al. 2014).
gdpar_geom_metric_subriemannian(target, fisher, reference = NULL, tau = NULL, softness = 1, floor = 1e-8)
Purpose Builds the level-3 "sub-Riemannian" metric of the Block RG hierarchy. It partitions the eigen-spectrum of a reference Fisher information matrix into "wall" directions (curvature above threshold
Arguments
| Argument | Type | Meaning |
|---|---|---|
target |
gdpar_geom_target or coercible |
The target distribution. If not already a gdpar_geom_target, it is coerced via gdpar_geom_target(). Provides $dim, $grad_log_prob. |
fisher |
function | A function theta ↦ I(θ) returning the expected Fisher information matrix at theta. Must return a d × d symmetric positive-(semi)definite matrix. |
reference |
numeric vector or NULL
|
The reference parameter point at which fisher is evaluated to determine the spectral decomposition. Defaults to the zero vector of length d. |
tau |
numeric scalar or NULL
|
Spectral threshold separating walls from residuals. Eigenvalues min(λ) (the softest direction), meaning everything is a residual (no walls). |
softness |
numeric scalar ≥ 0 | Controls the sharpness of the sigmoid that assigns each eigenvalue to the wall or residual partition. Smaller values approach a hard threshold; larger values make a smoother blend. |
floor |
numeric scalar ≥ 0 | Eigenvalue floor; any eigenvalue of the reference Fisher below this is clamped. Also used by .gdpar_geom_floor_spd when flooring the Fisher matrix itself. |
Mathematics
The reference Fisher matrix at the reference point is
Verticality weights (logistic sigmoid):
Wall curvature and frequencies:
Wall Hessian matrix:
Residual potential gradient. The function returns a gradient closure grad_U_rest(θ) that computes
Strang (leapfrog + exact flow) integrator. For
For each step
- Half kick:
$;p \leftarrow p - \frac{\varepsilon}{2},\nabla U_{\text{rest}}(\theta)$ - Exact sub-Riemannian flow via
.gdpar_geom_subriemann_flow(θ, p, ref, U, ω, ε)for time$\varepsilon$ in the wall directions. - Half kick:
$;p \leftarrow p - \frac{\varepsilon}{2},\nabla U_{\text{rest}}(\theta)$
If any iterate is non-finite the flag converged is set to FALSE (the proposal will be rejected).
Suggested step size. The residual maximum curvature is
Returns A list of class c("gdpar_geom_metric", "list") with:
| Field | Value |
|---|---|
position_dependent |
FALSE |
dim |
integer, parameter dimension |
metric_kind |
"sub_riemannian" |
mass(θ), inv_mass(θ), chol_mass(θ)
|
Identity matrix |
logdet(θ) |
0 |
integrator |
Closure (theta, p, target, eps, L) → list(theta, p, converged) implementing the Strang scheme above |
reference |
numeric vector, the reference point |
eigenvalues |
numeric vector |
verticality |
numeric vector |
frequencies |
numeric vector |
n_walls |
integer, sum(w > 0.5)
|
tau |
numeric, the spectral threshold |
softness |
numeric, the sigmoid softness |
suggested_epsilon |
numeric, the suggested step size |
Notes
- Raises
gdpar_input_erroriffisheris not a function, ifreferencehas wrong length, or iffisherdoes not return ad × dmatrix. -
softness = 0would yield a degenerate sigmoid; the code does not guard against this (the logistic denominator becomes1 + exp(±∞)). - When
tau = min(λ)(default),$w_k \approx 0.5$ for the softest direction and$w_k \approx 1$ for stiffer ones; the exact assignment depends onsoftness. - The mass matrix is identity because the sub-Riemannian geometry encodes stiffness entirely through the potential, not through a position-dependent kinetic energy.
Purpose Internal factory that builds a pre-computed inverse-CDF sampler for the radial component of relativistic momentum draws. Under the change of variables
Arguments
| Argument | Type | Meaning |
|---|---|---|
d |
integer | Dimension of the parameter space. |
speed |
numeric > 0 | Speed of light |
mass |
numeric > 0 | Rest mass |
n_grid |
integer | Number of grid points for the numerical CDF approximation (default 8192). |
Mathematics
The unnormalised log-density of the radius
The algorithm:
- Maximise
$\log g$ on$[10^{-8}, 10^6]$ to find the peak value. - Extend the upper grid bound
$r_{\text{hi}}$ until$\log g(r_{\text{hi}}) < \text{peak} - 60$ (or$r_{\text{hi}} = 10^9$ ). - Build a grid
$r_0 = 0, r_1, \dots, r_{n-1} = r_{\text{hi}}$ . - Evaluate
$\log g$ on the grid; set$\log g(0) = -\infty$ when$d > 1$ (since$r^{d-1} = 0$ ). - Compute the CDF via the trapezoidal rule, normalise to 1.
- Build a monotone linear interpolation
invmapping$[0, 1] \to [0, r_{\text{hi}}]$ (only on strictly-increasing CDF segments).
Returns A function function(n) that draws n samples from the radial distribution via inverse-CDF transform of runif(n).
Notes
- When
$d = 1$ , the first grid pointlg[1]is kept (not set to$-\infty$ ), because$r^{d-1} = r^0 = 1$ at$r = 0$ . - The
pmax(r, 1e-300)guards againstlog(0)on the grid. - The CDF interpolation uses
rule = 2(extrapolation by boundary values) for numerical safety at the extremes. - The
keepmask ensures only strictly-increasing CDF entries feed intoapproxfun, preventing non-invertibility. - The result is independent of
$\theta$ and is built once per call to.gdpar_geom_kinetic_relativistic.
Purpose Internal factory that constructs the relativistic kinetic energy object: its value, its partial gradients with respect to
Arguments
| Argument | Type | Meaning |
|---|---|---|
metric |
gdpar_geom_metric |
A Riemannian metric object providing $dim, $inv_mass(θ), $logdet(θ), $chol_mass(θ), $dmass(θ), $position_dependent. |
speed |
numeric > 0 | Speed of light |
mass |
numeric > 0 | Rest mass |
Mathematics
Define
Gradient w.r.t.
Its
Gradient w.r.t.
When metric$position_dependent is FALSE, this gradient is identically zero.
Momentum sampler. Under .gdpar_geom_relativistic_radial:
Returns A named list with four closures:
| Field | Signature | Returns |
|---|---|---|
value |
(theta, p) → numeric |
|
grad_p |
(theta, p) → numeric vector |
|
grad_theta |
(theta, p) → numeric vector |
|
draw_momentum |
(theta) → numeric vector |
One sample from |
Notes
- The precomputed radial sampler (
radial) is captured in the closure environment; it is built exactly once. -
metric$dmass(theta)must return a length-$d$ list of$d\times d$ matrices$\partial M/\partial\theta_i$ . This is only called whenmetric$position_dependentisTRUE. - The
0.5 * metric$logdet(theta)term invalueis the same normaliser as in Gaussian RMHMC; it cancels in the Metropolis acceptance ratio, ensuring exactness.
Purpose Internal factory that builds the dedicated generalised implicit leapfrog integrator for the non-separable relativistic Hamiltonian integrator slot and dispatched by gdpar_geom_hmc when a relativistic geometry is present.
Arguments
| Argument | Type | Meaning |
|---|---|---|
kinetic |
list of closures | The kinetic energy object returned by .gdpar_geom_kinetic_relativistic, providing grad_p, grad_theta. |
Mathematics
The integrator follows Girolami & Calderhead (2011), performing three sub-steps per leapfrog iteration (each iteration is time-reversible and volume-preserving to the fixed-point tolerance). Let
For each of
Sub-step 1 — Implicit half kick in fp_max iterations.
Sub-step 2 — Implicit drift in fp_max iterations.
Sub-step 3 — Explicit half kick in
Returns A closure function(theta, p, target, eps, L, fp_tol = 1e-9, fp_max = 100L) that returns list(theta, p, converged).
Notes
- If either fixed-point iteration (sub-steps 1 or 2) does not converge, or any iterate is non-finite,
convergedis set toFALSEand the loop breaks early. The caller (the Metropolis step ingdpar_geom_hmc) will reject the proposal. - The entire integration is wrapped in
tryCatch; any runtime error returnsconverged = FALSEwith the last valid(theta, p). -
fp_tol = 1e-9andfp_max = 100are defaults; the caller may override them. - The implicit scheme is necessary because
$H$ is non-separable: the velocity$\nabla_p K$ depends on$\theta$ through$M(\theta)$ , so the standard explicit leapfrog is not symplectic. - Bit-identical behaviour with the default (Gaussian) leapfrog is achieved by having
gdpar_geom_hmcdispatch to this integrator only when the metric provides one, leaving the default branch unchanged.
gdpar_geom_metric_relativistic(target, curvature = c("fisher", "softabs"), fisher = NULL, dfisher = NULL, speed = 10, rest_mass = 1, alpha = 1e6, floor = 1e-8, fd_step = 1e-4)
Purpose Constructs a relativistic metric object that augments a Riemannian metric (Euclidean, Fisher, or SoftAbs) with a relativistic kinetic energy. This corresponds to the Finsler/relativistic level of the Block RG hierarchy. The metric delegates the position-dependent mass matrix and its derivatives to the underlying Riemannian layer, but wraps a relativistic kinetic energy and a corresponding implicit integrator on top.
Arguments
-
target— An object of classgdpar_geom_targetor a list/closure coercible to one viagdpar_geom_target(target). -
curvature— Character string, one of"fisher"or"softabs". Selects the curvature scheme for the underlying Riemannian metric. Passed through togdpar_geom_metric_riemannian. -
fisher— An optional user-supplied Fisher information functionfisher(theta)returning the$d \times d$ Fisher matrix at position$\theta$ . IfNULL, the Riemannian metric constructor supplies its own. -
dfisher— An optional user-supplied derivative-of-Fisher functiondfisher(theta)returning a list of$d$ matrices$\partial G / \partial \theta_i$ . IfNULL, finite-differenced. -
speed— Numeric scalar,$c > 0$ . The relativistic "speed of light" constant controlling the saturation of momentum magnitude. -
rest_mass— Numeric scalar,$m_0 > 0$ . The relativistic rest mass constant. -
alpha— Numeric scalar. SoftAbs regularization parameter forwarded to the Riemannian metric. -
floor— Numeric scalar. Eigenvalue floor for the Riemannian metric. Forwarded to the Riemannian metric. -
fd_step— Numeric scalar. Finite-difference step size for numerically differentiating the Fisher matrix whendfisherisNULL. Forwarded to the Riemannian metric.
Mathematics
The relativistic kinetic energy is:
where
The Hamiltonian is
Returns A list of class c("gdpar_geom_metric", "list") with components:
| Component | Description |
|---|---|
position_dependent |
TRUE (relativistic metrics are always position-dependent via $M(\theta)$). |
dim |
Integer, the parameter dimension |
metric_kind |
"relativistic". |
speed |
The speed constant |
rest_mass |
The rest mass |
curvature |
The curvature scheme string. |
mass |
Function mass(theta) returning |
inv_mass |
Function inv_mass(theta) returning |
chol_mass |
Function chol_mass(theta) returning the Cholesky factor |
logdet |
Function logdet(theta) returning |
dmass |
Function dmass(theta) returning the list |
kinetic |
A list of closures for the relativistic kinetic energy (see .gdpar_geom_kinetic_relativistic). |
integrator |
A closure for the relativistic generalised leapfrog integrator (see .gdpar_geom_relativistic_integrator). |
Notes
-
speedandrest_massare validated to be strictly positive; a zero or negative value raises agdpar_input_errorviagdpar_abort. - The underlying Riemannian metric is constructed via
gdpar_geom_metric_riemannian, passing all curvature-related arguments through. - The
kineticandintegratorfields are populated by two internal helpers (.gdpar_geom_kinetic_relativisticand.gdpar_geom_relativistic_integrator), which are not defined in this section — they are defined elsewhere in the package. - All mass-matrix accessor functions (
mass,inv_mass,chol_mass,logdet,dmass) are references to the Riemannian base metric's implementations.
Purpose Constructs the standard Gaussian kinetic energy closure list used by the Euclidean and Riemannian HMC levels. The Gaussian kinetic energy gdpar_geom_hmc when the metric does not supply its own kinetic.
Arguments
-
metric— A metric object (typically of classgdpar_geom_metric) exposinginv_mass,logdet,dmass,chol_mass, and the booleanposition_dependent.
Mathematics
The kinetic energy and its derivatives are:
The gradient with respect to
Momentum is drawn as
Returns A list with four named function components:
| Component | Signature | Returns |
|---|---|---|
value |
(theta, p) |
Numeric scalar, the kinetic energy |
grad_p |
(theta, p) |
Numeric vector of length |
grad_theta |
(theta, p) |
Numeric vector of length |
draw_momentum |
(theta) |
Numeric vector of length |
Notes
- When
metric$position_dependentisFALSE,grad_thetashort-circuits torep(0, length(theta)), avoiding unnecessary computation. - The
grad_thetacomputation usesvapplyover the dimension index$i$ , indexing into the list returned bymetric$dmass(theta). Each element $dMi$ is a$d \times d$ matrix. - The expression
sum(Minv * dMi)computes$\mathrm{tr}(M^{-1} , dM_i)$ efficiently via elementwise multiplication and summation, exploiting the fact that both matrices are the same size. - The expression
as.numeric(crossprod(Minv_p, dMi %*% Minv_p))computes$p^\top M^{-1} dM_i M^{-1} p$ via$\texttt{Minv\_p}^\top \, dM_i \, \texttt{Minv\_p}$ .
Purpose Computes the gradient of the Hamiltonian
Arguments
-
target— Agdpar_geom_targetprovidinggrad_log_prob(theta). -
kinetic— A kinetic energy list (as returned by.gdpar_geom_kinetic_gaussianor.gdpar_geom_kinetic_relativistic) providinggrad_theta(theta, p). -
theta— Numeric vector, the current position. -
p— Numeric vector, the current momentum.
Mathematics
Since
Returns A numeric vector of length
Notes
- This is a thin composition; all heavy lifting is delegated to
target$grad_log_probandkinetic$grad_theta.
Purpose Performs a single leapfrog integration step. Dispatches to an explicit Stoermer–Verlet step for constant metrics or to the implicit generalised leapfrog of Girolami & Calderhead (2011) for position-dependent (Riemannian/relativistic) metrics. Internal helper.
Arguments
-
theta— Numeric vector of length$d$ , current position. -
p— Numeric vector of length$d$ , current momentum. -
target— Agdpar_geom_targetprovidinggrad_log_prob. -
metric— A metric object providinginv_mass,position_dependent, and optionallyintegrator. -
kinetic— A kinetic energy list providinggrad_pandgrad_theta. -
eps— Numeric scalar, the leapfrog step size$\varepsilon$ . -
fp_tol— Numeric scalar, convergence tolerance$\delta$ for the fixed-point iterations. Only used for position-dependent metrics. -
fp_max— Integer, maximum iterations per fixed-point solve. Only used for position-dependent metrics.
Mathematics
Explicit (Euclidean/constant metric) step — the standard Stoermer–Verlet integrator:
Implicit (position-dependent metric) step — three sub-steps with two fixed-point solves:
Sub-step 1 (implicit half-kick in $p$): Iterate to convergence:
converging to
Sub-step 2 (implicit full drift in $\theta$): Iterate to convergence:
converging to
Sub-step 3 (explicit half-kick in $p$):
Returns A list with:
| Component | Type | Description |
|---|---|---|
theta |
Numeric vector | Updated position after one step. |
p |
Numeric vector | Updated momentum after one step. |
converged |
Logical |
TRUE if both fixed-point solves converged (always TRUE for the explicit branch); FALSE otherwise. Present only for the implicit branch. |
Notes
- The constant-metric branch returns a two-element list (
theta,p) with noconvergedfield. - The implicit branch is wrapped in
tryCatch; any error (e.g., non-finite iterates, matrix overflow) is caught, and the function returns the original(theta, p)withconverged = FALSE. This causes the Metropolis correction to reject the proposal rather than crashing the sampler. - Non-finite
deltain any fixed-point loop immediately breaks out of that loop, leavingconverged = FALSE. - The integrator is exactly time-reversible and volume-preserving (symplectic) up to the fixed-point tolerance.
- When
metric$integratoris non-NULL (as in the relativistic case),gdpar_geom_hmcbypasses this function entirely and calls the metric's own integrator.
Purpose Chains
Arguments
-
theta— Numeric vector of length$d$ , initial position. -
p— Numeric vector of length$d$ , initial momentum. -
target— Agdpar_geom_target. -
metric— A metric object. -
kinetic— A kinetic energy list. -
eps— Numeric scalar, leapfrog step size$\varepsilon$ . -
L— Integer, number of leapfrog steps$L$ . -
fp_tol— Numeric scalar, fixed-point convergence tolerance. -
fp_max— Integer, maximum fixed-point iterations.
Mathematics
Iterates the leapfrog map
Returns A list with:
| Component | Type | Description |
|---|---|---|
theta |
Numeric vector | Position after |
p |
Numeric vector | Momentum after |
converged |
Logical |
TRUE if every step converged; FALSE if any step failed (in which case iteration halts early at the failed step's output). |
Notes
- If any step reports
converged = FALSE, the loop breaks immediately and the remaining steps are not attempted. The returned state is that of the last (failed) step. - For a constant metric (explicit leapfrog), every step's
convergedis effectively alwaysTRUE.
Purpose Evaluates the Hamiltonian
Arguments
-
target— Agdpar_geom_targetprovidinglog_prob(theta). -
kinetic— A kinetic energy list providingvalue(theta, p). -
theta— Numeric vector, position. -
p— Numeric vector, momentum.
Mathematics
where
Returns A numeric scalar, the Hamiltonian energy at
Notes
- This function does not guard against non-finite return values; callers (
gdpar_geom_hmc) wrap the call intryCatchand treatInfas a divergent proposal.
gdpar_geom_hmc(target, metric = NULL, epsilon = 0.1, L = 20L, n_iter = 1000L, n_warmup = 500L, init = NULL, seed = NULL, fp_tol = 1e-9, fp_max = 100L)
Purpose The main exported HMC sampler implementing the Block RG engine. Runs a fixed step-size, fixed trajectory-length Hamiltonian Monte Carlo with Metropolis correction over a pluggable metric geometry. Supports Euclidean, Riemannian (position-dependent), and (via metric$integrator) relativistic/sub-Riemannian levels of the hierarchy. No adaptation is performed; warmup iterations are simply discarded.
Arguments
-
target— Agdpar_geom_targetor a list/closure coercible to one. -
metric— A metric object (e.g., fromgdpar_geom_metric_euclidean,gdpar_geom_metric_riemannian,gdpar_geom_metric_relativistic). IfNULL, defaults to an identity Euclidean metric of dimensiontarget$dim. -
epsilon— Numeric scalar$> 0$ . Leapfrog step size$\varepsilon$ . -
L— Positive integer. Number of leapfrog steps per proposal. -
n_iter— Positive integer. Number of retained (post-warmup) iterations. -
n_warmup— Non-negative integer. Number of warmup iterations (burn-in, no adaptation). -
init— Optional numeric vector of length$d$ giving the initial position$\theta_0$ . Defaults torep(0, d). -
seed— Optional integer. If supplied, the RNG state is saved, the seed is set, and the state is restored on exit viaon.exit. -
fp_tol— Numeric scalar. Fixed-point convergence tolerance for implicit leapfrog steps. Only used when the metric is position-dependent. -
fp_max— Integer. Maximum fixed-point iterations per implicit sub-step.
Mathematics
The sampler implements the standard HMC algorithm:
- Draw momentum
$p_0 \sim \mathcal{N}(0, M(\theta))$ . - Compute
$H_0 = H(\theta, p_0)$ . - Integrate
$L$ leapfrog steps to obtain proposal$(\theta^\ast, p^\ast)$ . - Compute
$H_1 = H(\theta^\ast, p^\ast)$ . - Accept with probability
$\min(1, \exp(-(H_1 - H_0)))$ .
The energy difference
- the implicit solve did not converge (
converged == FALSE), or -
$\Delta H$ is non-finite, or -
$|\Delta H| > 1000$ .
The energy Bayesian fraction of missing information (E-BFMI) is computed as:
where
Returns A list of class c("gdpar_geom_hmc", "list") with:
| Component | Type | Description |
|---|---|---|
draws |
|
Post-warmup position draws. Column names from target$param_names. |
accept_rate |
Numeric scalar | Acceptance rate |
n_divergent |
Integer | Count of divergent proposals across all iterations. |
energy |
Numeric vector of length |
Hamiltonian trace (pre-acceptance |
ebfmi |
Numeric scalar | Energy Bayesian fraction of missing information. NA_real_ if fewer than 2 finite energy values. |
epsilon |
Numeric scalar | The step size used. |
L |
Integer | The trajectory length used. |
metric_type |
Character |
"euclidean_constant" if metric$position_dependent is FALSE, "position_dependent" otherwise. |
Notes
- The
targetis coerced togdpar_geom_targetif not already one. -
epsilonmust be strictly positive; validated viaassert_numeric_scalarwithlower = 0(and then effectively checked by the leapfrog). -
L,n_iterare validated viaassert_count(must be positive integers). -
n_warmupis validated manually to be a non-negative integer scalar. - If
metric$integratoris a function (e.g., provided by a relativistic or sub-Riemannian metric), it is called in place of the default.gdpar_geom_leapfrog_traj. The Metropolis correction still uses the exact Hamiltonian from the kinetic energy object. - Seed handling: if
seedis supplied and.Random.seedexists in.GlobalEnv, it is saved and restored viaon.exit; otherwise,set.seed(seed)is called directly. - If
metric$kineticis non-NULL (e.g., the relativistic metric populates this), that kinetic is used; otherwise.gdpar_geom_kinetic_gaussian(metric)is the default. - The warmup phase simply runs iterations without recording draws or tracking energy; no adaptation of
epsilonorLoccurs. - A proposal that raises any error (e.g., target
log_proborgrad_log_probthrowing on a non-finite unconstrained value, an overflow in the Hessian, a non-PD metric) is caught by the outertryCatchin the main loop and treated as a divergent rejection.
Purpose Computes the energy Bayesian fraction of missing information (E-BFMI), a diagnostic for how well the metric resolves the target geometry (Betancourt 2016). Internal helper.
Arguments
-
energy— Numeric vector of per-iteration Hamiltonian energies (may contain non-finite values).
Mathematics
where the sum is over finite energies only. Low values (well below 1) indicate that the metric does not adequately capture the target's geometry, leading to slow exploration of the energy landscape.
Returns A numeric scalar. Returns NA_real_ if fewer than 2 finite energy values are available, or if the denominator is non-finite or non-positive.
Notes
- Non-finite entries in
energyare filtered out before computation viaenergy[is.finite(energy)]. - A denominator of zero or negative (degenerate case: all energies identical) yields
NA_real_.
Purpose S3 print method for objects of class gdpar_geom_hmc. Produces a concise summary to the console.
Arguments
-
x— An object of classgdpar_geom_hmc. -
...— Unused; present for S3 method compatibility.
Mathematics None.
Returns Invisibly returns x (via invisible(x)).
Notes
- Prints the draw matrix dimensions, metric type, epsilon, L, acceptance rate, number of divergent transitions, and E-BFMI to
stdoutviacat. - The acceptance rate and E-BFMI are formatted with 3 significant digits.
- This is an exported S3 method registered via
@export.
gdpar_geom_rmhmc_adaptive(target, fisher, n_sim, n_sites_init, max_rounds, batch, n_add, decay, novelty_tol, warmup_metric, epsilon, L, n_iter, n_warmup, init, seed, alpha, nugget, lengthscale)
Purpose
Implements an adaptive Riemannian Manifold Hamiltonian Monte Carlo (RMHMC) sampler that uses a Gaussian process (GP) surrogate to approximate the local Fisher information matrix. The algorithm sequentially builds and refines the GP metric by collecting novel parameter sites in a reservoir.
Arguments
-
target: an object of class"gdpar_geom_target"or any object coercible to one viagdpar_geom_target(). Defines the target distribution and its geometric properties. -
fisher: a function ofthetareturning the Fisher information matrix attheta, orNULL. IfNULL, a simulator (gdpar_geom_fisher_simulator) is constructed internally. -
n_sim: integer. Number of draws used by the Fisher simulator whenfisher = NULL. -
n_sites_init: integer. Initial number of parameter sites collected in the reservoir. -
max_rounds: integer. Maximum number of novelty‑driven adaptation rounds. -
batch: integer. Number of HMC draws generated per adaptation round. -
n_add: integer. Base number of novel sites to admit per round before decay. -
decay: numeric (≥ 0). Multiplicative decay factor for the number of admitted sites per round. -
novelty_tol: numeric (≥ 0). Novelty threshold; only draws with novelty above this value are eligible for admission. -
warmup_metric: a metric object (e.g., fromgdpar_geom_metric_riemannian) used for the initial reservoir generation. IfNULL, a Riemannian metric with softabs curvature is constructed. -
epsilon: numeric. Leapfrog step size for all HMC runs. -
L: integer. Number of leapfrog steps per HMC proposal. -
n_iter: integer. Number of post‑warmup draws in the final sampling phase. -
n_warmup: integer. Number of warmup draws in the final sampling phase. -
init: numeric vector of lengthtarget$dimgiving the initial parameter vector. IfNULL, defaults to a zero vector. -
seed: integer orNULL. If non‑NULL, the random seed is set and restored on exit. -
alpha: numeric. Tuning parameter for the softabs curvature and the GP metric. -
nugget: numeric. Jitter added to the diagonal of the GP kernel matrix to ensure positive‑definiteness. -
lengthscale: numeric orNULL. Length‑scale hyper‑parameter for the GP kernel. IfNULL, it is estimated from the reservoir sites.
Mathematics
The algorithm proceeds in three phases:
-
Cold‑start reservoir
Collect$M_0 = \texttt{n\_sites\_init}$ parameter sites$\theta^{(1)},\dots,\theta^{(M_0)}$ by running HMC with the fixedwarmup_metric. The Fisher information at each site is either provided byfisher(\theta)or approximated via the simulator. These sites form the initial reservoir$S_0$ . -
Novelty‑driven adaptation
For round$r = 1,\dots,R$ (where$R \le \texttt{max\_rounds}$ ):- Draw a batch of
$B = \texttt{batch}$ samples using HMC with the current GP metric. - Compute the novelty
$\nu(\theta)$ for each draw via the GP metric'snoveltymethod. - Let
$\nu_{\max} = \max{\nu(\theta): \theta \text{ in batch}}$ . If$\nu_{\max} < \texttt{novelty\_tol}$ , stop. - Otherwise, select the
$n_{\text{admit}} = \lceil \texttt{n\_add} \cdot \texttt{decay}^{\,r-1} \rceil$ draws with highest novelty that exceed the threshold, and append them to the reservoir: $S_r = S_{r-1} \cup {\text{selected draws}}$. - Rebuild the GP metric on the enlarged reservoir
$S_r$ .
- Draw a batch of
-
Final sampling
Using the final GP metric, draw$\texttt{n\_iter}$ samples after$\texttt{n\_warmup}$ warmup iterations via HMC.
Returns
An object of class c("gdpar_geom_rmhmc_adaptive", "list") with the following components:
-
draws: numeric matrix of posterior draws (rows = iterations, columns = parameters). -
metric: the final GP metric object. -
reservoir: numeric matrix of all parameter sites collected in the reservoir. -
n_sites_trace: integer vector recording the reservoir size after each adaptation round. -
novelty_trace: numeric vector of the maximum novelty observed in each adaptation round. -
accept_rate: acceptance rate of the final HMC run. -
ebfmi: empirical Bayes fraction of missing information (E‑BFMI) of the final run. -
n_divergent: number of divergent transitions in the final run. -
n_rounds: number of adaptation rounds actually performed. -
epsilon: step size used. -
L: integer, number of leapfrog steps used.
Notes
- If
targetis not already a"gdpar_geom_target", it is coerced viagdpar_geom_target(). - The function aborts with class
"gdpar_input_error"ifn_sites_init < 2or iffisheris neither a function norNULL. - When
seedis provided, the function temporarily modifies.Random.seedand restores it on exit. - If
warmup_metricisNULL, a Riemannian metric with softabs curvature is built usingalpha. - The GP metric is built by
gdpar_geom_metric_gp_fisher; itsnoveltymethod is used to assess new points. - The decay formula for the number of admitted sites is
$n_{\text{admit}} = \lceil \texttt{n\_add} \cdot \texttt{decay}^{\,r-1} \rceil$ .
Purpose
S3 print method for objects of class "gdpar_geom_rmhmc_adaptive". Prints a concise summary of the sampling results.
Arguments
-
x: an object of class"gdpar_geom_rmhmc_adaptive". -
...: additional arguments (currently unused).
Returns
Invisibly returns x.
Notes
- The output includes: number of draws, dimensionality, number of adaptation rounds, final reservoir size, tuning parameters (
epsilon,L), acceptance rate, number of divergences, E‑BFMI, and the final novelty value (if available).
.gdpar_geom_newton_climb(geom_target, ref, n_steps = 300L, ridge = 1e-6, tol = 1e-3, max_step_norm = 5)
Purpose
Modified-Newton ascent to the posterior mode, used as the polish stage of .gdpar_geom_laplace_climb. It is the in-package promotion of the benchmark extractor routine rg7_climb_to_mode. It reads the same log_prob, grad_log_prob, and hessian that the sampler uses, so the mode it finds is the mode of the same target the orchestrator certified.
Arguments
| Argument | Type | Meaning |
|---|---|---|
geom_target |
list (a gdpar_geom_target) |
Must expose $log_prob, $grad_log_prob, $hessian (a function; the caller checks first). |
ref |
numeric vector | Starting position for the climb. |
n_steps |
integer (scalar) | Maximum number of Newton iterations (default 300L). |
ridge |
numeric (scalar) | Relative eigenvalue floor: each eigenvalue of |
tol |
numeric (scalar) | Gradient-norm convergence tolerance (default 1e-3). |
max_step_norm |
numeric (scalar) | Trust-region radius; any step with max_step_norm is rescaled (default 5). |
Mathematics
At each iteration
- Evaluate
$g = \nabla\log p(\theta_k)$ and$H = \nabla^2\log p(\theta_k)$ . - Symmetrise:
$H \leftarrow \tfrac12(H + H^\top)$ . - Eigen-decompose
$A = -H = V\Lambda V^\top$ (symmetric). - Floor the spectrum:
$$\tilde\lambda_i = \max!\bigl(\lambda_i,;\texttt{ridge}\cdot\max(|\lambda_i|,1)\bigr).$$ - Form the ascent direction
$$\text{step} = V,\bigl(\tilde\Lambda^{-1}(V^\top g)\bigr) = \tilde A^{-1}g,$$ which is$A^{-1}g$ on the floored spectrum. - Trust-region cap: if
$\lVert\text{step}\rVert_2 > \texttt{max\_step\_norm}$ , rescale$\text{step}\leftarrow\text{step}\cdot\texttt{max\_step\_norm}/\lVert\text{step}\rVert_2$ . - Backtracking Armijo line search over
$t\in{1,\tfrac12,\tfrac14,\dots}$ (up to 20 halvings): accept$\theta_k + t,\text{step}$ when$$\log p(\theta_k + t,\text{step}) ;\ge; f_0 + 10^{-4},t,g^\top\text{step}.$$ - Convergence test:
$\lVert g\rVert_2 < \texttt{tol}$ or improvement$f_{\text{new}}-f_0 < \texttt{tol}\cdot 10^{-2}$ .
When eigen() is never fed a non-finite matrix.
Returns
A list:
| Element | Type | Meaning |
|---|---|---|
mode |
numeric vector | Best point found. |
converged |
logical |
TRUE if the gradient/improvement test fired. |
iters |
integer | Iteration count at convergence (present only when converged = TRUE). |
grad_norm |
numeric |
|
logp |
numeric |
|
reason |
character |
"no exact Hessian" (present only on the early-return path). |
Notes
- Early return with
converged = FALSEandreason = "no exact Hessian"whengeom_target$hessianis not a function. - The loop
breaks (returningconverged = FALSE) when the gradient is non-finite, the step is non-finite, or the line search exhausts all 20 backtracks without acceptance. - No side effects; no RNG usage; no S3 dispatch.
Purpose
Two-stage mode-finder: an L-BFGS-B warm start on start is far from the mode) followed by a modified-Newton polish when an exact Hessian is available; L-BFGS-B alone otherwise. Both stages read the same geom_target target and gradient the sampler uses.
Arguments
| Argument | Type | Meaning |
|---|---|---|
geom_target |
list (gdpar_geom_target) |
Exposes $log_prob, $grad_log_prob, optionally $hessian. |
start |
numeric vector | Warm-start position (the orchestrator passes the best on-ridge position found during sampling). |
climb_steps |
integer (scalar) | Maximum Newton steps passed as n_steps to .gdpar_geom_newton_climb. |
Mathematics
Stage 1 — L-BFGS-B on the negated log-density:
with non-finite values clamped: stats::optim is called with method = "L-BFGS-B", maxit = 5000L, factr = 1e1.
Stage 2 — if geom_target$hessian is a function, the L-BFGS-B solution warm is passed to .gdpar_geom_newton_climb(geom_target, warm, n_steps = climb_steps) for Newton polishing. Otherwise the warm start is returned directly.
Returns
A list:
| Element | Type | Meaning |
|---|---|---|
mode |
numeric vector | Best point found. |
converged |
logical | From Newton polish (converged field) when Hessian available; otherwise isTRUE(opt$convergence == 0L). |
grad_norm |
numeric |
|
logp |
numeric |
|
iters |
integer | Present only when the Newton polish converged (inherited from .gdpar_geom_newton_climb). |
Notes
- If
stats::optimerrors,optisNULLandwarmfalls back tostart. - All
tryCatchblocks swallow errors silently, returning sentinel values (Inf,NULL, zero vectors). - No side effects beyond the optimisation; no S3 dispatch.
Purpose
Compute the observed information matrix reference, symmetrised. Promotion of rg7_observed_information. Prefers the exact Hessian; falls back to central finite differences of the gradient when no Hessian is exposed or it is non-finite. The route taken is recorded in attr(., "method").
Arguments
| Argument | Type | Meaning |
|---|---|---|
geom_target |
list (gdpar_geom_target) |
Exposes $dim, $grad_log_prob, optionally $hessian. |
reference |
numeric vector | Point at which to evaluate the curvature. |
h |
numeric (scalar) | Central finite-difference step (default 1e-4). |
Mathematics
Exact-Hessian route (when geom_target$hessian is a function and returns a finite matrix):
Finite-difference route (fallback): the Jacobian of the gradient is approximated column-wise by central differences,
and then
Returns
A "method" set to one of:
method value |
Condition |
|---|---|
"exact_hessian" |
Hessian function present and all entries finite. |
"finite_difference" |
No usable Hessian; finite-difference matrix all finite. |
"finite_difference_nonfinite" |
No usable Hessian; finite-difference matrix contains non-finite entries. |
Notes
- The exact-Hessian route is tried inside a
tryCatch; any error yieldsNULLand triggers the fallback. - Finite differences loop over
seq_len(d)columns, evaluating the gradient atreference ± h*e_j. - No side effects; no S3 dispatch.
Purpose
Generate .Random.seed, so the caller's RNG stream is untouched.
Arguments
| Argument | Type | Meaning |
|---|---|---|
mode |
numeric vector ( |
The posterior mode. |
Lhalf |
|
The symmetric matrix square root |
S |
integer (scalar) | Number of draws. |
seed |
integer (scalar) | Seed passed to set.seed(). |
Mathematics
Draw
Because mode is performed via sweep(U, 2L, mode, "+").
Returns
An
Notes
- RNG isolation:
old_seedis captured fromglobalenv()if.Random.seedexists;on.exitrestores it (or does nothing if it did not exist).set.seed(seed)is called after the save. - No S3 dispatch; no error handling on
Lhalfdimensions — the caller is responsible for conformability.
Purpose
Assess the fidelity of the Laplace Gaussian U. Promotion of rg7_laplace_fit_quality. Computes the self-normalised importance-sampling ESS, the PSIS Pareto-$k$ of the weights
Arguments
| Argument | Type | Meaning |
|---|---|---|
geom_target |
list (gdpar_geom_target) |
Exposes $log_prob. |
mode |
numeric vector ( |
The mode. |
M |
|
The (floored, positive-definite) precision matrix. |
logdetM |
numeric (scalar) |
|
U |
|
Draws at which to evaluate (the same draws returned to the caller or internally generated for diagnostics). |
Mathematics
The Laplace Gaussian log-density at each draw
The quadratic form is computed as rowSums((cen %*% M) * cen) where cen = sweep(U, 2, mode, "-").
The true log-density geom_target$log_prob, with errors caught and mapped to NA_real_.
Importance weights (log-scale, max-stabilised):
Self-normalised IS effective sample size:
PSIS Pareto-$k$: when the loo package is available and there are more than 10 finite draws, loo::psis() is called on the log-weights r_eff = NA), and $diagnostics$pareto_k is extracted. Otherwise pareto_k = NA_real_.
Log-density drop:
Under a perfect Gaussian,
Returns
A list:
| Element | Type | Meaning |
|---|---|---|
ess_is |
numeric | Self-normalised IS ESS (raw count). |
ess_is_frac |
numeric |
ess_is / sum(fin) (fraction of finite draws). |
pareto_k |
numeric | PSIS Pareto-$k$, or NA if unavailable. |
logdrop_mean |
numeric | Mean of |
logdrop_max |
numeric | Max of |
logdrop_expected |
numeric | Always |
n_finite |
integer | Count of draws with both |
n_total |
integer |
S (total draws). |
When sum(fin) < 2L, all diagnostic fields except logdrop_expected, n_finite, and n_total are NA_real_.
Notes
-
logp_modeis evaluated separately (with error handling) for the drop calculation. - The
loo::psiscall is wrapped insuppressWarnings()and atryCatch; any error yieldsNA_real_. - No side effects; no S3 dispatch.
Purpose
Distil the raw fidelity diagnostics from .gdpar_geom_laplace_fit_quality into a single, unambiguous scalar label ("good" / "poor" / "very_poor") so a caller can never mis-read raw numbers. Implements the data-driven thresholds adjudicated in design session B9.34.
Arguments
| Argument | Type | Meaning |
|---|---|---|
fq |
list | The fidelity-diagnostics list returned by .gdpar_geom_laplace_fit_quality. |
all_pos |
logical | Was the raw (un-floored) curvature positive-definite? |
d |
integer (scalar) | Unconstrained dimension. |
mode_offset_sd |
numeric (scalar) | Newton-decrement bound on the mode offset in posterior SDs (default 0). |
Mathematics / decision logic
Let
-
Non-PD curvature (saddle, not a maximum): if
!all_pos$\Rightarrow$ "very_poor". -
Non-finite diagnostics: if
essorldis non-finite$\Rightarrow$ "very_poor". -
Mode-location gate: if
$\text{off} > 2$ $\Rightarrow$ "very_poor". Definewell_centred$= (\text{off} \le 0.5)$ . -
Excellent fit override: if
well_centredand$\text{ess}\ge 0.9$ and$\text{ld}\le d$ $\Rightarrow$ "good"(Pareto-$k$ on numerically constant weights is uninformative and must not override). -
Severe degradation: define
pk_bad$= (\text{pk}\ge 1.0)$ . If$\text{ess}<0.1$ or$\text{ld}\ge 2d$ or (pk_badand$\text{ess}<0.5$ )$\Rightarrow$ "very_poor". -
Middle ground: define
pk_ok$= (\text{is.na(pk)} \text{ or } \text{pk}<0.7)$ . Ifwell_centredand$\text{ess}\ge 0.5$ and$\text{ld}\le d$ andpk_ok$\Rightarrow$ "good". -
Otherwise:
"poor".
Returns
A character scalar: "good", "poor", or "very_poor".
Notes
- The ESS fraction is the stable primary signal (no tail fit); Pareto-$k$ only refines the middle ground and is damning only when ESS is also weak.
- The mean log-density drop threshold uses
$d$ (benign$\le d$ ) and$2d$ (severe$\ge 2d$ ) relative to the Gaussian expectation$d/2$ . - A non-positive-definite curvature at the "mode" returns
"very_poor"outright because the Laplace Gaussian is ill-defined there. - No side effects; no S3 dispatch.
gdpar_geom_laplace(geom_target, reference = ..., draws = 0L, climb = TRUE, seed = ..., fit_quality_draws = 256L, eigen_floor_rel = 1e-10, climb_steps = 300L, cond_warn = 1e12)
Note: The roxygen documentation for this exported function appears in this section, but the function body (
gdpar_geom_laplace <- function(...) { ... }) is in section 2 of 2. The subsection below documents the interface and contract as declared by the roxygen; the implementation details will be covered when section 2 is processed.
Purpose
The exported, first-class Laplace approximation of a posterior at its mode. Compute the mode + curvature Gaussian mgcv/REML and INLA/Laplace, accompanied by a measurement of how good the Gaussian is.
Arguments
| Argument | Type | Meaning |
|---|---|---|
geom_target |
gdpar_geom_target (or coercible) |
Exposes log_prob, grad_log_prob, dim, and ideally hessian. |
reference |
numeric vector | Optional unconstrained warm-start position for the mode climb; defaults to the origin. When called by the orchestrator this is the best on-ridge position found during sampling. |
draws |
integer (scalar) | Number of iid Laplace draws 0L: the mode plus precision Gaussian is the approximation). |
climb |
logical | Whether to climb to the mode from reference (default TRUE). FALSE treats reference as the mode and only reads the curvature there. |
seed |
integer | Seed for the local, stream-preserving draw RNG; defaults to a fixed value for reproducibility. |
fit_quality_draws |
integer (scalar) | Number of internal iid draws used to assess fidelity when draws is small (default 256L); the fidelity label is always computed independently of user-facing draws. |
eigen_floor_rel |
numeric (scalar) | Relative eigenvalue floor applied to 1e-10 of the largest eigenvalue). |
climb_steps |
integer (scalar) | Maximum modified-Newton steps in the mode climb (default 300L). |
cond_warn |
numeric (scalar) | Condition-number threshold above which the un-floored curvature triggers an ill-conditioning warning (default 1e12). |
Mathematics
The Laplace approximation is
where .gdpar_geom_laplace_climb → .gdpar_geom_newton_climb). The precision .gdpar_geom_observed_information), eigen-floored to be positive-definite. Draws are .gdpar_geom_laplace_draws_unconstrained). Fidelity is scored via IS-ESS, PSIS Pareto-$k$, and the log-density drop against .gdpar_geom_laplace_fit_quality), then distilled into a label (.gdpar_geom_laplace_label).
Returns
An object of class gdpar_geom_laplace: a list with:
| Element | Type | Meaning |
|---|---|---|
mode |
numeric vector |
|
M |
|
Floored precision. |
cov |
|
|
Lhalf |
|
|
logdet |
numeric |
|
eig |
numeric vector | Eigenvalues of |
cond |
numeric | Floored condition number. |
cond_unfloored |
numeric | Un-floored condition number. |
n_floored |
integer | Count of eigen-floored directions. |
floor_value |
numeric | The floor value used. |
all_pos |
logical | Was the raw curvature positive-definite? |
mode_offset_sd |
numeric | Newton-decrement bound on the mode offset in posterior SDs. |
grad_norm |
numeric |
|
logp |
numeric |
|
converged |
logical | Did the mode climb converge? |
method |
character | Hessian route ("exact_hessian", "finite_difference", etc.). |
dim |
integer |
|
draws |
|
Laplace draws (possibly zero rows), with attr(., "approximation") = "laplace". |
fit_quality |
list | Fidelity diagnostics from .gdpar_geom_laplace_fit_quality. |
fit_quality_label |
character | Scalar label: "good" / "poor" / "very_poor". |
Notes
- The Laplace approximation is never advertised as exact; the
drawsmatrix carriesattr(., "approximation") = "laplace"so it is never mistaken for exact MCMC downstream. - A warning is raised when the un-floored condition number exceeds
cond_warn, indicating the eigen-floor could be masking ill-conditioning. - A non-positive-definite raw curvature (a saddle) is flagged loudly (the label is
"very_poor"). - The default fit path of the package is byte-for-byte unchanged when
laplace_fallback = FALSE(the default); this function is only wired in behind the opt-in flag. - S3 class: the return value has class
gdpar_geom_laplace.
gdpar_geom_laplace(geom_target, reference = NULL, draws = 0L, climb = TRUE, seed = NULL, fit_quality_draws = 256L, eigen_floor_rel = 1e-10, climb_steps = 300L, cond_warn = 1e12)
Purpose
Constructor for the Laplace (Gaussian) approximation of a target density exposed through a gdpar_geom_target object. It locates the posterior mode, builds the precision matrix from the observed information, eigen-floors it to guarantee positive-definiteness, draws an internal sample for fidelity diagnostics, and returns a tagged gdpar_geom_laplace object consumed by downstream gdpar reference-anchoring routines.
Arguments
-
geom_target: object of class"gdpar_geom_target"(or coercible viagdpar_geom_target()). Must expose$dim,$log_prob(x),$grad_log_prob(x), and whatever.gdpar_geom_observed_information()requires. -
reference:numericvector of lengthd(the unconstrained dimension). Starting point for mode climbing whenclimb = TRUE, or used directly as the mode whenclimb = FALSE. Defaults torep(0, d). -
draws: non-negative integer. Number of Laplace draws to attach to the returned object. Coerced viaas.integer. -
climb:logical. IfTRUE, the mode is sought with.gdpar_geom_laplace_climb(geom_target, reference, climb_steps); ifFALSE,referenceis treated as the mode and its gradient norm / log-density are evaluated directly. -
seed: integer count seed for the internal RNG used by.gdpar_geom_laplace_draws_unconstrained(). Defaults to20260625LwhenNULL; validated byassert_count(seed, "seed"). -
fit_quality_draws: integer lower-bounding the internal sample size used to compute fit quality. Coerced tomax(as.integer(.), 2L). Defaults to256L. -
eigen_floor_rel: positive numeric. Relative eigenvalue floor:$\lambda_{\text{floor}} = \lambda_{\max}\cdot\text{eigen\_floor\_rel}$ . Defaults to1e-10. -
climb_steps: integer passed to.gdpar_geom_laplace_climb(). Defaults to300L. -
cond_warn: numeric threshold on the un-floored curvature condition number above which a warning is emitted. Set to a non-finite value to suppress. Defaults to1e12.
Mathematics
Mode: $$ \hat\mu = \begin{cases} \text{climb}(\text{geom_target}, \text{reference}, \text{climb_steps}) & \text{if } \text{climb} = \text{TRUE},\ \text{reference} & \text{otherwise.} \end{cases} $$
Precision (observed information), symmetrized: $$ M = -\nabla^2 \log p(\hat\mu), \qquad M \leftarrow \tfrac{1}{2}(M + M^\top). $$
Symmetric eigendecomposition
Covariance, symmetric inverse square root, and log-determinant (all using floored eigenvalues): $$ M^{-1} = V,\mathrm{diag}(\tilde\lambda_i^{-1}),V^\top, \qquad M^{-1/2} = V,\mathrm{diag}(\tilde\lambda_i^{-1/2}),V^\top, \qquad \log\det M = \sum_i \log \tilde\lambda_i. $$
Newton step and Newton decrement (mode offset in SD units): $$ \Delta = M^{-1},\nabla\log p(\hat\mu), \qquad \sigma_{\text{offset}} = \sqrt{\max!\bigl((\nabla\log p)^\top M^{-1},\nabla\log p,;0\bigr)}. $$
Condition numbers: $$ \kappa_{\text{floored}} = \frac{\max_i \tilde\lambda_i}{\min_i \tilde\lambda_i}, \qquad \kappa_{\text{unfloored}} = \begin{cases} \dfrac{\max_i |\lambda_i|}{\min_i |\lambda_i|} & \min_i |\lambda_i| > 0,\ +\infty & \text{otherwise.} \end{cases} $$
Internal sample size .gdpar_geom_laplace_draws_unconstrained(mode, Lhalf, S_fit, seed), then fed to .gdpar_geom_laplace_fit_quality(geom_target, mode, M, logdetM, U_full). The fidelity label is .gdpar_geom_laplace_label(fq, all_pos, d, mode_offset_sd).
Returns
A list with class c("gdpar_geom_laplace", "list") and elements:
-
mode: numeric vector of lengthd, the located mode. -
M: the symmetrized (but not eigen-floored) precision matrix$-\nabla^2\log p(\hat\mu)$ ; may carry a"method"attribute set by.gdpar_geom_observed_information(). -
cov:$M^{-1}$ computed with floored eigenvalues. -
Lhalf: symmetric$M^{-1/2}$ computed with floored eigenvalues. -
logdet:$\log\det M$ using floored eigenvalues. -
eig: numeric vector of unfloored eigenvalues of$M$ . -
cond: floored condition number$\kappa_{\text{floored}}$ . -
cond_unfloored:$\kappa_{\text{unfloored}}$ . -
n_floored: integer count of eigenvalues strictly belowfloor_value. -
floor_value:$\lambda_{\max}\cdot\text{eigen\_floor\_rel}$ . -
all_pos:logical,TRUEiff every unfloored eigenvalue is$>0$ . -
mode_offset_sd: Newton decrement$\sigma_{\text{offset}}$ . -
grad_norm: gradient$L_2$ norm at the mode (fromcl$grad_norm, orNA_real_if missing). -
logp:as.numeric(cl$logp %||% geom_target$log_prob(mode)). -
converged:isTRUE(cl$converged). -
method:attr(M, "method") %||% "unknown". -
dim: integer dimensiond. -
draws:$S_{\text{returned}}\times d$ numeric matrix, where$S_{\text{returned}}=\text{draws}$ (a 0-row matrix whendraws == 0L), carrying attribute"approximation" = "laplace". -
fit_quality: the object returned by.gdpar_geom_laplace_fit_quality(). -
fit_quality_label: the label string returned by.gdpar_geom_laplace_label().
Notes
- Input errors (class
"gdpar_input_error", raised viagdpar_abort):drawsisNAor negative;reference(when non-NULL) has length$\neq d$ . - Geometry warnings (class
"gdpar_geometry_warning", raised viagdpar_warn): (i) any unfloored eigenvalue$\le 0$ (reports the count and minimum eigenvalue, flags the approximation as"very_poor"via the label machinery); (ii)cond_unfloored > cond_warnwhencond_warnis finite (reports both numbers, cautions that the eigen-floor may mask ill-conditioning). -
fit_quality_drawsis silently raised to at least2L. - The stored
Mis symmetrized but not floored; onlycov,Lhalf,logdet, andconduse the floored spectrum.eigreports the unfloored spectrum so callers can detect silent flooring vian_flooredandfloor_value. - The returned
drawsmatrix is tagged withattr(., "approximation") <- "laplace"so downstream methods cannot silently consume it as exact MCMC output; the attribute survives subsetting because it is set on the final sliced matrix. - When
climb = FALSE,convergedisNA(propagated throughisTRUE→FALSE), andgrad_normis computed explicitly as$\sqrt{\sum_i (\nabla\log p(\text{reference})_i)^2}$ . -
logpfalls back togeom_target$log_prob(mode)whencl$logpisNULL. -
seed = NULLis replaced by the fixed default20260625Lbefore validation.
Purpose
S3 print method for objects of class "gdpar_geom_laplace". Renders a compact multi-line diagnostic summary covering dimension, fit-quality label, mode optimality, curvature conditioning, importance-sampling fidelity, and draw dimensions.
Arguments
-
x: agdpar_geom_laplaceobject (as produced bygdpar_geom_laplace()). -
...: unused; absorbed for S3 generic conformance.
Mathematics
The "unreliable" flag on the fidelity line is raised when
$$
\hat k_{\text{Pareto}} \ge 0.7,
$$
using x$fit_quality$pareto_k. The expected log-drop reference shown is x$fit_quality$logdrop_expected, printed as E[d/2] (the theoretical Laplace expectation
Returns
Invisibly returns x (via invisible(x)).
Notes
- Exported S3 method registered for class
"gdpar_geom_laplace". - Output layout (in order):
<gdpar_geom_laplace> dim {dim} | fit-quality: {fit_quality_label}mode: |grad| {grad_norm} | offset {mode_offset_sd} SD | PD {all_pos} | method {method}condition: floored {cond} | un-floored {cond_unfloored} | eigen-floored dirs {n_floored}/{dim}-
fidelity: IS-ESS {ess_is} ({100*ess_is_frac}%) [unreliable: Pareto-k >= 0.7] | Pareto-k {pareto_k} | log-drop {logdrop_mean} (E[d/2] = {logdrop_expected})— the bracketed[unreliable: ...]segment is emitted only whenisTRUE(fq$pareto_k >= 0.7). - If
fit_quality_labelis not"good":NOTE: a '{label}' Laplace approximation -- a plug-in / INLA-style fallback, NOT exact MCMC. draws: {nrow(draws)} x {ncol(draws)}
- Numeric formatting uses
format(..., digits = 3)(ordigits = 2for the ESS percentage); integers (dim,n_floored,method) are printed raw viasep = "". - No side effects beyond console output; does not mutate
x.
Purpose Defines the multi-signal success criteria for the geometry-adaptive orchestrator. It establishes the conjunction of size-invariant sampler signals (acceptance rate, divergence rate, and E-BFMI) that a sampler level must meet to be considered successful, preventing false positives from a sampler that moves too little.
Arguments None.
Mathematics Implements a success gate requiring:
- Acceptance rate in a healthy band:
$0.5 \leq \hat{\alpha} \leq 0.999$ - Divergence rate below a ceiling:
$\hat{\delta} \leq 0.02$ - Energy Bayesian Fraction of Missing Information (E-BFMI) above a floor:
$\widehat{\text{E-BFMI}} \geq 0.3$
Returns A named list with four numeric elements:
-
accept_low(0.5): Lower bound of the healthy acceptance band. -
accept_high(0.999): Upper bound of the healthy acceptance band. -
divergent_rate_high(0.02): Maximum allowed divergence rate. -
ebfmi_low(0.3): Minimum allowed E-BFMI.
Notes These are static, calibrated defaults. The decisive gate used in gdpar_geom_orchestrate applies a hysteresis margin (tightening these thresholds by 1 + hysteresis).
Purpose Defines the budget, stopping rules, and tuning knobs that bound the closed-loop operation of the geometry-adaptive orchestrator. It controls the maximum computational effort (rounds, fits, time) and the parameters for the successive-halving budget tiering and step-size tuning.
Arguments None.
Mathematics Sets the parameters for:
- A cheap probe tier:
$\text{probe\_warmup}=150, \text{probe\_iter}=150$ - A full tier:
$\text{full\_warmup}=500, \text{full\_iter}=500$ - Base HMC integrator:
$\epsilon = 0.25, L=25$ - Tuning: if
tune_epsilon=TRUE, a short search over$\epsilon$ is run withtune_iter=60iterations per probe.
Returns A named list with 15 elements:
-
max_rounds(8L): Hard ceiling on diagnose–sample cycles. -
max_levels(5L): Cap on distinct ladder levels tried. -
probe_warmup(150L),probe_iter(150L): Budget for cheap probes. -
full_warmup(500L),full_iter(500L): Budget for full runs. -
epsilon(0.25),L(25L): Base leapfrog step size and trajectory length. -
tune_epsilon(TRUE): Whether to run a coarse$\epsilon$ search. -
tune_iter(60L): Iterations per tuning probe. -
max_seconds(Inf),max_seconds_per_fit(Inf): Wall-time caps. -
max_fits(40L): Global fit cap. -
n_rediagnose(1L): Number of pilots per re-diagnosis. -
stall_limit(2L): Consecutive no-progress rounds before stopping. -
hysteresis(0.1): Margin for tightening the decisive success gate.
Notes The loop checks remaining budget before starting each fit and emits a certified limit if caps are exceeded. The n_rediagnose ≥ 2 enables a stability check on the re-diagnosis.
Purpose Defines the ordered hierarchy of sampler levels (the "ladder") that the orchestrator can select and escalate through. Each level is a geometry-aware HMC variant.
Arguments None.
Mathematics The hierarchy levels and their indices: 0. Euclidean diagonal
- Euclidean dense
- Riemannian
- Relativistic
- Sub-Riemannian
Note: Indices are not contiguous; levels 6 (tempering/reparametrisation) and -1 (reparametrise/eliminate) are deliberately excluded.
Returns A named list of five elements, each a sublist with:
-
key: Character string identifier. -
index: Integer for ordering. -
needs_fisher: Logical,TRUEonly forsub_riemannian.
Notes Levels 6 and -1 are out-of-scope for the orchestrator; a diagnosis pointing to them short-circuits to a certificate.
Purpose Returns the character vector of in-ladder level keys in ascending hierarchical order.
Arguments None.
Mathematics None.
Returns A character vector: c("euclidean_diagonal", "euclidean_dense", "riemannian", "relativistic", "sub_riemannian").
Notes Pure utility function; used for iteration over the ladder.
Purpose Maps a classified pathology (as returned by gdpar_geometry_diagnostic) to the corresponding ladder key, or NA for out-of-scope pathologies.
Arguments
-
pathology(character): The pathology classification string (e.g.,"isotropic","funnel").
Mathematics None; a direct lookup table.
Returns A character string (ladder key) or NA_character_ for:
-
"multimodal","boundary","flat_direction","unknown".
Notes This is a one-to-one mapping except for the out-of-scope pathologies, which yield NA.
Purpose Finds the first ladder key strictly above a given hierarchy index. Used for monotone escalation.
Arguments
-
index(integer): The current ladder index.
Mathematics Given an index
Returns A character string (ladder key) or NA_character_ if the input index is at or above the top of the ladder.
Notes Assumes the ladder defined by .gdpar_orch_ladder() is non-empty and indexed.
Purpose Computes a continuous, size-invariant proximity score for each in-ladder pathology. This is the second, independent estimator in the combined level selection map, cross-checked against the discrete classifier.
Arguments
-
sig(data.frame row): A single row of size-invariant diagnostic signals (columns includecondition_number,heavy_kurtosis,ebfmi_min). -
grows(logical): Whether the difficulty curve indicates the problem grows with sample size (fromdiag$difficulty_curve$grows_with_n). -
th(list): Thresholds from the diagnostic classifier (must includecondition_high,heavy_kurtosis_high,funnel_ebfmi_low,heavy_cond_max).
Mathematics Computes five scores (one per pathology) from the signals, scaled by the classifier's thresholds:
Let:
-
$c = \text{sig\$condition\_number}$ (default 1 if non-finite) -
$k = \text{sig\$heavy\_kurtosis}$ (default 0 if non-finite) $e = \max(\text{sig\$ebfmi\_min}, 10^{-3})$
Ratios:
$r_c = c / \text{th\$condition\_high}$ $r_k = k / \text{th\$heavy\_kurtosis\_high}$
Scores:
- Isotropic:
$- \max(r_c, r_k)$ - Anisotropic: $r_c \times \begin{cases} 0 & \text{if } \text{grows} \ 1 & \text{otherwise} \end{cases} \times \begin{cases} 1 & \text{if } r_k < 1 \ 0.25 & \text{otherwise} \end{cases}$
- Funnel:
$r_k \times (\text{th\$funnel\_ebfmi\_low} / e)$ - Heavy tails:
$r_k \times (\text{th\$heavy\_cond\_max} / \max(c, 1)) \times \min(1, e / \text{th\$funnel\_ebfmi\_low})$ - Quasi-deterministic: $r_c \times \begin{cases} 2 & \text{if } \text{grows} \ 0.5 & \text{otherwise} \end{cases}$
Returns A named numeric vector of length 5 with the scores for each pathology: isotropic, anisotropic, funnel, heavy_tails, quasi_deterministic.
Notes The design ensures that a deep funnel (low E-BFMI) is not mistaken for heavy tails by damping the heavy-tail score when E-BFMI is depressed. All signals are made safe for non-finite values.
Purpose Resolves the entry level for the orchestrator using the combined map: a transparent, calibrated rule-based classifier (primary) and a continuous proximity score (secondary). Implements the conflict-resolution strategy: on disagreement or low confidence, start at the lower of the two candidate ladder levels.
Arguments
-
diag(list): Agdpar_geometry_diagnosticobject (or compatible list) with fields$pathology,$signals,$difficulty_curve$grows_with_n,$confidence. -
level_map(list or NULL): User override mapping from pathology to ladder key. -
entry_level(character or NULL): Direct user override for the starting level key. -
th(list): Thresholds for the proximity scores (from the diagnostic's classifier).
Mathematics Let:
-
$p_d$ : discrete pathology fromdiag$pathology. -
$p_p$ : proximity pathology (argmax of.gdpar_orch_pathology_scores). -
$k_d$ ,$k_p$ : corresponding ladder keys (orNAif out-of-scope).
Decision logic:
- If
entry_levelis provided, use it directly (after validation). - Else if
level_mapmaps$p_d$ to a key, use that. - Else if
$k_d$ isNA(out-of-scope), returnNA(caller will short-circuit to a certificate). - Else if (
$p_d \neq p_p$ or confidence$< 0.6$ ) and$k_p$ is notNA, start at$\min(\text{index}(k_d), \text{index}(k_p))$ . - Else use
$k_d$ .
Returns A named list with:
-
key: Character string (ladder key orNA). -
discrete: The discrete pathology. -
proximity: The proximity pathology. -
conflict: Logical, whether discrete and proximity disagree. -
confident: Logical, whether confidence$\geq 0.6$ . -
source: Character string indicating the decision source ("user_entry_level","user_level_map", or"combined_map").
Notes The key may be NA for out-of-scope pathologies. The caller uses this to decide whether to emit a certified limit. The algorithm is designed to be robust at the funnel/heavy-tail border.
Purpose (internal). Constructs a dense, symmetric positive-definite mass matrix euclidean_dense geometry level. It implements a three-tier fallback chain: (1) evaluate the caller-supplied Fisher information function at the reference, (2) invert the sample covariance of warmup draws, (3) compute the SoftAbs Riemannian mass at the reference. The result is SPD-floored before return.
Arguments
| Argument | Type | Meaning |
|---|---|---|
geom_target |
gdpar_geom_target |
The geometry target; its $dim field supplies |
fisher |
function or NULL
|
Optional fisher(theta) returning the expected Fisher information matrix at |
reference |
numeric vector of length |
Position at which position-dependent metrics are evaluated. |
warmup_draws |
numeric matrix or NULL
|
Matrix of warmup draws (rows = draws, columns fisher fails. |
Mathematics
The fallback chain is:
- If
fisheris a function, compute$M \leftarrow \texttt{fisher}(\texttt{reference})$ , coerced viaas.matrix. - If step 1 returned
NULLandwarmup_drawshas$n > d$ rows, compute the sample covariance$\hat{\Sigma} = \texttt{cov}(\texttt{warmup\_draws})$ and then$M \leftarrow \hat{\Sigma}^{-1}$ viachol2invof the Cholesky factor obtained from.gdpar_geom_chol_spd. - If both steps 1 and 2 returned
NULL, fall back to the SoftAbs Riemannian metric ofgeom_targetand evaluate its$massat the reference.
Finally, the result is floored to the nearest SPD matrix with eigenvalue lower bound
Returns A dense numeric matrix of class matrix, dimension
Notes
- Every intermediate computation is wrapped in
tryCatch; errors silently yieldNULLand the chain advances to the next tier. - The SoftAbs fallback (tier 3) is always available as long as
geom_targetsupports Riemannian geometry, making the function total. -
warmup_drawsmust have strictly more rows than$d$ for tier 2 to activate; a matrix with$n \leq d$ rows is silently ignored. -
fishermust return a square matrix of dimension$d$ ; if it returns something non-coercible, the error is caught and the fallback proceeds.
Purpose (internal). Constructs the metric object for a given ladder key (one of the five geometry levels). This is the dispatcher that translates a string key into the appropriate gdpar_geom_metric_* object. It is armoured: errors are caught and reported in-band rather than propagated as crashes, so the controller can record the failure and escalate.
Arguments
| Argument | Type | Meaning |
|---|---|---|
key |
character string | Ladder key: one of "euclidean_diagonal", "euclidean_dense", "riemannian", "relativistic", "sub_riemannian". |
geom_target |
gdpar_geom_target |
The geometry target to sample. |
fisher |
function or NULL
|
Optional expected Fisher information function. |
reference |
numeric vector of length |
Reference position for position-dependent metrics. |
speed |
numeric scalar | Relativistic speed parameter (used only when key = "relativistic"). |
rest_mass |
numeric scalar | Relativistic rest-mass parameter (used only when key = "relativistic"). |
warmup_draws |
numeric matrix or NULL
|
Warmup draws for the dense-mass fallback (used only when key = "euclidean_dense"). |
Mathematics
No explicit formula; the function dispatches on key via switch:
key |
Metric constructed |
|---|---|
euclidean_diagonal |
gdpar_geom_metric_euclidean(dim = d) |
euclidean_dense |
gdpar_geom_metric_euclidean(M = .gdpar_orch_dense_mass(...)) |
riemannian |
gdpar_geom_metric_riemannian(geom_target, curvature = "fisher"/"softabs", fisher = fisher) |
relativistic |
gdpar_geom_metric_relativistic(geom_target, curvature = "fisher"/"softabs", fisher = fisher, speed = speed, rest_mass = rest_mass) |
sub_riemannian |
gdpar_geom_metric_subriemannian(geom_target, fisher = fisher, reference = reference) |
The curvature argument for the riemannian and relativistic levels is "fisher" when fisher is a function, "softabs" otherwise.
Returns A list with three elements:
-
ok(logical):TRUEif the metric was successfully built,FALSEon error. -
metric(metric object orNULL): the constructed metric, orNULLon failure. -
reason(character):NA_character_on success, theconditionMessageof the caught error on failure.
Notes
- If
keyis not one of the five recognized strings, astop(sprintf("unknown ladder key '%s'", key))is raised inside thetryCatchand captured, yieldingok = FALSE. - For
sub_riemannian,fishermust be a function; otherwise a descriptive error is raised and caught. This is because the sub-Riemannian level structurally requires the expected Fisher information to define the wall/floor split. - The
tryCatchwrapper means the caller (the orchestrator) never receives a thrown error from this function; it always receives the list.
Purpose (internal). Evaluates the multi-signal success gate for a single HMC fit. The gate checks three independent criteria (acceptance rate, divergence rate, and E-BFMI) and returns whether the fit passes all of them. The margin parameter implements hysteresis: for the decisive full gate a positive margin tightens the thresholds (making it harder to pass), while the probe gate passes margin = 0.
Arguments
| Argument | Type | Meaning |
|---|---|---|
fit |
list or NULL
|
The HMC fit object; must contain $accept_rate, $n_divergent, $ebfmi. A NULL fit immediately yields a failure. |
total |
integer | Total number of proposals (warmup + sampling) against which the divergence rate is computed. |
criteria |
list | Named list with fields $accept_low, $accept_high, $divergent_rate_high, $ebfmi_low (see gdpar_geom_orchestrate_criteria). |
margin |
numeric scalar | Hysteresis margin; default 0 (probe gate). A positive value tightens thresholds. |
Mathematics
Let
The three pass/fail conditions are:
The gate passes if and only if all three conditions hold:
Note the asymmetric application of the hysteresis factor
Returns A list with fields:
-
pass(logical): whether the fit passed the gate. -
accept(numeric): the acceptance rate, orNA_real_iffitisNULL. -
divergent_rate(numeric):$n_d / \max(N, 1)$ , orNA_real_iffitisNULL. -
ebfmi(numeric): the E-BFMI, orNA_real_iffitisNULL. -
reasons(character vector): human-readable reasons for failure, or"all criteria met"on success, or"no fit"iffitisNULL.
Notes
- The divergence rate denominator is
$\max(\texttt{total}, 1)$ to avoid division by zero. - Each criterion uses
isTRUE(...)which returnsFALSEforNAvalues, soNAacceptance orNAE-BFMI will fail the gate. - The hysteresis margin tightens both the lower acceptance bound and the upper divergence bound, and raises the E-BFMI floor, but leaves the upper acceptance bound (
accept_high) unchanged. This means a fit can pass the tight probe gate and still fail the looser full gate only if it violates the upper acceptance bound. - When
fitisNULL, all returned values areNAandpassisFALSE.
Purpose (internal). Computes a scalar "health" score for a single HMC fit. Used by the orchestrator for best-so-far tracking (the best result) and by the no-progress detector (to determine whether the current round improved on the previous best). Higher scores are better.
Arguments
| Argument | Type | Meaning |
|---|---|---|
fit |
list or NULL
|
The HMC fit object; must contain $accept_rate, $n_divergent, $ebfmi. |
total |
integer | Total number of proposals (warmup + sampling). |
Mathematics
The score is a linear combination of three statistics:
where $e = \texttt{fit$ebfmi}$ (clamped to 0 if not finite),
The E-BFMI term has unit weight, acceptance is weighted at
Returns A numeric scalar: the health score, or fit is NULL or fit$accept_rate is NULL.
Notes
- Non-finite E-BFMI values (e.g.,
NaN,Inf) are silently replaced with 0 before scoring, so a pathological energy geometry degrades to a penalty rather than producing a non-finite score. - The
$-\infty$ sentinel for a missing fit ensures that any valid fit always outranks aNULLfit in comparisons. - The penalty weight of 5 for divergences means that a single divergence in 100 proposals (
$r_d = 0.01$ ) reduces the score by$0.05$ , roughly offsetting an acceptance rate improvement of$0.25$ .
Purpose (internal). Performs a short, coarse step-size (
Arguments
| Argument | Type | Meaning |
|---|---|---|
geom_target |
gdpar_geom_target |
The geometry target to sample. |
metric |
metric object | The metric for the current level; may contain $suggested_epsilon. |
budget |
list | Budget object; uses $epsilon (default step), $tune_iter (iterations per probe), $L (number of leapfrog steps). |
base_seed |
integer | Base seed for the orchestrator. |
round |
integer | Current round index (used for deterministic seeding). |
Mathematics
The candidate
The raw grid is pmin(..., 5), duplicates removed, and sorted ascending:
For each tune_iter) is run with a deterministic seed. The gap metric is:
where
Returns A list:
-
epsilon(numeric): the chosen step size (defaults tobaseif all probes fail or yield non-finite gaps). -
fits(integer): number of HMC fits actually run (each probe, including failed ones, counts as one). -
seconds(numeric): cumulative wall-clock time in seconds spent on all probes.
Notes
- If a probe's
gdpar_geom_hmccall throws an error, it is caught and silently skipped (the grid iteration advances). - The target acceptance rate is hardcoded at
$0.8$ . - The penalty weight for divergences (5) matches the score function.
- The seed for each probe is derived deterministically via
.gdpar_orch_seed(base_seed, round, 100L + k), wherekis the 1-based grid index. The offset of 100 ensures these seeds do not collide with other slots in the orchestrator. - The function is bounded: at most
$|G| \leq 5$ probes are run, each oftune_iteriterations, so the cost is predictable and small relative to a full sampling phase.
Purpose (internal). Produces a deterministic, reproducible integer seed from a base seed, a round index, and a slot index. The entire adaptive trajectory of the orchestrator is a deterministic function of the base seed, because every child seed is derived through this function. This is the resumability guarantee: re-running with the same base seed retraces the identical sequence of random numbers.
Arguments
| Argument | Type | Meaning |
|---|---|---|
base |
integer | The caller-provided base seed. |
round |
integer | The current round index (0-based or 1-based, depending on the caller). |
slot |
integer | A slot identifier distinguishing different uses within a round (e.g., pilot, tune, full sampling, or epsilon probes). |
Mathematics
The constants + 1 shifts the result to
Returns An integer scalar in
Notes
- The result is passed through
as.integer(...), so it is of R typeinteger. - Because the formula is linear in
roundandslot, distinct (round, slot) pairs can collide if the base seed is adversarial, but the prime multipliers make this practically negligible for reasonable seed spaces. - The modulus is not the standard Mersenne prime
$2^{31} - 1$ but rather$2^{31} - 2$ , yielding a range of$[1, 2^{31} - 1]$ after the+ 1offset.
Purpose (internal). Derives a conjectured, falsifiable prescription from the accumulated evidence of an orchestrator run that ended in a certified limit. Each prescription item is tagged as a "conjectured" status (following the ORPHEUS framework, section 16.3) and carries a falsifiable re-run test. The function implements the "smallest change predicted to break the limit" heuristic.
Arguments
| Argument | Type | Meaning |
|---|---|---|
final_diag |
list | The final diagnostic object; fields used include $pathology, $difficulty_curve$grows_with_n, $difficulty_curve$slope, $cost$tractability. |
ledger |
list of lists | The per-round decision and sampling record; each element may contain $level and $metric_ok. |
fisher_available |
logical | Whether a Fisher information function was supplied by the caller. |
budget |
list | The budget object used for the run. |
best_score |
numeric | The best health score achieved across all rounds (from .gdpar_orch_score). |
tried_keys |
character vector | The ladder keys that were actually tried during the run. |
Mathematics
No explicit formula; the logic is a rule-based cascade of conditional prescriptions (see Returns). The key decision variables are:
-
patho$= \texttt{final\_diag\$pathology}$ : one of"multimodal","boundary","flat_direction","quasi_deterministic", or other curvature pathologies. -
grows$= \texttt{final\_diag\$difficulty\_curve\$grows\_with\_n}$ : whether the diagnostic difficulty grows with sample size. -
sub_sampled: whether the sub-Riemannian level was actually built and sampled (not just attempted). -
tract$= \texttt{final\_diag\$cost\$tractability}$ : the cost verdict from the diagnostic.
Returns A list of prescription items. Each item is a list with fields:
-
action(character): the recommended action. -
rationale(character): why this action is recommended. -
falsifiable_test(character): a re-run test that would falsify the prescription. -
status(character): always"conjectured".
The prescription cascade (first match wins for out-of-scope pathologies):
-
"multimodal"pathology$\Rightarrow$ single item recommending tempering / a multimodal sampler; short-circuit return. -
"boundary"pathology$\Rightarrow$ single item recommending reparametrisation of the boundary-pinned parameter; short-circuit return. -
"flat_direction"pathology$\Rightarrow$ single item recommending reparametrisation or elimination of the flat direction (Option A); short-circuit return. -
No Fisher + sub-Riemannian not sampled + geometry contracts (
$\texttt{grows} \lor \texttt{patho} = \texttt{"quasi\_deterministic"}$ )$\Rightarrow$ item recommending supply of the expected Fisher and enabling the sub-Riemannian level. -
Sub-Riemannian was sampled
$\Rightarrow$ item recommending setting the sub-Riemannian reference to the warmup mode rather than the origin. -
Best score is finite and positive + tractability is
"expensive"$\Rightarrow$ item recommending increased per-fit budget and epsilon/L tuning. -
Fallback (no items above)
$\Rightarrow$ item recommending increased budget (more rounds / iterations) and revisiting epsilon / L.
Notes
- The out-of-scope pathologies (multimodal, boundary, flat_direction) short-circuit immediately; they name the proper remedy outside the geometry ladder, deliberately avoiding spurious geometry advice.
- The
sub_sampledflag is computed by scanning the ledger: it isTRUEonly if some round haslevel = "sub_riemannian"andmetric_ok = TRUE(the metric was actually built successfully). - The function uses a local
addclosure to append items, modifying the outeritemslist via<<-. - The
budgetandtried_keysarguments are accepted but not explicitly referenced in the current implementation (they may be used in future expansions or are present for API consistency).
.gdpar_orch_certificate(verdict, final_diag, ledger, culprit, prescription, reproducibility, budget_spent)
Purpose (internal). Assembles the certified-limit certificate object. This is the structured output when the orchestrator exhausts its budget without achieving a passing gate, or when the diagnostic identifies an out-of-scope pathology. The certificate has three rigour layers (algebraic, statistical, numerical) plus a conjectured prescription and reproducibility metadata.
Arguments
| Argument | Type | Meaning |
|---|---|---|
verdict |
character | The overall verdict string (e.g., "certified_limit", "out_of_scope"). |
final_diag |
list | The final diagnostic object; fields used include $pathology, $signals$condition_number, $difficulty_curve$slope, $difficulty_curve$grows_with_n. |
ledger |
list of lists | The per-round decision and sampling record (the statistical layer). |
culprit |
character or list | Identifies the primary cause or level at which the limit was reached. |
prescription |
list of lists | The prescription items from .gdpar_orch_prescription. |
reproducibility |
list | Reproducibility metadata (base seed, round structure, etc.). |
budget_spent |
list | Numerical accounting: total fits, total seconds, rounds completed, etc. |
Mathematics
No formula; this is a pure data-assembly function.
Returns A list of class c("gdpar_geom_certificate", "list") with the following structure:
-
$verdict: the verdict string. -
$algebraic: the algebraic rigour layer —-
$pathology: the identified pathology class. -
$condition_number: the final condition number from the signals. -
$difficulty_slope: the difficulty-vs-$n$ slope. -
$grows_with_n: logical, whether difficulty grows with sample size. -
$culprit: the culprit identifier.
-
-
$statistical: the fullledger(per-level sampling diagnostics). -
$numerical: thebudget_spentaccounting. -
$prescription: the list of conjectured prescription items. -
$reproducibility: the reproducibility metadata.
Notes
- The class is set to
c("gdpar_geom_certificate", "list"), enabling S3 dispatch (e.g., aprint.gdpar_geom_certificatemethod if one exists). - The
condition_numberis extracted as the last row offinal_diag$signals$condition_number, i.e., the most recent diagnostic signal. - The function is purely structural: it does no computation, validation, or transformation beyond field extraction and list assembly.
Purpose (internal). Implements the opt-in Laplace fallback (RG.7 step 4, D99). When a run ends in a certified limit and the caller opted in via laplace_fallback = TRUE, this function attempts to attach a Laplace approximation to the certificate and relabels the status accordingly. The function is conservative: on the out-of-scope path it only attaches the Laplace when the curvature at the mode is genuinely positive-definite, to avoid presenting a Gaussian approximation when the Gaussian premise is violated.
Arguments
| Argument | Type | Meaning |
|---|---|---|
obj |
list | The orchestrator result object; must have $status and will gain $laplace on success. |
geom_target |
gdpar_geom_target |
The geometry target for the Laplace computation. |
reference |
numeric vector of length |
The position (mode) at which the Laplace is computed. |
draws |
integer | Number of iid draws from the Laplace approximation to carry (0 = mode + precision only). |
seed |
integer | Seed for reproducibility of the Laplace draws. |
inform |
function | Callback for informational messages (e.g., message). |
Mathematics
The function calls gdpar_geom_laplace(geom_target, reference, draws, seed) which computes the Laplace approximation around reference:
The returned object carries $all_pos (whether the Hessian is positive-definite at the reference) and $fit_quality_label (a human-readable fidelity diagnosis).
Returns The modified obj (the same list, mutated in place by adding $laplace and updating $status), or the original obj unchanged if the Laplace computation fails or the Gaussian premise is violated.
Status relabeling logic:
Original $status
|
Condition | New $status
|
|---|---|---|
"certified_limit" |
Laplace succeeds | "certified_limit_laplace" |
"out_of_scope" |
Laplace succeeds and $all_pos = TRUE
|
"out_of_scope_laplace" |
"out_of_scope" |
Laplace succeeds and $all_pos = FALSE
|
unchanged (no attachment) |
| any | Laplace throws an error | unchanged (warning emitted) |
Notes
- If
gdpar_geom_laplacethrows an error, it is caught, a warning of class"gdpar_geometry_warning"is emitted viagdpar_warn, and the originalobjis returned unmodified. This makes the function fail-safe. - On the
"out_of_scope"path, if the curvature at the mode is not positive-definite ($all_pos = FALSE), the function deliberately does not attach the Laplace and prints an informational message explaining why. This prevents overreach: the Gaussian premise does not hold for multimodal, boundary, or flat-direction pathologies. - The
informcallback is called with a formatted message indicating whichfit_quality_labelwas attached and what the new status is. - When
draws = 0(the default forlaplace_draws), the Laplace object contains only the mode and the precision matrix; no random draws are generated. - The function mutates
objby direct assignment (obj$laplace <- lap,obj$status <- ...) before returning it, so the caller receives the modified object.
gdpar_geom_orchestrate(target, geom_target = NULL, fisher = NULL, reference = NULL, level_map = NULL, entry_level = NULL, budget = NULL, criteria = NULL, speed = 10, rest_mass = 1, n_grid = NULL, checkpoint_dir = NULL, laplace_fallback = FALSE, laplace_draws = 0L, seed = 20260603L, verbose = TRUE, ...)
Purpose
Main exported orchestrator for geometry-adaptive parameter estimation. It implements a closed-loop control system that diagnoses the geometry of a sampling target, selects and evaluates parameterization levels on a ladder of increasing complexity, and applies a two-phase probe-and-full sampling strategy (successive-halving spirit) under a configurable computational budget. The function aims to find a parameterization level that passes a success gate, certifying intractability if none are found within the budget.
Arguments
-
target(object): The primary diagnostic target. Can be agdpar_geometry_targetobject, a list with amodel(e.g., aCmdStanModel) anddim, or another object coercible to a geometry target. Its structure determines howgeom_targetis derived if not supplied. -
geom_target(list orNULL): The explicit engine sampling target, agdpar_geom_targetobject. IfNULL, derived fromtargetvia.gdpar_orch_derive_geom_target. -
fisher(function orNULL): A function of parameter vectorthetareturning the Fisher information matrix (or its approximation). Used to construct a Riemannian metric at certain ladder levels. Must beNULLor a function. -
reference(numeric vector orNULL): The reference point for parameterization anchoring. Its length must match the dimensiondofgeom_target. Defaults to a zero vector of lengthd. -
level_map(list orNULL): A mapping from diagnosed pathology codes to specific ladder level keys. Used by.gdpar_orch_select_entryto override the default pathology-to-level mapping. -
entry_level(character orNULL): A specific ladder level key to start the orchestration, bypassing the initial diagnosis-based selection. -
budget(list orNULL): Computational budget controls. IfNULL, defaults togdpar_geom_orchestrate_budget(). Elements includemax_rounds,max_fits,max_seconds,max_seconds_per_fit,max_levels,L,probe_warmup,probe_iter,full_warmup,full_iter,epsilon,tune_epsilon,hysteresis,stall_limit,n_rediagnose. -
criteria(list orNULL): Success gate criteria. IfNULL, defaults togdpar_geom_orchestrate_criteria(). Used by.gdpar_orch_success_gateto evaluate sampler output. -
speed(numeric scalar): Parameter controlling the speed of the dynamics (e.g., in the kinetic energy). Must be non-negative. -
rest_mass(numeric scalar): Parameter controlling the rest mass (inverse scale) in the kinetic energy. Must be non-negative. -
n_grid(integer orNULL): Number of grid points for the initial geometry diagnostic (passed togdpar_geometry_diagnostic).NULLuses the diagnostic's default. -
checkpoint_dir(character orNULL): Path to a directory. If non-NULL, the ledger is atomically saved here after each round via.gdpar_orch_checkpoint. -
laplace_fallback(logical): IfTRUE, attaches a Laplace approximation fallback to the output object when the orchestration terminates (eitherout_of_scopeorcertified_limit). -
laplace_draws(integer): Number of Laplace draws to take iflaplace_fallbackisTRUE. -
seed(integer): Master seed for reproducibility. Used to generate per-round, per-slot seeds via.gdpar_orch_seed(seed, round, slot). -
verbose(logical): IfTRUE, emits progress messages viagdpar_inform. -
...(additional arguments): Passed togdpar_geometry_diagnostic.
Mathematics
The orchestration implements a monotonically increasing ladder index .gdpar_orch_ladder(), where each level has an index. The controller cycles through levels
- Initial diagnosis
$D_0$ viagdpar_geometry_diagnostic. - Selection
$s = \text{select\_entry}(D_0)$ . - For each round
$r = 1, \dots, R_{\max}$ : a. Budget admission check: Stop if estimated remaining time exceedsmax_secondsor ifmax_fitsreached. b. Cycle guard: If state$(s, \text{"probe"})$ visited, escalate to next level above floor$f$ . c. Build metric$M_s$ for level$s$ . d. Step-size tuning (optional):$\epsilon \leftarrow \text{tune\_epsilon}(\text{target}, M_s)$ . e. Probe: sample$n_p = \text{probe\_warmup} + \text{probe\_iter}$ draws with metric$M_s$ , step size$\epsilon$ , and leapfrog steps$L$ . f. Gate probe:$G_p = \text{success\_gate}(\text{probe\_fit}, n_p, \text{criteria}, 0)$ . g. If$G_p.\text{pass}$ , full phase: sample$n_f = \text{full\_warmup} + \text{full\_iter}$ draws. Gate full:$G_f = \text{success\_gate}(\text{full\_fit}, n_f, \text{criteria}, \text{hysteresis})$ . h. Update best score:$\text{score} = \text{score\_fit}(\cdot)$ . i. If$G_f.\text{pass}$ , return resolved. j. Else, update floor$f = \max(f, \text{index}(s))$ , re-diagnose$D_r$ with fresh seed, choose next level$s'$ :- If re-diagnosis unstable (checked with
n_rediagnose >= 2), choose safe level above$f$ . - Else, map pathology to candidate
$c$ . If$c$ is valid and$\text{index}(c) > f$ , choose$c$ ; else safe level. - If
$s' == s$ , increment stall counter; if stall$\ge$ stall_limit, stop. k. Loop until budget exhausted or top of ladder reached.
- If re-diagnosis unstable (checked with
- If loop ends without resolution, output
certified_limitwith a prescription.
The metric construction at level fisher and reference to define a Riemannian metric (e.g., Euclidean, Fisher-Rao, or Hessian-based) depending on the level's definition.
Returns
A list of class c("gdpar_geom_orchestration", "list") with elements:
-
status(character): One of"out_of_scope","resolved","certified_limit". -
level(character): Ladder level key at which resolution occurred (if"resolved"). -
level_index(integer): Index of the resolved level. -
metric(list): The metric object used at the resolved level. -
draws(matrix): Post-warmup draws from the final successful fit (if"resolved"). -
fit(list): The final successful HMC fit object. -
gate(list): The success gate result for the final fit. -
certificate(list): A certificate object (for"out_of_scope"or"certified_limit"statuses). -
ledger(list): A list of per-round records with diagnostics, gates, acceptance rates, EBFMI, timing, etc. -
diagnosis(list): The initial geometry diagnostic object (diag0). -
best(list): Best fit found during orchestration, with elementsscore,draws,fit,key. -
budget_spent(list):fits(integer count) andseconds(total elapsed time). -
reproducibility(list):seed,gdpar_version,budget,criteria,initial_pathology,dim. -
laplace_fit(list, optional): Attached iflaplace_fallback = TRUEand status is"out_of_scope"or"certified_limit".
Notes
- The function performs validation on
seed,speed,rest_mass, andfisher(must be a function or NULL). Raises"gdpar_input_error"on failure. - Emits an initial message stating it is an "opt-in geometry-adaptive controller; it does not modify any fit."
- Uses
setTimeLimit(elapsed = cap, transient = TRUE)to enforce per-fit time limits; timeout is caught as an error and recorded. - Checkpointing writes the ledger atomically via
.gdpar_orch_checkpoint(temp file then rename). - The
floor_indexacts as a monotone ratchet: the controller never considers levels with index$\le f$ . - The
stallcounter resets when the selected level changes; triggers stop atbudget$stall_limitconsecutive stalls. - The
warmup_drawsare updated from the best probe or full fit to warm-start subsequent metric constructions (viareferenceupdate and passing draws to.gdpar_orch_build_metric). - The
bestobject tracks the highest-scoring fit seen across all rounds, regardless of gate outcome. - The
...arguments are passed togdpar_geometry_diagnostic, allowing custom diagnostic settings. - If
geom_targetis not agdpar_geom_target, it is coerced viagdpar_geom_target(geom_target). - The
referenceis updated in-place to the last row of the best fit's draws (if finite) to potentially guide future metric constructions.
Purpose
Internal helper that constructs a gdpar_geom_target object from the primary target argument when geom_target is not supplied by the user. It handles two supported target types: a gdpar_geometry_target (from the diagnostic phase) or a list containing a Stan model (CmdStanModel) with dim and optionally data.
Arguments
-
target(object): The diagnostic target. Must be either:- A
gdpar_geometry_targetobject (has amakemethod and default parameters). - A list with a
modelelement (e.g., aCmdStanModel) and adimelement (integer, the unconstrained dimension). An optionaldataelement is passed togdpar_geom_target.
- A
Mathematics
None.
Returns
A gdpar_geom_target object, created by calling gdpar_geom_target on the appropriate object(s).
Notes
- Raises a
"gdpar_input_error"if the target type is unrecognized or if a Stan-model target lacksdim. - For a
gdpar_geometry_target, it callstarget$make(target$default_n, target$default_difficulty)to instantiate the model, then passes the result togdpar_geom_target. - For a Stan-model list target, it passes
object = target$model,dim = target$dim, anddata = target$datatogdpar_geom_target. - The error message for unrecognized targets suggests supplying
geom_targetexplicitly.
Purpose Internal helper that atomically persists the orchestrator's ledger (the running log of level attempts) to disk. It writes to a temporary file first, then renames, so that a crash mid-write cannot corrupt the canonical ledger file. Called by the orchestrator loop to create recoverable checkpoints.
Arguments
| Argument | Type | Meaning |
|---|---|---|
dir |
character(1) |
Directory in which to store the checkpoint. Created recursively if it does not exist. |
ledger |
list (typically a list of per-level result records) |
The current state of the orchestration ledger to persist. |
Returns
invisible(NULL). Called purely for its side effect of writing orchestrate_ledger.rds into dir.
Notes
-
Atomic write: The function writes to a temporary
.rdsfile (viatempfilewithtmpdir = dir) and then atomically renames it toorchestrate_ledger.rdsusingfile.rename. This ensures that readers oforchestrate_ledger.rdsnever observe a partially written file. -
Directory creation:
dir.createis called withrecursive = TRUEandshowWarnings = FALSE, so nested directories are created silently and an existing directory does not raise a warning. -
No return value on purpose:
invisible(NULL)signals that callers should not depend on a return value; the meaningful output is the file on disk.
Purpose
S3 print method for objects of class gdpar_geom_orchestration. Provides a concise, human-readable summary of the orchestrator's final status—whether it resolved successfully or issued a certificate of failure—including key diagnostics and budget consumption.
Arguments
| Argument | Type | Meaning |
|---|---|---|
x |
gdpar_geom_orchestration (list) |
The orchestration result object produced by gdpar_geom_orchestrate. |
... |
(unused) | Generic ellipsis for S3 method compatibility; not consumed. |
Returns
invisible(x). The object is printed as a side effect and returned invisibly so that it can be piped.
Notes
-
Branching on
x$status:- If
identical(x$status, "resolved"), the printout includes:- The resolution level and its index (
x$level,x$level_index). - The dimensions of the draw matrix (
nrow(x$draws)×ncol(x$draws)). - The acceptance rate (
x$fit$accept_rate) and E-BFMI (x$fit$ebfmi), both formatted to 3 significant digits.
- The resolution level and its index (
- Otherwise (failure / certificate case), the printout includes:
- The certificate verdict (
x$certificate$verdict). - The chain of levels attempted, extracted by
vapplyoverx$ledgerpulling each record's$levelfield, deduplicated withunique, and joined with" -> ". - The number of prescription items in
x$certificate$prescription.
- The certificate verdict (
- If
-
Laplace fallback block: If
x$laplaceis notNULL, a secondary block is printed showing the Laplace approximation's fit-quality label, the (unfloored) condition number (x$laplace$cond_unfloored), and the Pareto-$k$ diagnostic (x$laplace$fit_quality$pareto_k), each formatted to 3 significant digits. -
Budget summary: Always prints
x$budget_spent$fits(number of model fits) andx$budget_spent$seconds(wall-clock time, 3 significant digits). -
Side effect: Output is written to the console via
cat.
Purpose
S3 print method for objects of class gdpar_geom_certificate. Summarises the structured certificate emitted by gdpar_geom_orchestrate when the geometry-adaptive ladder cannot be resolved within the prescribed budget or when diagnosis points outside the ladder entirely. The certificate separates evidence into rigour layers and attaches a falsifiable prescription.
Arguments
| Argument | Type | Meaning |
|---|---|---|
x |
gdpar_geom_certificate (list) |
The certificate object. Expected to contain the named elements verdict, algebraic, statistical, numerical, prescription, and (optionally) reproducibility. |
... |
(unused) | Generic ellipsis for S3 method compatibility; not consumed. |
Returns
invisible(x). The certificate is printed as a side effect and returned invisibly.
Notes
-
Algebraic layer (
x$algebraic): Printed fields are:-
pathology— a label describing the detected geometric pathology (e.g., ill-conditioning). -
condition_number— the condition number of the relevant matrix, formatted to 3 significant digits. -
grows_with_n— logical flag indicating whether the pathology worsens with increasing sample size$n$ .
-
-
Statistical layer (
x$statistical): Only the count of recorded levels is printed (length(x$statistical)), not the per-level details themselves. -
Numerical layer (
x$numerical): Total number of fits and total seconds consumed, the latter formatted to 3 significant digits. -
Prescription (
x$prescription): Iterated withfor; each element is expected to be a list with at least an$actionfield, which is printed as a bullet. The prescription represents conjectured, falsifiable fixes to the identified pathology. -
Side effect: All output goes to the console via
cat.
Purpose
Exported entry point that assembles and returns the Block RG calibration suite — a named registry of eight synthetic target distributions (G0–G7), each engineered to exhibit one row of the posterior-geometry pathology taxonomy with a known ground truth. The suite is the falsifiable backbone against which gdpar_geometry_diagnostic is calibrated.
Arguments
| Argument | Type | Meaning |
|---|---|---|
which |
NULL or character vector |
Optional subset of target ids to select. When NULL (default), all eight targets are returned. |
Returns
A named list of gdpar_geometry_target objects. The names are exactly G0_isotropic, G1_anisotropic, G2_funnel, G3_heavy_tails, G4_quasi_deterministic, G5_multimodal, G6_boundary, G7_flat_direction. When which is non-NULL, the returned list is the sub-list registry[which], preserving the original names and order.
Notes
- If
whichis supplied but is not a character vector, or if any element is not amongnames(registry), the function callsgdpar_abortwith a message of the form"Argument 'which' must be a subset of: G0_isotropic, G1_anisotropic, …"and error class"gdpar_input_error". - The eight registry entries are built by the internal constructors
.gdpar_geom_G0()through.gdpar_geom_G7()(onlyG0–G4are defined in this section;G5–G7are referenced but their definitions lie in the subsequent section).
.gdpar_geom_target(id, label, pathology, geometry_remedy, culprit, difficulty_scales_with_n, bounds, default_n, default_difficulty, n_grid, make)
Purpose
Internal constructor helper that bundles the static metadata fields of a synthetic target together with its make instance-constructor and stamps the S3 class gdpar_geometry_target on the result. Every .gdpar_geom_G*() factory delegates to this helper to produce its registry entry.
Arguments
| Argument | Type | Meaning |
|---|---|---|
id |
character | Short identifier of the target (e.g. "G0_isotropic"). |
label |
character | Human-readable descriptive label. |
pathology |
character | Pathology category from the Block RG taxonomy. |
geometry_remedy |
character | The geometry level that remedies the pathology (the "remedy" column). |
culprit |
character or NA_character_
|
Name(s) of the culprit parameter(s), or NA_character_ when no single parameter is implicated. |
difficulty_scales_with_n |
logical | Ground-truth flag: does the difficulty grow with the size knob n? |
bounds |
named list or NULL
|
Constrained-scale parameter bounds; NULL when the target is unconstrained. |
default_n |
numeric/integer | Default value of the size knob n. |
default_difficulty |
numeric/integer | Default difficulty value. |
n_grid |
numeric/integer vector | Suggested sweep of n values for measuring the difficulty-vs-n curve. |
make |
function | Instance constructor with signature make(n, difficulty) returning a target instance list. |
Returns
A list with elements id, label, pathology, geometry_remedy, culprit, difficulty_scales_with_n, bounds, default_n, default_difficulty, n_grid, make, assigned S3 class c("gdpar_geometry_target", "list").
Notes
- The leading comment states the helper "validates the
make()contract lightly so a malformed target surfaces early," but the code as written performs no validation: it only assembles the list and assigns the class. A malformedmakewould not be caught here.
Purpose
Builds the G0_isotropic registry entry: an isotropic standard normal serving as the easy negative control. The prescribed remedy is the Euclidean diagonal metric (the current default).
Arguments None.
Mathematics
The make(n, difficulty) closure sets
The Stan program is:
data { int<lower=1> d; }
parameters { vector[d] theta; }
model { theta ~ std_normal(); }
Returns
A gdpar_geometry_target with static fields:
| Field | Value |
|---|---|
id |
"G0_isotropic" |
label |
"Isotropic standard normal (easy control)" |
pathology |
"isotropic" |
geometry_remedy |
"euclidean_diagonal" |
culprit |
NA_character_ |
difficulty_scales_with_n |
FALSE |
bounds |
NULL |
default_n |
1 |
default_difficulty |
5 |
n_grid |
c(1, 4, 16) |
The make(n = 1, difficulty = 5) closure returns an instance list with components stan_code (character), stan_data = list(d = d), log_prob (function), grad_log_prob (function), dim = d, and param_names = paste0("theta[", seq_len(d), "]").
Notes
- The argument
nis accepted but ignored; onlydifficultyis used (it sets the dimension$d$ ). -
stan_datacontains onlyd.
Purpose
Builds the G1_anisotropic registry entry: a diagonal normal with a fixed, large condition number n — a straight canyon. The prescribed remedy is a constant dense Euclidean metric.
Arguments None.
Mathematics
The make(n, difficulty) closure fixes
Equivalently, vars <- exp(seq(0, log(kappa), length.out = d)). With
The Stan program is:
data { int<lower=1> d; vector<lower=0>[d] sigma; }
parameters { vector[d] theta; }
model { theta ~ normal(0, sigma); }
Returns
A gdpar_geometry_target with static fields:
| Field | Value |
|---|---|
id |
"G1_anisotropic" |
label |
"Anisotropic normal, fixed condition number" |
pathology |
"anisotropic" |
geometry_remedy |
"euclidean_dense" |
culprit |
NA_character_ |
difficulty_scales_with_n |
FALSE |
bounds |
NULL |
default_n |
1 |
default_difficulty |
100 |
n_grid |
c(1, 4, 16) |
The make closure returns an instance with stan_data = list(d = d, sigma = sigma), dim = 5L, and param_names = paste0("theta[", seq_len(d), "]").
Notes
-
nis ignored; the dimension is fixed at$d = 5$ . -
difficultyis interpreted as the condition number$\kappa$ (variance ratio), defaulting to100.
Purpose
Builds the G2_funnel registry entry: Neal's funnel, in which a log-scale variable v governs the spread of the remaining coordinates, producing position-dependent curvature. The prescribed remedy is a position-dependent (Riemannian) metric.
Arguments None.
Mathematics
The make(n, difficulty) closure fixes
On the unconstrained scale, the log-density (up to an additive constant) is
The gradient components are
The Stan program is:
data { int<lower=1> d; real<lower=0> scale_v; }
parameters { real v; vector[d - 1] x; }
model {
v ~ normal(0, scale_v);
x ~ normal(0, exp(v / 2));
}
Returns
A gdpar_geometry_target with static fields:
| Field | Value |
|---|---|
id |
"G2_funnel" |
label |
"Neal's funnel (variable curvature)" |
pathology |
"funnel" |
geometry_remedy |
"riemannian" |
culprit |
"v" |
difficulty_scales_with_n |
FALSE |
bounds |
NULL |
default_n |
1 |
default_difficulty |
3 |
n_grid |
c(1, 4, 16) |
The make closure returns an instance with stan_data = list(d = d, scale_v = scale_v), dim = 10L, and param_names = c("v", paste0("x[", seq_len(d - 1), "]")).
Notes
-
nis ignored; the dimension is fixed at$d = 10$ (so there are$d - 1 = 9$ xcoordinates). -
difficultyis interpreted as the prior standard deviationscale_vof the funnel variablev. - The gradient closure returns
c(gv, gx)wheregvis the scalar derivative w.r.t.vandgxis the length-$(d-1)$ vector of derivatives w.r.t.x.
Purpose
Builds the G3_heavy_tails registry entry: independent Student-$t$ distributions with a small number of degrees of freedom, exhibiting directional heaviness. The prescribed remedy is a non-inner-product (Finsler / relativistic) kinetic energy.
Arguments None.
Mathematics
The make(n, difficulty) closure fixes
and the gradient is
The Stan program is:
data { int<lower=1> d; real<lower=0> nu; }
parameters { vector[d] theta; }
model { theta ~ student_t(nu, 0, 1); }
Returns
A gdpar_geometry_target with static fields:
| Field | Value |
|---|---|
id |
"G3_heavy_tails" |
label |
"Heavy tails (independent Student-t)" |
pathology |
"heavy_tails" |
geometry_remedy |
"finsler_relativistic" |
culprit |
NA_character_ |
difficulty_scales_with_n |
FALSE |
bounds |
NULL |
default_n |
1 |
default_difficulty |
2 |
n_grid |
c(1, 4, 16) |
The make closure returns an instance with stan_data = list(d = d, nu = nu), dim = 5L, and param_names = paste0("theta[", seq_len(d), "]").
Notes
-
nis ignored; the dimension is fixed at$d = 5$ . -
difficultyis interpreted as the degrees-of-freedom parameter$\nu$ ; low$\nu$ (default2) yields heavier tails. - The gradient uses
log1pinside the log-density but the closed-form derivative avoidslog1pentirely.
Purpose
Builds the G4_quasi_deterministic registry entry: a near-degenerate canyon in which n and the posterior collapses toward a lower-dimensional manifold. This is the eBird count case; the prescribed remedy is a sub-Riemannian (distribution-of-directions) treatment.
Arguments None.
Mathematics
The make(n, difficulty) closure sets
Thus the first direction has variance
The Stan program is:
data { int<lower=1> d; real<lower=0> n_eff; }
parameters { vector[d] theta; }
model {
theta[1] ~ normal(0, 1);
theta[2:d] ~ normal(0, inv_sqrt(n_eff));
}
Returns
A gdpar_geometry_target with static fields:
| Field | Value |
|---|---|
id |
"G4_quasi_deterministic" |
label |
"Quasi-deterministic canyon (condition number grows with n)" |
pathology |
"quasi_deterministic" |
geometry_remedy |
"sub_riemannian" |
culprit |
NA_character_ |
difficulty_scales_with_n |
TRUE |
bounds |
NULL |
default_n |
100 |
default_difficulty |
5 |
n_grid |
c(10, 100, 1000) |
The make closure returns an instance with stan_data = list(d = d, n_eff = n_eff), dim = d, and param_names = paste0("theta[", seq_len(d), "]").
Notes
- This is the only target in this section for which
difficulty_scales_with_nisTRUE:nis the genuine size knob driving the condition number. -
difficultycontrols the dimension$d$ (default5), whilencontrols the inverse variance of the pinned directions (default100). - The
n_gridc(10, 100, 1000)spans three orders of magnitude, reflecting the structural (not merely quasi-deterministic) nature of the pathology. - The
inv_varvector is constructed asc(1, rep(n_eff, d - 1)); when$d = 1$ this reduces toc(1)(no pinned directions), though the defaultdifficulty = 5gives$d = 5$ .
Purpose
Factory function that constructs and registers the "G5_multimodal" geometry target — a
Arguments
This function takes no arguments.
Mathematics
The target density (up to additive constants) is the equal-weight mixture
with log_prob omits the normalising constant
then applies the log-sum-exp stabilisation with
The gradient uses the posterior responsibilities (mixture weights)
giving
The Stan code declares d and sep as data, theta as a vector[d] parameter, and adds log_mix(0.5, normal_lpdf(theta | -sep, 1), normal_lpdf(theta | sep, 1)) to the target.
Returns
The return value of .gdpar_geom_target(...), i.e. a gdpar_geometry_target object. The embedded make closure (with signature make(n = 1, difficulty = 4)) returns a list containing:
| Element | Type / Structure |
|---|---|
stan_code |
Character scalar (Stan model string) |
stan_data |
list(d = 3L, sep = <difficulty>) |
log_prob |
Function: function(theta) → numeric scalar |
grad_log_prob |
Function: function(theta) → numeric vector of length d
|
dim |
Integer scalar 3L
|
param_names |
Character vector c("theta[1]", "theta[2]", "theta[3]")
|
The registered metadata is: id = "G5_multimodal", label = "Two well-separated normal modes", pathology = "multimodal", geometry_remedy = "tempering", culprit = NA_character_, difficulty_scales_with_n = FALSE, bounds = NULL, default_n = 1, default_difficulty = 4, n_grid = c(1, 4, 16).
Notes
-
difficultyis coerced to numeric viaas.numeric(difficulty)and used directly assep; larger values place the modes farther apart, increasing multimodality severity. - The
nargument ofmakeis accepted but unused — the geometry is structural and does not scale withn(difficulty_scales_with_n = FALSE). - The R-side
log_probandgrad_log_probomit normalising constants that the Stannormal_lpdfincludes; they are self-consistent (gradient matches the log-density) but not identical to the Stan log-density up to a constant. -
culpritisNA_character_(no single parameter is responsible).
Purpose
Factory function that constructs and registers the "G6_boundary" geometry target — a
Arguments
This function takes no arguments.
Mathematics
On the constrained scale, the density is
where
where
The gradient with respect to
and with respect to each
The Stan code declares d and alpha as data, b as a real<lower=0, upper=1> parameter, z as a vector[d-1] parameter, with b ~ beta(alpha, 1) and z ~ std_normal().
Returns
The return value of .gdpar_geom_target(...), i.e. a gdpar_geometry_target object. The embedded make closure (with signature make(n = 1, difficulty = 0.3)) returns a list containing:
| Element | Type / Structure |
|---|---|
stan_code |
Character scalar (Stan model string) |
stan_data |
list(d = 4L, alpha = <difficulty>) |
log_prob |
Function: function(theta) → numeric scalar |
grad_log_prob |
Function: function(theta) → numeric vector of length d
|
dim |
Integer scalar 4L
|
param_names |
Character vector c("phi_b", "z[1]", "z[2]", "z[3]")
|
The registered metadata is: id = "G6_boundary", label = "Mass pinned against a (0, 1) bound", pathology = "boundary", geometry_remedy = "boundary_reparam", culprit = "b", difficulty_scales_with_n = FALSE, bounds = list(b = c(0, 1)), default_n = 1, default_difficulty = 0.3, n_grid = c(1, 4, 16).
Notes
-
difficultyis coerced to numeric and used asalpha; values below 1 (default 0.3) concentrate Beta mass near$b=0$ . - The
inv_logithelper is defined insidemakeand closes over nothing external. -
log1p(-b)is used for numerical stability in computing$\log(1-b)$ . - The R-side
log_probdrops the constant$\log\alpha$ ; the gradient is exact for the unnormalised log-density. -
nis accepted but unused (structural geometry,difficulty_scales_with_n = FALSE). -
boundsis set tolist(b = c(0, 1)), recording the constrained range of the culprit parameter. -
culpritis"b"(the constrained parameter), not"phi_b"(the unconstrained working parameter).
Purpose
Factory function that constructs and registers the "G7_flat_direction" geometry target — a 2-dimensional target where the sum
Arguments
This function takes no arguments.
Mathematics
The log-density (up to additive constants) is
where the first term is the log-likelihood
The gradient is
The Hessian is
whose eigenvalues are
The Stan code declares tau as data, a and b as real parameters, with target += normal_lpdf(0 | a + b, 1), a ~ normal(0, tau), b ~ normal(0, tau).
Returns
The return value of .gdpar_geom_target(...), i.e. a gdpar_geometry_target object. The embedded make closure (with signature make(n = 1, difficulty = 100)) returns a list containing:
| Element | Type / Structure |
|---|---|
stan_code |
Character scalar (Stan model string) |
stan_data |
list(tau = <difficulty>) |
log_prob |
Function: function(theta) → numeric scalar |
grad_log_prob |
Function: function(theta) → numeric vector of length 2 |
dim |
Integer scalar 2L
|
param_names |
Character vector c("a", "b")
|
The registered metadata is: id = "G7_flat_direction", label = "Reparametrisation redundancy (flat a - b)", pathology = "flat_direction", geometry_remedy = "reparam_eliminate", culprit = c("a", "b"), difficulty_scales_with_n = FALSE, bounds = NULL, default_n = 1, default_difficulty = 100, n_grid = c(1, 4, 16).
Notes
-
difficultyis coerced to numeric and used astau; larger values widen the priors and flatten the contrast direction. -
inv_tau2 <- 1 / tau^2is precomputed once inmakeand captured by thelog_probandgrad_log_probclosures for efficiency. - The R-side
log_probomits normalising constants from both the likelihood and the priors; the gradient is exact for the unnormalised log-density. -
nis accepted but unused (structural geometry,difficulty_scales_with_n = FALSE). -
culpritis a length-2 character vectorc("a", "b")because both parameters jointly contribute to the redundancy. -
boundsisNULL(both parameters are unconstrained).
Purpose
S3 print method for objects of class gdpar_geometry_target. Produces a human-readable multi-line summary of the geometry target's identity, pathology, remedy, culprit, and difficulty-scaling flag.
Arguments
| Argument | Type | Meaning |
|---|---|---|
x |
gdpar_geometry_target |
The geometry target object to print. Expected to contain elements id, label, pathology, geometry_remedy, culprit, difficulty_scales_with_n. |
... |
Any | Unused; present for S3 generic compatibility. |
Returns
Invisibly returns x (via invisible(x)).
Notes
- Dispatches on class
gdpar_geometry_targetvia the S3printgeneric. - The
culpritfield is checked withall(is.na(x$culprit)); if all entries areNA, the string"none"is printed. Otherwise, the culprit values are collapsed withpaste(x$culprit, collapse = ", "). - Output format (four lines):
<gdpar_geometry_target> <id><label>pathology: <pathology> | remedy: <geometry_remedy>culprit: <culprits or "none"> | difficulty scales with n: <difficulty_scales_with_n>
- No validation is performed on
x; missing elements would trigger R's standard subsetting error. - Exported (has
@exportroxygen tag).
← Part IV — Exhaustive Function Reference (4/7) · gdpar Wiki Home · Part IV — Exhaustive Function Reference (6/7) →
- Part I — Conceptual Framework
- Part II — Mathematical Foundations
- Part III — Computational Architecture
- Part IV — Exhaustive Function Reference (1/7)
- Part IV — Exhaustive Function Reference (2/7)
- Part IV — Exhaustive Function Reference (3/7)
- Part IV — Exhaustive Function Reference (4/7)
- Part IV — Exhaustive Function Reference (5/7)
- Part IV — Exhaustive Function Reference (6/7)
- Part IV — Exhaustive Function Reference (7/7)
- Part V — Stan Templates (1/3)
- Part V — Stan Templates (2/3)
- Part V — Stan Templates (3/3)
- Part VI — Data, Benchmarks, Tests & References