(sortedValues: number[], quantileValue: number)
| 71 | * @returns The computed quantile value |
| 72 | */ |
| 73 | export function quantile(sortedValues: number[], quantileValue: number): number { |
| 74 | const length = sortedValues.length |
| 75 | |
| 76 | if (length === 0) return 0 |
| 77 | |
| 78 | if (quantileValue <= 0) { |
| 79 | const first = sortedValues[0] |
| 80 | return first === undefined ? 0 : first |
| 81 | } |
| 82 | |
| 83 | if (quantileValue >= 1) { |
| 84 | const last = sortedValues[length - 1] |
| 85 | return last === undefined ? 0 : last |
| 86 | } |
| 87 | |
| 88 | const position = (length - 1) * quantileValue |
| 89 | const lowerIndex = Math.floor(position) |
| 90 | const upperIndex = Math.ceil(position) |
| 91 | const weight = position - lowerIndex |
| 92 | const lower = sortedValues[lowerIndex]! |
| 93 | const upper = sortedValues[upperIndex]! |
| 94 | |
| 95 | return lower + (upper - lower) * weight |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Applies winsorization to a numeric array. |
no outgoing calls
no test coverage detected