"""Recompute a retained observation with Python's standard library only. Usage: python verify.py observation.json Uses the supplied fixed-finish candidate population and parsed memberships. Checks endpoint exclusions, benchmark, coverage, medians, gaps, breadth and the $5 sensitivity check. It cannot validate upstream prices, catalogue completeness, name parsing, transaction recency or future returns. """ import json import datetime as dt import math import statistics import sys from collections import Counter def verify(observation): def equal(actual, expected, label): if expected is None: if actual is not None: raise ValueError(label + ": expected no value") elif not isinstance(actual, (int, float)) or not math.isclose(actual, expected, rel_tol=1e-10, abs_tol=1e-10): raise ValueError(f"{label}: {actual!r} != {expected!r}") def positive(value): return value is not None and math.isfinite(value) and value > 0 def trusted(row, prefix=""): price, low, mid = (row.get(prefix + k) for k in ("market", "low", "mid")) same = low is not None and mid is not None and abs(low - mid) <= 1e-8 + 1e-9 * abs(mid) return positive(price) and not same and not (mid is not None and mid / price > 3) def reason(row, method): if not positive(row["market"]): return "no_current" if not positive(row["start_market"]): return "missing_baseline" run, mid = row.get("unchanged_days"), row.get("mid") stale = (run is not None and run > method["unchanged_run_limit_days"] and positive(mid) and mid < method["stale_ask_ratio"] * row["market"]) if not trusted(row) or stale: return "unreliable_current" if not trusted(row, "start_"): return "unreliable_baseline" if row["start_market"] < method["baseline_floor_usd"]: return "below_floor" return "" def move(rows): return statistics.median([r["market"] / r["start_market"] - 1 for r in rows]) if rows else None method = observation["method"] checked = 0 seen_windows = set() for window in observation["windows"]: label = str(window["days"]) + "d" if window["days"] not in (30, 90) or window["days"] in seen_windows: raise ValueError("Unsupported or repeated comparison window") seen_windows.add(window["days"]) end = dt.date.fromisoformat(observation["as_of"]) if window["end_on"] != str(end) or window["start_on"] != str(end - dt.timedelta(days=window["days"])): raise ValueError(label + ": endpoint dates differ") candidates = observation["evidence"][str(window["days"])] if len({r["product_id"] for r in candidates}) != len(candidates): raise ValueError(label + ": repeated product votes") for row in candidates: if row["reason"] != reason(row, method): raise ValueError(label + f': exclusion differs for product {row["product_id"]}') good = [r for r in candidates if not r["reason"]] benchmark = move(good) equal(window["benchmark"]["n"], len(good), label + " benchmark count") equal(window["benchmark"]["median_return"], benchmark, label + " benchmark median") floor = method["sensitivity_floor_usd"] higher = [r for r in good if r["start_market"] >= floor] higher_benchmark = move(higher) equal(window["benchmark"]["higher_floor"]["n"], len(higher), label + " higher benchmark count") equal(window["benchmark"]["higher_floor"]["median_return"], higher_benchmark, label + " higher benchmark median") by_name, population = {}, Counter() for row in candidates: for name in row["members"]: population[name] += 1 if not row["reason"]: by_name.setdefault(name, []).append(row) qualified = {name for name, rows in by_name.items() if len(rows) >= method["minimum_cards"] and len({r["group_id"] for r in rows if r["group_id"] is not None}) >= method["minimum_sets"]} if {r["name"] for r in window["rows"]} != qualified: raise ValueError(label + ": qualifying population differs") for summary in window["rows"]: name = summary["name"] rows = by_name[name] median = move(rows) equal(summary["n"], len(rows), name + " card count") equal(summary["candidate_n"], population[name], name + " catalogue count") equal(summary["n_sets"], len({r["group_id"] for r in rows if r["group_id"] is not None}), name + " set count") equal(summary["median_return"], median, name + " median") equal(summary["gap_pp"], (median - benchmark) * 100, name + " gap") equal(summary["above_market_n"], sum(r["market"] / r["start_market"] - 1 > benchmark + 1e-9 for r in rows), name + " breadth") equal(summary["team_n"], sum(bool(r["is_team"]) for r in rows), name + " team count") subset = [r for r in rows if r["start_market"] >= floor] h = summary["higher_floor"] equal(h["n"], len(subset), name + " higher count") equal(h["n_sets"], len({r["group_id"] for r in subset if r["group_id"] is not None}), name + " higher set count") equal(h["median_return"], move(subset), name + " higher median") equal(h["benchmark_return"], higher_benchmark, name + " higher benchmark") checked += 1 return {"as_of": observation["as_of"], "comparison_rows_verified": checked, "windows_verified": len(observation["windows"])} if __name__ == "__main__": with open(sys.argv[1], encoding="utf-8") as source: print(json.dumps(verify(json.load(source))))