---
# System prepended metadata

title: Further napkin math

---

# Further napkin math

Heavily inspired by https://github.com/valory-xyz/trader/pull/119 with a bit of own thoughts for own understanding. Hopefully it's also useful to other people understanding Prediction markets on-chain.


We want to derive an expression for the [Kelly Criterion](https://www.investopedia.com/terms/k/kellycriterion.asp), which is used in gambling and investing and gives the optimal bet fraction (from your entire portfolio) that you should put on a given market. It's given by

$$
f^{\star} = \frac{bp - (1-p)}{b}
$$

where $f^\star$ is the Kelly fraction, $p$ is the estimated probability of a win and $b$ reflects the market odds, as described on this [paper](https://arxiv.org/abs/1201.6655). 

**Very important** - note that $b$ means "$b$-to-1" odds, i.e. they are given in fractional odds and mean that, when wagering 1 unit, the total payout is $b$ plus the wagered amount (1).

To make it more clear, we also define (as in this [PR](https://github.com/valory-xyz/trader/pull/119)) the quantity Q (gross payoff odds) as:

$$
Q = b + 1
$$

and, expressing the Kelly criterion using $Q$,

$$
f^\star = \frac{Qp - 1}{Q-1}
$$

Until now we were still only in the Prediction Markets realm. The above is easy to calculate (as seen in previous [posts](https://gabrielfior.github.io/projects/math_prediction_markets/)) if the market odds are not strongly changed by the bet we are about to place following the Kelly criterion. However, specially considering that there is an AMM pricing the two outcome tokens, the market odds (represented by the ratio of outcome tokens of each type in the pool) can move considerably after bets, hence we expand the Kelly formula to include this effect.

As a preview of what is to come, we want to leverage the Fixed Product Market Maker (FPMM) mechanism to calculate the payout we will receive by placing a bet (assuming we picked the correct outcome).

In the FPMM case, let's imagine we place a bet where we wager $X$ and recee $n_a$ outcome tokens in return. If outcome A is indeed the correct one, each outcome token will be worth 1 unit of collateral, hence our payout will be $n_a$. Expressing in terms of $Q$:

$$
\text{Total payout if correct} = QX = n_a
$$

Now we change gears and focus on the FPMM mechanism for determining $n_a$. Once we have an expression for that, we can simply substitute it in the Kelly criterion formula and have a generalized version for the FPMM case.

### AMM section

When we place a bet on a FPMM market, we are essentially adding liquidity and immediately afterwards executing a swap. Hence we can formulate the following equation:

$$
n_r = n_{\text{add_liq}} + n_{\text{swap}}
$$

In other words, the total number of outcome tokens we receive has 2 parts: one that comes from the liquidity that was added, and the second one from the swap of outcome tokens B for outcome tokens A.

#### Adding liquidity
This involves simply adding equal amounts of value (as priced in collateral units) to both sides of the pool, as given by the market pricing. One can of course add different amounts to each side, however that's not optimal as it opens the door to arbitrageurs to extract value and rebalance the pool to the previous odds (which should reflect the realistic expectations for that given market).

In order to add equal value to both sides, following conditions must hold (deposit amount $X$):

$$
n_a = \frac{X}{2}\frac{1}{p_a} 
$$

Now we need to calculate $p_a$ and $p_b$ in terms of the collateral token. 

See [paper](https://arxiv.org/pdf/2107.12484) on page 10 for a derivation of formulas:

Unscaled prices= $\nabla{\varphi(R)_i} = \frac{\partial\varphi}{\partial R_i} (R)$

thus the unscaled price is given by 

$$P_a = R_b$$

The formula for scaling prices is (see eq. 9, pg 10)
$p_i = \frac{P_i}{P_n} = \frac{w_i R_n}{w_n R_i}$

Finally, for price of $p_a$

$p_a = \frac{R_B}{R_B + R_A}$

and the number of outcome tokens minted were

$$
n_a = \frac{X(R_B + R_A)}{2 * R_B}
n_b = \frac{X(R_B + R_A)}{2 * R_A}
$$

From my own calculations, I observed 2 things:

1 - If you calculate the entire value of the pool, you arrive at $P_N$

$$
P_N = R_A  \frac{R_B}{R_A} + R_B \frac{R_A}{R_B} = R_A + R_B
$$

2 - If you note that $p_a + p_b = 1$ (since if you buy 2 outcome tokens, you are guaranteed to hold $1 xDAI in value), we can do

$$
p_a + p_b = \frac{R_B}{\text{unknown}} + \frac{R_A}{\text{unknown}} = 1
$$
and we learn that $\text{unknown}$ should be $R_A + R_B$ so that it equals 1.

#### Swap

Now we swap the minted tokens for outcome B into tokens of outcome A. After some algebra (see [PR](https://github.com/valory-xyz/trader/pull/119)), we arrive at a value for $n_r$ and substitute it into $Q$ and finally into the Kelly formula to find a value for $f\star$.
