MCPcopy
hub / github.com/npmx-dev/npmx.dev / quantile

Function quantile

app/utils/charts.ts:73–96  ·  view source on GitHub ↗
(sortedValues: number[], quantileValue: number)

Source from the content-addressed store, hash-verified

71 * @returns The computed quantile value
72 */
73export 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.

Callers 2

charts.spec.tsFile · 0.90
winsorizeFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected