Examples tested against actual runtime. CI re-verifies continuously. Only documented examples are tested.
How to use
Install via yarn add locutus and import:
import { quantiles } from 'locutus/python/statistics/quantiles'.
Or with CommonJS: const { quantiles } = require('locutus/python/statistics/quantiles')
Use a bundler that supports tree-shaking so you only ship the functions you actually use.
Vite,
webpack,
Rollup, and
Parcel
all handle this. For server-side use this is less of a concern.
Examples
These examples are extracted from test cases that automatically verify our functions against their native counterparts.
#
code
expected result
1
quantiles([1, 2, 3, 4, 5])
[1.5, 3, 4.5]
2
quantiles([1, 2, 3, 4, 5], 4, 'inclusive')
[2, 3, 4]
3
quantiles([1, 2], 4)
[0.75, 1.5, 2.25]
Notes
Divides numeric sample data into equally probable intervals using Python’s inclusive or exclusive interpolation.
Dependencies
This function uses the following Locutus functions:
exportfunctionquantiles(data: unknown, n = 4, method: 'exclusive' | 'inclusive' = 'exclusive'): number[] { // discuss at: https://locutus.io/python/statistics/quantiles/ // parity verified: Python 3.12 // original by: Kevin van Zonneveld (https://kvz.io) // note 1: Divides numeric sample data into equally probable intervals using Python's inclusive or exclusive interpolation. // example 1: quantiles([1, 2, 3, 4, 5]) // returns 1: [1.5, 3, 4.5] // example 2: quantiles([1, 2, 3, 4, 5], 4, 'inclusive') // returns 2: [2, 3, 4] // example 3: quantiles([1, 2], 4) // returns 3: [0.75, 1.5, 2.25]
if (n < 1) { thrownewError('n must be at least 1') }
const sorted = sortStatisticsValues(assertStatisticsArray(data, 'quantiles'), 'quantiles') if (sorted.length < 2) { thrownewError('must have at least two data points') }
const values = sorted.map((value) => { if (typeof value === 'string') { thrownewTypeError("unsupported operand type(s) for /: 'str' and 'int'") } returntypeof value === 'boolean' ? (value ? 1 : 0) : Number(value) })
constresult: number[] = [] if (method === 'inclusive') { const m = values.length - 1 for (let i = 1; i < n; i += 1) { const product = i * m const j = Math.floor(product / n) const delta = product % n const interpolated = ((values[j] ?? 0) * (n - delta) + (values[j + 1] ?? 0) * delta) / n result.push(interpolated) } return result }
if (method === 'exclusive') { const ld = values.length const m = ld + 1 for (let i = 1; i < n; i += 1) { let j = Math.floor((i * m) / n) j = j < 1 ? 1 : j > ld - 1 ? ld - 1 : j const delta = i * m - j * n const interpolated = ((values[j - 1] ?? 0) * (n - delta) + (values[j] ?? 0) * delta) / n result.push(interpolated) } return result }
exportfunctionquantiles(data, n = 4, method = 'exclusive') { // discuss at: https://locutus.io/python/statistics/quantiles/ // parity verified: Python 3.12 // original by: Kevin van Zonneveld (https://kvz.io) // note 1: Divides numeric sample data into equally probable intervals using Python's inclusive or exclusive interpolation. // example 1: quantiles([1, 2, 3, 4, 5]) // returns 1: [1.5, 3, 4.5] // example 2: quantiles([1, 2, 3, 4, 5], 4, 'inclusive') // returns 2: [2, 3, 4] // example 3: quantiles([1, 2], 4) // returns 3: [0.75, 1.5, 2.25]
if (n < 1) { thrownewError('n must be at least 1') }
const sorted = sortStatisticsValues(assertStatisticsArray(data, 'quantiles'), 'quantiles') if (sorted.length < 2) { thrownewError('must have at least two data points') }
const values = sorted.map((value) => { if (typeof value === 'string') { thrownewTypeError("unsupported operand type(s) for /: 'str' and 'int'") } returntypeof value === 'boolean' ? (value ? 1 : 0) : Number(value) })
const result = [] if (method === 'inclusive') { const m = values.length - 1 for (let i = 1; i < n; i += 1) { const product = i * m const j = Math.floor(product / n) const delta = product % n const interpolated = ((values[j] ?? 0) * (n - delta) + (values[j + 1] ?? 0) * delta) / n result.push(interpolated) } return result }
if (method === 'exclusive') { const ld = values.length const m = ld + 1 for (let i = 1; i < n; i += 1) { let j = Math.floor((i * m) / n) j = j < 1 ? 1 : j > ld - 1 ? ld - 1 : j const delta = i * m - j * n const interpolated = ((values[j - 1] ?? 0) * (n - delta) + (values[j] ?? 0) * delta) / n result.push(interpolated) } return result }
functionassertStatisticsArray(data: unknown, functionName: string): unknown[] { if (!Array.isArray(data)) { thrownewTypeError(`${functionName}() data must be an array`) }
const firstKind = getStatisticsSortKind(values[0], functionName) for (let index = 1; index < values.length; index += 1) { const currentKind = getStatisticsSortKind(values[index], functionName) if (currentKind !== firstKind) { thrownewTypeError(`${functionName}() requires data values that can be ordered together`) } }
functiontoNumericSortValue(value: StatisticsSortableInput): number { returntypeof value === 'boolean' ? (value ? 1 : 0) : Number(value) }
// python/statistics/quantiles (target function module) functionquantiles(data: unknown, n = 4, method: 'exclusive' | 'inclusive' = 'exclusive'): number[] { // discuss at: https://locutus.io/python/statistics/quantiles/ // parity verified: Python 3.12 // original by: Kevin van Zonneveld (https://kvz.io) // note 1: Divides numeric sample data into equally probable intervals using Python's inclusive or exclusive interpolation. // example 1: quantiles([1, 2, 3, 4, 5]) // returns 1: [1.5, 3, 4.5] // example 2: quantiles([1, 2, 3, 4, 5], 4, 'inclusive') // returns 2: [2, 3, 4] // example 3: quantiles([1, 2], 4) // returns 3: [0.75, 1.5, 2.25]
if (n < 1) { thrownewError('n must be at least 1') }
const sorted = sortStatisticsValues(assertStatisticsArray(data, 'quantiles'), 'quantiles') if (sorted.length < 2) { thrownewError('must have at least two data points') }
const values = sorted.map((value) => { if (typeof value === 'string') { thrownewTypeError("unsupported operand type(s) for /: 'str' and 'int'") } returntypeof value === 'boolean' ? (value ? 1 : 0) : Number(value) })
constresult: number[] = [] if (method === 'inclusive') { const m = values.length - 1 for (let i = 1; i < n; i += 1) { const product = i * m const j = Math.floor(product / n) const delta = product % n const interpolated = ((values[j] ?? 0) * (n - delta) + (values[j + 1] ?? 0) * delta) / n result.push(interpolated) } return result }
if (method === 'exclusive') { const ld = values.length const m = ld + 1 for (let i = 1; i < n; i += 1) { let j = Math.floor((i * m) / n) j = j < 1 ? 1 : j > ld - 1 ? ld - 1 : j const delta = i * m - j * n const interpolated = ((values[j - 1] ?? 0) * (n - delta) + (values[j] ?? 0) * delta) / n result.push(interpolated) } return result }
// python/_helpers/_statistics (Locutus helper dependency) functionassertStatisticsArray(data, functionName) { if (!Array.isArray(data)) { thrownewTypeError(`${functionName}() data must be an array`) }
const firstKind = getStatisticsSortKind(values[0], functionName) for (let index = 1; index < values.length; index += 1) { const currentKind = getStatisticsSortKind(values[index], functionName) if (currentKind !== firstKind) { thrownewTypeError(`${functionName}() requires data values that can be ordered together`) } }
// python/statistics/quantiles (target function module) functionquantiles(data, n = 4, method = 'exclusive') { // discuss at: https://locutus.io/python/statistics/quantiles/ // parity verified: Python 3.12 // original by: Kevin van Zonneveld (https://kvz.io) // note 1: Divides numeric sample data into equally probable intervals using Python's inclusive or exclusive interpolation. // example 1: quantiles([1, 2, 3, 4, 5]) // returns 1: [1.5, 3, 4.5] // example 2: quantiles([1, 2, 3, 4, 5], 4, 'inclusive') // returns 2: [2, 3, 4] // example 3: quantiles([1, 2], 4) // returns 3: [0.75, 1.5, 2.25]
if (n < 1) { thrownewError('n must be at least 1') }
const sorted = sortStatisticsValues(assertStatisticsArray(data, 'quantiles'), 'quantiles') if (sorted.length < 2) { thrownewError('must have at least two data points') }
const values = sorted.map((value) => { if (typeof value === 'string') { thrownewTypeError("unsupported operand type(s) for /: 'str' and 'int'") } returntypeof value === 'boolean' ? (value ? 1 : 0) : Number(value) })
const result = [] if (method === 'inclusive') { const m = values.length - 1 for (let i = 1; i < n; i += 1) { const product = i * m const j = Math.floor(product / n) const delta = product % n const interpolated = ((values[j] ?? 0) * (n - delta) + (values[j + 1] ?? 0) * delta) / n result.push(interpolated) } return result }
if (method === 'exclusive') { const ld = values.length const m = ld + 1 for (let i = 1; i < n; i += 1) { let j = Math.floor((i * m) / n) j = j < 1 ? 1 : j > ld - 1 ? ld - 1 : j const delta = i * m - j * n const interpolated = ((values[j - 1] ?? 0) * (n - delta) + (values[j] ?? 0) * delta) / n result.push(interpolated) } return result }
Locutus is a community effort following
The McDonald's Theory:
we ship first iterations, hoping others will improve them.
If you see something that could be better, we'd love your contribution.
Click "New file" in the appropriate folder
on GitHub.
This will fork the project to your account, directly add the file to it, and send a
Pull Request to us.
We will then review it. If it's useful to the project and in line with our
contributing guidelines
your work will become part of Locutus and you'll be automatically credited
in the authors
section accordingly.