# Local certificates, abundant reactions and deletion outcomes

Example for **Abundant reactions in autocatalytic networks with locally ranked supplier cores**, manuscript entry 35, SHA-256 `3636b2e6c87ecf1bc38331bd6b2d2cb4c33cc63b2b90b5079048b1e7316ed7f4`.

A RAF is a nonempty reaction selection whose reactants can be made from food using that selection, and whose reactions each have a catalyst in the resulting closure. The paper supplies a local structural certificate guaranteeing that some reaction in a designated core belongs to at least half of the entire RAF family **with the empty set included**. No restriction is imposed on the surrounding reactions. The certificate identifies a set containing an abundant reaction, not which member it is.

This package provides a reusable candidate validator, literal food-closure model, exact family enumerator, exterior-fibre analyzer with the paper's counting injection, rational sampling-weight checks and deletion counts. It does not add kinetics to a structural theorem.

## Run and supply a network

Python 3.11 or newer:

```sh
python -m venv .venv
# Windows: .venv\Scripts\activate
# macOS/Linux: source .venv/bin/activate
python -m pip install -r requirements.txt
python -m unittest -v test_example.py
python example.py --output outputs
python example.py --input outputs/network_input.json --output custom
python example.py --input outputs/network_input.json --enumerate --output counted
```

The top of `example.py` exposes chain length, formula-sweep lengths, within-fibre distortion, exterior weights and computation budgets. The default has 12 reactions. Full enumeration is limited to 18 reactions; exceeding the budget raises an error and never yields a misleading partial family. The local validator needs no enumeration and can be used on larger systems. `--input` only validates unless `--enumerate` is supplied. The plotted large-chain sweep uses the paper's proved formula, not exhaustive enumeration of hundreds of reactions.

Minimal custom input:

```json
{
  "food": ["f"],
  "reactions": {
    "a": {"reactants": ["f"], "products": ["x"], "catalysts": ["y"]},
    "b": {"reactants": ["x"], "products": ["y"], "catalysts": ["f"]},
    "e": {"reactants": ["y"], "products": ["x"], "catalysts": ["f"]}
  },
  "core": ["a", "b"],
  "certificate": {
    "rank": {"a": 0, "b": 1},
    "supplier": {"a": "b", "b": "a"}
  }
}
```

The certificate field is optional: the validator can find a rank and supplier map for the given candidate. It checks an explicitly supplied certificate independently. Distinct reaction keys remain distinct even when their incidence data match. Reactants/products/catalysts are sets; catalysts are alternatives, not an AND complex. Empty catalyst sets cannot catalyse a selected reaction. Stoichiometric multiplicities and rates are outside this representation.

## What the certificate checks

For each core reaction r, one designated internal reaction d(r) must produce **all** of r's non-food reactants and a catalyst, unless food already catalyses r. A different producer for each requirement does not suffice. Every internal substrate-production incidence p -> r must increase rank. This checks all alternative producers and self-incidences, not only the supplier map. Catalytic cycles are allowed.

`CoreCertificate.validate` reads core incidence data and food, without food closure or exterior enumeration. `CoreValidator.propose` topologically sorts the internal substrate graph and searches for complete suppliers for an already supplied candidate. It does not discover all cores. A failed candidate does not imply that the system lacks RAFs or abundant reactions.

In the three-reaction input above, a and b form a valid core even though the ambient substrate graph contains b -> e -> b. Exterior selection {e} is not food-generated by itself, but {a,b,e} is a RAF. The exact family is empty, {a,b}, {a,b,e}; both core reactions occur twice out of three. The same certificate survives arbitrary exterior edits, food enlargement and added catalysis with core inputs/products preserved. The theorem's individually abundant witness may change after such edits.

## Reusable analysis components

`CatalyticSystem.closure_stages` implements synchronous food closure without consulting catalysis. `is_raf` then checks reactants and catalysts in that closure. `ExactFamily` uses these literal semantics to enumerate the original reaction subsets. It computes frequencies and irreducible RAFs without replacing the full family by unions of irreducibles.

`FibreAnalyzer` fixes an exterior selection T and examines every core subset S. Its exterior predicate checks reactions of T in the **combined closure H(S union T)**. Its local predicate asks whether each selected core reaction has producers for all non-food reactants and a catalyst. Under the validated rank, these predicates match literal RAF membership exactly. The analyzer checks that equivalence and the exterior predicate's monotonicity.

