Package 'admvn'

Title: Multivariate Normal CDF and SUN Density with CppAD Derivatives
Description: Evaluates the cumulative distribution function of the multivariate normal distribution using specialized bivariate/trivariate algorithms for dimension <= 3 and Genz quasi-Monte Carlo for higher dimensions, with gradient and Hessian with respect to the upper integration limits computed via CppAD automatic differentiation. Also provides the SUN(2,2), SUN(3,3) and SUN(4,4) densities with gradient and Hessian with respect to a block-Cholesky parameter vector, Gauss-Hermite utilities, and KL minimisation of SUN approximations on weighted quadrature.
Authors: Patrick Brown [aut, cre, cph]
Maintainer: Patrick Brown <[email protected]>
License: GPL (>= 2)
Version: 0.2.0
Built: 2026-10-16 21:43:15 UTC
Source: https://github.com/eborgnine/adlaplace

Help Index


Multivariate normal CDF and SUN density with CppAD derivatives

Description

Genz quasi-Monte Carlo MVN probabilities with gradient and Hessian, SUN(3,3)/SUN(4,4) log-densities with derivatives with respect to a block-Cholesky parameter vector, and KL minimisation of SUN approximations on weighted Gauss-Hermite quadrature.

Author(s)

Maintainer: Patrick Brown [email protected] [copyright holder]

Authors:


SUN(3,3) density with derivatives

Description

Evaluates the log-density (or density) of a SUN(3,3) distribution using the block-Cholesky parameterization from make_sun_params(). For a matrix of observations the returned value is the summed log-likelihood, or a weighted sum if weights is supplied. Derivatives are with respect to the 21-component parameter vector only; the data are treated as fixed.

Usage

dsun(
  x,
  par,
  log = TRUE,
  deriv = 0L,
  n_points = 1021L,
  n_shifts = 8L,
  seed = 1L,
  n_threads = 1L,
  weights = NULL
)

Arguments

x

Numeric vector of length 3 or an n×3n \times 3 matrix of observations.

par

Numeric vector of length 21; see make_sun_params().

log

If TRUE (default), return the log-density / log-likelihood.

deriv

Integer derivative order: 0 (default) value only, 1 also gradient, 2 also Hessian, all with respect to par.

n_points, n_shifts, seed

Quasi-Monte Carlo settings for the orthant probabilities in the SUN density.

n_threads

Number of OpenMP threads for parallel shard evaluation. Defaults to 1. Use 0 in $eval() to reuse the value stored when the tape was built.

weights

Optional numeric vector of length nrow(x). If supplied, returns ∑iwilog⁡q(xi)\sum_i w_i \log q(x_i) (and matching derivatives) instead of the unweighted sum. Defaults to equal weights of 1.

Value

A list with value and error. If deriv >= 1, also gradient (length 21). If deriv >= 2, also hessian (21×2121 \times 21).

See Also

make_sun_params(), dsun_fun()


Create a reusable SUN(3,3) log-likelihood tape

Description

Builds per-observation CppAD shard tapes for the summed SUN log-likelihood with the data x fixed and the independent variables equal to the 21-parameter vector. Use this for MLE: build once, then call $eval(par) at new parameter values. Pass weights for a weighted sum (e.g. Gauss–Hermite KL objectives).

Usage

dsun_fun(
  x,
  par_seed,
  n_points = 1021L,
  n_shifts = 8L,
  seed = 1L,
  n_threads = NULL,
  weights = NULL
)

Arguments

x

Numeric vector of length 3 or n×3n \times 3 matrix of observations fixed in the tape.

par_seed

Seed parameter vector used when building the tape.

n_points, n_shifts, seed

QMC settings passed to dsun().

n_threads

Default number of OpenMP threads for $eval(). When NULL, uses the maximum available OpenMP thread count.

weights

Optional numeric vector of length nrow(x) for a weighted log-likelihood ∑iwilog⁡q(xi)\sum_i w_i \log q(x_i).

Value

An object of class "admvn_sun_tape" with components eval(par, log = TRUE, deriv = 0, n_threads = 0) and optim_fns() for optim(): fn/gr share one deriv = 1 evaluation cached by parameter value.


SUN(3,3) density with joint hyperspherical parameterization

Description

Like dsun(), but the length-21 parameter vector uses the joint hyperspherical map of make_sun_hs_params() instead of the block-Cholesky layout of make_sun_params().

Usage

dsun_hs(
  x,
  par,
  log = TRUE,
  deriv = 0L,
  n_points = 1021L,
  n_shifts = 8L,
  seed = 1L,
  n_threads = 1L,
  weights = NULL
)

Arguments

x

Numeric vector of length 3 or an n×3n \times 3 matrix of observations.

par

Numeric vector of length 21; see make_sun_hs_params().

log

If TRUE (default), return the log-density / log-likelihood.

deriv

Integer derivative order: 0 (default) value only, 1 also gradient, 2 also Hessian, all with respect to par.

n_points, n_shifts, seed

Quasi-Monte Carlo settings for the orthant probabilities in the SUN density.

n_threads

Number of OpenMP threads for parallel shard evaluation. Defaults to 1. Use 0 in $eval() to reuse the value stored when the tape was built.

weights

Optional numeric vector of length nrow(x). If supplied, returns ∑iwilog⁡q(xi)\sum_i w_i \log q(x_i) (and matching derivatives) instead of the unweighted sum. Defaults to equal weights of 1.

Value

A list with value and error. If deriv >= 1, also gradient. If deriv >= 2, also hessian.

See Also

make_sun_hs_params(), dsun_hs_fun(), dsun()


Create a reusable SUN(3,3) hyperspherical log-likelihood tape

Description

Like dsun_fun(), for the joint hyperspherical parameterization of make_sun_hs_params().

Usage

dsun_hs_fun(
  x,
  par_seed,
  n_points = 1021L,
  n_shifts = 8L,
  seed = 1L,
  n_threads = NULL,
  weights = NULL
)

Arguments

x

Numeric vector of length 3 or n×3n \times 3 matrix of observations fixed in the tape.

par_seed

Seed parameter vector (length 21) for make_sun_hs_params().

n_points, n_shifts, seed

QMC settings passed to dsun().

n_threads

Default number of OpenMP threads for $eval(). When NULL, uses the maximum available OpenMP thread count.

weights

Optional numeric vector of length nrow(x) for a weighted log-likelihood ∑iwilog⁡q(xi)\sum_i w_i \log q(x_i).

Value

An object of class "admvn_sun_tape" with eval() and optim_fns() (same interface as dsun_fun()).

See Also

dsun_hs(), sun_mle()


SUN(2,2) density with derivatives

Description

Evaluates the log-density (or density) of a SUN(2,2) distribution using the block-Cholesky parameterization from make_sun22_params(). For a matrix of observations the returned value is the summed log-likelihood, or a weighted sum if weights is supplied. Derivatives are with respect to the 10-component parameter vector only.

Usage

dsun22(
  x,
  par,
  log = TRUE,
  deriv = 0L,
  n_points = 1021L,
  n_shifts = 8L,
  seed = 1L,
  n_threads = 1L,
  weights = NULL
)

Arguments

x

Numeric vector of length 2 or an n×2n \times 2 matrix of observations.

par

Numeric vector of length 10; see make_sun22_params().

log

If TRUE (default), return the log-density / log-likelihood.

deriv

Integer derivative order: 0 (default) value only, 1 also gradient, 2 also Hessian, all with respect to par.

n_points, n_shifts, seed

Quasi-Monte Carlo settings for orthant CDFs.

n_threads

Number of OpenMP threads for parallel shard evaluation.

weights

Optional numeric vector of length nrow(x).

Value

A list with value and error. If deriv >= 1, also gradient (length 10). If deriv >= 2, also hessian (10×1010 \times 10).

See Also

make_sun22_params(), dsun22_fun(), dsun()


Create a reusable SUN(2,2) log-likelihood tape

Description

Builds per-observation CppAD shard tapes for the (weighted) SUN(2,2) log-likelihood with data x fixed. Use for KL / MLE: build once, then call $eval(par) or $optim_fns().

Usage

dsun22_fun(
  x,
  par_seed,
  n_points = 1021L,
  n_shifts = 8L,
  seed = 1L,
  n_threads = NULL,
  weights = NULL
)

Arguments

x

Numeric vector of length 2 or an n×2n \times 2 matrix of observations.

par_seed

Seed parameter vector used when building the tape.

n_points, n_shifts, seed

Quasi-Monte Carlo settings for orthant CDFs.

n_threads

Default OpenMP threads for $eval(). When NULL, uses the maximum available OpenMP thread count.

weights

Optional numeric vector of length nrow(x).

Value

An object of class "admvn_sun22_tape" with eval() and optim_fns() (cached fn/gr).


SUN(2,2) density with joint hyperspherical parameterization

Description

SUN(2,2) density with joint hyperspherical parameterization

Usage

dsun22_hs(
  x,
  par,
  log = TRUE,
  deriv = 0L,
  n_points = 1021L,
  n_shifts = 8L,
  seed = 1L,
  n_threads = 1L,
  weights = NULL
)

Arguments

x

Numeric vector of length 2 or an n×2n \times 2 matrix of observations.

par

Numeric vector of length 10; see make_sun22_hs_params().

log

If TRUE (default), return the log-density / log-likelihood.

deriv

Integer derivative order: 0 (default) value only, 1 also gradient, 2 also Hessian, all with respect to par.

n_points, n_shifts, seed

Quasi-Monte Carlo settings for orthant CDFs.

n_threads

Number of OpenMP threads for parallel shard evaluation.

weights

Optional numeric vector of length nrow(x).


Create a reusable SUN(2,2) hyperspherical likelihood tape

Description

Create a reusable SUN(2,2) hyperspherical likelihood tape

Usage

dsun22_hs_fun(
  x,
  par_seed,
  n_points = 1021L,
  n_shifts = 8L,
  seed = 1L,
  n_threads = NULL,
  weights = NULL
)

Arguments

x

Numeric vector of length 2 or an n×2n \times 2 matrix of observations.

par_seed

Seed vector for make_sun22_hs_params().

n_points, n_shifts, seed

Quasi-Monte Carlo settings for orthant CDFs.

n_threads

Default OpenMP threads for $eval(). When NULL, uses the maximum available OpenMP thread count.

weights

Optional numeric vector of length nrow(x).


SUN(3,2) density with joint hyperspherical parameterization

Description

SUN(3,2) density with joint hyperspherical parameterization

Usage

dsun32_hs(
  x,
  par,
  log = TRUE,
  deriv = 0L,
  n_points = 1021L,
  n_shifts = 8L,
  seed = 1L,
  n_threads = 1L,
  weights = NULL
)

Arguments

x

Numeric matrix with 3 columns (observations in rows).

par

Numeric vector of length 16; see make_sun32_hs_params().

log

Logical; return log-density / log-likelihood if TRUE.

deriv

Integer 0, 1, or 2 for value / gradient / Hessian.

n_points, n_shifts

Orthant QMC settings.

seed

QMC seed.

n_threads

OpenMP threads.

weights

Optional observation weights (length nrow(x)).


Create a reusable SUN(3,2) hyperspherical likelihood tape

Description

Create a reusable SUN(3,2) hyperspherical likelihood tape

Usage

dsun32_hs_fun(
  x,
  par_seed,
  n_points = 1021L,
  n_shifts = 8L,
  seed = 1L,
  n_threads = NULL,
  weights = NULL
)

Arguments

x

Numeric matrix with 3 columns.

par_seed

Seed vector for make_sun32_hs_params().

n_points, n_shifts

Orthant QMC settings.

seed

QMC seed.

n_threads

OpenMP threads (default from package helper).

weights

Optional observation weights.


SUN(4,2) density with joint hyperspherical parameterization

Description

SUN(4,2) density with joint hyperspherical parameterization

Usage

dsun42_hs(
  x,
  par,
  log = TRUE,
  deriv = 0L,
  n_points = 1021L,
  n_shifts = 8L,
  seed = 1L,
  n_threads = 1L,
  weights = NULL
)

Arguments

x

Numeric matrix with 4 columns (observations in rows).

par

Numeric vector of length 23; see make_sun42_hs_params().

log

Logical; return log-density / log-likelihood if TRUE.

deriv

Integer 0, 1, or 2 for value / gradient / Hessian.

n_points, n_shifts

Orthant QMC settings.

seed

QMC seed.

n_threads

OpenMP threads.

weights

Optional observation weights (length nrow(x)).


Create a reusable SUN(4,2) hyperspherical likelihood tape

Description

Create a reusable SUN(4,2) hyperspherical likelihood tape

Usage

dsun42_hs_fun(
  x,
  par_seed,
  n_points = 1021L,
  n_shifts = 8L,
  seed = 1L,
  n_threads = NULL,
  weights = NULL
)

Arguments

x

Numeric matrix with 4 columns.

par_seed

Seed vector for make_sun42_hs_params().

n_points, n_shifts

Orthant QMC settings.

seed

QMC seed.

n_threads

OpenMP threads (default from package helper).

weights

Optional observation weights.


SUN(4,3) density with joint hyperspherical parameterization

Description

SUN(4,3) density with joint hyperspherical parameterization

Usage

dsun43_hs(
  x,
  par,
  log = TRUE,
  deriv = 0L,
  n_points = 1021L,
  n_shifts = 8L,
  seed = 1L,
  n_threads = 1L,
  weights = NULL
)

Arguments

x

Numeric matrix with 4 columns (observations in rows).

par

Numeric vector of length 29; see make_sun43_hs_params().

log

Logical; return log-density / log-likelihood if TRUE.

deriv

Integer 0, 1, or 2 for value / gradient / Hessian.

n_points, n_shifts

Orthant QMC settings.

seed

QMC seed.

n_threads

OpenMP threads.

weights

Optional observation weights (length nrow(x)).


Create a reusable SUN(4,3) hyperspherical likelihood tape

Description

Create a reusable SUN(4,3) hyperspherical likelihood tape

Usage

dsun43_hs_fun(
  x,
  par_seed,
  n_points = 1021L,
  n_shifts = 8L,
  seed = 1L,
  n_threads = NULL,
  weights = NULL
)

Arguments

x

Numeric matrix with 4 columns.

par_seed

Seed vector for make_sun43_hs_params().

n_points, n_shifts

Orthant QMC settings.

seed

QMC seed.

n_threads

OpenMP threads (default from package helper).

weights

Optional observation weights.


SUN(4,4) density with derivatives

Description

Evaluates the log-density (or density) of a SUN(4,4) distribution using the block-Cholesky parameterization from make_sun44_params(). For a matrix of observations the returned value is the summed log-likelihood, or a weighted sum if weights is supplied. Derivatives are with respect to the 36-component parameter vector only.

Usage

dsun44(
  x,
  par,
  log = TRUE,
  deriv = 0L,
  n_points = 1021L,
  n_shifts = 8L,
  seed = 1L,
  n_threads = 1L,
  weights = NULL
)

Arguments

x

Numeric vector of length 4 or an n×4n \times 4 matrix of observations.

par

Numeric vector of length 36; see make_sun44_params().

log

If TRUE (default), return the log-density / log-likelihood.

deriv

Integer derivative order: 0 (default) value only, 1 also gradient, 2 also Hessian, all with respect to par.

n_points, n_shifts, seed

Quasi-Monte Carlo settings for orthant CDFs.

n_threads

Number of OpenMP threads for parallel shard evaluation.

weights

Optional numeric vector of length nrow(x).

Details

Orthant probabilities use Genz QMC with CppAD (dimension 4), so prefer a modest n_points/n_shifts budget for speed.

Value

A list with value and error. If deriv >= 1, also gradient (length 36). If deriv >= 2, also hessian (36×3636 \times 36).

See Also

make_sun44_params(), dsun44_fun(), dsun()


Create a reusable SUN(4,4) log-likelihood tape

Description

Builds per-observation CppAD shard tapes for the (weighted) SUN(4,4) log-likelihood with data x fixed. Use for KL / MLE: build once, then call $eval(par) or $optim_fns().

Usage

dsun44_fun(
  x,
  par_seed,
  n_points = 1021L,
  n_shifts = 8L,
  seed = 1L,
  n_threads = NULL,
  weights = NULL
)

Arguments

x

Numeric vector of length 4 or an n×4n \times 4 matrix of observations.

par_seed

Seed parameter vector used when building the tape.

n_points, n_shifts, seed

Quasi-Monte Carlo settings for orthant CDFs.

n_threads

Default OpenMP threads for $eval(). When NULL, uses the maximum available OpenMP thread count.

weights

Optional numeric vector of length nrow(x).

Value

An object of class "admvn_sun44_tape" with eval() and optim_fns() (cached fn/gr).


SUN(4,4) density with joint hyperspherical parameterization

Description

SUN(4,4) density with joint hyperspherical parameterization

Usage

dsun44_hs(
  x,
  par,
  log = TRUE,
  deriv = 0L,
  n_points = 1021L,
  n_shifts = 8L,
  seed = 1L,
  n_threads = 1L,
  weights = NULL
)

Arguments

x

Numeric vector of length 4 or an n×4n \times 4 matrix of observations.

par

Numeric vector of length 36; see make_sun44_hs_params().

log

If TRUE (default), return the log-density / log-likelihood.

deriv

Integer derivative order: 0 (default) value only, 1 also gradient, 2 also Hessian, all with respect to par.

n_points, n_shifts, seed

Quasi-Monte Carlo settings for orthant CDFs.

n_threads

Number of OpenMP threads for parallel shard evaluation.

weights

Optional numeric vector of length nrow(x).


Create a reusable SUN(4,4) hyperspherical likelihood tape

Description

Create a reusable SUN(4,4) hyperspherical likelihood tape

Usage

dsun44_hs_fun(
  x,
  par_seed,
  n_points = 1021L,
  n_shifts = 8L,
  seed = 1L,
  n_threads = NULL,
  weights = NULL
)

Arguments

x

Numeric vector of length 4 or an n×4n \times 4 matrix of observations.

par_seed

Seed vector for make_sun44_hs_params().

n_points, n_shifts, seed

Quasi-Monte Carlo settings for orthant CDFs.

n_threads

Default OpenMP threads for $eval(). When NULL, uses the maximum available OpenMP thread count.

weights

Optional numeric vector of length nrow(x).


SUN(5,2) density with joint hyperspherical parameterization

Description

SUN(5,2) density with joint hyperspherical parameterization

Usage

dsun52_hs(
  x,
  par,
  log = TRUE,
  deriv = 0L,
  n_points = 1021L,
  n_shifts = 8L,
  seed = 1L,
  n_threads = 1L,
  weights = NULL
)

Arguments

x

Numeric matrix with 5 columns (observations in rows).

par

Numeric vector of length 31; see make_sun52_hs_params().

log

Logical; return log-density / log-likelihood if TRUE.

deriv

Integer 0, 1, or 2 for value / gradient / Hessian.

n_points, n_shifts

Orthant QMC settings.

seed

QMC seed.

n_threads

OpenMP threads.

weights

Optional observation weights (length nrow(x)).


Create a reusable SUN(5,2) hyperspherical likelihood tape

Description

Create a reusable SUN(5,2) hyperspherical likelihood tape

Usage

dsun52_hs_fun(
  x,
  par_seed,
  n_points = 1021L,
  n_shifts = 8L,
  seed = 1L,
  n_threads = NULL,
  weights = NULL
)

Arguments

x

Numeric matrix with 5 columns.

par_seed

Seed vector for make_sun52_hs_params().

n_points, n_shifts

Orthant QMC settings.

seed

QMC seed.

n_threads

OpenMP threads (default from package helper).

weights

Optional observation weights.


Slice KL / cross-entropy of a SUN(2,2) vs a weighted target

Description

Uses dsun22_fun() (not sn) so the score matches the AD optim objective.

Usage

evaluate_sun22_slice(par, quad, n_points = 64L, n_shifts = 2L, n_threads = 1L)

Arguments

par

Length-10 SUN(2,2) parameter vector.

quad

List with x, w, and optionally logp (target log-density at each row of x).

n_points, n_shifts

Orthant QMC settings (defaults 64, 2).

n_threads

OpenMP threads for the density tape.

Value

Named vector c(kl, neg_Elq). kl is NA if logp is missing.


Slice KL / cross-entropy of a SUN(3,2) hyperspherical fit

Description

Slice KL / cross-entropy of a SUN(3,2) hyperspherical fit

Usage

evaluate_sun32_hs_slice(
  par,
  quad,
  n_points = 64L,
  n_shifts = 2L,
  n_threads = 1L
)

Arguments

par

Length-21 hyperspherical parameter vector (see make_sun_hs_params()).

quad

List with x, w, and optionally logp (target log-density at each row of x).

n_points, n_shifts

Orthant QMC settings (defaults 64, 2).

n_threads

OpenMP threads for the density tape.


Slice KL / cross-entropy of a SUN(3,3) hyperspherical vs a weighted target

Description

Like evaluate_sun33_slice(), but uses dsun_hs_fun() and make_sun_hs_params().

Usage

evaluate_sun33_hs_slice(
  par,
  quad,
  n_points = 64L,
  n_shifts = 2L,
  n_threads = 1L
)

Arguments

par

Length-21 hyperspherical parameter vector (see make_sun_hs_params()).

quad

List with x, w, and optionally logp (target log-density at each row of x).

n_points, n_shifts

Orthant QMC settings (defaults 64, 2).

n_threads

OpenMP threads for the density tape.


Slice KL / cross-entropy of a SUN(3,3) vs a weighted target

Description

Uses dsun_fun() (not sn) so the score matches the AD optim objective.

Usage

evaluate_sun33_slice(par, quad, n_points = 64L, n_shifts = 2L, n_threads = 1L)

Arguments

par

Length-21 SUN(3,3) parameter vector.

quad

List with x, w, and optionally logp (target log-density at each row of x).

n_points, n_shifts

Orthant QMC settings (defaults 64, 2).

n_threads

OpenMP threads for the density tape.

Value

Named vector c(kl, neg_Elq). kl is NA if logp is missing.


Slice KL / cross-entropy of a SUN(4,2) hyperspherical fit

Description

Slice KL / cross-entropy of a SUN(4,2) hyperspherical fit

Usage

evaluate_sun42_hs_slice(
  par,
  quad,
  n_points = 64L,
  n_shifts = 2L,
  n_threads = 1L
)

Arguments

par

Length-21 hyperspherical parameter vector (see make_sun_hs_params()).

quad

List with x, w, and optionally logp (target log-density at each row of x).

n_points, n_shifts

Orthant QMC settings (defaults 64, 2).

n_threads

OpenMP threads for the density tape.


Slice KL / cross-entropy of a SUN(4,3) hyperspherical fit

Description

Slice KL / cross-entropy of a SUN(4,3) hyperspherical fit

Usage

evaluate_sun43_hs_slice(
  par,
  quad,
  n_points = 64L,
  n_shifts = 2L,
  n_threads = 1L
)

Arguments

par

Length-21 hyperspherical parameter vector (see make_sun_hs_params()).

quad

List with x, w, and optionally logp (target log-density at each row of x).

n_points, n_shifts

Orthant QMC settings (defaults 64, 2).

n_threads

OpenMP threads for the density tape.


