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

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more