Skip to content

Add nano joint preferential-discovery estimator to numerical library - #90

Merged
chian merged 5 commits into
mainfrom
feat/joint-preferential-discovery
Dec 4, 2026
Merged

chian merged 5 commits into
mainfrom
feat/joint-preferential-discovery

Conversation

@chian

@chian chian commented Nov 6, 2026 •

Copy link
Copy Markdown
Owner

What does this PR do?

Adds nano-graphrag's joint_preferential_discovery_v1 estimator and controller to OpenChia's reusable numerical function library, with two corrections to how its uncertainty is computed. Episodes can select the joint observation/discovery model and controller while preserving ordered attempts, identity deduplication, failures, exclusions, and independent histories for each Episode path.

The existing Chao2 estimator and approved Architecture bindings remain unchanged. The new model needs ordered history, which cannot be recovered from their anonymous incidence counts.

Changes Made

  • numeric_control_library/preferential_discovery.py is adapted from question_pipeline/utilities/rarefaction.py at nano-graphrag revision 60a167f9eeda0c9ea2ca294724174948947baf3f (source SHA256 02af1965…cdd35). It is no longer a byte-for-byte copy; see the next section.
  • Register rarefaction.joint_preferential_discovery_v1 and controller_composition.compose_preferential_controller; provide typed observations at the existing Grain.controller boundary.
  • Expose the definitions through EpisodeBuilder's reusable catalog and constructor admission, and document their selection and observation contract.

Correction to the uncertainty bands

The source labels its bands Laplace but takes their covariance from the BFGS search's working matrix. That matrix starts as the identity and is reset to it after any short step, which is every step near convergence. On simulated histories it was exactly the identity in 53 of 60 fits. The stop rule reads the upper bound that comes out of it.

Here the covariance is the inverse of the loss's curvature at the fitted parameters, measured by central differences after the search ends. The search keeps its matrix for the next warm start only. A curvature that is not positive definite reports the estimate as unidentifiable.

What this changes and what it does not:

  • Unchanged: the search. Against the fixture generated by the source program, every fitted parameter, point estimate, count and status code is the same.
  • Changed: the ends of the bands and the stop decisions read from them. See the next section for the numbers.
  • Marked: the estimator reports preferential_incidence_estimator_v3, and the notices, doc page and registered provenance state the difference.
  • Cost: about 20% more time per fit (29 → 36 ms at 50 attempts, 127 → 153 ms at 200).

The same defect is still in nano-graphrag at 60a167f.

Correction to the shape of the bands

With the covariance corrected, a band symmetric about the estimate was too narrow on the side the stop rule reads. On histories drawn from the model itself, the true next yield lay above the upper end in 13% to 21% of runs; 2.5% is intended.

The banded quantities cannot fall below a floor (zero, or the count already observed), so the normal approximation is now applied to the logarithm of the amount above that floor.

Regime, history length Truth above upper end: symmetric log scale importance-sampled posterior
Steady yield, 15 17% 3.3% 7.2%
Steady yield, 30 13% 3.3% 2.4%
Drying out fast, 15 16% 1.7% 0.8%
Drying out fast, 30 21% 1.7% 1.2%

250 to 300 simulated histories per cell. The sampled posterior is a check on the form, not something this PR ships.

This makes Episodes stop later. Upper bound on expected next discoveries over the last five attempts of the fixture's single-channel drying-out history:

  • source (identity-matrix covariance): 1.66, 0.37, 0.54, 0.35, 0.23, no stop within 13 attempts
  • corrected covariance, symmetric band: 0.87, 0.37, 0.23, 0.14, 0.09, stop at the twelfth attempt
  • corrected covariance, log-scale band (this PR): 4.85, 3.52, 1.96, 1.37, 1.05, no stop within 13 attempts

Thresholds chosen against the earlier bands will stop later. The band change is its own commit (74df8b2b12) and can be reverted separately from the covariance fix.

Also corrected: the emitter contract and doc page told generated code to call failed(reason, ...); the argument is note.

How to Test

scripts/run_tests.sh \
  tests/numeric_control_library/test_preferential_discovery.py \
  tests/episode_builder/test_preferential_catalog.py \
  tests/episode_builder/test_constructor_signatures.py \
  tests/episode_builder/test_emitter_import_surface.py \
  tests/episode_builder/test_fixed_numeric_bindings.py

32 passed, 0 failed on Linux x86-64, Python 3.14.7, through the canonical runner.

  • test_the_search_still_produces_what_the_source_program_does compares against the source-generated fixture, leaving out band ends, stop decisions and the version label.
  • test_band_covariance_inverts_the_curvature_of_the_fitted_loss multiplies the reported covariance by an independently measured curvature and requires the identity. Fails on the source file.
  • test_bands_do_not_depend_on_the_route_the_search_took fits one history step by step and in one go and requires the same bands. Fails on the source file (0.139 against 0.415).
  • test_a_band_spreads_by_the_same_ratio_on_both_sides_of_its_floor and test_the_true_next_yield_rarely_exceeds_the_upper_bound_the_stop_rule_reads (200 simulated histories, at most 10% above the upper end; 10 of 200 here, 38 with the symmetric band).

tests/numeric_control_library/ tests/episode_builder/ tests/function_library/ tests/method_loop/: 115 passed, 5 failed. The same five fail on main: two in test_repeatable_call_materialization.py, two in test_episode_tree_recursion.py, one in test_api_failure_continuation.py.

