# Effective approximation and finite-size RAF predictions

This package makes three different quantities explicit: finite catalytic RAF
probability P_n(p), finite static escape e_B(a), and infinite static survival S(a).
The useful exact calculations are runnable without constructing the enormous cap
in the general approximation theorem. The general evaluator reports a resource
limit when it cannot finish; it never substitutes a finite escape probability or
a sampled curve for the survival profile.

## Run

Python 3.11 was used. In a virtual environment:

```sh
python -m pip install -r requirements.txt
python example.py --output outputs
python -m unittest -v
```

All scientific inputs appear at the top. Defaults reproduce the paper's n=16,
lambda=1e-4 examples, exact small-openness certificates, depth-two history census,
and q=1/2 seed-cost example. These are structural random-network parameters, not
kinetic or biochemical calibrations. All probability bounds use integers and
`Fraction`; floating point is used for plot coordinates and explicitly approximate
cost displays. No random trials are needed.

## Components and reuse

- `PolymerCatalogue('sp'/'qt')` creates ordered split channels or their commuting
  quotient. Only opposite factor orders with the same concatenated product merge.
  Its primitive-word census computes channel counts without materializing the
  full network. At n=16 the counts are 1,835,012 and 1,834,798.
- `ReversibleClosure` implements ordinary type availability, both directions of
  every channel, and maximal-RAF deletion. One available word type suffices for a
  repeated factor; there are no molecular copy counts. One catalyst mark serves
  both directions, and a product may catalyze its own reaction.
- `ProductiveHistories` counts legal next channels and enumerates small history
  states. Deduplication preserves ordered-history multiplicities. Earlier open
  channels are settled, so they cannot trigger another cascade when a new channel
  is added. The current set is exactly the closure under the used channels.
- `FinitePrediction` calculates the exact singleton contribution, a Bonferroni
  lower bound, the two-coordinate remainder, and legal history-plus-short-gateway
  upper bounds. It chooses the smallest bound after including a gateway union
  bound and clipping at one. It enforces n>=4 and 2^(r+2)<=n for history predictions.
- `StaticField` evaluates an explicit finite independent-open subnetwork by exact
  adaptive recursion. Only fresh productive channels are queried; all previously
  open channels are settled. Memoized states consist of closure plus known closed
  channels. Exhausting its decision budget raises `ResourceLimit`, never zero.
- `RepairConstruction` appends each target letter to a record carrier, then
  cleaves off the whole target. All repair products are longer than the carrier,
  so these coordinates are outside its exposed prefix. Duplicates are removed.
- `RationalConversion` encloses 1-exp(-lambda) and -log(1-a) by series with explicit
  rational remainder bounds. It keeps intensity, catalytic p, and static a distinct.
- `EffectiveApproximation` handles endpoints exactly and first tries a useful
  history upper bound. If that gives an interval [0,upper] narrower than the
  requested tolerance, it returns it. Otherwise it computes an exact seed and a
  conservative record index, checks the cap and enumeration budgets, and applies
  the finite escape enclosure only if the computation fits. At a=1/2 it explicitly
  returns no interior estimate. Increasing budgets changes feasibility, not truth.

For example:

```python
from fractions import Fraction as Q
from example import PolymerCatalogue, FinitePrediction, ProductiveHistories
cat = PolymerCatalogue('qt')
model = FinitePrediction(cat, 32)
p = model.canonical_probability(Q(1, 100000))
prediction = model.bounds(p)
depth, upper = ProductiveHistories(cat).bound(Q(1, 100000))
```

`channels(n)`, `words(n)` and exhaustive history census grow exponentially; keep
their explicit caps small. Formula-based channel counts and finite probability
bounds are useful far beyond those literal enumerations. The configured census
checks states after zero, one and two steps, including all their next transitions;
it is not an exhaustive simulation of n=16 catalytic fields.

## Core calculations

There are 32/30 growth gateways and 36/34 food-inclusive RAF gateways. Four
food-only channels have six possible singleton catalysts each; each growth gateway
has seven, including its product. Their 248/234 witness coordinates are independent
even when they share a catalyst word, since channel identity is part of a mark.
Thus the singleton probability is exactly 1-(1-p)^A. Any RAF with no singleton
needs at least two positive molecule-channel coordinates. This proves the linear
finite-n behavior near p=0. It does not require a catalyzed first firing.

