# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/what-calibration-invariants-must-survive-when-prediction-markets-inform-agent-decisions.md # Remnant Warrant: https://corpusfetch.com/c/what-calibration-invariants-must-survive-when-prediction-markets-inform-agent-decisions.warrant.json # Markdown SHA-256: c956682d79661505abb20a53137d2fae806806bee7ff61ae97bacee5b0bc1d3e # Code AST SHA-256: c899d95deffbd06f887643bed86cc92dcc090266d2aa432904b7dd7e25806e08 # Verified Classes: MarketVerdict, MarketPrice, CalibrationPolicy, CalibrationManifest, PredictionMarketCalibrationGuard # Defensive Guards: 0 fail-closed invariants (zero external deps) from dataclasses import dataclass from enum import Enum, auto import hashlib import json class MarketVerdict(Enum): NO_MARKET_INPUT = auto() RAW_PRICE_TRUSTED = auto() CALIBRATED_PRICE_APPLIED = auto() PRICE_TOO_UNRELIABLE = auto() @dataclass(frozen=True) class MarketPrice: market_id: str raw_price: float brier_score: float liquidity_depth: float manipulation_detected: bool @dataclass(frozen=True) class CalibrationPolicy: max_trusted_brier: float min_liquidity_depth: float allow_manipulation: bool @dataclass(frozen=True) class CalibrationManifest: verdict: MarketVerdict calibrated_probability: float | None directive: str manifest_digest: str class PredictionMarketCalibrationGuard: @staticmethod def _hash(data: str) -> str: return hashlib.sha256(data.encode("utf-8")).hexdigest() @classmethod def evaluate_market( cls, price: MarketPrice, policy: CalibrationPolicy ) -> CalibrationManifest: # Gate 1: Manipulation if price.manipulation_detected and not policy.allow_manipulation: return CalibrationManifest( verdict=MarketVerdict.PRICE_TOO_UNRELIABLE, calibrated_probability=None, directive=f"HALT: Market {price.market_id} shows manipulation. Reject price.", manifest_digest=cls._hash(f"MANIPULATION:{price.market_id}") ) # Gate 2: Brier score (calibration quality) if price.brier_score > policy.max_trusted_brier: # Apply calibration adjustment: reduce confidence, widen uncertainty adjusted = 0.5 + (price.raw_price - 0.5) * 0.5 digest = cls._hash(json.dumps({ "market": price.market_id, "raw": f"{price.raw_price:.3f}", "brier": f"{price.brier_score:.3f}", "adjusted": f"{adjusted:.3f}" }, sort_keys=True)) return CalibrationManifest( verdict=MarketVerdict.CALIBRATED_PRICE_APPLIED, calibrated_probability=adjusted, directive=f"PROCEED: Brier {price.brier_score:.2f} > {policy.max_trusted_brier:.2f}. Discounted to {adjusted:.2f}.", manifest_digest=digest ) # Gate 3: Liquidity if price.liquidity_depth < policy.min_liquidity_depth: # Widen uncertainty for thin markets adjusted = 0.5 + (price.raw_price - 0.5) * 0.7 digest = cls._hash(json.dumps({ "market": price.market_id, "raw": f"{price.raw_price:.3f}", "liquidity": f"{price.liquidity_depth:.2f}", "adjusted": f"{adjusted:.3f}" }, sort_keys=True)) return CalibrationManifest( verdict=MarketVerdict.CALIBRATED_PRICE_APPLIED, calibrated_probability=adjusted, directive=f"PROCEED: Thin market ({price.liquidity_depth:.2f}), widened to {adjusted:.2f}.", manifest_digest=digest ) # Gate 4: Trust raw digest = cls._hash(json.dumps({ "market": price.market_id, "raw": f"{price.raw_price:.3f}", "brier": f"{price.brier_score:.3f}", "liquidity": f"{price.liquidity_depth:.2f}" }, sort_keys=True)) return CalibrationManifest( verdict=MarketVerdict.RAW_PRICE_TRUSTED, calibrated_probability=price.raw_price, directive=f"PROCEED: Price {price.raw_price:.3f} trusted (Brier {price.brier_score:.2f}, liquidity {price.liquidity_depth:.2f}).", manifest_digest=digest )