# Designing Prediction Markets

*Prerequisite: basic familiarity with what a prediction market is*

So you want to run a prediction market. You need a way for people to trade shares. What are your options?

### CLOBs and Markets

If you were making a prediction market from scratch, you'd probably come up with a **Central Limit Order Book (CLOB)**. Traders post BUY and SELL orders, stating what they're willing to buy and sell, and at what price, and you record these orders in your book.

*   Alice posts: "I'll buy 20 YES shares at $0.60 each"
*   Bob posts: "I'll sell 25 YES shares at $0.65 each"
*   Carol posts: "I'll buy 10 YES shares at $0.61 each"

When someone wants to trade, and doesn't want to wait for someone to fulfill their order, you match them with the best available offer. Pretty intuitive.

This system shows up directly in Hypixel Skyblock and other MMOs. The Bazaar lets you post orders and wait, or instantly fulfill existing orders. Have some Enchanted Iron to sell? You can list it at 540 coins and wait for a buyer, or instantly sell it by fulfilling the highest buy order at 470 coins.

The gap between the highest buy order ("bid") and the lowest sell order ("ask") is called the **bid-ask spread**.

Enter the Market Maker
======================

CLOBs work well, but they have a problem: they need people actively posting orders from both sides. If nobody's posting orders, the market can't function. The spread can also become very wide when few traders are active, making it expensive to trade.

This is where **market makers** come in. A market maker continuously posts both buy and sell orders, ensuring there's always someone to trade with.

Market makers profit by maintaining a gap in prices between their bid and asks. For example:

*   They **buy** YES shares at $0.60
*   **Sell** YES shares at $0.65
*   Whenever one person buys YES from them and another person sells it to them, they pocket the $0.05 difference

This is called **crossing the spread**. The market maker provides liquidity to the market and is compensated for it through the spread. In traditional finance, firms like Citadel Securities make billions doing exactly this. In Hypixel Skyblock, this strategy is called "bazaar flipping".

Pricing Shares
--------------

How do Market Makers price their shares? In an existing market, they can simply look at existing price charts to determine their prices, but that's not very feasible with prediction markets. Thus, we need some way of determining a fair price for shares.

For simplicity, let's ignore extracting profit. We'll assume someone's paying our market maker, whom we'll call Duncan, a flat fee to provide this service, and they're otherwise operating in a fully efficient market where the bid-ask spread is 0.

Duncan holds some inventory of YES and NO shares, and people can trade with him. How should Duncan price his shares? Examining the question, we can see some key constraints:

1.  **Prices should sum to $1**: If a YES share pays out $1 when the market resolves YES, and a NO share pays out $1 when the market resolves NO, then the price of a YES share and a NO share together should be exactly $1, since the market can only resolve to one of these two options.
2.  **Price equals probability**: If YES shares cost $0.70, then that means the market thinks there's a 70% chance of the outcome being YES, since that's how expected value works. This is the key mechanism by which prediction markets work and even if you don't know the details of the market implementation yet, you should know this already.

### Creating New Shares

