const asc = (arr: Array) => arr.sort((a, b) => a - b); const sum = (arr: Array) => arr.reduce((a, b) => a + b, 0); const quantile = (arr: Array, q: number) => { if (!arr.length) { return 0; } const sorted = asc(arr); const pos = (sorted.length - 1) * q; const base = Math.floor(pos); const rest = pos - base; if (sorted[base + 1] !== undefined) { return sorted[base] + rest * (sorted[base + 1] - sorted[base]); } else { return sorted[base]; } }; export const mean = (arr: Array) => arr.length ? sum(arr) / arr.length : 0; export const median = (arr: Array) => q50(arr); export const stdDev = (arr: Array) => { if (!arr.length && arr.length < 2) { return 0; } const meanValue = mean(arr); const diffArr = arr.map((item) => (item - meanValue) ** 2); return Math.sqrt(sum(diffArr) / (arr.length - 1)); }; export const q5 = (arr: Array) => quantile(arr, 0.5); export const q50 = (arr: Array) => quantile(arr, 0.5); export const q95 = (arr: Array) => quantile(arr, 0.95); export const q99 = (arr: Array) => quantile(arr, 0.99);