# Randomized stopped lineage identification

Example for manuscript 75, SHA-256 `a7b2582bd9ee8a5baac720ca0cbb02720cca9660490dc7c9ac944b2cd9ad22fe`.

Resistant outgrowth can reflect initial composition, selective survival, within-cell switching or daughter production. This example constructs a branching source, derives its stopped observation law, recovers its separate mechanisms, and calculates finite-sample switching intervals. It then shows why the joint daughter kernel matters: changing sister dependence can change extinction while leaving every mean trajectory unchanged.

## Run and change inputs

Python 3.11 was tested:

```sh
python -m venv .venv
# Activate the environment for your shell.
python -m pip install -r requirements.txt
python example.py
python -m unittest -v
python example.py --output replay
```

Inputs at the top of `example.py` specify the synthetic source, marker calibration, independent deadline rate, daughter delays, founder/calibration counts, seed, feature-bias reserve and prediction horizon. Rates are per hour; times are hours. The defaults are the paper's synthetic values, not measured biological parameters. Use rational strings for exact immediate-read calculations. The prospective class is specified separately in `prospective_boxes.py`; changing the synthetic source does not silently change that class.

## The source and record

`BranchingSource` accepts any finite number of latent states, positive preparation probabilities, nonnegative switching/division/death rates, and an ordered sister-pair kernel with one probability row per mother state. Its killed generator is `H = Q - diag(b+d)`. Its full exit matrix has one death column followed by `m*m` division-pair columns. Division replaces the mother; it does not add two daughters while keeping her alive. The mean generator counts both daughter marginals, so ordered asymmetric kernels are allowed as well as exchange-symmetric ones.

`MarkerChannel` supplies a column-stochastic full-column-rank observation matrix, including rectangular markers. Each column corresponds to the **same anchored latent state** in each arm. Calibration errors are independent conditional on the complete latent history and the reporter is assumed nonperturbing. `known_mixture_calibration` recovers the marker from independently known preparation mixtures at readout; it does not validate mixtures defined using the uncalibrated marker itself.

`RandomDeadline` follows a founder until an independently sampled exponential deadline or its first division/death. Hidden switches do not stop follow-up. Initial markers and **all** outcomes are retained. Immediate binary-marker data have 14 categories; with monitored daughter delay and interruption flags there are 24. `law` constructs the entire unconditioned law, which can be sampled directly as a multinomial for independent founders.

```python
from lineage import BranchingSource, MarkerChannel, RandomDeadline

source = BranchingSource(
    ['.65','.35'], [[0,'.05'],['.07',0]],
    ['.22','.16'], ['.12','.04'],
    [['.62','.12','.12','.14'],['.10','.10','.10','.70']])
marker = MarkerChannel([['.9','.15'],['.1','.85']])
design = RandomDeadline('1')
record = design.law(source, marker)
recovered = design.recover(record['law'], marker)
assert recovered['killed'] == source.killed
```

For immediate reads, rational arithmetic implements `A = lambda*(lambda*I-H)^(-1)` and `H = lambda*(I-A^(-1))`. The full marker-cancelled inverse recovers preparation and exit masses as well. It does not invert `H`, take a matrix logarithm or assume diagonalizability. Demonstrations include a three-state cycle with complex spectrum, a rectangular channel, a reducible singular generator, and zero division. Where division is zero, the returned sister row is `None`: the unused kernel is not identified.

The delayed model first recovers `H` from the unaffected founder deadline block, then builds the daughter channel from `exp(tau*H)` with an extra interruption row. Its tensor square acts on the **joint** birth-state kernel. This calculation and its inverse use floating linear algebra, record numerical residuals, and reject numerically unresolved rank. Full structural rank at finite delay does not guarantee useful precision. No inference discards interrupted daughters or conditions on both surviving.

`recover` is an inverse of a **population law**, not a constrained estimator for noisy empirical frequencies. It rejects incompatible negative rates rather than clipping them to a plausible model. Use the finite-sample layer for switching uncertainty. A full finite-data confidence region for every rate and sister kernel is not implemented.

## Inference from your own counts

`FounderCounts` needs six counts: deadline records `j00,j01,j10,j11`, then demographic-exit totals for initial marks zero and one. Here demographic exit means **death or division**. The initial-marker-one feature includes both deadline and demographic records. `from_full` coarsens a 2-by-7 or 2-by-12 table without losing any feature used by the switching target.

```python
from inference import FounderCounts, CalibrationCounts, InductionInference

control = FounderCounts((19497,4654,4270,10288), (7826,3465))
treated = FounderCounts((13937,10895,3754,11178), (6926,3310))
calibration = CalibrationCounts((10211,85049), 100000)
result = InductionInference().contrast(control, treated, calibration)
print(result['display'])  # approximately [0.578259, 1.091585] per hour
```

The inference routine receives counts, calibration and the deadline rate; it receives no demographic nuisance rates, preparation fractions or sister kernel. It encloses ten founder features with error parameter `25/4` and two calibration features with parameter `6`. Their joint union-bound event has probability above 0.95 under the declared model and independent experimental units. Features within a founder may overlap; no independence between those features is assumed.

Both entrywise matrix inversion and the paper's determinant-cancelled target are included, using either Hoeffding or Chernoff–KL feature intervals. The same data reproduce all four routes. The combined improvement changes approximately `[0.362376,1.497818]` to `[0.578259,1.091585]`, with no new data or additional confidence claim. The cancelled formula is narrower on these data, not universally promised to be narrower.

Each KL endpoint starts as a floating proposal and is then verified using **rational logarithm bounds**. `rational_endpoint_audit.json` records nonnegative margins for the actual condition `n*KL_lower >= L`. The reusable inference layer repeats this check independently of the helper's return value. If the marker-contrast or determinant interval reaches zero, the arm returns `[0,infinity)` and the contrast returns an unbounded unresolved interval. An entirely negative switching interval produces a model-conflict flag, not a clipped zero estimate. The small-sample example exercises the unresolved path.

For a justified per-feature bias, set `feature_bias`; founder intervals widen before the target is evaluated. A movie cap has total-variation reserve at most `exp(-lambda*cap)`. Its exact capped kernel is **not** the resolvent. The reported cap reserve is a numerical display, and setting a bias reserve is the user's explicit modeling choice; the driver does not silently treat the capped experiment as uncapped. Repeated truncations of one trace or cells sharing a well deadline are not additional independent founders.

## Prospective design and inheritance consequences

The prospective calculation encloses the paper's continuous rate/preparation boxes, including the **entire** control switching interval. At 150,000 founders per arm and 150,000 calibration reads per state, the improved route encloses all null intervals inside approximately `[-0.144829,0.144829]` and all alternative intervals inside `[0.407270,1.313568]`, separated by threshold 0.4. At 140,000 it does not certify separation. The older route certifies at one million per arm/state. These are sufficient budgets for a narrow declared class, not optimal or recommended sample sizes.

`BranchingSource.population` solves the full nonlinear branching PGF; setting `z=0` gives extinction by time. It also calculates the mean trajectory independently from the matrix exponential. `concordance_shift` changes two-state kernels by `epsilon*(1,-1,-1,1)`, preserving marginals. Exact arithmetic verifies the unchanged mean generator and offspring polynomial difference `epsilon*(xS-xT)^2`. Numerical ODEs reproduce the small baseline and larger synthetic extinction effects. The ordering comes from the paper's comparison proof; the displayed effect sizes are not interval-certified. State-independent division/death is a possible zero-effect case. Predictions require the fitted source to remain valid over the prediction horizon.

## Source corrections, evidence and scope

`source_provenance.json` records the source hashes and modifications. Three corrections are deliberate:

1. The supplied KL helper checked `n*KL_lower >= L/n`. It now checks `KL_lower >= L/n`, and the inference layer separately verifies every endpoint. The default numerical interval is unchanged.
2. An entirely negative target enclosure now raises a model-conflict signal instead of returning zero.
3. The prospective control range is enclosed as a whole interval. Two endpoint evaluations alone are not used as a substitute for a monotonicity proof. This slightly widens the paper's printed prospective bounds while preserving its 150,000-per-arm sufficient result.

The exact rational immediate inverse, KL endpoint margins, cancelled target, finite feature bounds, mixture calibration and offspring identities are separated from numerical delayed channels and PGF trajectories. JSON/CSV outputs retain the inputs and results; PNG/SVG figures explain inference precision and the extinction consequence. Tests independently exercise resolvent identities, exact source recovery, delayed pair recovery, rank/preparation failures, endpoint certification, unresolved/model-conflict outcomes, prospective boxes and identical means with different extinction.

No biological data are fitted. Constant hazards, calibrated state-conditional markers, independent roots, complete event records and aligned state labels are substantive assumptions. A two-rate Erlang example shows how incompatible apparent birth rates can falsify a homogeneous source; passing finitely many deadline checks does not prove Markov behavior. The normalized-mixture joint confidence projection and the paper's 300-repetition Monte Carlo diagnostic are not implemented here; neither is needed for the stated exact-feature coverage construction. Lean is not rerun, and the stochastic theorems are not described as machine verified.

## Reuse permission

MIT is proposed for the new example code, pending the owner's license decision. This note is not a license grant. Source-derived helpers are identified in the provenance file.
