Skip to content

Commit

Permalink
Updated quantile to use sorted values for min and max
Browse files Browse the repository at this point in the history
Fixes #192
  • Loading branch information
sanjayginde committed Nov 22, 2024
1 parent f7aeeb4 commit 9abfc28
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
38 changes: 22 additions & 16 deletions src/statistics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,36 @@
// (c) Mathigon
// ============================================================================


import {total} from '@mathigon/core';
import {lerp} from './arithmetic';

import { total } from "@mathigon/core";
import { lerp } from "./arithmetic";

/** Calculates the mean of an array of numbers. */
export function mean(values: number[]) {
return values.length ? total(values) / values.length : 0;
}

/** Finds the quantile of an array of numbers for the cumulative probability p. */
export function quantile(values: number[], p: number, method: number = 1): number {
export function quantile(
values: number[],
p: number,
method: number = 1
): number {
const n = values.length;
if (!n) return 0;

const sorted = values.slice(0).sort((a, b) => (a - b));
if (p === 0) return values[0];
if (p === 1) return values[n - 1];
const sorted = values.slice(0).sort((a, b) => a - b);
if (p === 0) return sorted[0];
if (p === 1) return sorted[n - 1];

// See https://en.wikipedia.org/wiki/Quantile#Estimating_quantiles_from_a_sample
if (![1, 2, 3].includes(method)) throw new RangeError('Invalid quantile method.');
const index = (method === 1) ? n * p - 0.5 : // Matlab, Mathematica
(method === 2) ? (n - 1) * p : // Excel, NumPy, Google Docs, R, Python (option)
(n + 1) * p - 1; // Python, Excel (option)
if (![1, 2, 3].includes(method))
throw new RangeError("Invalid quantile method.");
const index =
method === 1
? n * p - 0.5 // Matlab, Mathematica
: method === 2
? (n - 1) * p // Excel, NumPy, Google Docs, R, Python (option)
: (n + 1) * p - 1; // Python, Excel (option)

if (Number.isInteger(index)) return sorted[index];
const floor = Math.floor(index);
Expand All @@ -46,7 +52,7 @@ export function mode(values: number[]) {
const counts = new Map<number, number>();

let maxCount = -1;
let result: number|undefined = undefined;
let result: number | undefined = undefined;

for (const v of values) {
if (!counts.has(v)) counts.set(v, 0);
Expand Down Expand Up @@ -80,14 +86,14 @@ export function stdDev(values: number[]) {

/** Calculates the covariance of the numbers in two arrays aX and aY. */
export function covariance(aX: number[], aY: number[]) {
if (aX.length !== aY.length) throw new Error('Array length mismatch.');
if (aX.length !== aY.length) throw new Error("Array length mismatch.");
const sum = aX.reduce((a, v, i) => a + v * aY[i], 0);
return (sum - total(aX) * total(aY) / aX.length) / aX.length;
return (sum - (total(aX) * total(aY)) / aX.length) / aX.length;
}

/** Calculates the correlation between the numbers in two arrays aX and aY. */
export function correlation(aX: number[], aY: number[]) {
if (aX.length !== aY.length) throw new Error('Array length mismatch.');
if (aX.length !== aY.length) throw new Error("Array length mismatch.");
const covarXY = covariance(aX, aY);
const stdDevX = stdDev(aX);
const stdDevY = stdDev(aY);
Expand Down
17 changes: 11 additions & 6 deletions test/statistics-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
// (c) Mathigon
// =============================================================================

import tape from "tape";
import { mode, quantile } from "../src";

import tape from 'tape';
import {mode, quantile} from '../src';


tape('mode', (test) => {
tape("mode", (test) => {
test.equal(mode([]), undefined);
test.equal(mode([2]), 2);
test.equal(mode([2, 3]), undefined);
Expand All @@ -19,7 +17,7 @@ tape('mode', (test) => {
test.end();
});

tape('quantile', (test) => {
tape("quantile", (test) => {
test.equal(quantile([], 0.5), 0);

const evenPopulation = [1, 2, 3, 4, 5];
Expand All @@ -35,5 +33,12 @@ tape('quantile', (test) => {
test.equal(quantile(oddPopulation, 0.5), 3.5);
test.equal(quantile(oddPopulation, 0.75), 5);
test.equal(quantile(oddPopulation, 1), 6);

const randomPopulation = [4, 1, 2, 6, 3, 5];
test.equal(quantile(randomPopulation, 0), 1);
test.equal(quantile(randomPopulation, 0.25), 2);
test.equal(quantile(randomPopulation, 0.5), 3.5);
test.equal(quantile(randomPopulation, 0.75), 5);
test.equal(quantile(randomPopulation, 1), 6);
test.end();
});

0 comments on commit 9abfc28

Please sign in to comment.