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):
Center columns over time: X <- scale(P, center = TRUE, scale = FALSE).
Compute SVD: sv <- svd(X).
Take the first right singular vector (first column of V matrix); set \(l = |v|\).
If \(\sum l \leq tol\) or PC1 is degenerate, fall back to column means of P (over time) and renormalize.