Public
Edited
Jul 23, 2023
Insert cell
Insert cell
function _virtualIndex(alpha, beta) {
return function (q, n) {
return q * (n - alpha - beta + 1) + alpha;
};
}
Insert cell
discontinuous = _virtualIndex(0,0)
Insert cell
x = math.range(0,10,0.1)._data
Insert cell
function _percentile(a, q, method, sorted) {
const A = sorted ? a : math.sort(a);
return methods[method](A, q);
}
Insert cell
linearVI = _virtualIndex(1,1)
Insert cell
methods = ({
linear: (a, q) => {
const n = a.length;
const ig = linearVI(q, n);
const i = Math.trunc(ig);
const g = ig-i;
return g > 0 ? a[i-1] + (a[i] - a[i-1]) * g : a[i-1];
},
invertedCDF : (a, q) => {
const n = a.length;
const ig = q*n
const i = Math.trunc(ig);
const g = ig-i;
return g > 0 ? a[i] : a[i-1]
},
averagedInvertedCDF : (a, q) => {
const n = a.length;
const ig = q*n
const i = Math.trunc(ig);
const g = ig-i;
return g > 0 ? a[i] : (a[i-1]+a[i])/2
},
closestObservation : (a, q) => {
const n = a.length;
const ig = q*n
const i = Math.trunc(ig);
const g = ig%1//ig-i;
if(g>0.001){
return a[i]
} else if(i%2 > 0.1){
return a[i]
} else {
return a[i-1]
}
},
normalUnbiased : (a, q) => {
const n = a.length;
const ig = _virtualIndex(3/8, 3/8)(q, n) -1
const j = Math.trunc(ig);
const i = j + 1;
const g = ig-j
return g > 0 ? a[i] + (a[j] - a[i]) * g : a[i];
},
})
Insert cell
Plot.plot({
color: {legend: true},
marks: [
Plot.ruleY([0]),
Plot.lineY(data2, {x: "p", y: "val", stroke: "method"})
]
})
Insert cell
evenAndODD(4)
Insert cell
function evenAndODD(x){return {isOdd : x%2 !== 0, isEven : x%2 === 0}}
Insert cell
function percentile(a, q, axis, options) {
return math.apply(a, axis, (x) =>
_percentile(x, q, options.method, options.sorted)
);
}
Insert cell
viewof pslide = Inputs.range([0, 1], {label: "P-th percentile", step: 0.001})
Insert cell
viewof method = Inputs.select(["linear", "invertedCDF"], {label: "Select one"})
Insert cell
percentile([15, 20, 35, 40, 50], pslide, 0, {method:method, sorted:true})
Insert cell
test = [0,1,2,3]
Insert cell
perc2 = math.range(0,1,0.01,true)._data
Insert cell
data2 = {
const D = []
perc2.forEach(d => D.push(({p:d, val:percentile(test, d, 0, {method:"linear", sorted:true}), method:"linear" })))
perc2.forEach(d => D.push(({p:d, val:percentile(test, d, 0, {method:"invertedCDF", sorted:true}), method:"invertedCDF" })))
perc2.forEach(d => D.push(({p:d, val:percentile(test, d, 0, {method:"averagedInvertedCDF", sorted:true}), method:"averagedInvertedCDF" })))
perc2.forEach(d => D.push(({p:d, val:percentile(test, d, 0, {method:"closestObservation", sorted:true}), method:"closestObservation" })))
return D
}
Insert cell
m = math.parser()
Insert cell
math = require("mathjs@11.8.2")
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