Evan K
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # <center> Bayesian Inference on RMM-01 Pool Liquidity Utilization </center> ## Introduction Statistical inference is a largely unexplored area to analyze DeFi datasets with little work done to date. In this post, we analyze a DeFi dataset containing RMM-01 historical pool data using Bayesian inference [in this notebook](https://github.com/primitivefinance/pool-analytics/blob/main/notebooks/rmm_bayesian_inference_liquidity_utilization.ipynb). We derive liquidity behavior insights, providing empirical evidence to answer questions such as: - What is the optimal liquidity utilization ratio? - What are the upper bounds of arbitrage swap sizes over time? - What is the potential slippage/price impact on LPs? ### Replicating Market Makers (RMMs) and RMM-01 Trading Function RMMs are a [novel class of AMMs](https://arxiv.org/abs/2103.14769) with the key property that a trading function can be explicitly defined to replicate a myriad of financial derivative payoff. The first implementation of an RMM is [RMM-01](https://primitive.xyz/whitepaper-rmm-01.pdf), which uses a Black-Scholes trading function to replicate the payoff of a covered call for liquidity providers (LPs). Launching in April 2022 on Ethereum, little is known about the behavior of RMM-01 pools, with less than 500 data points of swaps being available in the historical dataset. RMM-01 pools are unique because they are set with an unusually high swap fee around 1%, which no human would pay. Instead, all of the RMM-01 swap volume comes entirely from arbitrage bots who can pay a higher fee while still profiting from the trade. As a result, RMM-01 provides an interesting opportunity to study the intersection between DeFi (providing liquidity), TradFi (replicating a covered call payoff), and MEV (attracting sufficient arbitrage volume). ## Bayesian Inference Bayesian inference is a powerful probabilistic framework that approximates a posterior probability distribution conditionally dependent on the prior distribution of observed data. The posterior distribution provides insight into the uncertainty of prior statistical parameters conditioned on observed data and can be visualized easily as a highest posterior density interval. Bayesian inference leverages [Bayes Formula](https://www.psychologyinaction.org/psychology-in-action-1/2012/10/22/bayes-rule-and-bomb-threats) to compute the posterior distribution from a prior distribution of observed data. A good description of the Bayesian procedure is shown below, taken from [here](https://www.stat.cmu.edu/~larry/=sml/Bayes.pdf): ![](https://i.imgur.com/D5aW4SM.png) ### cmdstanpy `cmdstanpy` is a state of the art Bayesian inference library that lets users draw samples from a posterior distribution via the [UTS-HMC](http://www.stat.columbia.edu/~gelman/research/published/nuts.pdf) aglorithm. Hamiltonian Monte Carlo (HMC) is an algorithm used to draw samples from a posterior distribution. The performance of HMC depends strongly on choosing suitable values for $\epsilon$ and $L$. If $\epsilon$ is too large, then the simulation will be inaccurate and yield low acceptance rates. If $\epsilon$ is too small, then computation will be wasted taking many small steps. If $L$ is too small, then successive samples will be close to one another, resulting in undesirable random walk behavior and slow mixing. If $L$ is too large, then HMC will generate trajectories that loop back and retrace their steps. The No-U-Turn Sampler (NUTS) extends HMC by offering automatic selection of step sizes $\epsilon$ and desired number of steps $L$. The selection of the step size $\epsilon$ relies on using [nonsmooth stochastic optimization with a vanishing gradient adaptation](https://people.eecs.berkeley.edu/~jordan/sail/readings/andrieu-thoms.pdf) adapted from [Nesterov's primal-dual algorithm](https://ium.mccme.ru/postscript/s12/GS-Nesterov%20Primal-dual.pdf). NUTS uses a recursive algorithm to set the desired number $L$ of leapfrog steps by building a balanced binary tree via repeated doubling. This preserves time reversibility by running the Hamiltonian simulation both forward and backward in time. This is shown in the below figure, taken from [here](http://www.stat.columbia.edu/~gelman/research/published/nuts.pdf). ![](https://hackmd.io/_uploads/SJcmEt8nc.png) ## Analysis and Results This model samples two posterior distributions from two prior distributions of observed liquidity utilization data and $\tau$ (time between swaps). Liquidity utilization was calculated on a per swap basis as the swap size divided by the USDC value of liquidity being provided. The two charts below are kernel density estimate (KDE) charts which draw contours to visualize different levels of density between the posterior distributions. The high density contour intervals are defined as 25% (yellow), 50% (green), 75% (teal), and 95% (dark blue). ![](https://i.imgur.com/kIop19Q.png) ![](https://i.imgur.com/HKz4R1a.png) The first insight is that the prior distribution of liquidity utilization has very large intervals where liquidity utilization could reach levels upwards of 30-50% with a very large probability density interval whereas the posterior distribution does not go past 20% with very high certainty. The second insight is that the posterior distribution shows that liquidity utilization is stable over a longer period of time up to ~28 days (tau=0.08). In contrast, no insight is available for the prior distribution past ~14 days (tau=0.05). Higher liquidity utilization rates generally imply illiquidity and lead to higher price impact/slippage, making swaps riskier for both LPs and swappers. However since swap volume is driven entirely from arbitrage bots who only execute swaps for profit, the higher liquidity utilization rates are actually hurting the liquidity providers as they are not being compensated with a high enough fee for the level of LP risk. More research is needed in the direction of using dynamic fees to compensate LPs more fairly for the risk of LPing. If risk can be priced more accurately using dynamic fees at a swap level, this would provide better risk compensation to LPs as well as discouraging liquidity utilization past a level that is too risky for LPs. The chart below shows a more granular view of the posterior probability densities as well as showing the distribution curves for both liquidity utilization and tau. The highest density occurs around the 5-10% liquidity utilizatn area and around the .02-.04 (1-2 week) $\tau$ range. ![](https://i.imgur.com/LfGFK6h.png) ## Conclusion & Further Research By using Bayesian inference, key initial insights were derived describing the behavior of RMM-01 pools as well as illuminate areas of risk for LPs from excessively high liquidity utilization. Additionally Bayesian inference allowed robust analysis to be completed on a small data set of less than 500 swaps. Some additional research directions could be: * How to set fees that compensate LPs dynamically for risk in the form of high liquidity utilization rates? * Are the initial parameters set by RMM-01 LPs ideal? How do different parameters affect liquidity utilization and swap volume? * How does RMM-01 pool liquidity utilization compare to other AMM designs such as Uniswap and Curve? * Is there a way to distribute LP utilization risk across different RMM-01 pools in a way that mitigates LP utilization risk for the entire portfolio? ## References 1. [NUTS-HMC](http://www.stat.columbia.edu/~gelman/research/published/nuts.pdf) 2. [Chapter 12 - Bayesian Inference](https://www.stat.cmu.edu/~larry/=sml/Bayes.pdf) 3. [cmdstanpy, a package for Bayesian inference](https://mc-stan.org/cmdstanpy/) 4. [cmdstanpy examples](https://github.com/stan-dev/cmdstanpy/tree/master/docsrc/examples) 5. [rmm pool bayesian analysis notebook](https://github.com/primitivefinance/pool-analytics/blob/main/notebooks/rmm_bayesian_inference_liquidity_utilization.ipynb)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully