Public
Edited
Aug 14, 2023
Insert cell
Insert cell
d3.quantile([1, 2, 3, 4, 5], 0.5)
Insert cell
d3.quantile([1, 2, 3, 4, 5], 0.0)
Insert cell
d3.quantile([1, 2, 3, 4, 5], 0.1)
Insert cell
quantile([1, 2, 3, 4, 5], 0.1)
Insert cell
strictEqual = (actual, expected) => {
if (actual !== expected) {
throw new Error(`actual !== expected, ${JSON.stringify({ actual, expected })}`)
}
}
Insert cell
{
const even = [3, 6, 7, 8, 8, 10, 13, 15, 16, 20];
strictEqual(quantile(even, 0), 3);
strictEqual(quantile(even, 0.25), 7.25);
strictEqual(quantile(even, 0.5), 9);
strictEqual(quantile(even, 0.75), 14.5);
strictEqual(quantile(even, 1), 20);
const odd = [3, 6, 7, 8, 8, 9, 10, 13, 15, 16, 20];
strictEqual(quantile(odd, 0), 3);
strictEqual(quantile(odd, 0.25), 7.5);
strictEqual(quantile(odd, 0.5), 9);
strictEqual(quantile(odd, 0.75), 14);
strictEqual(quantile(odd, 1), 20);
}
Insert cell
// R-7 estimate type and interpolation scheme.
quantile = {
const sort = items => items.sort((item, otherItem) => item - otherItem)

return (items, p) => {
items = sort(items)
const n = items.length

if (p <= 0 || n < 2) {
return items[0]
}
if (p >= 1) {
return items[n - 1]
}

const index = (n - 1) * p
const index0 = Math.floor(index)
const item = items[index0]
const item1 = items[index0 + 1]
return item + (item1 - item) * (index - index0)
}
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more