Converting Odds Formats in Code: Decimal, Fractional and Implied Probability
Sports data, games and forecasting tools all express likelihood as odds. The conversions are simple, but rounding and the bookmaker's margin catch people out.
What odds express
In probability theory, odds measure how likely an outcome is. Sports data feeds and betting markets use several notations for the same idea, so tools often need to convert between them.
The core conversions
def implied_probability(decimal_odds):
return 1 / decimal_odds
def fractional_to_decimal(numerator, denominator):
return numerator / denominator + 1
def overround(decimal_prices):
return sum(1 / p for p in decimal_prices) - 1
Why the probabilities add up to more than one
If you convert every price in a market to implied probability and add them, the total is usually above one. The excess, the overround, is the operator's built-in margin. To estimate "fair" probabilities, divide each implied probability by the total.
Rounding and display
- Store prices as exact decimals or rationals; convert to strings only for display.
- Be explicit about rounding rules. Published prices are often already rounded.
- Write unit tests with known inputs from a published reference.
A worked reference
When building a converter, it helps to check results against an explanation written for readers. One step-by-step example, in Traditional Chinese, is a guide that derives implied probability from sports-lottery odds and computes the operator's margin as a percentage (運彩賠率與隱含機率). Its worked numbers make good test cases. Remember that margin means the expected return to bettors is below the stake; code that models odds should never present them as a way to profit.
last updated 2026-10-20