Skip to content

Metrics

Held-out scores for model selection. Robusta.score() returns a ZMoments; read .kl for the paper's cross-validation score. The functions below compute the same quantities directly from arrays.

ZMoments

robusta_hmf.ZMoments dataclass

Sufficient statistics for the moment-based held-out scores.

Attributes:

Name Type Description
n int

Number of residuals summed over, N * M. Every pixel counts, including those masked to zero weight, matching the convention used throughout the paper's analysis scripts.

sum_z, sum_z2 float

sum(z) and sum(z**2).

sum_chi2 float

sum(W * r**2), the unrobustified chi-squared.

n_excluded_rows, n_excluded_cols int

Rows and columns the scored state excludes (see RHMFState). Their entries enter the sums as zero-weight entries do: z and chi-squared are 0 there, but they still count in n.

n_excluded_entries int

Entries with non-zero weight that fall in an excluded row or column.

mean_z property

Mean of the robustly-weighted residuals. Target: 0.

var_z property

Variance of the robustly-weighted residuals. Target: 1.

std_z property

Standard deviation of the robustly-weighted residuals. Target: 1.

chi2_red property

Reduced chi-squared, mean(W * r**2). Target: 1.

rmse property

Weighted RMSE, sqrt(chi2_red). Lower is better.

kl property

KL divergence of the z distribution from N(0, 1). Target: 0.

The paper's model-selection score (Eq. 20). See kl_from_moments(). Lower is better.

as_dict()

The scores as a plain dict, for tabulating over a (K, Q) grid.

Functions

robusta_hmf.kl_from_moments(mean_z, std_z)

KL divergence from N(mean_z, std_z^2) to N(0, 1). Target: 0.

The paper's model-selection score (Eq. 20), under a Gaussian approximation to the empirical distribution of the standardised residuals:

.. math:: \mathrm{KL} = -\log\sigma_z + (\sigma_z^2 + \mu_z^2) / 2 - 1/2

which is zero only for :math:\mu_z = 0, \sigma_z = 1 and positive otherwise. Lower is better.

robusta_hmf.z_scores(Y, W, A, G, likelihood)

Robustly-weighted standardised residuals.

Parameters:

Name Type Description Default
Y (Array, shape(N, M))

Data matrix.

required
W (Array, shape(N, M))

Data weights (inverse variance).

required
A (Array, shape(N, K))

Coefficients.

required
G (Array, shape(M, K))

Basis vectors.

required
likelihood Likelihood

Supplies the IRLS downweighting.

required

Returns:

Name Type Description
z (Array, shape(N, M))

z = (Y - A @ G.T) * sqrt(W * w_irls), which is unit normal under a calibrated model whose outliers are correctly downweighted.

robusta_hmf.z_moments(Y, W, A, G, likelihood)

Held-out moments for (A, G) on (Y, W), forming the full residual.

Parameters:

Name Type Description Default
Y (Array, shape(N, M))

Data and weights.

required
W (Array, shape(N, M))

Data and weights.

required
A (Array, shape(N, K))

Coefficients, e.g. from Robusta.infer().

required
G (Array, shape(M, K))

Basis vectors.

required
likelihood Likelihood

Supplies the IRLS downweighting.

required

Returns:

Name Type Description
moments ZMoments
See Also

robusta_hmf.sharded.ShardedFrame.z_moments : chunked, out-of-core route.

robusta_hmf.mad_z(Y, W, A, G, likelihood)

Median absolute z-score. Target: 0.6745 (the normal MAD).

Not available from ZMoments: a median needs the whole (N, M) z array, so this has no out-of-core equivalent.