Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated quantile to use sorted values for min and max #193

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/statistics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export function quantile(values: number[], p: number, method: number = 1): numbe
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];
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.');
Expand Down
8 changes: 8 additions & 0 deletions test/statistics-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,13 @@ 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();
});