Slice KL / cross-entropy of a SUN(4,4) vs a weighted target

Description

Uses dsun44_fun() (not sn).

Usage

evaluate_sun44_slice(par, quad, n_points = 256L, n_shifts = 4L, n_threads = 1L)

Arguments

par

Length-36 SUN(4,4) parameter vector.

quad

List with x, w, and optionally logp (target log-density at each row of x).

n_points, n_shifts

Defaults 256, 4.

n_threads

OpenMP threads for the density tape.


Slice KL / cross-entropy of a SUN(5,2) hyperspherical fit

Description

Slice KL / cross-entropy of a SUN(5,2) hyperspherical fit

Usage

evaluate_sun52_hs_slice(
  par,
  quad,
  n_points = 64L,
  n_shifts = 2L,
  n_threads = 1L
)

Arguments

par

Length-21 hyperspherical parameter vector (see make_sun_hs_params()).

quad

List with x, w, and optionally logp (target log-density at each row of x).

n_points, n_shifts

Orthant QMC settings (defaults 64, 2).

n_threads

OpenMP threads for the density tape.


Fit SUN(2,2) by minimising KL on a weighted quadrature

Description

Minimises the weighted cross-entropy −∑iwilog⁡q(xi)-\sum_i w_i \log q(x_i) with analytic gradients from dsun22_fun().

Usage

fit_sun22_quad(
  quad,
  start,
  lower = NULL,
  upper = NULL,
  obj_scale = 10000,
  mc.cores = 1L,
  verbose = FALSE,
  mvquad_opts = list(),
  optim_opts = list()
)

Arguments

quad

List with x (n×2n\times 2), w, and optionally logp (for returned KL).

start

Length-10 start (see make_sun22_start_from_normal()).

lower, upper

Box constraints (defaults from sun22_bounds()).

obj_scale

Scale applied to the optim objective (default 1e4).

mc.cores

OpenMP threads for the density tape.

verbose

If TRUE, set optim trace = 1.

mvquad_opts

Named list; used keys n_points, n_shifts for orthant QMC (defaults 64, 2).

optim_opts

Named list merged into stats::optim L-BFGS-B control.

Value

A list with fit, par, dp, start, gradient (objective-scale at par), KL / cross-entropy summaries, and engine.


Fit SUN(3,2) hyperspherical by minimising KL on a weighted quadrature

Description

Fit SUN(3,2) hyperspherical by minimising KL on a weighted quadrature

Usage

fit_sun32_hs_quad(
  quad,
  start,
  lower = NULL,
  upper = NULL,
  obj_scale = 10000,
  mc.cores = 1L,
  verbose = FALSE,
  mvquad_opts = list(),
  optim_opts = list()
)

Arguments

quad

List with x (n×3n\times 3), w, and optionally logp (for returned KL).

start

Length-16 hyperspherical start (see make_sun32_hs_start_from_normal()).

lower, upper

Box constraints (defaults from sun32_hs_bounds()).

obj_scale

Scale applied to the optim objective (default 1e4).

mc.cores

OpenMP threads for the density tape.

verbose

If TRUE, set optim trace = 1.

mvquad_opts

Named list; used keys n_points, n_shifts for orthant QMC (defaults 64, 2).

optim_opts

Named list merged into stats::optim L-BFGS-B control.


Fit SUN(3,3) hyperspherical by minimising KL on a weighted quadrature

Description

Like fit_sun33_quad(), but uses dsun_hs_fun() and defaults from sun33_hs_bounds() / make_sun33_hs_start_from_normal().

Usage

fit_sun33_hs_quad(
  quad,
  start,
  lower = NULL,
  upper = NULL,
  obj_scale = 10000,
  mc.cores = 1L,
  verbose = FALSE,
  mvquad_opts = list(),
  optim_opts = list()
)

Arguments

quad

List with x (n×3n\times 3), w, and optionally logp (for returned KL).

start

Length-21 hyperspherical start (see make_sun33_hs_start_from_normal()).

lower, upper

Box constraints (defaults from sun33_hs_bounds()).

obj_scale

Scale applied to the optim objective (default 1e4).

mc.cores

OpenMP threads for the density tape.

verbose

If TRUE, set optim trace = 1.

mvquad_opts

Named list; used keys n_points, n_shifts for orthant QMC (defaults 64, 2).

optim_opts

Named list merged into stats::optim L-BFGS-B control.


Fit SUN(3,3) by minimising KL on a weighted quadrature

Description

Minimises the weighted cross-entropy −∑iwilog⁡q(xi)-\sum_i w_i \log q(x_i) with analytic gradients from dsun_fun().

Usage

fit_sun33_quad(
  quad,
  start,
  lower = NULL,
  upper = NULL,
  obj_scale = 10000,
  mc.cores = 1L,
  verbose = FALSE,
  mvquad_opts = list(),
  optim_opts = list()
)

Arguments

quad

List with x (n×3n\times 3), w, and optionally logp (for returned KL).

start

Length-21 start (see make_sun33_start_from_normal()).

lower, upper

Box constraints (defaults from sun33_bounds()).

obj_scale

Scale applied to the optim objective (default 1e4).

mc.cores

OpenMP threads for the density tape.

verbose

If TRUE, set optim trace = 1.

mvquad_opts

Named list; used keys n_points, n_shifts for orthant QMC (defaults 64, 2).

optim_opts

Named list merged into stats::optim L-BFGS-B control.

Value

A list with fit, par, dp, start, gradient (objective-scale at par), KL / cross-entropy summaries, and engine.


Fit SUN(4,2) hyperspherical by minimising KL on a weighted quadrature

Description

Fit SUN(4,2) hyperspherical by minimising KL on a weighted quadrature

Usage

fit_sun42_hs_quad(
  quad,
  start,
  lower = NULL,
  upper = NULL,
  obj_scale = 10000,
  mc.cores = 1L,
  verbose = FALSE,
  mvquad_opts = list(),
  optim_opts = list()
)

Arguments

quad

List with x (n×3n\times 3), w, and optionally logp (for returned KL).

start

Length-23 hyperspherical start (see make_sun42_hs_start_from_normal()).

lower, upper

Box constraints (defaults from sun42_hs_bounds()).

obj_scale

Scale applied to the optim objective (default 1e4).

mc.cores

OpenMP threads for the density tape.

verbose

If TRUE, set optim trace = 1.

mvquad_opts

Named list; used keys n_points, n_shifts for orthant QMC (defaults 64, 2).

optim_opts

Named list merged into stats::optim L-BFGS-B control.


Fit SUN(4,3) hyperspherical by minimising KL on a weighted quadrature

Description

Fit SUN(4,3) hyperspherical by minimising KL on a weighted quadrature

Usage

fit_sun43_hs_quad(
  quad,
  start,
  lower = NULL,
  upper = NULL,
  obj_scale = 10000,
  mc.cores = 1L,
  verbose = FALSE,
  mvquad_opts = list(),
  optim_opts = list()
)

Arguments

quad

List with x (n×3n\times 3), w, and optionally logp (for returned KL).

start

Length-29 hyperspherical start (see make_sun43_hs_start_from_normal()).

lower, upper

Box constraints (defaults from sun43_hs_bounds()).

obj_scale

Scale applied to the optim objective (default 1e4).

mc.cores

OpenMP threads for the density tape.

verbose

If TRUE, set optim trace = 1.

mvquad_opts

Named list; used keys n_points, n_shifts for orthant QMC (defaults 64, 2).

optim_opts

Named list merged into stats::optim L-BFGS-B control.


Fit SUN(4,4) by minimising KL on a weighted quadrature

Description

Like fit_sun33_quad() for SUN(4,4) via dsun44_fun(). Default QMC budget is n_points = 256, n_shifts = 4.

Usage

fit_sun44_quad(
  quad,
  start,
  lower = NULL,
  upper = NULL,
  obj_scale = 10000,
  mc.cores = 1L,
  verbose = FALSE,
  mvquad_opts = list(),
  optim_opts = list()
)

