derivkit.utils.diagnostics module#
Numerical diagnostics for Fisher and DALI forecasts.
These utilities inspect supplied matrices and model derivatives. They do not re-evaluate the model or compute additional derivatives.
- derivkit.utils.diagnostics.dali_diagnostics(derivatives: dict[int, NDArray[float64]], cov: ArrayLike, displacement: ArrayLike, *, covariance_rcond: float = 1e-12) dict[str, Any]#
Measure directional nonlinear model response from DALI derivatives.
- Parameters:
derivatives – Model derivative tensors keyed by orders 1 up to 4. Order 1 is required. An order
ktensor has shape(n_observables, p, ..., p)withkparameter axes.cov – Symmetric positive semidefinite observational covariance of shape
(n_observables, n_observables).displacement – Parameter displacement from the expansion point, with shape
(p,).covariance_rcond – Relative cutoff used for the covariance pseudoinverse square root. Defaults to
1e-12.
- Returns:
Dictionary with
response_norms(covariance weighted norm of each Taylor response contribution),relative_response_strength(higher order norms divided by the linear norm),linear_response_norm, andtotal_response_norm(norm of the sum of available Taylor contributions).If the linear norm is zero, a positive higher order norm produces an infinite ratio, while zero divided by zero produces
NaN.- Raises:
ValueError – If the derivative dictionary is missing order 1, includes unsupported orders, or contains invalid tensor shapes or values; if the displacement is invalid; or if the covariance is not positive semidefinite within tolerance.
Notes
At displacement \(\Delta\theta\), the order
kmodel response contribution is\[r_k = \frac{1}{k!}\nu_{,i_1\ldots i_k} \Delta\theta_{i_1}\cdots\Delta\theta_{i_k}.\]Its noise-weighted norm is
\[\|r_k\|_{C^+} = \sqrt{r_k^{\mathsf T}C^+r_k},\]and the relative nonlinear response strength is
\[R_k = \frac{\|r_k\|_{C^+}}{\|r_1\|_{C^+}}.\]These are directional diagnostics of the model response, not diagnostics of the assembled DALI likelihood tensors and not guarantees of convergence or posterior accuracy. They depend on the chosen displacement.
- derivkit.utils.diagnostics.fisher_diagnostics(fisher: ArrayLike, *, jacobian: ArrayLike | None = None, cov: ArrayLike | None = None, rtol: float | None = None, covariance_rcond: float = 1e-12) dict[str, Any]#
Compute Fisher matrix and optional noise-weighted sensitivity diagnostics.
- Parameters:
fisher – Symmetric Fisher information matrix of shape
(p, p).jacobian – Optional model Jacobian of shape
(n_observables, p). Must be provided together withcov, or both omitted.cov – Optional positive semidefinite observational covariance of shape
(n_observables, n_observables). Must be provided together withjacobian, or both omitted.rtol – Relative cutoff for matrix and Jacobian numerical ranks. The default is dimension dependent for each calculation.
covariance_rcond – Relative eigenvalue cutoff used when forming the covariance pseudoinverse square root. Defaults to
1e-12.
- Returns:
The dictionary from
matrix_diagnostics(). Ifjacobianandcovare provided, also includesjacobian_singular_values,jacobian_rank, andjacobian_condition_number.- Raises:
ValueError – If inputs have invalid shapes or values, only one of
jacobianandcovis supplied, or the covariance has a significantly negative eigenvalue.
Notes
For a parameter independent covariance, the Fisher matrix is
\[F = J^{\mathsf T} C^{+} J,\]where
+denotes a pseudoinverse when necessary. The whitened Jacobian isW = C^{+/2} J. Its singular values satisfyW.T @ W = J.T @ C^+ @ J. When the covariance is strictly positive definite, this agrees with the usual Fisher expression.Numerical ranks of
FandWcan differ because formingFsquares singular values. The suppliedfisheris not checked againstJ.T @ C^+ @ J.
- derivkit.utils.diagnostics.matrix_diagnostics(matrix: ArrayLike, *, rtol: float | None = None) dict[str, Any]#
Compute eigenvalue diagnostics for a symmetric matrix.
- Parameters:
matrix – Nonempty symmetric matrix of shape
(p, p).rtol – Relative eigenvalue cutoff for numerical rank and positive semidefiniteness. Defaults to
p * epsfor float64 precision.
- Returns:
Dictionary with
eigenvalues(ascending),eigenvectors(columns corresponding to eigenvalues),condition_number(ratio of largest to smallest absolute eigenvalue, or infinity if numerically rank deficient),rank, andis_positive_semidefinite.- Raises:
ValueError – If the matrix is empty, nonsquare, nonfinite, or nonsymmetric, or if
rtolis invalid.
Notes
Rank and conditioning depend on parameter scaling and tolerance. An eigenvalue is counted as nonzero only if its absolute value exceeds
rtol * max(abs(eigenvalues)). A zero matrix has rank zero and infinite condition number.