After j productive steps, available types number at most 6+2j, total word length
at most 2^(j+2)+6, and cleavage slots at most 4*2^j-j. Therefore the next branching
budget is b_j=(6+2j)^2+4*2^j-j, with the first-step count g=32 or 30. The history
coefficient C_r=g product(b_j, j=1..r-1) gives S(a)<=C_r*a^r. Histories are counted
before probabilities are assigned; independence applies to their distinct channel
identities, not to an unconditioned adaptive next mark.

Exact comparisons recover S(1e-3)<1e-5, S(1e-4)<1e-15,
S(1e-6)<1e-45 and S(1e-20)<1e-629 for both conventions. These are upper bounds,
not computed survival probabilities or matching asymptotics. The plot compares
them with the much larger earlier sparse-support envelope. The optimized bound is
only the minimum among the configured finite range of history depths.

For n=16 and each model's own p=lambda*n/R_n, exact arithmetic gives

```
2.16e-7 < split RAF probability    < 4.57e-5
2.04e-7 < quotient RAF probability < 4.31e-5
```

The saved fractions are the actual bounds; rounded float displays are not outward
endpoints. The quotient computed upper is about 4.29469e-5. Meanwhile
S(1-exp(-1e-4))<1e-15. The fixed finite model is not already a good approximation
to its critical-window limit. The unscaled probabilities in both orders of limits
vanish; their logarithmic rates differ. The curves show inequalities, not estimates
inside the finite interval and not the shape of the true infinite profile.

## The effective algorithm and its cost

The inherited contour constant is C*=2*7^64*81. The seed selector finds the least
k>=1 with C*(1-b^2)^k*(m*(m+1)+1)<=1, then sets L=10k. Here b=q for split channels
and b=q/2 for the quotient. It uses exact tests with exponential search and binary
refinement, guarded by index and integer-bit budgets. At q=1/2,m=100, the exact
seed lengths are 4830 and 21520. These describe the proof cost, not the probability.

The error contract is 0<=e_D(a)-S(a)<=1/m+(2^(L+1)-2)*(1-a^(L+1))^r,
where D=2^r*(L+2)-L. The code chooses m=ceil(4/tolerance)+2. Its record index is
conservative rather than the paper's least index: Bernoulli's inequality gives
(1-t)^r<=1/(1+r*t), so choosing

```
r >= (2^(L+1)/(tolerance/4)-1) / q^(L+1)
```

suffices. This number can be represented as an integer while the cap 2^r cannot
be materialized. The package records its bit length and stops before allocating
the cap. Separately, the approximate log2 of the paper's *least* record index is
about 4843 in the split example. The conservative implemented index is larger;
these quantities are labeled separately. Finite-record contraction does not
condition on eventual survival or assume independence between repair attempts.

The finite subnetwork demonstration uses the same repair channels and exact
enumeration; omitted channels are closed. It is not the full polymer law. For
the full static model, escape beyond food length two is exactly 1-(1-a)^g and is
implemented as an algebraic shortcut. This is still not S(a). The package does
not implement the paper's full inverse-probability or global-modulus algorithms;
their general evaluation bottleneck remains the same.

## Evidence and scope

Seven scientific test groups independently check literal catalogue counts, every
single catalytic coordinate at cap four, product self-catalysis and a two-channel
mutual RAF, history counts, finite inequalities/cap restrictions, finite-field
recursion against every Boolean configuration, repair/seed constants and explicit
budget failures, and rational conversions. Results, exact fractions, CSV curves,
figures, console output and hashes are included. The default run freshly computes
all evidence; it does not load precomputed success verdicts.

Universal supplied-seed mass, finite catalytic comparison, history coverage and
profile conclusions are imported from the manuscript and its companions. Exact
arithmetic does not independently prove their universal semantics. The paper's
total-length census, singleton semantics and regularity theorems have conventional
proofs, with the formal scopes distinguished in its appendix; this package does
not rerun Lean. Flatness and non-analyticity do not assert smoothness of all orders.

Independence is essential: one shared Bernoulli(a) mark opening all channels at
once has the same marginal openness but survival exactly a. Structural RAFs and
ordinary closure have no concentrations, fluxes, energy, degradation, inhibition
or finite-copy viability. No operational reactor interpretation is attached here.

## Provenance and license

Manuscript SHA-256:
`1d561295f36a0415f2aa64ea12e4a310e078a3fbbc3be17eb3b8122d5e388c45`.
The full PDF and the author's Critical_Window_Shape_Law_arxiv/check_paper.py were
read for equations, exact constants and source conventions. This is a standalone
implementation; the tests use independent small enumerations. Output hashes and
Python version are saved in run_metadata.json.

MIT is proposed for the example code pending the owner's license choice.
No license grant is made here; the manuscript retains its own terms.