Arguments

quad

List with x (n×3n\times 3), w, and optionally logp (for returned KL).

start

Length-36 start (see make_sun44_start_from_normal()).

lower, upper

Box constraints (defaults from sun33_bounds()).

obj_scale

Scale applied to the optim objective (default 1e4).

mc.cores

OpenMP threads for the density tape.

verbose

If TRUE, set optim trace = 1.

mvquad_opts

Named list; used keys n_points, n_shifts for orthant QMC (defaults 64, 2).

optim_opts

Named list merged into stats::optim L-BFGS-B control.


Fit SUN(5,2) hyperspherical by minimising KL on a weighted quadrature

Description

Fit SUN(5,2) hyperspherical by minimising KL on a weighted quadrature

Usage

fit_sun52_hs_quad(
  quad,
  start,
  lower = NULL,
  upper = NULL,
  obj_scale = 10000,
  mc.cores = 1L,
  verbose = FALSE,
  mvquad_opts = list(),
  optim_opts = list()
)

Arguments

quad

List with x (n×5n\times 5), w, and optionally logp (for returned KL).

start

Length-31 hyperspherical start (see make_sun52_hs_start_from_normal()).

lower, upper

Box constraints (defaults from sun52_hs_bounds()).

obj_scale

Scale applied to the optim objective (default 1e4).

mc.cores

OpenMP threads for the density tape.

verbose

If TRUE, set optim trace = 1.

mvquad_opts

Named list; used keys n_points, n_shifts for orthant QMC (defaults 64, 2).

optim_opts

Named list merged into stats::optim L-BFGS-B control.


Gauss-Hermite nodes and weights under a multivariate normal

Description

Product Gauss-Hermite quadrature for N(μ,Σ)N(\mu,\Sigma), returned as abscissae in the original scale and corresponding product weights.

Usage

gh_points(mu, Sigma, n = 15)

Arguments

mu

Mean vector.

Sigma

Covariance matrix (made symmetric; Cholesky used for the map).

n

Number of univariate GH nodes per dimension (default 15).

Value

A list with x (nd×dn^d \times d matrix) and w (length ndn^d weights).


Build SUN(3,3) dp from a joint hyperspherical parameter vector

Description

Alternative to make_sun_params(): the latent joint correlation of (U⋆,V)∈R6(U_\star, V)\in\mathbb{R}^6 is J=BB⊤J = B B^\top with unit-row Cholesky factor BB from fifteen unconstrained coordinates zijz_{ij} via tij=tanh⁡(zij)t_{ij}=\tanh(z_{ij}). Then Cu=JUUC_u=J_{UU}, Δ=JUV\Delta=J_{UV}, Γ=JVV\Gamma=J_{VV}, and Ω=DuCuDu\Omega=D_u C_u D_u with Du=diag⁡(ν)D_u=\operatorname{diag}(\nu).

Usage

make_sun_hs_params(par)

Arguments

par

Numeric vector of length 21: xi(3), nu(3), then free z in row-major fill order z21,z31,z32,z41,...,z65. nu are marginal SDs of Ω\Omega (Ωjj=νj2\Omega_{jj}=\nu_j^2).

Details

The leading six parameters match the block-Cholesky layout (xi, nu), so the C++ sun_mle() driver works unchanged on hyperspherical tapes.

Independent skew-normals. Only the pair slots z4,1,z5,2,z6,3z_{4,1},z_{5,2},z_{6,3} nonzero (equal to atanh⁡(di)\operatorname{atanh}(d_i)) yields J=[IDDI]J=\begin{bmatrix}I&D\\D&I\end{bmatrix} with D=diag⁡(d)D=\operatorname{diag}(d). All z=0z=0 recovers a Gaussian.

Fill order. For dimension d=6d=6, free entries are filled by rows i=2,…,6i=2,\ldots,6, columns j=1,…,i−1j=1,\ldots,i-1.

Value

A list with components xi, Omega, Delta, tau (fixed at zero), and Gamma.

See Also

make_sun_params(), dsun_hs(), sun33_hs_bounds()

Examples

par <- c(
  xi1 = 0, xi2 = 0, xi3 = 0,
  nu1 = 1, nu2 = 1, nu3 = 1,
  z21 = 0, z31 = 0, z32 = 0,
  z41 = atanh(0.4), z42 = 0, z43 = 0,
  z51 = 0, z52 = atanh(0.3), z53 = 0, z54 = 0,
  z61 = 0, z62 = 0, z63 = atanh(0.2), z64 = 0, z65 = 0
)
dp <- make_sun_hs_params(par)
diag(dp$Gamma)

Build SUN distribution parameters from a free parameter vector

Description

Converts the SUN(3,3) block-Cholesky parameterization used in the supervision notes into the dp list expected by the sn package (xi, Omega, Delta, tau, Gamma).

Usage

make_sun_params(par)

Arguments

par

Numeric vector of length 21: c(xi1, xi2, xi3, nu1, nu2, nu3, omega12, omega13, omega23, L11, L12, L13, L21, L22, L23, L31, L32, L33, gamma12, gamma13, gamma23). Here nu are marginal standard deviations of Ω\Omega (Ωjj=νj2\Omega_{jj}=\nu_j^2), and omega* are unconstrained hyperspherical coordinates for the correlation CuC_u of the standardized latent. Skew loadings LL act on that correlation scale (all nine entries free; diagonals typically in [−1,1][-1,1], off-diagonals in [−0.5,0.5][-0.5,0.5]). gamma* parameterize the residual correlation of VV. Valid parameters need diag⁡(LCuL⊤)<1\operatorname{diag}(L C_u L^\top) < 1.

Details

Correlation-scale SUN, then scale. With correlation CuC_u from the unit-row Cholesky of (ω12,ω13,ω23)(\omega_{12},\omega_{13},\omega_{23}) and marginal standard deviations νj>0\nu_j>0, writing Du=diag⁡(ν)D_u=\operatorname{diag}(\nu),

Ω=DuCuDu.\Omega = D_u C_u D_u.

Skew loadings LL multiply CuC_u (not Ω\Omega):

M=LCuL⊤,Σuv=DuCuL⊤.M = L C_u L^\top,\qquad \Sigma_{uv} = D_u C_u L^\top.

Free (γ12,γ13,γ23)(\gamma_{12},\gamma_{13},\gamma_{23}) build residual correlation CC. With D=diag⁡(1−diag⁡(M))D = \operatorname{diag}(1 - \operatorname{diag}(M)),

R=D1/2CD1/2,R = D^{1/2} C D^{1/2},

so Σvv=M+R\Sigma_{vv} = M + R is a correlation matrix. Then Γ=Σvv\Gamma = \Sigma_{vv} and Δ=Du−1Σuv=CuL⊤\Delta = D_u^{-1}\Sigma_{uv} = C_u L^\top.

Skew loadings. All nine entries of LL are free:

L=[L11L12L13L21L22L23L31L32L33].L = \begin{bmatrix} L_{11} & L_{12} & L_{13} \\ L_{21} & L_{22} & L_{23} \\ L_{31} & L_{32} & L_{33} \end{bmatrix}.

Value

A list with components xi, Omega, Delta, tau (fixed at zero), and Gamma.

Examples

par <- c(
  xi1 = 0, xi2 = 0, xi3 = 0,
  nu1 = 1, nu2 = 1, nu3 = 1,
  omega12 = 0.2, omega13 = 0.1, omega23 = 0.2,
  L11 = 0.4, L12 = 0.1, L13 = 0.1,
  L21 = 0.1, L22 = 0.4, L23 = 0.1,
  L31 = 0, L32 = 0.1, L33 = 0.35,
  gamma12 = 0.3, gamma13 = 0.2, gamma23 = 0.3
)
make_sun_params(par)

