"""Exact fixed-forecast sampling examples. CC0-1.0; no fleet observations.""" from fractions import Fraction from math import comb import json def probability(value): result = Fraction(value) if not 0 <= result <= 1: raise ValueError("Probability must lie in [0, 1]") return result def count_moments(forecast, n, positives): """Empirical terms for one identical-probability group.""" p = probability(forecast) if isinstance(n, bool) or not isinstance(n, int) or n < 1: raise ValueError("n must be a positive integer") if isinstance(positives, bool) or not isinstance(positives, int) or not 0 <= positives <= n: raise ValueError("positives must be an integer in [0, n]") rate = Fraction(positives, n) rel = (p - rate) ** 2 unc = rate * (1 - rate) return { "brier": (positives * (1-p)**2 + (n-positives) * p**2) / n, "reliability": rel, "resolution": Fraction(0), "uncertainty": unc, "corrected_reliability": rel - unc / (n-1) if n > 1 else None, } def expectations(forecast, event_probability, n, dependence="independent"): """Sum count probabilities exactly, not by Monte Carlo.""" p, q = probability(forecast), probability(event_probability) count_moments(p, n, 0) if dependence == "independent": counts = [(k, comb(n, k) * q**k * (1-q)**(n-k)) for k in range(n+1)] elif dependence == "identical-outcome": counts = [(0, 1-q), (n, q)] else: raise ValueError("Unknown dependence model") result = {key: Fraction(0) for key in count_moments(p, n, 0)} if n == 1: result["corrected_reliability"] = None for k, weight in counts: for key, value in count_moments(p, n, k).items(): if value is not None: result[key] += weight * value result["no_events"] = sum(weight for k, weight in counts if k == 0) return result def encode(value): return None if value is None else { "fraction": str(value), "decimal": float(value), } def report(): p = Fraction(1, 100) rows = [] for n in (20, 100): for dependence in ("independent", "identical-outcome"): values = expectations(p, p, n, dependence) rows.append({ "records": n, "independent_draws": n if dependence == "independent" else 1, "dependence": dependence, "forecast_probability": str(p), "event_probability": str(p), "expected": {key: encode(value) for key, value in values.items()}, }) return { "schema_version": 1, "kind": "exact pedagogical probability model; not measured drive data", "license": "CC0-1.0", "method": "Exact binomial count enumeration, or one Bernoulli outcome copied n times", "scope": "One fixed issued probability, known event probability and fully observed binary outcomes", "limits": "No censoring, fitted probabilities, changing horizons, real drive dependence or fleet inference", "reference": "https://empslocal.ex.ac.uk/people/staff/ferro/Publications/ferro-fricker2012copyright.pdf", "rows": rows, "negative_correction_control": { "forecast_probability": "1/2", "records": 2, "positives": 1, "values": {key: encode(value) for key, value in count_moments(Fraction(1, 2), 2, 1).items()}, }, } if __name__ == "__main__": print(json.dumps(report(), indent=2) + "\n", end="")