Implementations
A small Python library, mirrored by parity-tested JavaScript ports.
Every mechanism has a Python reference implementation in the
mechanisms package — dependency-light (numpy only)
and written to be read. The core deterministic mechanisms additionally have
JavaScript ports that power the browser demos and
are checked for numerical parity against Python in the test suite.
Python package
pip install -e ".[test]"
| Module | Mechanism |
|---|---|
| scoring_rules | Log, Brier, spherical; pinball & interval; CRPS; energy score |
| parimutuel | Pool betting; dynamic parimutuel market |
| lmsr | Logarithmic market scoring rule |
| cmm | Generic convex cost-function market maker |
| amm | Constant-product / constant-mean CFMMs; impermanent loss |
| pm_amm | Paradigm's pm-AMM (Gaussian score dynamics) |
| cda | Continuous double auction / limit order book |
| fba | Frequent batch auction (uniform-price clearing) |
| perp | Perpetual futures funding & liquidation |
| peer_prediction | Bayesian Truth Serum; output agreement |
| aggregation | Linear / logarithmic / depth-trimmed opinion pools |
| calibration | Reliability diagram, ECE, Brier decomposition |
JavaScript ports, tested against Python
The browser demos must not quietly drift from the library, so the JavaScript
in docs/js/mechanisms.js is a faithful port of the Python
reference and is verified against it: tests/test_js_parity.py
runs each JS function in a Node subprocess (via
tests/js_parity_runner.js) and asserts the result equals the
Python implementation to floating-point tolerance. The parity tests skip
cleanly if node is not installed, so Python-only CI is
unaffected. (Pattern borrowed from
microprediction/humpday,
where JS optimizers are validated against their Python counterparts.)
# Python reference, JS port, and the parity check
pytest -q # full suite (Python + JS parity)
node tests/js_parity_runner.js lmsrPrices '[[10,-5,3],50]'
Run the tour
python examples/quickstart.py
Example simulations
Each demo in examples/ drives one mechanism through a short
Monte-Carlo story and prints the result as a terminal chart
(numpy-only, no plotting dependencies), so they run anywhere.
sim_scoring_rules.py— honest reporting beats a confident misreport under the log, Brier & spherical rules: what strictly proper means.sim_lmsr.py— informed traders move an LMSR market to the true probability while the maker's loss stays underb·log n.sim_parimutuel.py— a pool aggregates noisy private beliefs, then reproduces the favourite–longshot bias.sim_cda.py— zero-intelligence traders drive a continuous double auction to competitive equilibrium (Gode & Sunder, 1993).sim_amm.py— a constant-product LP's fees raced against impermanent loss under a random-walk price.sim_perp.py— the funding rate tethers a perpetual to its index; a 5× leveraged long gets liquidated.sim_pm_amm.py— the pm-AMM prices a binary market as the Gaussian CDFΦ((y−x)/L): always a probability, outcomes summing to 1.sim_fba.py— a frequent batch auction clears everyone at one uniform price at the equilibrium volume (Budish, Cramton & Shim, 2015).sim_peer_prediction.py— the Bayesian Truth Serum out-scores a strategist with no ground truth (Prelec, 2004).sim_aggregation.py— linear vs logarithmic vs depth-trimmed opinion pools under confidently-wrong nodes — trimming wins.sim_cmm.py— one convex potential subsumes LMSR (and a quadratic maker); the finite-difference price fallback matches the analytic gradient.sim_calibration.py— diagnose an overconfident forecaster (reliability diagram, ECE, Brier decomposition) and fix it with temperature recalibration.
python examples/sim_lmsr.py