Build SUN(2,2) parameters from a joint hyperspherical vector

Description

The joint correlation of (U,V)(U,V) is formed from six unconstrained unit-row Cholesky coordinates in row-major order.

Usage

make_sun22_hs_params(par)

Arguments

par

Numeric vector of length 10: xi(2), nu(2), then z21,z31,z32,z41,z42,z43.

Value

A SUN dp list with xi, Omega, Delta, zero tau, and Gamma.

See Also

dsun22_hs(), make_sun22_params(), sun22_hs_bounds()


SUN(2,2) hyperspherical start from weighted sample moments

Description

SUN(2,2) hyperspherical start from weighted sample moments

Usage

make_sun22_hs_start_from_gh(quad, skew_strength = 0.01)

Arguments

quad

List with x and w.

skew_strength

Passed to make_sun22_start_from_normal().

Value

Named length-10 parameter vector.


Map mean and covariance to a SUN(2,2) hyperspherical start

Description

Map mean and covariance to a SUN(2,2) hyperspherical start

Usage

make_sun22_hs_start_from_normal(mu, Sigma, skew_strength = 0)

Arguments

mu

Length-2 mean.

Sigma

2×22\times 2 covariance.

skew_strength

Diagonal loading for L* (default 0).

Value

Named length-10 vector for make_sun22_hs_params().


Build SUN(2,2) distribution parameters from a free parameter vector

Description

Converts the SUN(2,2) block-Cholesky parameterization into the dp list expected by the sn package (xi, Omega, Delta, tau, Gamma).

Usage

make_sun22_params(par)

Arguments

par

Numeric vector of length 10: xi(2), nu(2), omega12(1), L11, L12, L21, L22, gamma12. nu are marginal standard deviations of Ω\Omega (Ωjj=νj2\Omega_{jj}=\nu_j^2); omega12 free hyperspherical coordinate for correlation CuC_u; all four entries of LL are free on the correlation scale; gamma12 parameterizes the residual correlation of VV. Valid parameters need diag⁡(LCuL⊤)<1\operatorname{diag}(L C_u L^\top) < 1.

Details

As in make_sun_params(): Ω=DuCuDu\Omega = D_u C_u D_u with Du=diag⁡(ν)D_u=\operatorname{diag}(\nu), M=LCuL⊤M = L C_u L^\top, Σuv=DuCuL⊤\Sigma_{uv} = D_u C_u L^\top, and Σvv=M+D1/2CD1/2\Sigma_{vv} = M + D^{1/2} C D^{1/2} with unit diagonal.

Value

A list with components xi, Omega, Delta, tau (fixed at zero), and Gamma.

See Also

make_sun_params(), dsun22(), dsun22_fun()


SUN(2,2) start from weighted sample moments on a quadrature

Description

SUN(2,2) start from weighted sample moments on a quadrature

Usage

make_sun22_start_from_gh(quad, skew_strength = 0.01)

Arguments

quad

List with x and w.

skew_strength

Passed to make_sun22_start_from_normal().

Value

Named length-10 parameter vector.


Map mean and covariance to a SUN(2,2) start (zero / tiny skewness)

Description

Uses marginal standard deviations and unconstrained correlation free parameters for (nu, omega). Skew loadings and residual-correlation free parameters are zero, or a tiny diagonal loading if skew_strength > 0.

Usage

make_sun22_start_from_normal(mu, Sigma, skew_strength = 0)

Arguments

mu

Length-2 mean.

Sigma

2×22\times 2 covariance.

skew_strength

Diagonal loading for L* (default 0).

Value

Named length-10 parameter vector for make_sun22_params().


Build SUN(3,2) parameters from a joint hyperspherical vector

Description

The joint correlation of (U,V)∈R5(U,V)\in\mathbb{R}^5 is formed from ten unconstrained unit-row Cholesky coordinates in row-major order.

Usage

make_sun32_hs_params(par)

Arguments

par

Numeric vector of length 16: xi(3), nu(3), then z21,z31,z32,z41,z42,z43,z51,z52,z53,z54.

Value

A SUN dp list with xi, Omega (3×33\times 3), Delta (3×23\times 2), zero tau (length 2), and Gamma (2×22\times 2).

See Also

dsun32_hs(), sun32_hs_bounds()


SUN(3,2) hyperspherical start from weighted sample moments

Description

SUN(3,2) hyperspherical start from weighted sample moments

Usage

make_sun32_hs_start_from_gh(quad, skew_strength = 0.01)

Arguments

quad

List with x and w.

skew_strength

Passed to make_sun32_hs_start_from_normal().

Value

Named length-16 parameter vector.


Map mean and covariance to a SUN(3,2) hyperspherical start

Description

Exact Gaussian start on the joint hyperspherical layout of make_sun32_hs_params(): the three UU-block free coordinates come from inverting cov2cor(Sigma); all cross / VV coordinates are zero. Optional skew_strength sets pair slots z41,z52 to atanh(skew_strength).

Usage

make_sun32_hs_start_from_normal(mu, Sigma, skew_strength = 0)

Arguments

mu

Length-3 mean.

Sigma

3×33\times 3 covariance.

skew_strength

Pair-slot t=tanh⁡(z)t=\tanh(z) value in (−1,1)(-1,1) (default 0).

Value

Named length-16 parameter vector for make_sun32_hs_params().


SUN(3,3) hyperspherical start from weighted sample moments

Description

SUN(3,3) hyperspherical start from weighted sample moments

Usage

make_sun33_hs_start_from_gh(quad, skew_strength = 0.01)

Arguments

quad

List with x and w.

skew_strength

Passed to make_sun33_hs_start_from_normal().

Value

Named length-21 parameter vector.


Map mean and covariance to a SUN(3,3) hyperspherical start

Description

Exact Gaussian start on the joint hyperspherical layout of make_sun_hs_params(): the three UU-block free coordinates come from inverting cov2cor(Sigma); all cross / VV coordinates are zero (so J=blockdiag⁡(Cu,I)J=\operatorname{blockdiag}(C_u,I)). Optional skew_strength sets the independent-SN pair slots to atanh(skew_strength).

Usage

make_sun33_hs_start_from_normal(mu, Sigma, skew_strength = 0)

Arguments

mu

Length-3 mean.

Sigma

3×33\times 3 covariance.

skew_strength

Pair-slot t=tanh⁡(z)t=\tanh(z) value in (−1,1)(-1,1) (default 0).

Value

Named length-21 parameter vector for make_sun_hs_params().


SUN(3,3) start from weighted sample moments on a quadrature

Description

SUN(3,3) start from weighted sample moments on a quadrature

Usage

make_sun33_start_from_gh(quad, skew_strength = 0.01)

Arguments

quad

List with x and w.

skew_strength

Passed to make_sun33_start_from_normal().

Value

Named length-21 parameter vector.


Map mean and covariance to a SUN(3,3) start (zero / tiny skewness)

Description

Uses marginal standard deviations and unconstrained correlation free parameters for (nu, omega). Skew loadings and residual-correlation free parameters are zero, or a tiny diagonal loading if skew_strength > 0.

Usage

make_sun33_start_from_normal(mu, Sigma, skew_strength = 0)

Arguments

mu

Length-3 mean.

Sigma

3×33\times 3 covariance.

skew_strength

Diagonal loading for L* (default 0).

Value

Named length-21 parameter vector for make_sun_params().


Build SUN(4,2) parameters from a joint hyperspherical vector

Description

The joint correlation of (U,V)∈R6(U,V)\in\mathbb{R}^6 is formed from 15 unconstrained unit-row Cholesky coordinates in row-major order.

Usage

make_sun42_hs_params(par)

Arguments

par

Numeric vector of length 23: xi(4), nu(4), then 15 z coordinates named by .sun_hs_z_names(4, 2).

Value

A SUN dp list with xi, Omega (4×44\times 4), Delta (4×24\times 2), zero tau (length 2), and Gamma (2×22\times 2).

