Last active
August 19, 2021 16:55
-
-
Save syzer/293d8a3a6e0ea37c849af3ca4c86df5a to your computer and use it in GitHub Desktop.
How many to solve to improve percentile using ramda.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// sort array ascending | |
const asc = arr => arr.sort((a, b) => a - b); | |
// sample standard deviation | |
const std = (arr) => { | |
const mu = mean(arr) | |
const diffArr = arr.map(a => (a - mu) ** 2) | |
return Math.sqrt(sum(diffArr) / (arr.length - 1)) | |
} | |
const quantile = (arr, q) => { | |
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] | |
} | |
} | |
const median2 = arr => quantile(arr, .50) | |
// same as R.median | |
median2(arr) === median(arr) | |
const arr = [1,2,3,4,5,6,7,8,9,10] | |
const percentile = (arr, val) => | |
(100 * | |
arr.reduce( | |
(acc, v) => acc + (v < val ? 1 : 0) + (v === val ? 0.5 : 0), | |
0 | |
)) / | |
arr.length | |
percentile(arr, 6) // => 55 | |
quantile(arr, .55) // 5.95 | |
quantile(arr, .70) - quantile(arr, .55) | |
// need increase of 1.35 to get to 70% percentile | |
percentile(arr, 6 + 1.3499999999999996) // 70 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment