Applied Mathematics on Product Function Type AMM Liquidity Pools and Volatility
Abstract
Here, we discuss the properties and mechanics of different types of automated market making liquidity pools including constant product, constant mean and bounded liquidity. From there, we define and show how to calculate impermanent losses for theses pools. We then show that we can equate impermanent loss as projected APR from fees and rewards and derive a model to find this. We then discuss some pitfalls and remedies of calculation for bounded type pools such as narrow price ranges as well as additional considerations such as interest rates. Practical examples of calculations on live pools are shown to conclude.
Introduction
Automated market making (AMM) liquidity pools are a innovation from cryptocurrencies and decentralized finance (DEFI). Due to the nature of blockchain technology, where transactions can only be processed at the speed at which a new block is minded, a traditional limit order book type of exchange would not be very effective for price discovery nor very friendly towards market makers. For example, the Ethereum blockchain mines a new block around every 12 sec, meaning a traditional order book could only update its pricing quotes at that frequency. Obviously for most higher frequency type of trading that is simply way too slow. There has of course been ways to get around this in the past. For example, one could build the exchange on a much faster blockchain or layer 2 or even to host the actual exchange engine off chain and to periodically clear batch trades on the actual blockchain.
However, there is a third option that allows a robust market to exist natively on slower blockchains through AMM. Here instead of traditional market makers who post and cancel prices between asset pairs, pricing in these pools is defined through a mathematical invariant as a function of the quantity of the assets in the pool. As trades are done that deposit / withdrawal these assets, the price will dynamically update.
There has already been much work in investigating the theoretical properties of these types of pools, details of some can be found in the conclusion. However, the goal of this article is to provide an overview that is both broad and practical in not only understanding the theory but also some practical results and corollaries of these properties, most notably in evaluating the risk-reward profile of different types of pools through a derived implied volatility measure.
Price Derivation
For a given pair , we can define the price of asset in terms of asset , as the instantaneous rate in which unit of asset can by exchanged for units of asset . Taking the limit of this yields
(1)
Constant Product
Our most basic type of product based AMM is the constant product market maker pioneered by Uniswap V2 and is still used by many other forks. Here we have the formula where is the quantity of asset in the pool and is an constant. To derive this we apply the definition . We require an invariance that at any given time the total value of each of these set of assets inside the pool is the same.
(2)
Solving this differential equations yields
(3)
Constant Mean
Two Assets
Now suppose that instead of enforcing that both sides of the pool must have equal value, we fix their ratio to something else. For example, in an 80-20 type of pool, we enforce 80% of the equity of the pair in asset and 20% in asset . More generally supposing we have a two asset pool where of the equity is in asset and of the asset is in asset . Making these modifications to (2) yields
(4)
Again solving and rearranging yields
(5)
Multiple Assets
Finally let us assume the general case of assets. WLOG we can consider the asset as a numeraire. We then have quantity , ratio and price .
We reuse (4) and replace the total derivatives with partials.
for each asset pair . Taking this product across each pair and multiplying them together yields , which simplifies to
with constraint (6)
In the limit case of 2 assets and , this reduces to the constant product equation.
Bounded Liquidity
We now introduce Uniswap V3 bounded liquidity provision, a special application of the constant product pool. Instead of covering an entire range of , individuals are able to provide liquidity in specific ticks between where at:
i. the entire position is held in the volatille asset;
ii. , the entire position is held in the numeraire asset;
iii. the position is held in a mix of each asset
Case iii is specified by a modified virtual constant product liquidity curve that shifts the standard curve such that its endpoints will intersect at 0 for each asset at the respective price points. More details of the ticks and derivations are specified in the Uniswap V3 white-paper and technical note.
Given constant L, we have:
(7)
Applying boundary cases where either where and where and are 0 respectively we can also find:
(8)
(9)
Finally given L is a constant, we can find the values of , for
(10)
It can be seen recovers the constant product equation.
Impermanent Loss
Price conventions
In most papers and blog posts, the convention for an asset group x-y is to treat the first x-asset as a volatile asset and the y-asset as the numeraire, the unit of account of the pairs. By convention and in practice in many financial markets, the de-facto numeraire that is used is the US Dollar due to a range of historical and economic reasons. However many currency pairs, like ETH-BTC, or even multi-asset pools like ETH-BTC-UNI-DOGE, do not have the dollar as a reference. In these, it is then common practice to treat one of them as a numeraire. However, I am of the opinion that using this convention is not optimal.
There are a few reasons for this. The first is that despite the pipe-dream of every crypto maximalist, BTC, ETH, or any crypto asset is not at the stage of a universally accepted unit of account. Knowing a PnL in ETH is useful, but for practical purposes of using said PnL to pay for housing or buy foods, it’s generally necessary to have it in dollars. This is especially true given the relative volatility of said digital assets. The second reason is in terms of comparing different pools and opportunities it is necessary to have a standard unit of account. For example how would one compare a ETH-BTC pair vs a UNI-DOGE pair. In the case where there’s no common currency between both pools, an intermediate currency needs to be used anyways. Of course that intermediate currency can be ETH, but if we’re going through the effort why not just make it dollars anyways?
Two things can be done. The first is performing calculations with different numeraire assets for each pool and then converting everything in terms of dollars at the end. The second is normalizing all the assets in dollars to begin with, and perform the calculations afterward. I am generally a fan of the second as it’s more direct in terms of keepign track of PnL and balances across each step and less prone to currency conversion mistakes.
Therefore for the rest of this article, a price is the price of asset i denoted in USD unless otherwise specified.
Equity based calculation
There’s already a plethora of resources that instruct how IL is calculated for both standard x-y pools and bounded liquidity pools. Although useful resources I’ve found them to not do an intuitive job of showing what is happening. This is my attempt at a clearer explanation.
IL is the difference between what your PnL would be if you just held your assets compared to what your equity actually is after being transformed in these pools.
(11)
Here equity is (12)
At the inception of the pool we should also know the vector of initial prices, quantities, ratios of assets, and price bounds of liquidity (if a bounded liquidity pool), .
Note given any 2 of (and if it’s bounded), the remaining vector is implied.
will depend solely on the change from initial price vector to final price vector , with the only computable value being the final quantity vector from the equations above.
Constant Mean
Define
(13)
The proof of (13) as follows.
We equate the initial and final states of the pool due to (6).
For constant mean pools we have the relationship .
Furthermore to keep a pool in constant ratio .
Therefore we see .
Furthermore we get . Note that the denominator remains the same as .
From there it is evident that . Using the equality relationship, the RHS denominator of this term can be substituted , which when multiplied recovers the desired quantity .
Bounded Liquidity
The final state quantities follow directly from equations (7)-(10) by first finding from the initial state and using that value to solve for the quantities in the final state.
Treating Impermanent Loss as APR
The impermanent loss is strictly negative. As compensation for this divergence loss, protocols will compensate the providers of this with trading fees and/or token rewards. Generally this compensation will vary on both trading volume and pool policy. Fees are generally a function of trading volume. Token issuers may provide additional incentives for providers on their specific pools. For our purposes we will treat these as exogenous fixed variables.
Oftentimes this variable is reported as a historical APR rate by the protocol. If not, we can use the rules and data of the protocol ourselves to create our own model. For example, rewards based on trading volume can simply be calculated by
.
We notice that APR is strictly negative. Therefore the profitability of a pool is ultimately dependent on whether the rewards are enough compensation for the loss in IL. In other words, we can ask the question: For a given expected APR, how much IL does it compensate for?
(14)
Given an initial relative price between two assets, what new relative prices creates a breakeven point for this given APR for a delta-neutral initial position. Note that due to the symmetry of the IL loss function there will be two prices for this, one above the initial and another below.
In order to find this we utilize the same equation (11) as before. However, this time the LHS is known, and we are solving for the price vectors on the RHS by inverting the equation.
Constant Mean
We restrict our case to only evaluating two assets at a given time. For a given IL value with more than two assets, there is an infinite number of relative price combinations that can be taken.
Therefore we will use (5), , as our invariance and (11), (14) to find APR.
It is thus evident:
(15)
We now employ a trick by creating a new synthetic asset k that is a basket of units of asset j. From this we can see that and .
Rewriting (15) with asset k yields .
Because of this normalization we can pick any as well as any combination that satisfy .
For our case we will pick and
Next we can use equations 11-14 to calculate the IL and APR.
We fix asset as the numeraire asset and vary . As before, the final quantity vector given final price .
Taking where yields equity:
We see . Since by definition, , which is the relative ratio of the initial and final prices of asset .
(16)
Notice that the APR and impermanent loss is solely dictated by the ratio of the equity in the pool and the change in relative price. It is irrespective of the actual prices of the assets. This follows from how there are only two degrees of freedom of the pool from 3 variables, price, ratio and that the price of one asset can always be written in terms of the other.
Now suppose there are assets with ratios and prices normalized to asset :
(17)
(18)
(19)
Bounded Liquidity
A similar process can be done with bounded liquidity pools using equations 7-10. Again can be applied. Additionally, outside the range , . Prices outside the range means that the asset is not providing liquidity. Therefore they will will neither earn fees nor suffer impermanent loss.
Price can also be normalized, remapping , with the bounds of liquidity transforming .
Utilizing (10) finds and ; keeping in mind .
Afterwards, we find and in terms of and again apply (11) to find:
(20)
Two Solutions
Notice that should always yield two solutions . One where the relative price is higher than the initial and one in which it is lower. To find these two solutions, numerical root finding algorithms such as Secant’s method can be used. To recover the actual prices of asset , we then reintroduce the factor .
(21)
Impermanent Loss as Volatility
Notice that impermanent loss is suffered as a result of two assets diverging in price from each other. Another way of describing divergence is with volatility which is a very useful metric in options trading and pricing. In this case we can ask, “Assuming that the IL suffered is a certain amount, what is this implying about the volatility of the pair?” To answer this we compute the volatility of log prices. Conventionally, this is already normalized due to the work that was done before.
(22)
Using implies that any given APR will yield an implied volatility due to the rewards. The pool provider will come out ahead if the realized volatility is less than calculated value and come out behind if it is more. Note that the timeframe of this APR is synonymous with the timeframe of volatility. A quoted 1 year APR will return 1 year implied volatility while a 1 day APR will return a 1 day implied volatility.
Limits of Bounded Liquidity
When dealing with a bounded liquidity pool, there is a big problem Suppose there is a particularly big APR but a very fairly small price bound range . As before, inverting (20) can be used to see for this value, what prices or does the relative price have to move to breakeven? However, in certain cases, the requisite or . What should be done then?
First, consider what happens if we just take the derived prices as is? This doesn’t work. Once the price escapes the price bounds, the position will neither accrue more returns (collect APR), nor will it suffer impermanent loss. As a result, the APR value will be over stated for the periods outside the range. Naively, the model assume think it’s collecting a return and suffering impermanent loss even if it is not.
Another way to think about this is that equation (20) makes the assumption that the price is within the price range 100% of the time. However, if the bounds are small enough, the portfolio will venture outside of the bounds x% of the time. During this x% of the time, the position is “doing nothing” and therefore not collecting the stated APR.
Most directly, we derived equation (20) using the (10) as a starting assumption. Therefore, if the price at any point escape the bounds of equation (10), then the assumptions and therefore results are no longer valid.
Renormalized APR by Time
There is a solution to this. We know that where T is time. assuming simple compounding and assuming continuous compounding. Then, allow to find an . Use this to find an adjusted . Then we find our .
(22)
(23)
(24)
Very Narrow Ranges
For instances where your bounds selection is very narrow compared to the price, (24) will also start to break down. For these situations, it is better to just directly calculate the from a single tick detailed in this article.
(25)
Additionally, one can use a weighted sum of (25) and (24) with weights as a function of price width ratio.
Bounded Example
Suppose we have assets with . Naively using equation (20) will yield solutions of , which are outside of our range and therefore invalid. However remapping with equations (22)-(24) can correct this. I typically like to use as a balance between numerical stability and computational accuracy. Practically it converts a yearly rate into a daily rate which we can think of a daily rebalance hedge frequency. This we yield . For a narrower range, (25) may be a better fit.
Borrow Rates
The last bit of mechanics we can talk about is the borrow costs of the securities. Thus far all of the calculations have been based on a delta neutral strategy. In order to do this in practice, one must borrow these securities or hedge by selling an underlying future. However different securities can have different rates of borrow. Assuming borrow rate and ratio for asset , we must modify our APR by:
(26)
This can be used in place of all previously. Note that in a constant mean pool is constant while in a bounded liquidity pool varies. For varying , unless the borrow rates for each asset are very big and very different, then it’s “good enough” to just treat them as fixed to what they are when the position is initialized.
Examples
We now apply the methods above to analyze two examples. The first concerns a constant mean product type pool on Balancer, a BAL-WETH 80%-20% pool. The second is a Uniswap V3 bounded liquidity ETH-USDC pool.
To start we can find the prices and borrow rates for the affiliated assets.
For USDC, we use the most recent money market rate. For the coins we use the annualized funding rate on the most liquid exchange, Binance. Also note that the BAL funding rate is negative. This means that selling the future allows us to collect instead of pay the rate.
Balancer Pool
. Using equations (16) and (22) we can find that this yields . This is a pretty impressive number, though a significant portion of this is from the “friendly” BAL funding rate. If we just used the raw APR without the funding rates, .
Uniswap V3 Pool
We use this pool as an opportunity to demonstrate the value of renormalizing APR by time by selecting the thinnest possible width for our bounds of liquidity . Furthermore to find the fee rate, divide the 24h Fees by the total value locked at this given tick. Note, we do not divide by the TVL of the whole pool since at any given trade, the fees only go to the liquidity at the current tick, not the entire pool.
This not a typo and certainly looks very impressive as a raw APR figure. However, we keep in mind this is a dollar wide liquidity range on a asset. This implies in practice this APR will only be collected a very tiny fraction of time. Instead, we can use (24) and (25) to get convert that APR into a . Note that since it is such a narrow bound, most of the legwork will be from (25).
From this we can compare it to some recent historical ETH-USD realized volatilities of around 31% and 23% for 1 month and 1 week respectively.
Conclusion
This details analytically way to derive pricing, IL, and volatility for various types of pools. We’ve provided an intuitive, mathematical, and algorithmic way to calculate the properties and impermanent loss risk dynamics of constant product, constant mean product and bounded liquidity type pools. We have also provided a framework to compare these pools to each other as well as other non-linear assets such as options.
There have been other works that further detail pricing mechanics as well as hybrid invariant pools like Curve. Furthermore there is also discussion on derivation of option-like greeks as well as replicating payoffs in general. Additionally, Uniswap has recently released a V4 of their pools which introduces new features such as hooks and flash accounting that adds further dimensions to the products and levying an additional swap fee on some pairs. These new features can generally be incorporated into this analysis.