Python's statistics.median_grouped in TypeScript

✓ Verified: Python 3.12
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 { median_grouped } from 'locutus/python/statistics/median_grouped'.

Or with CommonJS: const { median_grouped } = require('locutus/python/statistics/median_grouped')

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.

#codeexpected result
1median_grouped([52, 52, 53, 54])52.5
2median_grouped([52, 52, 53, 54], 2)52
3median_grouped([true, false, true])0.75

Notes

  • Estimates the median for data binned around interval midpoints using grouped interpolation.

Dependencies

This function uses the following Locutus functions:

Here's what our current TypeScript equivalent to Python's statistics.median_grouped looks like.

import { assertStatisticsArray, sortStatisticsValues, toFloatStatisticNumber } from '../_helpers/_statistics.ts'

export function median_grouped(data: unknown, interval = 1): number {
// discuss at: https://locutus.io/python/statistics/median_grouped/
// parity verified: Python 3.12
// original by: Kevin van Zonneveld (https://kvz.io)
// note 1: Estimates the median for data binned around interval midpoints using grouped interpolation.
// example 1: median_grouped([52, 52, 53, 54])
// returns 1: 52.5
// example 2: median_grouped([52, 52, 53, 54], 2)
// returns 2: 52
// example 3: median_grouped([true, false, true])
// returns 3: 0.75

const values = sortStatisticsValues(assertStatisticsArray(data, 'median_grouped'), 'median_grouped')
const n = values.length
if (n === 0) {
throw new Error('no median for empty data')
}

const x = values[Math.floor(n / 2)]
let i = 0
while (i < values.length && values[i] !== x) {
i += 1
}

let j = i
while (j < values.length && values[j] === x) {
j += 1
}

const numericInterval = toFloatStatisticNumber(interval, 'median_grouped')
const numericX = toFloatStatisticNumber(x, 'median_grouped')
const lowerLimit = numericX - numericInterval / 2
const cumulativeFrequency = i
const frequency = j - i

return lowerLimit + (numericInterval * (n / 2 - cumulativeFrequency)) / frequency
}

Improve this function

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.

View on GitHub · Edit on GitHub · View Raw


Help us add more

Got a rainy Sunday afternoon and a taste for a porting puzzle?

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.

« More Python statistics functions


Star