# The Math Behind the PMM

The price curve described by the PMM is mathematically defined as follows:

$P=iR$

If

$B<B_0$

, then$R=1-k+(\frac{B_0}{B})^2k$

If

$Q<Q_0$

, then$R=1/(1-k+(\frac{Q_0}{Q})^2k)R$

Otherwise,

$R=1$

.where

- $i$is the initial "guide price"
- $k$is the "slippage factor"
- $B$denotes the current base token supply
- ${B_0}$denotes the equilibrium base token supply
- $Q$denotes the current quote token supply
- ${Q_0}$denotes the equilibrium quote token supply

The most important thing for a trader is the average transaction price. The average transaction price is the integral of the marginal price

$P$

. Let's examine the case where there is a shortage of base tokens.This equation describes the case where the user has

$B_1$

base tokens and wishes to purchase enough to have $B_2$

tokens. To do this, the user needs to calculate the average price they will pay per token purchased.

$\Delta Q =\int^{B_2}_{B_1}PdB$

$= \int^{B_2}_{B_1}(1-k)i+(B_0/B)^2kidB$

$= i(B_2-B_1)*(1-k+k\frac{B_0^2}{B_1B_2})$

The above equation can be rearranged to obtain the average token price as follows:

$P=\frac{\Delta Q}{B_2-B_1}=i*(1-k+k\frac{B_0^2}{B_1B_2})$

Let's derive how to calculate the price when there is a shortage of base tokens and only the number of quote tokens you want to buy or sell with (i.e.

$ΔQ$

) is given.

$\Delta Q = i(B_2-B_1)*(1-k+k\frac{B_0^2}{B_1B_2})$

Now that

$ΔQ, B_0, B_1$

are given, we need to calculate $B_2$

, which is found by solving a quadratic equation. Transforming the equation into standard form:

$(1-k)B_2^2+(\frac{kB_0^2}{B_1}-B_1+kB_1-\frac{\Delta Q}{i})B_2-kB_0^2$

$let \ a=1-k, \ b=\frac{kB_0^2}{B_1}-B_1+kB_1-\frac{\Delta Q}{i}, \ c=-kB_0^2$

Since

$B_2>=0$

, we discard the negative root, yielding

$B_2=\frac{-b+\sqrt{b^2-4ac}}{2a}$

DODO V2 focuses on verifying the special case of k=0 and k=1, to support a constant selling price and the bonding curve of the standard AMM.

$B$

and$Q$

represent the current token supply, i.e. the dependent variables. In order to determine this price curve, we also need to know the four parameters $i$

, $k$

, ${B_0}$

, and ${Q_0}$

. However, these four parameters are not independent of each other, and given any three of them, the fourth one can be calculated.

The process of calculating the fourth parameter is called "regression" and is done whenever the parameters change. DODO's PMM operates under a "long regression" rule, which is a rule that aims to return the overvalued asset to its equilibrium value.

When the token supply changes, one of the two assets of the trading pair must be more than the equilibrium value and the other less than the equilibrium value. This corresponds to the pool holding a long position in one asset and a short position in the other asset.

The "long regression" rule then acts to return the long-side asset to its equilibrium value. Once the long-side asset returns, the asset value is deemed to be the new equilibrium value, regardless of the short-side asset's new value. That is, the short side takes a profit or loss.

Suppose the current state is

$B<B_0, Q>Q_0$

. Here, $Q$

is a long-side asset and$B$

is a short-side asset. We know the parameters $i,k,Q_0$

and need to solve for the last parameter $B_0$

- the regression target. To calculate the regression target at a certain oracle price, we make the following derivation:

$Q-Q_0=\Delta Q = i(B_2-B_1)*(1-k+k\frac{B_0^2}{B_1B_2})$

By definition, when the supply returns to equilibrium, the new inventory will be

$B_2$

, which will be identical to the original equilibrium supply i.e.$B_0=B_2$

. Therefore, it is possible to organize the formula into the standard form of the quadratic equation.

$\frac{k}{B_1}B_0^2+(1-2k)B_0-[(1-k)B_1+\frac{\Delta Q}{i}] =0$

Eliminating the negative roots yields the regression target

$B_0$

.

$B_0=B_1+\frac{B_1}{2k}*(\sqrt{1+\frac{4k\Delta Q}{iB_1}}-1)$

Similarly, if

$B$

becomes the long-side asset, then we can to solve for $Q_0$

instead, yielding:

$Q_0=Q_1+\frac{Q_1}{2k}*(\sqrt{1+\frac{4ki\Delta B}{Q_1}}-1)$

When the system parameters are constant, the price curve is also constant. Even if the supply changes, the supply will return to equilibrium eventually. In other words, the gain or loss due to supply changes is just a "floating" gain or loss, and the term "impermanent loss" has been coined to describe this phenomenon.

The same is true for the PMM algorithm, which is similar to an AMM algorithm if the parameters remain unchanged. However, in practice, one often needs to make adjustments to the price curve. If the "long regression" rule is executed, the equilibrium value on the short side gets reset. The reset equilibrium value may be larger, which is equivalent to a profit. It may also become smaller, which is equivalent to a loss. Each regression converts an unrealized floating profit or loss into a real profit or loss.

Each time you change a parameter, it should ideally eliminate extra positions in time to avoid excessive losses. As for when and how to adjust the curve, it is subjective, and we hope that developers and traders can make the most of this framework to suit their own market-making strategy.

Last modified 30d ago