Skip to content

Native graph engine: degree assortativity without numpy (#3036) - #3047

Merged
squid-protocol merged 2 commits into
mainfrom
fix/3036-native-assortativity
Sep 14, 2026
Merged

squid-protocol merged 2 commits into
mainfrom
fix/3036-native-assortativity

Conversation

@squid-protocol

Copy link
Copy Markdown
Owner

Closes #3036. Part of #3033 (the native graph engine). Stacked on #3035's PR, which is stacked on #3044 (#3037): this branch carries their commits until they merge. Only the last commit (plus its bless, if any) is this PR's own.

What changed

  • graph_engine.degree_assortativity(index): one pass over the CSR out-runs, computing the Pearson correlation of (out-degree of source, in-degree of target) per edge.
    • Strict parity with networkx's default nx.degree_assortativity_coefficient(G) (x="out", y="in", unweighted).
    • Degrees are integers, so every sum (n, Σx, Σy, Σx², Σy², Σxy) is exact integer arithmetic. The only rounding is the final sqrt and division.
    • networkx instead sums floats over a normalised mixing matrix with numpy.
    • The result is NaN where the correlation is undefined, exactly as networkx returns: no edges, or a degree that never varies.
  • NetworkRiskSensor._topology_metrics() (renamed from Native graph engine: cyclic density (SCC) and articulation points in O(V+E) #3035's _cycle_metrics) runs it in both modes. It keeps the sensor's existing storage: NaN is stored as 0.0, and values are rounded to 4 dp. This replaces nx.degree_assortativity_coefficient.
  • Fixes the silent None with networkx but no numpy. networkx's assortativity imports numpy, which networkx does not install, so such a scan lost the metric while believing it ran at full precision. The native one needs neither. A new test runs the sensor with numpy blocked in sys.modules.

After this PR, modularity is the only macro metric networkx still computes (#3039).

Measured on language-crucible v1.2.0 (V=2,817, E=1,354; warm, best of 10, graph_parity.py --db)

metric parity native networkx
assortativity strict, max |diff| 2.2e-16 (−0.3052 both) 0.90 ms 11.07 ms

The prototype matched networkx at the stored 4 dp on all 300 seeded random graphs, including every edgeless graph (NaN in both, so 0.0 stored).

Golden masters

I diffed fresh scans of each mode against the committed fixtures with an uncapped golden_diff.deep_compare. The scans used GITGALAXY_DISABLE_GIT_HISTORY=1 and the same flags as test_golden_crucible.py.

  • Full precision: 0 differences. The native value equals networkx's at the stored 4 dp on the real corpus (−0.3033).
  • Zero dependency: 1 difference, intended. network_macro/assortativity went from None to −0.3033, the same value full precision reports.

The zero-dependency fixture is re-blessed in the second commit.

Observation, not changed here

Storing an undefined correlation as 0.0 is the pre-existing convention, and #3036 asks to keep it. It reads as "neutral mixing" rather than "not measurable", which is the same shape #3027 fixed for other metrics by using None. Changing it would change the definition and both fixtures, so it belongs in a follow-up if wanted.

Verification

  • pytest passes on the 9 affected suites (387 passed), run as plain pytest from outside the repo.
  • New tests:
    • NaN where the correlation is undefined
    • the sensor never calls nx.degree_assortativity_coefficient, and both modes agree
    • the sensor works with numpy blocked
    • the isolation test now fails modularity, the one metric networkx still computes
  • audit_check.py: all clear.
  • Docs updated: zero_dependency_mode.md (table, summary and heading; the "assortativity needs numpy" caveat is removed; older-snapshots note), the 02-16 wiki, README, CLAUDE.md, the console banner and the zero-dependency warning.

🤖 Generated with Claude Code

@squid-protocol squid-protocol added enhancement New feature, sensor, or structural signature metrics Heuristics, risk exposures, and topological math updates core-engine Modifications to the central physics and parsing engine labels Sep 14, 2026
squid-protocol and others added 2 commits September 14, 2026 18:55
- graph_engine.degree_assortativity(): single pass over the CSR out-runs
  computing the Pearson correlation of (out-degree of source, in-degree of
  target) per edge.
  - Strict parity with nx.degree_assortativity_coefficient(G).
  - Every sum is exact integer arithmetic, so the only rounding is the final
    sqrt and division.
  - NaN when undefined (no edges, or a degree that never varies), as
    networkx returns.
- NetworkRiskSensor._topology_metrics (was _cycle_metrics) runs it in BOTH
  modes, keeping the sensor's NaN -> 0.0 storage.
- This also fixes the silent None when networkx is installed without numpy:
  networkx's routine imports numpy, which networkx does not install.
- language-crucible: -0.3052 either way; 0.90 ms vs 11.07 ms (nx).
- Registered as strict in tests/tools/graph_parity.py. modularity is now
  the only macro metric networkx still computes. Docs updated.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
… now native)

Uncapped golden_diff scope: full precision 0 differences (crucible_check
--update: no drift); zero dependency exactly 1 -- network_macro/
assortativity None -> -0.3033, the value full precision reports.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@squid-protocol
squid-protocol force-pushed the fix/3036-native-assortativity branch from 8144d09 to 0644049 Compare September 14, 2026 22:57
@github-actions

Copy link
Copy Markdown
Contributor

🐦‍⬛ Muninn Security Scan

✅ No security issues found.

🐦‍⬛ Powered by Muninn · Skald Lab

@squid-protocol
squid-protocol merged commit 168259a into main Sep 14, 2026
29 checks passed
@squid-protocol
squid-protocol deleted the fix/3036-native-assortativity branch September 14, 2026 23:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core-engine Modifications to the central physics and parsing engine enhancement New feature, sensor, or structural signature metrics Heuristics, risk exposures, and topological math updates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Native graph engine: degree assortativity without numpy

1 participant