Published unlisted
Edited
Jun 28, 2020
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
check_quantileIndexWithQuantile = {
const a = qi.median;
const b = d3.quantile(testArray.array, quantile);
if (a === b) {
return html `check match with d3.quantile: <br><span style="background:lightgreen";>correct ${a} (quantileIndex.median) <br>${b} (d3.quantile)</span>`;
} else { return html `check match with d3.quantile: <span style="background:IndianRed";>wrong ${a} (quantileIndex.median)<br>${b} (d3.quantile)</span>`;
}
}
Insert cell
d3.quantile(testArray.array, quantile)
Insert cell
qi = {
return quantileIndex(testArray.array, quantile)
}
Insert cell
testArray = {
let ar = [];
let res = {};
for (let i = 0; i < 99; i++){
ar.push(Math.random());
}
ar.sort();
res.value0 = ar[Math.floor((ar.length - 1) * quantile)];
res.value1 = ar[Math.ceil((ar.length - 1) * quantile)] ;
res.median = (ar[Math.ceil((ar.length - 1) * quantile)] + ar[Math.floor((ar.length - 1) * quantile)]) * .5;
res.array = shuffle(ar);
res.index0 = ar.indexOf(res.value0);
res.index1 = ar.indexOf(res.value1);
return res;
}
Insert cell
Insert cell
Insert cell
Insert cell
values = [20, 30, 10, -2, -10, 0]
Insert cell
quantileIndex(values, 0.5)
Insert cell
function quantileIndex(values, p, valueof) {
var n,
mix = 0,
index0,
index1;
values = valueof
? Float64Array.from(values, valueof)
: Float64Array.from(values);

const value = i => values[i],
indices = Int32Array.from({ length: values.length }, (_, i) => i);
console.log(indices);
if (!(n = values.length)) return;
if ((p = +p) <= 0 || n < 2) {
index0 = index1 = d3.minIndex(values);
} else if (p >= 1) {
index0 = index1 = d3.maxIndex(values);
} else {
let i = (n - 1) * p,
i0 = Math.floor(i);
const indices0 = d3
.quickselect(indices, i0, 0, n - 1, (i, j) =>
d3.ascending(value(i), value(j))
)
.subarray(0, i0 + 1);
index0 = indices0[d3.maxIndex(indices0, value)];
const indices1 = indices.subarray(i0 + 1);
index1 = indices1[d3.minIndex(indices1, value)];
mix = i - i0;
}
const value0 = values[index0],
value1 = values[index1],
median = mix ? (value0 + value1) / 2 : value0;
return { index0, index1, value0, value1, median, indices };
}
Insert cell
Insert cell
quantileIndex2(values, 0.5)
Insert cell
function quantileIndex2(values, p, valueof) {
if (valueof) values = values.map((d, i) => valueof(d, i, values));
const m = d3.quantile(values, p);

let index0,
index1,
value0 = -Infinity,
value1 = Infinity;
for (let i = 0; i < values.length; i++) {
const x = values[i];
if (x <= m) {
if (x > value0) (index0 = i), (value0 = x);
} else {
if (x < value1) (index1 = i), (value1 = x);
}
}
return { index0, index1: m === value0 ? index0 : index1, value0, value1, m };
}
Insert cell
quantileIndex2([0,0,0], 0.5)
Insert cell
d3 = require("d3-array@2")
Insert cell
huge = Float64Array.from({ length: 1e6 }, Math.random)
Insert cell
function evaluate(f) {
const t = performance.now();
const r = f(huge, 0.5);
r.t = performance.now() - t;
return r;
}
Insert cell
evaluate(quantileIndex)
Insert cell
evaluate(quantileIndex2)
Insert cell
evaluate(d => ({ m: d3.quantile(huge, 0.5) }))
Insert cell
evaluate(d => ({ m: quantileIndex(huge, 0.5).median }))
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