Skip to content

Commit

Permalink
fix(nan-infinity): returns infinity rather than nan for pdf and ppf
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexangelj committed Dec 1, 2021
1 parent 276c532 commit 5f51248
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 82 deletions.
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.
11 changes: 6 additions & 5 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 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

0 comments on commit 5f51248

Please sign in to comment.