Skip to content

Commit

Permalink
Merge pull request #16 from primitivefinance/feat/beta-0
Browse files Browse the repository at this point in the history
fix(nan-infinity): returns infinity rather than nan for pdf and ppf
  • Loading branch information
Alexangelj authored Dec 1, 2021
2 parents 276c532 + cbe40e9 commit d867da1
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 2,356 deletions.
12 changes: 0 additions & 12 deletions .github/workflows/size.yml

This file was deleted.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# primitive-v2-math
# RMM-Math

Math library for RMM-01
Math library for RMM-01.

Includes typescript functions for solidity approximations.
27 changes: 7 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
{
"name": "@primitivefinance/v2-math",
"name": "@primitivefinance/rmm-math",
"author": "Primitive",
"homepage": "https://primitive.finance",
"version": "1.2.0",
"keywords": [
"primitive",
"v2",
"amm",
"rmm",
"black-scholes",
"ethereum",
"math",
"black-scholes"
"primitive"
],
"license": "GPL-3.0-or-later",
"main": "dist/index.js",
"repository": {
"type": "git",
"url": "https://github.com/primitivefinance/primitive-v2-math.git"
"url": "https://github.com/primitivefinance/rmm-math.git"
},
"files": [
"dist",
Expand All @@ -30,9 +31,7 @@
"build": "tsdx build",
"test": "tsdx test",
"lint": "tsdx lint",
"prepare": "tsdx build",
"size": "size-limit",
"analyze": "size-limit --why"
"prepare": "tsdx build"
},
"husky": {
"hooks": {
Expand All @@ -45,21 +44,9 @@
"singleQuote": true,
"trailingComma": "es5"
},
"size-limit": [
{
"path": "dist/src.cjs.production.min.js",
"limit": "20 KB"
},
{
"path": "dist/src.esm.js",
"limit": "20 KB"
}
],
"peerDependencies": {},
"devDependencies": {
"@size-limit/preset-small-lib": "^5.0.1",
"husky": "^7.0.1",
"size-limit": "^5.0.1",
"tsdx": "^0.14.1",
"tslib": "^2.3.0",
"typescript": "^4.3.5"
Expand Down
8 changes: 3 additions & 5 deletions src/CumulativeNormalDistribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export function std_n_pdf(x) {
* @returns CDF^-1 of x
*/
export function inverse_std_n_cdf(x) {
if (x >= 1) return Infinity
if (x <= 0) return -Infinity
return gaussian(0, 1).ppf(x)
}

Expand All @@ -32,7 +34,6 @@ export function inverse_std_n_cdf(x) {
* @returns CDF(CDF(x)^-1)^-1
*/
export function quantilePrime(x) {
if (x > 1 || x < 0) return NaN
return gaussian(0, 1).pdf(inverse_std_n_cdf(x)) ** -1
}

Expand Down Expand Up @@ -89,7 +90,7 @@ export const LOW_TAIL = 0.025
* @returns standard normal invervse cumulative distribution (quantile) function of x
*/
export function getInverseCDFSolidity(p) {
if (p >= 1 || p <= 0) return NaN
if (p >= 1 || p <= 0) return Infinity
if (p <= HIGH_TAIL && p >= LOW_TAIL) {
return centralInverseCDFSolidity(p)
} else if (p < LOW_TAIL) {
Expand Down Expand Up @@ -126,9 +127,6 @@ export function centralInverseCDFSolidity(p) {
*/
export function tailInverseCDFSolidity(p) {
const r = Math.sqrt(Math.log(1 / Math.pow(p, 2)))
/* const c0 = 16.896201479841517652
const c1 = -2.793522347562718412
const c2 = -8.731478129786263127 */
const c3 = -1.000182518730158122
const c0_D = 16.682320830719986527
const c1_D = 4.120411523939115059
Expand Down
60 changes: 0 additions & 60 deletions test/book.test.ts

This file was deleted.

31 changes: 21 additions & 10 deletions test/cumulativeNormalDistribution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,40 @@ describe('Stats Math Library', () => {

describe('standard normal probability density function', () => {
it('moneyness', () => {
expect(2).toEqual(2)
const x = 0.5
expect(math.std_n_pdf(x)).toEqual(0.3520653267642995)
})
})

describe('inverse standard normal cdf (quantile)', () => {
/* it('return nan for out of bounds value: x > 1', () => {
const x = 1.5
expect(math.inverse_std_n_cdf(x)).toEqual(NaN)
}) */
it('return infinity for out of bounds value: x >= 1', () => {
const x = 1
expect(math.inverse_std_n_cdf(x)).toEqual(Infinity)
})

it('return -infinity for out of bounds value: x <= 0', () => {
const x = 0
expect(math.inverse_std_n_cdf(x)).toEqual(-Infinity)
})
})

describe('quantilePrime', () => {
it('return nan for out of bounds value: x > 1', () => {
const x = 1.5
expect(math.quantilePrime(x)).toEqual(NaN)
it('return infinity for out of bounds value: x >= 1', () => {
const x = 1
expect(math.quantilePrime(x)).toEqual(Infinity)
})

it('return infinity for out of bounds value: x <= 0', () => {
const x = 0
expect(math.quantilePrime(x)).toEqual(Infinity)
})

it('return a number for in bounds number', () => {
const x = 1
const x = 0.5
expect(math.quantilePrime(x) > 0).toEqual(!NaN)
})
it('return a number for in bounds number', () => {
const x = 0
const x = 0.5
expect(math.quantilePrime(x) > 0).toEqual(!NaN)
})
})
Expand Down
Loading

0 comments on commit d867da1

Please sign in to comment.