"""Finite independent-erasure arithmetic, not a measured drive-risk estimator.""" import argparse from decimal import Decimal, localcontext from fractions import Fraction import json from pathlib import Path ROOT = Path(__file__).resolve().parent def validate(members, codewords, probability, precision): if type(members) is not int or not 2 <= members <= 64: raise ValueError("members must be an integer from 2 to 64") if type(codewords) is not int or not 1 <= codewords <= 10**15: raise ValueError("codewords must be an integer from 1 to 10**15") if not isinstance(probability, Fraction) or not 0 <= probability <= 1: raise ValueError("probability must be an exact Fraction in [0, 1]") if 0 < min(probability, 1 - probability) < Fraction(1, 10**30): raise ValueError("probability is too close to an endpoint for this worksheet") if type(precision) is not int or not 80 <= precision <= 200: raise ValueError("precision must be an integer from 80 to 200") def calculate(members, codewords, probability, precision=100): """Condition on one missing member and no further whole-device failure.""" validate(members, codewords, probability, precision) with localcontext() as ctx: ctx.prec = precision one = Decimal(1) p = Decimal(probability.numerator) / Decimal(probability.denominator) if p == 0: p0, p1, bad_word = one, Decimal(0), Decimal(0) elif p == 1: p0, p1, bad_word = Decimal(0), Decimal(int(codewords == 1)), one else: log_good_word = Decimal(members - 1) * (one - p).ln() bad_word = one - log_good_word.exp() p0 = (Decimal(codewords) * log_good_word).exp() p1 = Decimal(codewords) * bad_word * ( Decimal(codewords - 1) * log_good_word ).exp() p_any = one - p0 expected_words = Decimal(codewords) * bad_word return { "p_zero": p0, "p_exactly_one": p1, "p_two_or_more": Decimal(0) if codewords == 1 else one - p0 - p1, "p_any": p_any, "expected_bad_codewords": expected_words, "expected_bad_codewords_given_any": ( expected_words / p_any if p_any else None ), "expected_bad_codeword_fraction": bad_word, "expected_missing_symbols": Decimal(codewords) * ( bad_word + Decimal(members - 1) * p ), } def decimal_string(value): return None if value is None else format(value, ".24g") def rational_record(value): return {"numerator": value.numerator, "denominator": value.denominator} def mission_summary(outcomes, usable_bytes): """Exact, mutually exclusive loss outcomes for one fixed mission.""" if type(usable_bytes) is not int or usable_bytes <= 0: raise ValueError("usable_bytes must be a positive integer") if not outcomes: raise ValueError("at least one outcome required") total = Fraction(0) p_any = Fraction(0) expected = Fraction(0) for probability, lost_bytes in outcomes: if not isinstance(probability, Fraction) or not 0 <= probability <= 1: raise ValueError("invalid outcome probability") if type(lost_bytes) is not int or not 0 <= lost_bytes <= usable_bytes: raise ValueError("loss must be integer bytes within mission capacity") total += probability expected += probability * lost_bytes if lost_bytes: p_any += probability if total != 1: raise ValueError("mutually exclusive outcome probabilities must sum to one") return { "p_any": p_any, "expected_lost_bytes": expected, "expected_fraction_lost": expected / usable_bytes, "expected_lost_bytes_given_any": expected / p_any if p_any else None, } def results(): members, codewords = 8, 10**12 // 512 rows = [] for x in [Fraction(0), Fraction(1, 100), Fraction(1, 10), Fraction(1), Fraction(3), Fraction(10)]: p = x / ((members - 1) * codewords) rows.append({ "expected_unreadable_survivor_symbols": str(x), "symbol_error_probability": rational_record(p), **{key: decimal_string(value) for key, value in calculate(members, codewords, p).items()}, }) missions = [] for name, outcomes in [ ("baseline", [(Fraction(999, 1000), 0), (Fraction(1, 1000), 10**9)]), ("additional_small_losses", [(Fraction(989, 1000), 0), (Fraction(1, 100), 512), (Fraction(1, 1000), 10**9)]), ]: missions.append({ "name": name, "outcomes": [{"probability": rational_record(p), "lost_bytes": n} for p, n in outcomes], "metrics": {k: rational_record(v) if v is not None else None for k, v in mission_summary(outcomes, 10**12).items()}, }) return { "schema_version": "1.0", "evidence_type": "deterministic mathematical source audit and synthetic examples", "members": members, "codewords": codewords, "capacity_convention": "1 TB = 10^12 bytes; 512-byte symbols", "decimal_precision": 100, "reported_significant_digits": 24, "conditioning": "one member missing; no additional device failure", "assumptions": [ "Every surviving symbol has the same independent unreadability probability.", "Errors are known erasures; readable survivors and parity are correct.", "All codewords are attempted; no abort-on-first-error policy.", "No clustered errors, retries, metadata amplification or application dependencies.", "No empirical error probability, confidence interval or annual risk estimate.", ], "rows": rows, "mission_examples": { "evidence_type": "invented mutually exclusive outcomes, not fleet data", "mission_years": 1, "usable_bytes": 10**12, "scope": "unique bytes of the initial dataset lost within this mission", "not_an_eafdl_recurrent_rate_model": True, "rows": missions, }, } def table_text(data): lines = [ "# Generated finite-model results", "", "One missing member; no further device failure; independent known erasures.", "The dimensionless x is expected unreadable survivor symbols, not a field rate.", "", "| x | P(no bad codeword) | P(exactly one) | P(two or more) | E(bad codewords) |", "|---|---:|---:|---:|---:|", ] for row in data["rows"]: values = [row["expected_unreadable_survivor_symbols"]] values.extend(format(Decimal(row[key]), ".8f") for key in ["p_zero", "p_exactly_one", "p_two_or_more", "expected_bad_codewords"]) lines.append("| " + " | ".join(values) + " |") return "\n".join(lines) + "\n" def generated_files(): data = results() return { "results.json": json.dumps(data, indent=2, ensure_ascii=True) + "\n", "table.md": table_text(data), } def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--write", action="store_true") parser.add_argument("--check", action="store_true") args = parser.parse_args() if args.write and args.check: parser.error("choose --write or --check") generated = generated_files() if args.write: for name, text in generated.items(): (ROOT / name).write_text(text, encoding="utf-8") elif args.check: for name, text in generated.items(): if (ROOT / name).read_text(encoding="utf-8") != text: raise SystemExit("generated output differs: " + name) print("Generated JSON and table match exactly.") else: print(generated["results.json"], end="") if __name__ == "__main__": main()