UNPKG

@liquality/fee-suggestions

Version:

JavaScript library that suggest fees on Ethereum after EIP-1559 using historical data using ethers.js

63 lines (62 loc) 1.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.iqr = exports.quantile = void 0; /** * Computes a quantile for a numeric array. * @param arr * @param prob * @param sorted * @returns */ function quantile(arr, prob, sorted) { if (sorted === void 0) { sorted = true; } var len = arr.length; if (!sorted) { arr = arr.sort(ascending); } if (prob === 0.0) { return arr[0]; } if (prob === 1.0) { return arr[len - 1]; } var vectorIndex = len * prob - 1; if (vectorIndex === Math.floor(vectorIndex)) { return (arr[vectorIndex] + arr[vectorIndex + 1]) / 2.0; } return arr[Math.ceil(vectorIndex)]; } exports.quantile = quantile; /** * 1st quartile or lower quartile basically separate the lowest 25% of data from the highest 75%. */ var LOWER_QUARTILE = 0.25; /** * 3rd quartile or the upper quartile separate the highest 25% of data from the lowest 75%. */ var UPPER_QUARTILE = 0.75; /** * * The IQR (Interquartile Range) describes the middle 50% of values when ordered from lowest to highest. * To find the interquartile range (IQR), ​first find the median (middle value) of the lower and upper half of the data. * These values are quartile 1 (Q1) and quartile 3 (Q3). The IQR is the difference between Q3 and Q1. * ---------------------------------- * | | * | 25% 25% 25% 25% | * | Q1 Q2 Q3 | * ---------------------------------- * Median * @param arr * @returns */ function iqr(arr) { var sorted = arr.sort(ascending); var Q1 = quantile(sorted, LOWER_QUARTILE); var Q3 = quantile(sorted, UPPER_QUARTILE); var filtered = sorted.filter(function (n) { return n <= Q3 && n >= Q1; }); return filtered; } exports.iqr = iqr; function ascending(a, b) { return a - b; }