Beta
This library is in beta. It's not ready for production.
SolStat
SolStat is a Math library written in solidity for statistical function approximations. The library is composed of two core libraries; Gaussian.sol, and Invariant.sol. We will go over each of these libraries and their testing suites. We at Primitive use these libraries to support development with RMM-01s unique trading function, which utilizes the cumulative distribution function (CDF) of the normal distribution denoted by the greek capital letter Phi(
How to use
Requirements: forge, node >=v16.0.0
yarn install
forge install
yarn build
forge test
To compute values using the gaussian.js library, you can use this commmand in the cli:
yarn cli --cdf {value}
yarn cli --pdf {value}
yarn cli --ppf {value}
Irrational Functions
The primary reason for utilizing these approximation algorithms is that computers have trouble expressing irrational functions. This is because irrational numbers have an infinite number of decimals. Some examples of irrational numbers are
Computational Constraints
In classical computing, our computational resources have become abundant, allowing us the liberty to iterate these algorithms to achieve our desired accuracy. However, the Ethereum Virtual Machine (EVM) has a necessary monetary cost of computation. This computational environment has monetarily motivated developers to find efficient algorithms and hacks to reduce their applications' computational overhead (and thus the cost of computation).
Gaussian.sol
This contract implements a number of functions important to the gaussian distributions. Importantly all these implementations are only for a mean
Cumulative Distribution Function
The implementation of the CDF aproximation algorithm takes in a random variable erf
. The error functionโs identity is erfc(-x) = 2 - erfc(x)
and has a small collection of unique properties:
erfc(-infinity) = 2
erfc(0) = 1
erfc(infinity) = 0
The reference implementation for the error function is on p221 of Numerical Recipes in section C 2e. A helpful resource is this wolfram notebook.
Probability Density Function
The library also supports an approximation of the Probability Density Function(PPF) which is mathematically interpeted as
Percent Point Function / Quantile Function
Furthermore we implemented aproximation algorithms for the Percent Point Function(PPF) sometimes known as the inverse CDF or the quantile function. The function is mathmatically defined as 1.2e-7
, and depends on the inverse error function ierf
which satisfies ierfc(erfc(x)) = erfc(ierfc(x))
. The invers error function, defined as ierfc(1 - x) = ierf(x)
, has a domain of in the interval
ierfc(0) = infinity
ierfc(1) = 0
ierfc(2) = - infinity
Invariant.sol
Invariant.sol
is a contract used to compute the invariant of the RMM-01 trading function such that we compute R_x
: The reserves of token stk
: The strike price of the pool.
vol
: the implied volatility of the pool denoted by the lowercase greek letter sigma in finance literature.
tau
: The time until the pool expires. Once expired, there can be no swaps.
inv
: The current invariant given R_x
.
The function then returns the quantity of token y per unit of liquidity denoted R_y
, which is always within the bounds of
Differential Testing with Foundry
We leveraged foundry's support of differential testing for this library. Differential testing is a popular technique that seeds inputs to different implementations of the same application and detects differences in their execution. Differential testing is an excellent complement to traditional software testing as it is well suited to detect semantic errors. This library used differential testing against the javascript gaussian library to detect anomalies and varying bugs. This helped us to be confident in the performance and implementation of the library.
[1]: Replicating Market Makers
[2]: Replicating Portfolios: Contstructing Permissionless Derivatives