docs: add examples/polymarket_anomalies.py — prediction-market shocks#66
Conversation
Third worked example: pulls a Polymarket market's price history from the public Gamma + CLOB APIs (read-only, no key), enriches with per-step probability change, runs `anomalyx scan`, and maps findings back to UTC timestamps. Surfaces the information shocks — sharp probability jumps (point/mv) and sustained regime shifts in the odds (coll.cusum). The `timestamp` column is auto-classified a sequence (1.1.1) and skipped, so findings are about the odds, not the clock. Verified on a live market (MicroStrategy-sells-BTC, 648 hourly points): coll.cusum caught the 2026-05-20 regime shift (prob 0.345→0.133), mv.mahalanobis flagged the information-shock hours. Also fills in examples/README.md, which had only the stock example (the journal one from 1.1.1 was missing there). Docs/example only — outside the Cargo workspace, no build/gate/release impact. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
📝 WalkthroughWalkthroughThis PR adds a new worked example script that demonstrates anomaly detection on Polymarket price data. It includes the Python script implementation, accompanying documentation with usage examples, and a changelog entry documenting the feature. ChangesPolymarket Anomaly Detection Example
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
examples/polymarket_anomalies.py (1)
145-154: ⚡ Quick winClean up the temporary directory.
The script creates a temporary directory with
tempfile.mkdtemp()at line 145 but never removes it. For a short-lived example script this may be acceptable, but it does leak resources. Consider usingtempfile.TemporaryDirectory()as a context manager or explicitly callingshutil.rmtree(tmp)before exiting.♻️ Proposed fix using context manager
- tmp = tempfile.mkdtemp(prefix="anomalyx-polymarket-") - csv_path = os.path.join(tmp, "market.csv") - dates = write_csv(points, csv_path) + with tempfile.TemporaryDirectory(prefix="anomalyx-polymarket-") as tmp: + csv_path = os.path.join(tmp, "market.csv") + dates = write_csv(points, csv_path) - span = f"{points[0][0]} .. {points[-1][0]}" - print(f"# {question}") - print(f"# {len(points)} points, prob {points[0][1]:.3f} -> {points[-1][1]:.3f}\n") - env = anomalyx_scan(csv_path, scan_args) - report(env, dates) - sys.exit(0 if env["exit"] == 0 else 1) + span = f"{points[0][0]} .. {points[-1][0]}" + print(f"# {question}") + print(f"# {len(points)} points, prob {points[0][1]:.3f} -> {points[-1][1]:.3f}\n") + env = anomalyx_scan(csv_path, scan_args) + report(env, dates) + sys.exit(0 if env["exit"] == 0 else 1)🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@examples/polymarket_anomalies.py` around lines 145 - 154, The temporary directory created with tempfile.mkdtemp() (assigned to tmp) is never removed; wrap the block that writes csv_path, calls anomalyx_scan(env) and report(env, dates) and then exits in a tempfile.TemporaryDirectory() context manager (or ensure shutil.rmtree(tmp) is called before sys.exit) so the temp dir is cleaned up; update the code around tempfile.mkdtemp, csv_path, anomalyx_scan, report and sys.exit to use the context manager or explicit rmtree.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@examples/polymarket_anomalies.py`:
- Around line 145-154: The temporary directory created with tempfile.mkdtemp()
(assigned to tmp) is never removed; wrap the block that writes csv_path, calls
anomalyx_scan(env) and report(env, dates) and then exits in a
tempfile.TemporaryDirectory() context manager (or ensure shutil.rmtree(tmp) is
called before sys.exit) so the temp dir is cleaned up; update the code around
tempfile.mkdtemp, csv_path, anomalyx_scan, report and sys.exit to use the
context manager or explicit rmtree.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: f3bca295-264b-4924-a91b-97f54cd3f674
📒 Files selected for processing (3)
CHANGELOG.mdexamples/README.mdexamples/polymarket_anomalies.py
What
A third worked example: anomalyx on Polymarket prediction-market data. Pulls a market's price history from Polymarket's public APIs (Gamma for discovery, CLOB for the series — read-only, no key), enriches it with the per-step probability change, runs
anomalyx scan, and maps each finding back to its UTC timestamp.Why it's a good fit
A prediction market's implied probability is usually smooth, so a sudden jump is an information shock (news, a debate, a resolution). anomalyx catches:
point/mv.mahalanobis→ the sharp probability-jump hours;coll.cusum→ sustained regime shifts in the odds;timestampcolumn is auto-classified asequence(the 1.1.1 fix) and skipped, so findings are about the odds, not the clock advancing.Verified live on the MicroStrategy-sells-BTC market (648 hourly points, prob 0.110 → 0.004):
coll.cusumcaught the 2026‑05‑20 regime shift (mean 0.345 → 0.133),mv.mahalanobisflagged the information-shock hours (05‑11, 05‑15, 06‑01).Also
Fills in
examples/README.md, which had only the stock example — the journal example (shipped in 1.1.1) was missing there.Scope
Docs/example only — lives outside the Cargo workspace (shells out to the installed binary), so no build / gate / release impact.
🤖 Generated with Claude Code
Summary by CodeRabbit
Documentation
New Features