Duncan needs the ability to issue shares. Otherwise, he'll run out of them, and won't be able to trade anymore. (No, he can't just raise share prices in an inverse relationship with his supply, since he sells both YES and NO shares this would violate the constraint that prices must sum to $1.)

Fortunately, it's very easy to issue new shares. Since YES and NO sum to 1, for every dollar Duncan receives from a trader, he can mint one YES share and one NO share as a pair. When the market resolves, he'll pay out $1 to holders of the winning share type, fully covering his obligation.

From this, we can infer that any valid formula must have certain properties: buying YES must raise P(YES), the probability must depend on inventory ratios (when Duncan holds a lot of NO, the probability is high because it means he's sold a lot of YES), and YES shares should always cost less than $1, except when the market is at 100%, and vice versa. Since 0 and 1 aren't probabilities, this should never happen.

### A Natural Probability Formula

Given these constraints, you might come up with this formula for deriving the probability from Duncan's inventory (and thus the prices of YES and NO):

$$
P(\text{YES}) = \frac{n}{y + n}
$$

where $y$ is Duncan's YES inventory and $n$ is Duncan's NO inventory.

*   When $y=n$ (such as when the market is initialized), the probability is 50%
*   If Duncan fully runs out of YES shares, the probability is 1, meaning you can't profit from buying YES anymore and you can buy NO for free.
*   If Duncan fully runs out of NO shares, the probability is 0.

This formula seems to satisfy all of our desiderata, and is fairly intuitive. Since P(YES) is the price of yes, we now know how to price our shares.

### Discrete Shares

If Duncan has 50 YES and 50 NO shares, probability is 50%, so shares cost $0.50 each.

You give Duncan $1, and tell him you want to buy YES.

1.  YES costs $0.50, so $1 buys 2 YES shares
2.  He mints 1 YES + 1 NO (inventory: 51 YES, 51 NO)
3.  Duncan gives you 2 YES shares in exchange (inventory: 49 YES, 51 NO)
4.  New probability: 51/(49+51) = 51%

Another example. Duncan has 100 YES and 50 NO:

*   Probability: 50/150 = 33.33%
*   Price per YES: $0.33
*   Your $1 buys 3 YES shares
*   He mints $1 of shares (inventory: 101 YES, 51 NO)
*   He gives you back 3 YES: (inventory: 98 YES, 51 NO)
*   New probability: 51/149 = 34.23%

You might have noticed the problem already: Duncan isn't accounting for how the purchase itself affects the price.

When you buy multiple shares at once, you're getting them all at the initial price, but each share you buy should be more expensive than the last! You get a discount on bulk purchases!

Duncan could solve this by selling shares one at a time or even fractions of a share at a time, adjusting the price after each infinitesimal sale. But this is computationally expensive and assumes shares are discrete units rather than infinitely divisible.

For a continuous equation, we need to use calculus and solve a differential equation

### The Calculus of Market Making

*(warning: differential equations)*

Let's formalize this problem. Suppose Duncan starts with $y_s$ YES shares and $n_s$ NO shares. You deposit $m$ dollars. and buy YES from Duncan.

After the trade:

*   Duncan has minted $m$ new shares of each type
*   NO inventory: $n = n_s + m$
*   YES inventory: $y = y_s + m - \text{sold}$

where "sold" is the quantity of YES shares Duncan gives to the trader. (In this context, s stands for "starting".)

The market probability at any point is:

$$
P = \frac{n}{y + n}
$$

Substituting our inventory formulas:

$$
P = \frac{n_s + m}{(y_s + m - \text{sold}) + (n_s + m)} = \frac{n_s + m}{y_s + 2m + n_s - \text{sold}}
$$

Since we're obeying the constraint **price equals probability**, the rate at which Duncan sells you shares is determined by the current probability.

The trader deposits money at rate $dm$ and receives shares at rate $d(\text{sold})$. The price per marginal share is $\frac{dm}{d(\text{sold})}$. Since we want the price to be the probability, we get: 

$$
\frac{dm}{d(\text{sold})} = \frac{n_s + m}{y_s + 2m + n_s - \text{sold}}
$$

Since we're taking money as our input, we take the reciprocal:

$$
\frac{d(\text{sold})}{dm} = \frac{y_s + 2m + n_s - \text{sold}}{n_s + m}
$$

 This is our initial differentiation equation. I encourage you to try to solve it on your own,  but if you don't know calculus or get stuck, the solution is enclosed below.

$$
\frac{d(\text{sold})}{dm} = \frac{y_s + 2m + n_s}{n_s+m} - \frac{\text{sold}}{n_s + m}
$$

$$
\frac{\text{sold}}{n_s + m} + \frac{d(\text{sold})}{dm} = \frac{y_s + 2m + n_s}{n_s+m}
$$

 Multiply both sides by $n_s+m$: 

$$
\text{sold} + \frac{d(\text{sold})}{dm}\cdot(n_s+m) = y_s + 2m + n_s
$$

$$
\int(\text{sold} + \frac{d(\text{sold})}{dm}\cdot(n_s+m))\cdot dm = \int(y_s + 2m + n_s)\cdot dm
$$

 Observe that the $\frac{d}{dm}\text{sold} = \frac{d(\text{sold})}{dm}$ and $\frac{d}{dm}(n_s + m) = 1$. By product rule, then: 

$$
\text{sold}\cdot(n_s+m) = my_s+m^2+mn_s + C
$$

$$
\text{sold}=\frac{m(y_s+m+n_s)+C}{n_s+m}
$$

$$
\text{sold} = m + \frac{my_s+C}{n_s + m}
$$

 $\text{sold}(0) = 0$, since if you spend no money you don't get any shares. If you plug in $m = 0,  \text{sold} = 0$ and solve for $C$, you get $C = 0$, so we can just drop that term. 

$$
\text{sold} = m + \frac{my_s}{n_s + m}
$$

 Since $n$ is just $n_s + m$ and $y$ is $y_s + m - sold$, we get: 

$$
y = y_s + m - m - \frac{my_s}{n_s+m}
$$

 !<

$$
y= y_s - \frac{my_s}{n_s+m}
$$

 You might notice the term $n_s+m$ shows up in the denominator of a term of $y$, and is equivalent to $n$. If you multiply $y$ and $n$ together, you get: 

$$
yn = y_s(n_s+m)-my_s=y_sn_s+my_s-my_s
$$

$$
yn = y_sn_s
$$

 The product of Duncan's YES and NO shares remains constant, regardless of the trade![^98nhq4wx2dk]

Constant Product Market Maker
-----------------------------

Thus, we've discovered the fundamental invariant: 

$$
yn = k
$$

 where $k$ is a constant determined by Duncan's initial inventory. Because YES * NO is always constant, we call this a **Constant Product Market Maker (CPMM)**.

So Duncan, knowing this, has determined an algorithm for pricing shares:

1.  Receive money from trader
2.  Mint YES and NO shares
3.  Give out exactly enough YES shares (or NO shares, depending on what the trader wants) to maintain the constant product $y \cdot n = k$

Here's an example of this in practice:

*   Duncan starts out by initializing a market with $50 of liquidity. (Initial inventory: 50 YES, 50 NO)
*   He solves for his constant product, which needs to remain invariant. $k = 50\cdot50 = 2500$
*   You bet $50 on YES. Duncan uses this to mint more shares. (Inventory: 100 YES, 100 NO)
*   He now needs to pay out enough YES shares that he reaches his constant product again. $yn = k$, solve for $y$.
*   $y =\frac{k}{n}$
*   Plug in NO and $k$. $y = \frac{2500}{100} = 25$
*   He has 100 YES, and needs to have 25 YES, so he gives you 75 YES shares in exchange for your $50. (Inventory: 25 YES, 100 NO)
*   The new probability is $\frac{n}{y+n} = \frac{100}{25+100} = 80\%$.

Meanwhile, if a trader wants to sell shares, it's similarly simple: He adds the shares to his inventory, figures out how many YES + NO pairs he needs to give up in order to reach the constant product, and then exchanges these pairs for cash and gives them to the trader, removing the shares from circulation. Alternatively, and perhaps more elegantly, the trader can simply buy the opposite share and then give pairs to Duncan in exchange for cash.

(Note that, since Duncan's inventory is inversely related to the market probability, that means Duncan pockets a lot of money from traders when the market resolves counter to expectations, and loses more of his initial liquidity the more confident a correct market is.)

In fact, this process can be fully automated, creating an **Automated Market Maker (AMM)**. This is the foundation of Uniswap, and many prediction market protocols.

Conclusion
==========

Starting from basic constraints about prediction markets (prices sum to 1, price equals probability), we derived a unique solution. We didn't just arbitrarily choose the CPMM out of a list of options. It emerged, inexorably, from the requirements we placed.

When you properly formalize a problem with the right constraints, there's often exactly one correct answer. Independent researchers, solving similar problems with similar constraints, will converge on the same solution. When Newton and Leibniz invented calculus, they didn't get similar results because they shared their work, or because they were working on the same problem (they were working in very different fields). They got similar results because they were working on a *class* of problems with the *same underlying structure*, even if the similarities are not obvious at first.

The market itself does Bayesian updating—on expectation, as more people trade, the probability approaches the true likelihood, based on the accumulated knowledge of the traders. Our pricing mechanism *had* to respect this Bayesian structure. The constant product formula isn't arbitrary; it's what you get when you correctly formalize "each marginal share should cost the current probability" in continuous terms. While this isn't an empirical fact about the territory, the laws of probability nevertheless have carved out a unique shape in design space, and your map had better match it.[^np2t0b9s40s]

(This is especially obvious in the context of a prediction market (which is, in a certain sense, the purest form of market, separating the trading and aggregating of information from everything else), but it applies to markets and AMMs in full generality, being used in DeFi and Crypto space.)

[^98nhq4wx2dk]: If you don't know calculus, this is the important part. 

[^np2t0b9s40s]: Ok, I'm completely overstating my case here and these paragraphs are largely joking, there are other solutions to this problem if you pick different probability functions matching these desiderata or come at prediction market design from a different cases, many of which have their own pros and cons, and Hanson explicitly wrote about Constant Function Market Makers. It's just that this one is very intuitive and has useful properties for a purely probabilistic YES/NO market which is why I wrote about it