Logarithmic Market Scoring Rule — A standalone implementation for prediction markets.
The Logarithmic Market Scoring Rule (Hanson, 2003) is an automated market maker (AMM) designed specifically for prediction markets. Unlike traditional order books, LMSR provides infinite liquidity — any trade can execute at any time, with the price adjusting smoothly based on demand.
It powers platforms like Polymarket, where you trade on the probability of real-world events.
The cost function determines the total amount of money held by the market maker given outstanding share quantities q. The liquidity parameter b controls how sensitive prices are to trades.
The worst-case loss for the market maker, where n is the number of outcomes. This is bounded and known upfront — a key advantage of LMSR over traditional market making.
Prices are the partial derivatives of the cost function — they form a proper probability distribution (sum to 1, all positive). This is identical to the softmax function used in machine learning.
The cost of any trade is simply the difference in the cost function before and after.
This module is one component of a larger quantitative trading system:
┌─────────────────────────────────────────────────────────────┐
│ TRADING SYSTEM │
│ │
│ ┌──────────┐ ┌──────────────┐ ┌───────────────────┐ │
│ │ Market │──>│ Signal │──>│ Risk Manager │ │
│ │ Scanner │ │ Engine │ │ (Position Sizing)│ │
│ └──────────┘ └──────────────┘ └───────────────────┘ │
│ │ │ │ │
│ v v v │
│ ┌──────────┐ ┌──────────────┐ ┌───────────────────┐ │
│ │ Data │ │ ★ LMSR │──>│ Execution │ │
│ │ Feeds │ │ Engine ★ │ │ Engine │ │
│ └──────────┘ └──────────────┘ └───────────────────┘ │
│ │ │ │
│ v v │
│ ┌──────────────┐ ┌───────────────────┐ │
│ │ Inefficiency │ │ Monitoring & │ │
│ │ Detection │ │ Trade Journal │ │
│ └──────────────┘ └───────────────────┘ │
└─────────────────────────────────────────────────────────────┘
This repo contains the starred (★) component — the LMSR pricing engine with inefficiency detection. The rest of the system is proprietary.
pip install -r requirements.txt
python examples.pyfrom lmsr import LMSREngine, MarketState
import numpy as np
# Create engine with liquidity parameter b=100
engine = LMSREngine(b=100.0, fee_rate=0.02)
# Binary market (YES/NO)
state = MarketState.binary(q_yes=0.0, q_no=0.0, b=100.0)
# Get current prices
prices = engine.prices(state.quantities)
print(f"YES: {prices[0]:.4f}, NO: {prices[1]:.4f}")
# Output: YES: 0.5000, NO: 0.5000
# Quote a trade: buy 10 YES shares
quote = engine.quote_trade(state, outcome=0, delta=10)
print(f"Cost: ${quote.cost:.4f}, Slippage: {quote.slippage:.6f}")
# Detect inefficiencies
signals = engine.detect_inefficiency(
market_id="example",
market_prices=np.array([0.60, 0.40]),
estimated_probs=np.array([0.72, 0.28]),
confidences=np.array([0.85, 0.85]),
)
for s in signals:
print(f"{s.side} outcome {s.outcome_index}: edge={s.edge:+.2%}")- Log-sum-exp trick throughout for numerical stability with large quantity vectors
- Standalone — no external dependencies beyond NumPy
- Dataclass-based — clean, typed data structures for market state and trade quotes
- Production-ready — the same math runs in our live trading system
The choice of b is the most important parameter:
| b value | Slippage | Max Loss (binary) | Use Case |
|---|---|---|---|
| 10 | High | $6.93 | Thin/experimental markets |
| 100 | Moderate | $69.31 | Standard markets |
| 1,000 | Low | $693.15 | Deep liquidity markets |
Higher b = more liquidity (less price impact per trade) but more capital at risk.
- Hanson, R. (2003). Combinatorial Information Market Design. Information Systems Frontiers.
- Hanson, R. (2007). Logarithmic Market Scoring Rules for Modular Combinatorial Information Aggregation.
- Chen, Y. & Pennock, D. (2007). A Utility Framework for Bounded-Loss Market Makers.
We build quantitative trading systems for prediction markets. This is one piece of our infrastructure — open-sourced to contribute to the quant finance community.
We're hiring. If you think in probabilities and write clean Python, reach out.
Built with math, not vibes.