The original head of this PR failed its fixture test on Linux: the fixture is from macOS arm64, and the identity-matrix covariance amplified last-digit differences between hosts. With band ends out of the comparison, the Linux run agrees with the fixture to nine decimal places.

Known limits, not addressed here

  • Resume. A resumed root Episode can end differently from the same run uninterrupted, and resume pools history across an epoch transition. This is in method_loop/episode.py; this controller is the first to request a transition.
  • Planned Architectures cannot select this composer. The planner fixes the controller roles; it is reachable from hand-written Episodes through Grain.controller.
  • File size. The estimator file is about 2,900 lines, over the repository's 2,000-line guide.
  • Not validated: macOS and Windows with the correction, the async loop, Goal-proposal commits.

This transfers nano-graphrag's adaptation of a joint sampling/outcome model. It does not implement Diggle et al.'s spatial GP/Cox-process model.

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

૮ >ﻌ< ა ci review

running on 74df8b2 — fix(numeric): build bands on the log of the amount above the


Still running 17 jobs: Desktop core E2E / Desktop core E2E (Linux), Desktop update E2E / Desktop install/update E2E (Linux, app-update), Desktop update E2E / Desktop install/update E2E (Linux, build-fail), Desktop update E2E / Desktop install/update E2E (Linux, first-run), Desktop update E2E / Desktop install/update E2E (Linux, update-concurrency), OS-specific tests / Windows E2E (real processes), OS-specific tests / Windows install + update E2E / Windows install.ps1 + hermes update E2E, OS-specific tests / Windows-only tests, OS-specific tests / Windows-only tests (arm64), Python tests / Run tests, Python tests / e2e, Python tests / e2e-upgrade (core), Python tests / e2e-upgrade (git), Python tests / e2e-upgrade (handoff), Python tests / e2e-upgrade (hosts), Python tests / e2e-upgrade (network), Python tests / e2e-upgrade (pm)

❌ Job failures

OS-specific tests / macOS-only tests · View job

Job OS-specific tests / macOS-only tests failed.

chian and others added 4 commits December 3, 2026 21:18
The emitter contract and the doc page told generated code to call
failed(reason, ...) and excluded(reason, ...). The inherited
constructors take note, so failed(reason='x') raises TypeError.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The estimator labelled its bands Laplace but took their covariance from
the BFGS search's working matrix. That matrix starts as the identity and
is reset to it after any short step, which is every step near
convergence, so it described the route the search took and not how
sharply the history fixes the parameters. On simulated histories it was
exactly the identity in 53 of 60 fits, and the upper bound the stop rule
reads moved 0.87, 0.37, 0.23 with the correction against 1.66, 0.37,
0.54 without it on the same drying-out history.

The covariance is now the inverse of the loss's curvature at the fitted
parameters, measured by central differences after the search ends. The
search keeps its matrix for the next warm start only. A curvature that is
not positive definite reports the estimate as unidentifiable.

The search itself is untouched: against the fixture generated by the
source program, every fitted parameter, point estimate, count and status
code is unchanged. The ends of the bands and the stop decisions read from
them change, so the fixture test no longer compares those, and two tests
that fail on the previous file cover them: the covariance must invert an
independently measured curvature, and the bands must not depend on
whether the fit was reached in one step or many.

The file is therefore no longer a byte-for-byte copy of nano-graphrag's.
The notices, doc page and registered provenance say so, and the
estimator reports preferential_incidence_estimator_v3. The catalog test
now relates the reported version to the registered provenance instead of
repeating the string.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Conflicts with the value-of-computation estimator (#89) in README.md,
episode_builder/admission.py, episode_builder/planner.py and
numeric_control_library/__init__.py were additive; both sides are kept.
With the covariance corrected, the band symmetric about the estimate
proved too narrow on the side the stop rule reads. On histories drawn
from the model, the true next yield lay above the upper end in 13% to
21% of runs; 2.5% is intended.

The banded quantities cannot fall below a floor (zero, or the count
already observed), so the normal approximation now applies to the
logarithm of the amount above that floor. The true value then lies above
the upper end in about 2% to 5% of runs, in a steady regime and in a
fast-drying one. An importance-sampled posterior around the same fit
gave 1% to 7% and upper ends of similar size, so the wider upper end is
what the fitted model implies and not an artefact of the form.

The upper end is further from the estimate than before. On the
fixture's single-channel drying-out history it reads 4.85, 3.52, 1.96,
1.37, 1.05 over the last five attempts and no stop is reached within
the 13 attempts; with the symmetric band it read 0.87, 0.37, 0.23, 0.14,
0.09 and stopped at the twelfth. Thresholds chosen against the earlier
bands will stop later.

Two tests fail on the symmetric form: a band must spread by the same
ratio on both sides of its floor, and across 200 histories drawn from
the model the true next yield may exceed the reported upper end in at
most 10% (10 of 200 with this form, 38 with the symmetric one).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FfzyrvFGRVKR5LMMXbXonU
@chian
chian merged commit 6753815 into main Dec 4, 2026
33 of 52 checks passed
chian added a commit that referenced this pull request Dec 5, 2026
Both this branch and the preferential discovery estimator merged in #90
register an estimator in the same places and describe it in the README.
Keep both.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FfzyrvFGRVKR5LMMXbXonU
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant