cjd_flows.evaluation¶
Latent-space diagnostics for flows with an Lp-radial base distribution.
RadialFlowEvaluator maps data to the latent space of a trained flow
and checks how well the latents match the radial structure of the base:
norm distribution: Wasserstein distance and Kolmogorov-Smirnov test between the latent norms
||z - loc||_pand the base norm law; QQ, PP and KDE plots;radiality: sign symmetry of the coordinates and uniformity of the normalized absolute latents on the simplex (L1 bases), binned directional uniformity, and a permutation HSIC test of norm/direction independence;
density alignment: scatter plots of the negative log-likelihood against latent norms, and of learned against reference log-probabilities.
Mismatches in the norm diagnostics tell over- from under-approximation of the density level sets (latent norms too large / too small compared to the base).
Works with v1 flows (Flow with a
RadialBase or StandardNormal base) and
with the pyro-based 0.9 flows (base_distribution and backward).
Needs the eval extra (pip install "cjd-flows[eval]").
- class cjd_flows.evaluation.RadialFlowEvaluator[source]¶
Bases:
objectDiagnostics of the latent radial structure of a trained flow.
- Parameters:
flow – trained flow. For a v1
Flowwith aRadialBaseorStandardNormalbase, and for 0.9 flows with aRadialDistributionbase,p, the norm distribution andlocare read from the flow.data (
Tensor) – data tensor(N, *event_shape)to evaluate on.device – device for the computations.
p (
Optional[float]) – norm exponent, required if the base is not radial (e.g. aRadialMixture); overrides the flow’s otherwise.norm_distribution – distribution of the latent norm (needs
sample;cdfis used if available), required withp.loc (
Optional[Tensor]) – latent centre, required if the base has none.
- wasserstein_norm_distance(n_samples=10000)[source]¶
Wasserstein-1 distance between the latent norms and the base norm law.
- ks_norm_statistic(n_samples=10000)[source]¶
Two-sample Kolmogorov-Smirnov test of latent norms vs. base norm samples.
Returns the scipy result (
statistic,pvalue).
- qq_plot_norms(ax=None, n_samples=10000)[source]¶
QQ plot of the latent norms against the base norm law.
- kde_plot_norms(ax=None, n_samples=10000, bandwidth=0.2)[source]¶
Kernel density estimates of the latent norms and the base norm law.
- pp_plot_norms(ax=None, n_samples=10000)[source]¶
PP plot: empirical CDF of the latent norms against the base norm CDF.
Uses
norm_distribution.cdfif available, otherwise a Monte-Carlo CDF.
- binned_uniformity_test(n_bins=10, n_reference=None)[source]¶
Chi-squared test of the binned directions
z / ||z||_pagainst an Lp-radial law.Every coordinate of the direction is binned into
n_binscells on[-1, 1]. The expected cell probabilities are those of the cone measure on the Lp unit sphere (the direction law of every Lp-radial distribution), estimated fromn_referenceexact simulations (defaultmax(100_000, 50 N)). Cells with an expected count below 5 are pooled, and the statistic is divided by1 + N / n_referenceto account for the Monte-Carlo error of the reference. Only meaningful for low dimensions (n_bins ** dimcells). Returns(chi2, p_value).
- hs_independence_test(n_permutations=1000)[source]¶
Permutation HSIC test of H0: latent norm and direction are independent.
Returns
(hsic, p_value).
- test_uniformity_simplex(alpha=0.05, method='energy', n_samples_ref=1000, n_boot=1000)[source]¶
Uniformity of
|z| / ||z||_1on the simplex (L1 bases only).- Parameters:
alpha – significance level.
method –
"energy"(energy distance) or"bhattacharyya"(KS distance of centred log-coordinates); both with p-values from simulated uniform samples.n_samples_ref – reference samples for the energy distance.
n_boot – simulation repetitions for the p-value.
- Returns:
(p_value, reject).
- test_sign_symmetry(alpha=0.05, method='sign', combine='stouffer')[source]¶
Per-coordinate sign symmetry of the latents (L1 bases only).
- Parameters:
alpha – significance level.
method –
"sign"(binomial) or"wilcoxon"(signed rank).combine –
"fisher","stouffer"(sign test only) orNone(Bonferroni).
- Returns:
dict with
p_values,reject,combined_pandmethod.
- test_l1_radial_symmetry(alpha=0.05, sign_method='wilcoxon', sign_combine='fisher', uniform_method='energy')[source]¶
Sign symmetry and simplex uniformity, each at level
alpha / 2(L1 bases only).- Returns:
dict with the sign and uniformity results and
l1_radial_rejected.
- nll_norm_scatter_plot(ref_distribution, ax=None, n_samples=10000)[source]¶
Scatter of the reference NLL against the latent norms of reference samples.
ref_distributionneedssampleandlog_prob(e.g. the true data distribution of a toy dataset). The legend reports Pearson, Spearman and Kendall correlations.