Nothing
# A-posteriori per-fit accuracy certificate (M108; D-051, RR21) ---------------
#
# WHAT THIS IS. The degeneracy criterion in R/axes_corrected_se.R refuses a
# fitted matrix a-priori, on p*kappa^2*eps -- a conservative envelope that
# measurement puts 5 to 8 decades above the error actually committed in
# reachable geometry. This file is the other instrument: given the matrix a fit
# is priced at, it ESTIMATES the relative error that fit's own corrected
# component SEs and its `cval` carry, after the fact, from that fit's own
# numbers. Since M111 it is what decides the refusal below that floor:
# axes_degeneracy_refusal() (R/axes_corrected_se.R) consults it wherever the
# criterion says "ill_conditioned", and a fit whose worse estimate exceeds the
# accuracy target refuses as "uncertified" at both surfaces.
#
# THE MECHANISM (D-051). The shipped pricing pipeline is replayed once in
# compensated double-double arithmetic -- two doubles carrying an unevaluated
# sum, about 31 significant digits, base R and no dependency -- and the
# certificate is the relative disagreement between the shipped values and the
# replayed ones, times a stated safety factor. Both routes run the SAME
# algorithm on the SAME data, at precisions 16 decades apart (eps_dd ~ 6e-32
# against eps ~ 2.2e-16), so the disagreement IS the shipped route's committed
# error rather than a proxy for it. That precision separation is the whole
# decision: two routes at EQUAL precision were measured at RR21 section 1 and
# rejected -- their disagreement is dominated by whichever route is less
# accurate, overstating the shipped SE error by 3 to 4 decades on every
# reachable case while UNDER-reporting the cval error at two of six anchor
# geometries, which is the licensing failure this instrument exists to prevent.
# Runtime exact-rational arithmetic was rejected too (base R has no bignum; a
# hand-rolled one is a larger correctness surface than the certificate it
# serves, and an optional package cannot decide what the package refuses).
#
# WHAT IT ESTIMATES, exactly (RR21 section 2). With P the priced matrix -- the
# realigned cov2cor(Sigma-hat) both surfaces consume -- and se_r(P), cval(P)
# the exact-real-arithmetic values of the shipped formulas at P:
#
# E_SE = max over fitted components r of |se_r_hat - se_r(P)| / se_r(P)
# E_cval = |cval_hat - cval(P)| / |cval(P)|
# E_ratio = max over fitted components r of
# |ratio_r_hat - ratio_r(P)| / ratio_r(P)
#
# The SE side is aggregated by MAX because the reported vector refuses as a
# unit (the M62/M66 contract): a gate must protect the worst component, and a
# per-component certificate would license nothing the max does not. E_ratio is
# aggregated the same way and for the same reason.
#
# E_ratio IS THE THIRD ESTIMAND (M113; D-053). ratio_r is `fiml_ratio`, the
# per-component quotient sqrt(v_r_corrected / v_r_naive) that
# axes_corrected_se() forms with BOTH arms priced at P, and which on
# `missing = "fiml"` multiplies the reported standard error
# (R/axes_reliability.R). So it is user-reported arithmetic, and neither
# existing field covers it: its denominator is the naive-at-P arm, which E_SE
# does not price at all. Nor would an arm-wise bound cover it -- M113 T1
# measured the quotient's committed error ABOVE both arms' own at four of the
# six oracle geometries (factor up to 1.85), so a max over the two arms
# under-reports rather than over-reports it. It is priced here as its own
# quantity, from the same replay, for that reason.
#
# These are estimates of the COMMITTED ERROR, NOT proven upper bounds -- no
# theorem converts the disagreement into a bound. The safety factor F = 10 and
# the additive floor of two machine epsilons are what make the estimate behave
# as an upper estimate, and that behaviour is pinned empirically rather than
# assumed: at six anchor geometries against the exact-rational oracle
# (tests/testthat/test-axes-certificate.R, AC2/AC3), at the 83 graded region
# matrices of the M147 sweep (devel/degeneracy-oracle/tol0-sweep-summary.md,
# ratio 9.9996 to 10.005 wherever the true error is below 1e-2), and by
# planted-perturbation sensitivity invariants elsewhere. The ratio sits AT F
# for a reason (RR24 section 1): both routes run the same operations on
# bit-identical inputs with rounding at eps and at about eps^2, so each
# route's error is its own roundoff times one matrix-dependent amplification,
# and |d - r| = |d - e| (1 + O(eps)) to first order. That is an identity, not
# a calibration; it stops holding only where the errors are O(1) and the
# variance-to-SE conversion is no longer linear, which is past the target. F is load-bearing, not decorative: at F = 1
# the raw disagreement sits just BELOW the true error at two of those anchors
# (ratios 0.997 and 0.983, RR21 section 3), because dropping from v to sqrt(v)
# is a first-order conversion.
#
# NEITHER n NOR df ENTERS, structurally. The reported SE is sqrt(v_r / n), so
# the relative SE error is |sqrt(v_r_hat / v_r) - 1|: n cancels EXACTLY, not
# merely to first order. The certificate is therefore computed from the
# pre-square-root quadratic forms v_r and from the pre-division numerator of
# cval, in which neither the sample size nor the degrees of freedom appear, and
# takes no `n` and no `df` argument at all. What the n-touching tail (one
# division, one square root) and the df division can add is ~2 ulp regardless
# of their values, which is what the 2*eps additive floor covers. This is
# D-048's and D-049's requirement met by construction: a certificate that moved
# with the typed n would rebuild the yardstick-dependence they refused.
#
# SCOPE, stated so it cannot be overclaimed. The certificate prices the
# arithmetic FROM P TO THE REPORTED NUMBERS. The optimizer's error in Sigma-hat
# and cov2cor()'s own rounding are upstream of P and common to both routes --
# out of scope, exactly as they were for the a-priori bound, and the
# indefiniteness band prices optimizer error separately (M90).
#
# THE SENTINEL (RR21 section 5). Where either route fails to produce finite
# values, or a quadratic form is nonpositive, or a denominator is zero, all
# three estimates are 1 -- "no digits certified". It is finite and non-negative
# like any other return, and it sits four decades above the accuracy target, so a
# gate keyed to the certificate fails closed on it (GP2). Measured along the
# degradation path (RR21 section 5): until M147, past kappa ~ 3e8 the shipped
# pricing's own solve() refused "unidentified" at its default tolerance before
# the certificate mattered. Since M147 the certified path inverts under
# `tol = 0` and this certificate is the sole conditioning judge (D-061,
# D-062): the M147 sweep measured it at every matrix the old gate refused,
# with no under-report, and 37 of 85 such matrices compute inside the target. The sentinel is returned as a unit for all
# three quantities, never one field at a time, so the two surfaces cannot
# disagree about whether this fit is certified (M89's nestedness contract).
# ---- compensated (double-double) arithmetic ---------------------------------
#
# A dd value is list(hi, lo) with the value hi + lo unevaluated and
# |lo| <= ulp(hi)/2. hi and lo are ordinary numeric vectors or matrices of the
# same shape, so every operation below is elementwise and vectorized: the
# arithmetic is 10 to 30 R-level calls per operation, and doing it a scalar at
# a time measured 6x slower at p = 24 (RR21 section 7).
#
# The error-free transforms are Knuth's two-sum and Dekker's two-product. Both
# are theorems about IEEE-754 double rounding, and both are defeated by an
# arithmetic that does NOT round each operation: a fused multiply-add
# contracting `ahi * bhi - p`, or x87 80-bit intermediates. Each line below is
# a separate R-level operation on a materialized numeric vector, so R's
# evaluator forces the rounding a C compiler could contract away -- and
# axes_dd_selftest() checks the transforms against committed known answers at
# every certificate call rather than trusting that argument.
dd_of <- function(hi, lo = NULL) {
if (is.null(lo)) lo <- hi * 0
list(hi = hi, lo = lo)
}
# Knuth two-sum: hi = fl(a + b) and lo = the exact rounding error, for any a, b.
dd_two_sum <- function(a, b) {
s <- a + b
bb <- s - a
list(hi = s, lo = (a - (s - bb)) + (b - bb))
}
# The cheap variant, valid only where |a| >= |b| -- every use below has that
# from the branch it sits in, which is what the extra argument would otherwise
# have to assert.
dd_quick_two_sum <- function(a, b) {
s <- a + b
list(hi = s, lo = b - (s - a))
}
# Dekker two-product via the 2^27+1 splitting constant: hi = fl(a * b) and
# lo = the exact rounding error. No FMA is assumed (see the note above).
dd_two_prod <- function(a, b) {
p <- a * b
ca <- 134217729 * a
ahi <- ca - (ca - a)
alo <- a - ahi
cb <- 134217729 * b
bhi <- cb - (cb - b)
blo <- b - bhi
list(hi = p, lo = ((ahi * bhi - p) + ahi * blo + alo * bhi) + alo * blo)
}
dd_add <- function(x, y) {
s <- dd_two_sum(x$hi, y$hi)
t <- dd_two_sum(x$lo, y$lo)
s <- dd_quick_two_sum(s$hi, s$lo + t$hi)
dd_quick_two_sum(s$hi, s$lo + t$lo)
}
dd_neg <- function(x) list(hi = -x$hi, lo = -x$lo)
dd_sub <- function(x, y) dd_add(x, dd_neg(y))
dd_mul <- function(x, y) {
p <- dd_two_prod(x$hi, y$hi)
dd_quick_two_sum(p$hi, p$lo + (x$hi * y$lo + x$lo * y$hi))
}
# Three-step division: two corrections bring the quotient to full dd precision.
dd_div <- function(x, y) {
q1 <- x$hi / y$hi
r <- dd_sub(x, dd_mul(y, dd_of(q1)))
q2 <- r$hi / y$hi
r <- dd_sub(r, dd_mul(y, dd_of(q2)))
q3 <- r$hi / y$hi
s <- dd_quick_two_sum(q1, q2)
dd_add(s, dd_of(q3))
}
# Scaling by a power of two is exact in both words, so the 0.5 and 2 factors
# the pricing carries cost nothing and introduce no rounding at all.
dd_scale2 <- function(x, f) list(hi = x$hi * f, lo = x$lo * f)
dd_to_double <- function(x) x$hi + x$lo
# Tree (pairwise) summation over every element: log2(m) rounding steps instead
# of m, and it is also the fewest R-level calls, since each level is one
# vectorized dd_add over half the remaining terms.
dd_sum <- function(x) {
h <- as.vector(x$hi)
l <- as.vector(x$lo)
if (length(h) == 0L) return(list(hi = 0, lo = 0))
while (length(h) > 1L) {
n <- length(h)
m <- n %/% 2L
s <- dd_add(list(hi = h[seq_len(m)], lo = l[seq_len(m)]),
list(hi = h[m + seq_len(m)], lo = l[m + seq_len(m)]))
if (n %% 2L == 1L) {
h <- c(s$hi, h[[n]])
l <- c(s$lo, l[[n]])
} else {
h <- s$hi
l <- s$lo
}
}
list(hi = h[[1L]], lo = l[[1L]])
}
# Row sums, tree-reduced over columns for the same reason.
dd_rowsums <- function(x) {
h <- x$hi
l <- x$lo
while (ncol(h) > 1L) {
n <- ncol(h)
m <- n %/% 2L
s <- dd_add(list(hi = h[, seq_len(m), drop = FALSE],
lo = l[, seq_len(m), drop = FALSE]),
list(hi = h[, m + seq_len(m), drop = FALSE],
lo = l[, m + seq_len(m), drop = FALSE]))
if (n %% 2L == 1L) {
h <- cbind(s$hi, h[, n])
l <- cbind(s$lo, l[, n])
} else {
h <- s$hi
l <- s$lo
}
}
list(hi = as.vector(h), lo = as.vector(l))
}
dd_t <- function(x) list(hi = t(x$hi), lo = t(x$lo))
dd_diag <- function(x) {
i <- seq_len(nrow(x$hi))
ij <- cbind(i, i)
list(hi = x$hi[ij], lo = x$lo[ij])
}
dd_set_diag <- function(x, v) {
i <- seq_len(nrow(x$hi))
ij <- cbind(i, i)
x$hi[ij] <- v$hi
x$lo[ij] <- v$lo
x
}
# Matrix product, accumulated one rank-one term at a time so that each term is
# a single vectorized dd_mul over the whole n x m result.
dd_matmul <- function(a, b) {
n <- nrow(a$hi)
k <- ncol(a$hi)
m <- ncol(b$hi)
acc <- dd_of(matrix(0, n, m))
for (j in seq_len(k)) {
term <- dd_mul(
list(hi = matrix(a$hi[, j], n, m), lo = matrix(a$lo[, j], n, m)),
list(hi = matrix(b$hi[j, ], n, m, byrow = TRUE),
lo = matrix(b$lo[j, ], n, m, byrow = TRUE))
)
acc <- dd_add(acc, term)
}
acc
}
# Inverse by Gauss-Jordan elimination with partial pivoting on [A | I].
# Pivoting rather than a Cholesky-shaped factorization deliberately: the
# admitted domain includes matrices whose smallest eigenvalue is negative at
# roundoff level (the M90 band), where a positive-definiteness assumption is
# not available. Returns NULL on a zero pivot or a non-finite result -- the
# sentinel path, never a guess.
dd_solve <- function(a) {
n <- nrow(a$hi)
hi <- cbind(a$hi, diag(n))
lo <- cbind(a$lo, matrix(0, n, n))
for (k in seq_len(n)) {
# which.max() returns integer(0) on an all-NA column, and the finiteness
# test below would then evaluate to NA and error -- the sentinel path
# raising a condition instead of returning, which is what this guard
# stops. A column with no finite entry has no usable pivot, so it is the
# sentinel either way.
cand <- abs(hi[k:n, k])
if (!any(is.finite(cand))) return(NULL)
piv <- which.max(cand) + k - 1L
if (!is.finite(hi[piv, k]) || hi[piv, k] == 0) return(NULL)
if (piv != k) {
hi[c(k, piv), ] <- hi[c(piv, k), ]
lo[c(k, piv), ] <- lo[c(piv, k), ]
}
rowk <- dd_div(list(hi = hi[k, ], lo = lo[k, ]),
list(hi = hi[k, k], lo = lo[k, k]))
hi[k, ] <- rowk$hi
lo[k, ] <- rowk$lo
other <- seq_len(n)[-k]
nr <- length(other)
nc <- ncol(hi)
# n = 1 leaves nothing to eliminate. Guarded rather than left to recycling:
# matrix(row, 0, nc, byrow = TRUE) warns about non-empty data in a
# zero-extent matrix, and a warning from inside a certificate would reach
# the user through a surface that is supposed to be silent.
if (nr > 0L) {
upd <- dd_sub(
list(hi = hi[other, , drop = FALSE], lo = lo[other, , drop = FALSE]),
dd_mul(
list(hi = matrix(hi[other, k], nr, nc), lo = matrix(lo[other, k], nr, nc)),
list(hi = matrix(rowk$hi, nr, nc, byrow = TRUE),
lo = matrix(rowk$lo, nr, nc, byrow = TRUE))
)
)
hi[other, ] <- upd$hi
lo[other, ] <- upd$lo
}
}
out <- list(hi = hi[, n + seq_len(n), drop = FALSE],
lo = lo[, n + seq_len(n), drop = FALSE])
if (!all(is.finite(out$hi)) || !all(is.finite(out$lo))) return(NULL)
out
}
# Known-answer self-test of the two error-free transforms, on operand pairs
# whose exact errors are known in closed form: fl(1 + eps/2) is 1 with error
# eps/2 (ties-to-even), and (1 +/- eps)^2 differs from its rounded product by
# exactly +/- eps^2. If an arithmetic mode defeats the transforms, these are
# what notice: the certificate then returns its sentinel, so the a-priori
# criterion remains the operative gate instead of a silently wrong estimate.
axes_dd_selftest <- function() {
e <- .Machine$double.eps
s <- dd_two_sum(1, e / 2)
p <- dd_two_prod(1 + e, 1 + e)
m <- dd_two_prod(1 + e, 1 - e)
identical(s$hi, 1) && identical(s$lo, e / 2) &&
identical(p$hi, 1 + 2 * e) && identical(p$lo, e^2) &&
identical(m$hi, 1) && identical(m$lo, -(e^2))
}
# ---- the reference route ----------------------------------------------------
#
# The same pipeline axes_v_pricing() and axes_u_pricing() run, expression for
# expression, in dd arithmetic. Returns the corrected quadratic forms `v` and
# the naive ones `v_naive` (one per fitted component each) and the scaling
# factor's numerator `u`, all as dd values, or NULL where the route cannot
# produce them.
axes_dd_pricing <- function(sigma, d) {
ss <- dd_of(sigma)
si <- dd_solve(ss)
if (is.null(si)) return(NULL)
mats <- lapply(d$mats, dd_of)
sim <- lapply(mats, function(m) dd_matmul(si, m))
q <- length(sim)
info_hi <- matrix(0, q, q)
info_lo <- matrix(0, q, q)
simt <- lapply(sim, dd_t)
for (s in seq_len(q)) {
for (t in s:q) {
e <- dd_scale2(dd_sum(dd_mul(sim[[s]], simt[[t]])), 0.5)
info_hi[s, t] <- info_hi[t, s] <- e$hi
info_lo[s, t] <- info_lo[t, s] <- e$lo
}
}
acov <- dd_solve(list(hi = info_hi, lo = info_lo))
if (is.null(acov)) return(NULL)
# The component quadratic forms, BOTH arms (M113). The corrected arm is the
# reported SE's; the naive arm AT THIS SAME MATRIX is `fiml_ratio`'s
# denominator, and on the FIML path the reported SE is that quotient times
# lavaan's own -- so it is replayed here too, and E_ratio prices it. What is
# still not replayed is the naive arm at the RAW Sigma-hat: a different
# matrix, priced by a separate axes_v_pricing() call in axes_corrected_se(),
# whose result is never user-reported (it exists as the tie to lavaan's own
# SE, D-037), so there is nothing about that one to certify.
v_hi <- numeric(d$n_comp)
v_lo <- numeric(d$n_comp)
vn_hi <- numeric(d$n_comp)
vn_lo <- numeric(d$n_comp)
for (r in seq_len(d$n_comp)) {
acc <- dd_of(matrix(0, nrow(sigma), ncol(sigma)))
for (k in seq_len(q)) {
acc <- dd_add(acc, dd_mul(mats[[k]],
list(hi = acov$hi[r, k], lo = acov$lo[r, k])))
}
w <- dd_scale2(dd_matmul(dd_matmul(si, acc), si), 0.5)
ws <- dd_matmul(w, ss)
vn <- dd_scale2(dd_sum(dd_mul(ws, dd_t(ws))), 2)
vn_hi[[r]] <- vn$hi
vn_lo[[r]] <- vn$lo
wc <- dd_set_diag(w, dd_of(numeric(nrow(sigma))))
wc <- dd_set_diag(wc, dd_neg(dd_rowsums(dd_mul(wc, ss))))
wcs <- dd_matmul(wc, ss)
vv <- dd_scale2(dd_sum(dd_mul(wcs, dd_t(wcs))), 2)
v_hi[[r]] <- vv$hi
v_lo[[r]] <- vv$lo
}
# The scaling factor's numerator, tr_vg - sum(acov * bmat).
up <- upper.tri(sigma)
rho <- dd_of(sigma[up])
si_up <- list(hi = si$hi[up], lo = si$lo[up])
one <- dd_of(rep(1, length(rho$hi)))
tr_vg <- dd_sum(dd_sub(one, dd_mul(dd_mul(si_up, rho),
dd_sub(one, dd_mul(rho, rho)))))
ys <- lapply(sim, function(sm) {
w <- dd_scale2(dd_matmul(sm, si), 0.5)
w <- dd_set_diag(w, dd_sub(dd_diag(w), dd_diag(dd_matmul(ss, w))))
dd_matmul(w, ss)
})
yst <- lapply(ys, dd_t)
b_hi <- matrix(0, q, q)
b_lo <- matrix(0, q, q)
for (s in seq_len(q)) {
for (t in s:q) {
e <- dd_scale2(dd_sum(dd_mul(ys[[s]], yst[[t]])), 2)
b_hi[s, t] <- b_hi[t, s] <- e$hi
b_lo[s, t] <- b_lo[t, s] <- e$lo
}
}
u <- dd_sub(tr_vg, dd_sum(dd_mul(acov, list(hi = b_hi, lo = b_lo))))
if (!all(is.finite(c(v_hi, v_lo, vn_hi, vn_lo, u$hi, u$lo)))) return(NULL)
list(v = list(hi = v_hi, lo = v_lo),
v_naive = list(hi = vn_hi, lo = vn_lo),
u = u)
}
# ---- the certificate --------------------------------------------------------
#
# `sigma` is the priced matrix -- the realigned cov2cor(Sigma-hat) -- and `d`
# the derivative set from axes_se_derivs(). Returns a list of THREE finite,
# non-negative numbers: `se`, the estimated relative error of the corrected
# component SE vector (worst component); `cval`, the estimated relative error
# of the scaling factor; and `fiml_ratio`, the estimated relative error of the
# per-component quotient corrected/naive that the FIML path multiplies the
# reported standard error by (M113; D-053). See this file's header for what
# they estimate, what they are not, and what the sentinel value 1 means.
axes_certificate_safety_factor <- 10
# The one definition of the sentinel, so the certificate's own early returns
# and the fence around its call site (axes_degeneracy_refusal()) cannot come to
# disagree about the field set -- the drift axes_certificate_worst() exists to
# prevent, on the other side of the same call (M113 review, [O] F4).
axes_certificate_sentinel <- function() list(se = 1, cval = 1, fiml_ratio = 1)
axes_accuracy_certificate <- function(sigma, d) {
sentinel <- axes_certificate_sentinel()
if (!axes_dd_selftest()) return(sentinel)
if (!all(is.finite(sigma))) return(sentinel)
v_hat <- axes_v_pricing(sigma, d)
u_hat <- axes_u_pricing(sigma, d)
if (is.character(v_hat) || is.character(u_hat)) return(sentinel)
ref <- axes_dd_pricing(sigma, d)
if (is.null(ref)) return(sentinel)
vn_hat <- v_hat$naive
v_hat <- v_hat$corrected
if (!all(is.finite(v_hat)) || !all(is.finite(vn_hat)) ||
!is.finite(u_hat)) {
return(sentinel)
}
# Nonpositive quadratic forms and a vanished cval numerator are the
# denominators that can fail here: the first is the cancellation the
# "indefinite" backstop in axes_se_pricing() exists for, the second the one
# the scaling surface's cval <= 0 backstop exists for. Neither is a matrix
# about which a relative error can be stated at all. The naive arm is
# checked on BOTH routes (M113): it is the quotient's denominator, and the
# shipped one divides by its own value, not by the replayed one.
if (any(ref$v$hi <= 0) || any(ref$v_naive$hi <= 0) ||
any(vn_hat <= 0) || ref$u$hi == 0) {
return(sentinel)
}
delta_v <- max(abs(dd_to_double(
dd_div(dd_sub(dd_of(v_hat), ref$v), ref$v)
)))
delta_u <- abs(dd_to_double(
dd_div(dd_sub(dd_of(u_hat), ref$u), ref$u)
))
# The quotient is priced PRE-SQUARE-ROOT, like the other two, on
# q_r = v_r_corrected / v_r_naive: the shipped fiml_ratio is
# sqrt(v_c/n)/sqrt(v_n/n) = sqrt(q_r), in which n cancels exactly, so the
# certificate stays n-free by construction and the two square roots and two
# divisions the shipped tail actually performs add ~2 ulp -- the same tail
# the 2*eps floor already covers for `se`. It is the quotient of the SHIPPED
# arms against the quotient of the REPLAYED arms, never a combination of the
# two arms' own estimates: those errors are correlated in sign, and M113 T1
# measured the quotient's own error above both arms' at four of the six
# oracle geometries.
ref_q <- dd_div(ref$v, ref$v_naive)
delta_q <- max(abs(dd_to_double(
dd_div(dd_sub(dd_of(v_hat / vn_hat), ref_q), ref_q)
)))
# delta_v / 2 converts a variance's relative error to its square root's, to
# first order; the O(delta^2) that conversion drops is what the safety factor
# covers, and covers with two decades to spare at every geometry measured
# (RR21 section 3). delta_q / 2 is the same conversion on the quotient.
eps <- .Machine$double.eps
fac <- axes_certificate_safety_factor
out <- list(se = fac * max(delta_v / 2, 2 * eps),
cval = fac * max(delta_u, 2 * eps),
fiml_ratio = fac * max(delta_q / 2, 2 * eps))
if (!is.finite(out$se) || !is.finite(out$cval) ||
!is.finite(out$fiml_ratio)) {
return(sentinel)
}
out
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.