The counting certificate works with deleted core coordinates D=U minus S. In a bad row, a marked undeleted coordinate moves horizontally by adding its mark to D if another failure persists; otherwise it moves vertically to the failing reaction's designated supplier. The package records every move and checks that the target is a bad one-cell and no targets collide. The difference between bad one-cell and zero-cell counts equals the fibre occupancy slack 2*sum(|S|)-|U|*number_of_members. Empty fibres are retained. Each exterior completion is counted once, so summing fibres gives the correct original measure.

`WeightedAnalysis` accepts an exact rational weight function on family members. Weights constant within each exterior fibre retain the half bound. More general weights must have max/min ratio at most kappa within each fibre: an all-zero fibre is allowed, but a mixture of zero and positive weights in one fibre is rejected. Total weight must be positive. The guaranteed normalized core occupancy, and some reaction frequency, is at least 1/(1+kappa). Arbitrary member weights are not covered.

Example of composition in Python:

```python
from example import (strict_example, CoreValidator, ExactFamily,
                     FibreAnalyzer, WeightedAnalysis)

system = strict_example()
certificate = CoreValidator().certificate(system, {"a", "b"})
family = ExactFamily(system)
fibre = FibreAnalyzer(system, certificate).analyze({"e"})
weighted = WeightedAnalysis(family, certificate).analyze(
    lambda selected: 7 if "e" in selected else 1)
assert weighted["normalized_core_occupancy"] == "8/9"
```

## The three-chain construction

Each A, B or C chain consumes food. Its gate product catalyses the first reaction, and each later reaction receives its catalyst from its predecessor. The last A reaction produces xA and u; B produces u and v; C produces v and xC. Food-catalysed gates consume xA, u+v or xC. Each whole chain plus its gate is a certified core, with rank zero along the chain and one at the gate. No elementary RAF exists.

The extra feature is that A and C together can supply B's gate without finishing B's chain. At chain length ell, the family consists of the eight unions of whole cores and ell additional selections containing A, C, gB and a proper B prefix. Thus M=ell+8. Counting only unions of irreducible RAFs misses these extra selections and incorrectly reports uniform half-frequencies.

At ell=3, A/C reactions and all gates have frequency 7/11; b1, b2 and b3 have frequencies 6/11, 5/11 and 4/11. The B core average is exactly 1/2. Deleting b1, b2, b3 or gB leaves respectively 4, 5, 6 or 3 nonempty RAFs, with mean 9/2. The code separately enumerates each reduced system to verify the deletion identity N_after=N-f(r). This counts organizations; it does not measure product loss, flux, essentiality or viability. Repeated deletion requires a fresh certificate.

For every ell, the B core average stays at 1/2, while its gate frequency (ell+4)/(ell+8) tends to one and its last reaction's frequency 4/(ell+8) tends to zero. Three disjoint cores give three distinct abundant witnesses. The family is not a digraph support family: A and C are members, adding gB to either alone fails, but adding gB to their union succeeds because its two substrates have different producers.

The default exterior weighting 2^|T| keeps the B average at 1/2. Applying an additional factor three to below-half core selections produces average occupancy fraction 267/812, above the guaranteed 1/4. A separate two-member family, weighted three on empty and one on the whole core, attains exactly 1/4. These are explicit sampling choices, not probabilities derived from dynamics.

Adding an inert waste product to one-product chain reactions gives unit conservation with food mass two, zB mass two and all other molecules mass one. Every reaction balances and the enumerated RAF family stays unchanged. This is bookkeeping, not an experimentally supported or thermodynamically validated chemical realization.

## Evidence and limits

Seven test groups cover literal closures, missing suppliers, unseeded cycles, unchosen internal self-incidences, certificate transport, all family members for ell=3 and 4, counting injections, sharp weights and invalid sampling, every default deletion, the conservation lift, and 60 seeded random four-reaction systems. The clean package replays saved scientific JSON/CSV. Exact finite checks illustrate the paper's general theorem; they do not replace its proof. Lean is not rerun. The full problem of abundance for arbitrary RAF systems is not solved by this sufficient certificate.

Licensing: MIT is proposed for this example, pending owner confirmation; this statement does not grant a license. The manuscript retains its own terms.