See Also

dsun42_hs(), sun42_hs_bounds()


SUN(4,2) hyperspherical start from weighted sample moments

Description

SUN(4,2) hyperspherical start from weighted sample moments

Usage

make_sun42_hs_start_from_gh(quad, skew_strength = 0.01)

Arguments

quad

List with x and w.

skew_strength

Passed to make_sun42_hs_start_from_normal().

Value

Named length-23 parameter vector.


Map mean and covariance to a SUN(4,2) hyperspherical start

Description

Exact Gaussian start on the joint hyperspherical layout of make_sun42_hs_params(): the six UU-block free coordinates come from inverting cov2cor(Sigma); all cross / VV coordinates are zero. Optional skew_strength sets pair slots z51,z62 to atanh(skew_strength).

Usage

make_sun42_hs_start_from_normal(mu, Sigma, skew_strength = 0)

Arguments

mu

Length-4 mean.

Sigma

4×44\times 4 covariance.

skew_strength

Pair-slot t=tanh⁡(z)t=\tanh(z) value in (−1,1)(-1,1) (default 0).

Value

Named length-23 parameter vector for make_sun42_hs_params().


Build SUN(4,3) parameters from a joint hyperspherical vector

Description

The joint correlation of (U,V)∈R7(U,V)\in\mathbb{R}^7 is formed from 21 unconstrained unit-row Cholesky coordinates in row-major order.

Usage

make_sun43_hs_params(par)

Arguments

par

Numeric vector of length 29: xi(4), nu(4), then 21 z coordinates named by .sun_hs_z_names(4, 3).

Value

A SUN dp list with xi, Omega (4×44\times 4), Delta (4×34\times 3), zero tau (length 3), and Gamma (3×33\times 3).

See Also

dsun43_hs(), sun43_hs_bounds()


SUN(4,3) hyperspherical start from weighted sample moments

Description

SUN(4,3) hyperspherical start from weighted sample moments

Usage

make_sun43_hs_start_from_gh(quad, skew_strength = 0.01)

Arguments

quad

List with x and w.

skew_strength

Passed to make_sun43_hs_start_from_normal().

Value

Named length-29 parameter vector.


Map mean and covariance to a SUN(4,3) hyperspherical start

Description

Exact Gaussian start on the joint hyperspherical layout of make_sun43_hs_params(): the six UU-block free coordinates come from inverting cov2cor(Sigma); all cross / VV coordinates are zero. Optional skew_strength sets pair slots z51,z62,z73 to atanh(skew_strength).

Usage

make_sun43_hs_start_from_normal(mu, Sigma, skew_strength = 0)

Arguments

mu

Length-4 mean.

Sigma

4×44\times 4 covariance.

skew_strength

Pair-slot t=tanh⁡(z)t=\tanh(z) value in (−1,1)(-1,1) (default 0).

Value

Named length-29 parameter vector for make_sun43_hs_params().


Build SUN(4,4) parameters from a joint hyperspherical vector

Description

The joint correlation of (U,V)(U,V) is formed from 28 unconstrained unit-row Cholesky coordinates in row-major order.

Usage

make_sun44_hs_params(par)

Arguments

par

Numeric vector of length 36: xi(4), nu(4), then 28 z coordinates named by .sun_hs_z_names(4, 4).

Value

A SUN dp list with xi, Omega, Delta, zero tau, and Gamma.

See Also

dsun44_hs(), make_sun44_params(), sun44_hs_bounds()


SUN(4,4) hyperspherical start from weighted sample moments

Description

SUN(4,4) hyperspherical start from weighted sample moments

Usage

make_sun44_hs_start_from_gh(quad, skew_strength = 0.01)

Arguments

quad

List with x and w.

skew_strength

Passed to make_sun33_start_from_normal().

Value

Named length-36 parameter vector.


Map mean and covariance to a SUN(4,4) hyperspherical start

Description

Map mean and covariance to a SUN(4,4) hyperspherical start

Usage

make_sun44_hs_start_from_normal(mu, Sigma, skew_strength = 0)

Arguments

mu

Length-4 mean.

Sigma

4×44\times 4 covariance.

skew_strength

Diagonal loading for L* (default 0).

Value

Named length-36 vector for make_sun44_hs_params().


Build SUN(4,4) distribution parameters from a free parameter vector

Description

Converts the SUN(4,4) block-Cholesky parameterization into the dp list expected by the sn package (xi, Omega, Delta, tau, Gamma).

Usage

make_sun44_params(par)

Arguments

par

Numeric vector of length 36: xi(4), nu(4), omega(6), L(16), gamma(6). nu are marginal standard deviations of Ω\Omega (Ωjj=νj2\Omega_{jj}=\nu_j^2); omega* free hyperspherical coordinates for correlation CuC_u; LL acts on that correlation scale; gamma* parameterize the residual correlation of VV. Valid parameters need diag⁡(LCuL⊤)<1\operatorname{diag}(L C_u L^\top) < 1.

Details

As in make_sun_params(): Ω=DuCuDu\Omega = D_u C_u D_u with Du=diag⁡(ν)D_u=\operatorname{diag}(\nu), M=LCuL⊤M = L C_u L^\top, Σuv=DuCuL⊤\Sigma_{uv} = D_u C_u L^\top, and Σvv=M+D1/2CD1/2\Sigma_{vv} = M + D^{1/2} C D^{1/2} with unit diagonal.

Value

A list with components xi, Omega, Delta, tau (fixed at zero), and Gamma.

See Also

make_sun_params(), dsun44(), dsun44_fun()


SUN(4,4) start from weighted sample moments on a quadrature

Description

SUN(4,4) start from weighted sample moments on a quadrature

Usage

make_sun44_start_from_gh(quad, skew_strength = 0.01)

Arguments

quad

List with x and w.

skew_strength

Passed to make_sun33_start_from_normal().

Value

Named length-36 parameter vector.


Map mean and covariance to a SUN(4,4) start (zero / tiny skewness)

Description

Map mean and covariance to a SUN(4,4) start (zero / tiny skewness)

Usage

make_sun44_start_from_normal(mu, Sigma, skew_strength = 0)

Arguments

mu

Length-4 mean.

Sigma

4×44\times 4 covariance.

skew_strength

Diagonal loading for L* (default 0).

Value

Named length-36 parameter vector for make_sun44_params().


Build SUN(5,2) parameters from a joint hyperspherical vector

Description

The joint correlation of (U,V)∈R7(U,V)\in\mathbb{R}^7 is formed from 21 unconstrained unit-row Cholesky coordinates in row-major order.

Usage

make_sun52_hs_params(par)

Arguments

par

Numeric vector of length 31: xi(5), nu(5), then 21 z coordinates named by .sun_hs_z_names(5, 2).

Value

A SUN dp list with xi, Omega (5×55\times 5), Delta (5×25\times 2), zero tau (length 2), and Gamma (2×22\times 2).

See Also

dsun52_hs(), sun52_hs_bounds()


SUN(5,2) hyperspherical start from weighted sample moments

Description

SUN(5,2) hyperspherical start from weighted sample moments

Usage

make_sun52_hs_start_from_gh(quad, skew_strength = 0.01)

Arguments

quad

List with x and w.

skew_strength

Passed to make_sun52_hs_start_from_normal().

Value

Named length-31 parameter vector.


Map mean and covariance to a SUN(5,2) hyperspherical start

Description

Exact Gaussian start on the joint hyperspherical layout of make_sun52_hs_params(): the ten UU-block free coordinates come from inverting cov2cor(Sigma); all cross / VV coordinates are zero. Optional skew_strength sets pair slots z61,z72 to atanh(skew_strength).

Usage

make_sun52_hs_start_from_normal(mu, Sigma, skew_strength = 0)

Arguments

mu

Length-5 mean.

Sigma

5×55\times 5 covariance.

skew_strength

