Learn R Programming

BayesianDisaggregation (version 0.1.2)

compute_L_from_P: Compute likelihood vector from a prior matrix via SVD (center-only, robust)

Description

Builds a sectoral likelihood \(L\) (length \(K\)) from the prior weights matrix \(P \in \mathbb{R}^{T \times K}\) by taking the absolute value of the first right singular vector of the centered matrix (no scaling), then normalizing to the unit simplex. Includes input validation, optional row renormalization, and a safe fallback when PC1 is degenerate.

Usage

compute_L_from_P(P, renormalize_rows = TRUE, tol = 1e-12)

Value

Numeric vector \(L\) of length \(K\) (non-negative, sums to 1). Attributes:

  • "pc1_loadings": signed PC1 loadings \(v\).

  • "explained_var": fraction of variance explained by PC1.

  • "fallback": TRUE if column-mean fallback was used.

Arguments

P

Numeric matrix \(T \times K\); prior weights per period.

renormalize_rows

Logical; if TRUE (default), rows of P that are within tolerance of summing to 1 are renormalized. Otherwise an error is thrown.

tol

Numeric tolerance for simplex checks (default 1e-12).

Details

Validation: P must be a finite, non-negative numeric matrix. Each row must either (i) already sum to 1 within tol or (ii) be renormalizable within tol. Rows with (near-)zero sums are not renormalizable and raise an error. Missing values are not allowed.

Algorithm (exactly as implemented):

  1. Center columns over time: X <- scale(P, center = TRUE, scale = FALSE).

  2. Compute SVD: sv <- svd(X).

  3. Take the first right singular vector (first column of V matrix); set \(l = |v|\).

  4. If \(\sum l \leq tol\) or PC1 is degenerate, fall back to column means of P (over time) and renormalize.

  5. Otherwise, \(L = l / \sum l\).

See Also

spread_likelihood, bayesian_disaggregate

Examples

Run this code
set.seed(123)
T <- 10; K <- 4
P <- matrix(rexp(T*K), nrow = T); P <- P / rowSums(P)
L <- compute_L_from_P(P)
stopifnot(length(L) == K, all(L >= 0), abs(sum(L) - 1) < 1e-12)

Run the code above in your browser using DataLab