The Cartwright & Whitworth (2004) Q parameter quantifies the spatial
substructure of a stellar distribution as a single scalar derived from
the minimum spanning tree (MST). progenax computes Q at multiple stages —
during initial-condition validation, during dynamical evolution
diagnostics, and during fractal-substructure calibration
Goodwin & Whitworth, 2004Allison et al., 2009. The reference scipy implementation in
progenax.diagnostics.substructure.compute_q_parameter is correct but
non-differentiable, non-JIT-compatible, and in memory.
This chapter records the design choice — a kNN-based approximation as
the production path, with exact Borůvka MST available for cases that
demand it — and the rationale.
What CW04 Q measures¶
Given stellar positions projected onto a plane, the CW04 Q is
where is the total length of the 2D minimum spanning tree, is the convex-hull area, is the mean pairwise separation, and is the maximum distance from the centroid. The two normalisations and make dimensionless and -independent for self-similar distributions.
CW04 Q regimes.
Regime | value | Geometry |
|---|---|---|
Substructured | Fractal, clumpy; e.g. for Goodwin & Whitworth, 2004 | |
Uniform sphere | Reference baseline | |
Centrally concentrated | Power-law radial profile |
For dynamical N-body evolution, traces the rate at which initial substructure is erased by two-body relaxation and dynamical mixing Allison et al., 2009Küpper et al., 2011. Tracking as a function of crossing time is therefore a primary diagnostic of cluster violent-relaxation and tidal evolution.
Why MST is hard in JAX¶
A naive port of the scipy implementation to JAX runs into four fundamental obstructions:
Data-dependent control flow. Both Prim’s and Kruskal’s algorithms iterate until the tree completes, with the iteration count depending on edge values.
jax.lax.while_loopis not differentiable, and a fixed-iterationscanrequires a worst-case bound on the loop count.Union-find sequentiality. Cycle detection requires path-compressed disjoint-set operations, which are sequential by construction. JAX’s parallelism cannot be applied.
Discrete edge selection. “Include this edge in the tree” is a binary decision; the gradient of with respect to particle positions is a sum over a changing edge set, which yields piecewise-linear gradients with no useful smoothing.
Memory scaling. A naive distance matrix is in memory, exhausting GPU memory at .
MST algorithms ranked for JAX feasibility.
Algorithm | Parallelisable? | JAX feasibility | Notes |
|---|---|---|---|
Prim’s | No (sequential priority queue) | Poor | Cannot vmap; data-dependent extract-min |
Kruskal’s | No (sequential union-find) | Poor | Same union-find bottleneck |
Borůvka’s | Yes (phases parallel) | Partial | phases, each parallel; requires fixed-iteration |
Dual-tree Borůvka | Yes (Morton-coded) | Complex | Best asymptotic performance, hardest to write |
Borůvka’s algorithm is the only structurally JAX-friendly choice: each
phase does parallel edge comparisons, components halve
each phase, and the total depth is . Its weakness
is the per-phase union-find, which still has sequential bottlenecks
even with path compression replaced by scan-based merges.
The kNN-based approximation¶
The production path in progenax avoids the MST entirely and approximates from a -nearest-neighbour graph:
where is each particle’s distance to its single nearest neighbour and is a calibration factor determined empirically against the exact scipy MST on reference distributions. The factor of deduplicates edges seen from both endpoints.
The physical justification is that for clustered distributions, MST edges are dominated by nearest-neighbour distances within each cluster — the MST connects every point through its cheapest edges, and the cheapest edge from each point is its nearest neighbour modulo inter-cluster bridges. The bridges are the only edges captures that misses, and they enter at fixed cost per cluster (one bridge per cluster pair).
The kNN approximation therefore captures the intra-cluster topology exactly and the inter-cluster topology up to a small constant offset folded into . For the relative tracking use case — during dynamical evolution — the constant cancels.
Performance comparison: scipy MST vs. JAX kNN, single-snapshot .
Metric | scipy MST | JAX kNN | Speedup |
|---|---|---|---|
Single snapshot, (CPU) | ms | ms | |
JIT-compiled (CPU) | n/a | ms | |
GPU, | n/a | ms | |
100-snapshot time series | s (Python loop) | ms ( | |
Memory, | MB | MB | |
Differentiable | No | Partial (through distances) | — |
Differentiability is “partial” because the choice of nearest
neighbour is itself a discrete operation (argmin). The distance
through that neighbour is differentiable, but is undefined at the moment ’s nearest
neighbour changes. For HMC use this is acceptable as long as the
posterior does not concentrate near a NN-swap surface; in practice
this is rare for .
Implementation skeleton¶
The JAX-native implementation lives at progenax.diagnostics.q_jax
(planned; current location is progenax.diagnostics.substructure which
hosts the scipy reference). The pipeline has three stages:
@jax.jit
def compute_q_approx(positions, k=6, calibration=1.0, Nbins_per_dim=32):
xy = positions[:, :2] # 2D projection per CW04
grid = build_spatial_grid(xy, Nbins_per_dim) # Morton-coded bins
knn = compute_knn_graph(xy, grid, k=k) # vmap'd kNN
nn_d = knn.distances[:, 0] # Closest neighbour
L_approx = jnp.sum(nn_d) / 2
R = jnp.linalg.norm(xy - xy.mean(0), axis=1).max()
A = jnp.pi * R ** 2 # Circular hull approx
s_bar = mean_pairwise_separation(xy, R, max_pairs=10000)
return calibration * (L_approx / jnp.sqrt(xy.shape[0] * A)) / s_barThe circular-area approximation replaces the convex-hull computation with work and at most a bias on for non-spherical distributions. The bias is folded into during calibration.
For a time series , vectorisation over the time axis is one line:
@jax.jit
def compute_q_approx_timeseries(positions_t, k=6, calibration=1.0):
return jax.vmap(lambda x: compute_q_approx(x, k=k, calibration=calibration))(positions_t)This replaces a Python loop over scipy MST calls — the dominant cost in the existing diagnostic pipeline — with a single vmapped device call.
Calibration procedure¶
is determined by computing both (scipy MST) and (kNN) on a reference set of distributions spanning the physically relevant range of substructure: uniform spheres, fractals at Goodwin & Whitworth, 2004, and centrally-concentrated profiles at . A least-squares fit yields to better than 5% across the range. The calibration is -stable for , which spans the production regime.
When to use the exact Borůvka path¶
Three use cases require the exact MST rather than the kNN approximation:
Reproducing published Q values. Cross-checking a CW04 Q against Cartwright & Whitworth (2004) Table 1 or Allison et al. (2009) Figure 4 needs the exact algorithm.
Calibrating . The calibration procedure above requires the scipy MST; this is a one-time cost paid offline.
Survey-grade absolute Q. When the absolute value of enters a published quantitative claim, the kNN approximation’s bias is the limiting systematic.
For these, progenax retains the scipy reference implementation. A
JAX-native Borůvka MST is feasible (the parallel-phase structure maps
to fixed-iteration scan) but adds substantial code complexity for
improvement in the calibration regime. The current decision
is to defer Borůvka until a concrete use case requires it.
Connection to other substructure diagnostics¶
CW04 Q is one of three substructure metrics implemented in progenax:
Metric | What it measures | Status in progenax |
|---|---|---|
CW04 Q (this chapter) | MST + mean separation | scipy reference + JAX kNN approximation |
Azimuthal variation | Surface-density fluctuation | JAX-native; correlates with fractal dimension via Küpper et al., 2011 |
Local-density variance | Density-PDF dispersion | JAX-native; correlates with but not 1-to-1 |
Q and the azimuthal-variation metric agree on the trend with substructure but use different absolute scales. The fractal-substructure chapter (Fractal substructure) lays out the conversion table.
References¶
The CW04 Q definition follows Cartwright & Whitworth (2004). The kNN approximation is original to this work; the parallel Borůvka structure is standard (Cartwright & Whitworth (2004)’s reference list cites the 1926 original). The fractal calibration uses Goodwin & Whitworth (2004); the azimuthal-variation alternative uses Küpper et al. (2011).
- Cartwright, A., & Whitworth, A. P. (2004). The statistical analysis of star clusters. Monthly Notices of the Royal Astronomical Society, 348, 589–598. 10.1111/j.1365-2966.2004.07360.x
- Goodwin, S. P., & Whitworth, A. P. (2004). The dynamical evolution of fractal star clusters: The survival of substructure. Astronomy and Astrophysics, 413, 929–937. 10.1051/0004-6361:20031529
- Allison, R. J., Goodwin, S. P., Parker, R. J., Portegies Zwart, S. F., de Grijs, R., & Kouwenhoven, M. B. N. (2009). Using the minimum spanning tree to trace mass segregation. Monthly Notices of the Royal Astronomical Society, 395, 1449–1454. 10.1111/j.1365-2966.2009.14508.x
- Küpper, A. H. W., Maschberger, T., Kroupa, P., & Baumgardt, H. (2011). Mass segregation and fractal substructure in young massive clusters. Monthly Notices of the Royal Astronomical Society, 417, 2300–2317. 10.1111/j.1365-2966.2011.19412.x