Pair-slot t=tanh⁡(z)t=\tanh(z) value in (−1,1)(-1,1) (default 0).

Value

Named length-31 parameter vector for make_sun52_hs_params().


Pack covariance into Genz chol and scales

Description

Converts a covariance matrix into the Genz reordered correlation Cholesky factor and marginal scales used by the CppAD tape. The permutation perm must match the frozen order from pmvn_fun().

Usage

pack_genz_ch(sigma, perm)

Arguments

sigma

Positive-definite covariance matrix.

perm

Integer permutation vector from a tape object.

Value

A list with components scale and ch.


Multivariate normal CDF with derivatives

Description

Evaluates P(lower<X<upper)P(\code{lower} < X < \code{upper}) for X∼N(mean,sigma)X \sim N(\code{mean}, \code{sigma}) using Genz's quasi-Monte Carlo algorithm. Returns the probability, a Monte Carlo error estimate, and derivatives of the approximation with respect to upper.

Usage

pmvn(
  upper,
  lower = -Inf,
  mean = 0,
  sigma,
  n_points = 1021L,
  n_shifts = 8L,
  seed = 1L
)

Arguments

upper

Numeric vector of upper integration limits.

lower

Numeric vector of lower integration limits. Defaults to -Inf.

mean

Numeric mean vector. Defaults to zero.

sigma

Positive-definite covariance matrix.

n_points

Number of quasi-Monte Carlo points per random shift.

n_shifts

Number of random shifts for the QMC rule.

seed

Random seed for generating QMC shifts.

Value

A list with components value, error, gradient, and hessian. The gradient and Hessian are with respect to upper.

Examples

sigma <- matrix(c(1, 0.5, 0.5, 1), 2, 2)
pmvn(c(1, 1), lower = c(-Inf, -Inf), sigma = sigma)

Create a reusable MVN CDF tape

Description

Builds a CppAD tape for the multivariate normal CDF over upper, mean, marginal scales, and the Genz Cholesky factor. The tape is built once; subsequent evaluations pass new parameter values without re-taping. Derivatives with respect to upper use sparse inner patterns (as in adlaplace).

Usage

pmvn_fun(
  lower = -Inf,
  mean = 0,
  sigma,
  n_points = 1021L,
  n_shifts = 8L,
  seed = 1L
)

Arguments

lower, mean, sigma

Seed integration parameters used to fix the Genz permutation and build the tape (see pmvn()).

n_points, n_shifts, seed

QMC settings passed to pmvn().

Details

eval(upper, mean = NULL, sigma = NULL, inner = TRUE) evaluates the taped function. When mean or sigma are omitted, build-time values are used. inner = TRUE (default) returns the gradient and Hessian with respect to upper only; inner = FALSE also returns gradient_mean and includes mean, scale, and Cholesky blocks in the sparse derivatives.

Value

An object of class "admvn_tape" with components perm (frozen Genz permutation) and eval().

Examples

sigma <- matrix(c(1, 0.3, 0.3, 1), 2, 2)
f <- pmvn_fun(lower = c(-Inf, -Inf), mean = c(0, 0), sigma = sigma)
f$eval(c(0.5, 0.5))

SUN(3,3) MLE via C++ trust-region optimization

Description

Maximizes the taped SUN log-likelihood using the quasi-Newton trust-region solver from trustOptim (SR1 by default). The nu components are optimized on the log scale so that they remain positive. The entire optimization runs in C++ with one OpenMP-parallel value+gradient evaluation per iteration.

Usage

sun_mle(tape, start, control = list(), n_threads = 0L)

Arguments

tape

An object from dsun_fun().

start

Numeric start vector of length 21 (natural parameterization).

control

List of trust-region controls (see trustOptim), e.g. maxit, grad.tol, report.freq, quasi.newton.method (1 = SR1, 2 = BFGS).

n_threads

Threads for shard evaluation; 0 uses the tape default.

Value

A list with par, value (log-likelihood), gradient, iterations, status, and related fields.


Default L-BFGS-B box constraints for SUN(2,2) parameters

Description

Diagonal loadings L11,L22 are in [-1, 1] (correlation scale). Off-diagonal loadings L12,L21 are in [-0.5, 0.5], matching sun33_bounds().

Usage

sun22_bounds()

Value

List with lower and upper length-10 numeric vectors.

See Also

make_sun22_params(), fit_sun22_quad()


Default box constraints for SUN(2,2) hyperspherical parameters

Description

Independent-SN pair slots use atanh(+/-0.8) and all other joint coordinates use atanh(+/-0.5).

Usage

sun22_hs_bounds()

Value

List of length-10 lower and upper vectors.


Default box constraints for SUN(3,2) hyperspherical parameters

Description

Pair slots linking ViV_i to UiU_i for i=1,2i=1,2 (z41, z52) use atanh(+/-0.8); other joint coordinates use atanh(+/-0.5).

Usage

sun32_hs_bounds()

Value

List of length-16 lower and upper vectors.


Default L-BFGS-B box constraints for SUN(3,3) parameters

Description

Diagonal loadings L11,L22,L33 are in [-1, 1] (correlation scale). Off-diagonal loadings Lij/Lji are in [-0.5, 0.5].

Usage

sun33_bounds()

Value

List with lower and upper length-21 numeric vectors.

See Also

make_sun_params(), fit_sun33_quad()


Default L-BFGS-B box constraints for SUN(3,3) hyperspherical parameters

Description

Soft boxes on the joint unit-row Cholesky coordinates of make_sun_hs_params(): independent-SN pair slots z41,z52,z63 lie in atanh(+/-0.8); all other z* lie in atanh(+/-0.5). These are regularization / start boxes, not a Gershgorin PD certificate.

Usage

sun33_hs_bounds()

Value

List with lower and upper length-21 numeric vectors.

See Also

make_sun_hs_params(), dsun_hs()


Default box constraints for SUN(4,2) hyperspherical parameters

Description

Pair slots linking ViV_i to UiU_i for i=1,2i=1,2 (z51, z62) use atanh(+/-0.8); other joint coordinates use atanh(+/-0.5).

Usage

sun42_hs_bounds()

Value

List of length-23 lower and upper vectors.


Default box constraints for SUN(4,3) hyperspherical parameters

Description

Pair slots linking ViV_i to UiU_i for i=1,2,3i=1,2,3 (z51, z62, z73) use atanh(+/-0.8); other joint coordinates use atanh(+/-0.5).

Usage

sun43_hs_bounds()

Value

List of length-29 lower and upper vectors.


Default L-BFGS-B box constraints for SUN(4,4) parameters

Description

Skew loadings Lij act on the correlation scale, so they are restricted to [-1, 1].

Usage

sun44_bounds()

Value

List with lower and upper length-36 numeric vectors.

See Also

make_sun44_params(), fit_sun44_quad()


Default box constraints for SUN(4,4) hyperspherical parameters

Description

Independent-SN pair slots use atanh(+/-0.8) and all other joint coordinates use atanh(+/-0.5).

Usage

sun44_hs_bounds()

Value

List of length-36 lower and upper vectors.


Default box constraints for SUN(5,2) hyperspherical parameters

Description

Pair slots linking ViV_i to UiU_i for i=1,2i=1,2 (z61, z72) use atanh(+/-0.8); other joint coordinates use atanh(+/-0.5).

Usage

sun52_hs_bounds()

Value

List of length-31 lower and upper vectors.


Weighted covariance

Description

Weighted covariance

Usage

weighted_cov(X, w)

Arguments

X

Numeric matrix of observations (rows).

w

Nonnegative weights (recycled / renormalized).

Value

Symmetric ncol(X) covariance matrix.


Weighted column means

Description

Weighted column means

Usage

weighted_mean(X, w)

Arguments

X

Numeric matrix of observations (rows).

w

Nonnegative weights (recycled / renormalized).

Value

Named numeric vector of length ncol(X).