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 { harmonic_mean } from 'locutus/python/statistics/harmonic_mean'.
Or with CommonJS: const { harmonic_mean } = require('locutus/python/statistics/harmonic_mean')
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
harmonic_mean([40, 60])
47.99999999999999
2
harmonic_mean([40, 60], [5, 30])
56
3
harmonic_mean([0, 10])
0
Notes
Returns the harmonic mean and supports optional positive weights.
Dependencies
This function uses the following Locutus functions:
exportfunctionharmonic_mean(data: unknown, weights?: unknown): number { // discuss at: https://locutus.io/python/statistics/harmonic_mean/ // parity verified: Python 3.12 // original by: Kevin van Zonneveld (https://kvz.io) // note 1: Returns the harmonic mean and supports optional positive weights. // example 1: harmonic_mean([40, 60]) // returns 1: 47.99999999999999 // example 2: harmonic_mean([40, 60], [5, 30]) // returns 2: 56 // example 3: harmonic_mean([0, 10]) // returns 3: 0
const values = assertStatisticsArray(data, 'harmonic_mean').map((value) =>toStatisticNumber(value, 'harmonic_mean')) if (values.length < 1) { thrownewError('harmonic_mean requires at least one data point') }
if (values.length === 1 && weights === undefined) { const value = values[0] ?? 0 if (value < 0) { thrownewError('harmonic mean does not support negative values') } return value }
const errmsg = 'harmonic mean does not support negative values' const weightValues = weights === undefined ? newArray(values.length).fill(1) : assertStatisticsArray(weights, 'harmonic_mean').map((value) =>toStatisticNumber(value, 'harmonic_mean'))
if (weightValues.length !== values.length) { thrownewError('Number of weights does not match data size') }
if (weightValues.some((value) => value < 0) || values.some((value) => value < 0)) { thrownewError(errmsg) }
const sumWeights = weightValues.reduce((sum, value) => sum + value, 0)
try { let total = 0 for (let index = 0; index < values.length; index += 1) { const value = values[index] ?? 0 const weight = weightValues[index] ?? 0 total += weight ? weight / value : 0 } if (total <= 0) { thrownewError('Weighted sum must be positive') } return sumWeights / total } catch (error) { if (error instanceofError && error.message === 'Weighted sum must be positive') { throw error } return0 } }
exportfunctionharmonic_mean(data, weights) { // discuss at: https://locutus.io/python/statistics/harmonic_mean/ // parity verified: Python 3.12 // original by: Kevin van Zonneveld (https://kvz.io) // note 1: Returns the harmonic mean and supports optional positive weights. // example 1: harmonic_mean([40, 60]) // returns 1: 47.99999999999999 // example 2: harmonic_mean([40, 60], [5, 30]) // returns 2: 56 // example 3: harmonic_mean([0, 10]) // returns 3: 0
const values = assertStatisticsArray(data, 'harmonic_mean').map((value) =>toStatisticNumber(value, 'harmonic_mean')) if (values.length < 1) { thrownewError('harmonic_mean requires at least one data point') }
if (values.length === 1 && weights === undefined) { const value = values[0] ?? 0 if (value < 0) { thrownewError('harmonic mean does not support negative values') } return value }
const errmsg = 'harmonic mean does not support negative values' const weightValues = weights === undefined ? newArray(values.length).fill(1) : assertStatisticsArray(weights, 'harmonic_mean').map((value) =>toStatisticNumber(value, 'harmonic_mean'))
if (weightValues.length !== values.length) { thrownewError('Number of weights does not match data size') }
if (weightValues.some((value) => value < 0) || values.some((value) => value < 0)) { thrownewError(errmsg) }
const sumWeights = weightValues.reduce((sum, value) => sum + value, 0)
try { let total = 0 for (let index = 0; index < values.length; index += 1) { const value = values[index] ?? 0 const weight = weightValues[index] ?? 0 total += weight ? weight / value : 0 } if (total <= 0) { thrownewError('Weighted sum must be positive') } return sumWeights / total } catch (error) { if (error instanceofError && error.message === 'Weighted sum must be positive') { throw error } return0 } }
// python/_helpers/_statistics (Locutus helper dependency) functionassertStatisticsArray(data: unknown, functionName: string): unknown[] { if (!Array.isArray(data)) { thrownewTypeError(`${functionName}() data must be an array`) }
return data }
functiontoStatisticNumber(value: unknown, functionName: string): number { if (typeof value === 'number') { return value }
if (typeof value === 'boolean') { return value ? 1 : 0 }
if (typeof value === 'bigint') { const numericValue = Number(value) if (!Number.isSafeInteger(numericValue)) { thrownewRangeError(`${functionName}() bigint values must fit within JS safe integers`) }
return numericValue }
thrownewTypeError(`${functionName}() data must contain only real numbers`) }
// python/statistics/harmonic_mean (target function module) functionharmonic_mean(data: unknown, weights?: unknown): number { // discuss at: https://locutus.io/python/statistics/harmonic_mean/ // parity verified: Python 3.12 // original by: Kevin van Zonneveld (https://kvz.io) // note 1: Returns the harmonic mean and supports optional positive weights. // example 1: harmonic_mean([40, 60]) // returns 1: 47.99999999999999 // example 2: harmonic_mean([40, 60], [5, 30]) // returns 2: 56 // example 3: harmonic_mean([0, 10]) // returns 3: 0
const values = assertStatisticsArray(data, 'harmonic_mean').map((value) =>toStatisticNumber(value, 'harmonic_mean')) if (values.length < 1) { thrownewError('harmonic_mean requires at least one data point') }
if (values.length === 1 && weights === undefined) { const value = values[0] ?? 0 if (value < 0) { thrownewError('harmonic mean does not support negative values') } return value }
const errmsg = 'harmonic mean does not support negative values' const weightValues = weights === undefined ? newArray(values.length).fill(1) : assertStatisticsArray(weights, 'harmonic_mean').map((value) =>toStatisticNumber(value, 'harmonic_mean'))
if (weightValues.length !== values.length) { thrownewError('Number of weights does not match data size') }
if (weightValues.some((value) => value < 0) || values.some((value) => value < 0)) { thrownewError(errmsg) }
const sumWeights = weightValues.reduce((sum, value) => sum + value, 0)
try { let total = 0 for (let index = 0; index < values.length; index += 1) { const value = values[index] ?? 0 const weight = weightValues[index] ?? 0 total += weight ? weight / value : 0 } if (total <= 0) { thrownewError('Weighted sum must be positive') } return sumWeights / total } catch (error) { if (error instanceofError && error.message === 'Weighted sum must be positive') { throw error } return0 } }
// python/_helpers/_statistics (Locutus helper dependency) functionassertStatisticsArray(data, functionName) { if (!Array.isArray(data)) { thrownewTypeError(`${functionName}() data must be an array`) }
return data }
functiontoStatisticNumber(value, functionName) { if (typeof value === 'number') { return value }
if (typeof value === 'boolean') { return value ? 1 : 0 }
if (typeof value === 'bigint') { const numericValue = Number(value) if (!Number.isSafeInteger(numericValue)) { thrownewRangeError(`${functionName}() bigint values must fit within JS safe integers`) }
return numericValue }
thrownewTypeError(`${functionName}() data must contain only real numbers`) }
// python/statistics/harmonic_mean (target function module) functionharmonic_mean(data, weights) { // discuss at: https://locutus.io/python/statistics/harmonic_mean/ // parity verified: Python 3.12 // original by: Kevin van Zonneveld (https://kvz.io) // note 1: Returns the harmonic mean and supports optional positive weights. // example 1: harmonic_mean([40, 60]) // returns 1: 47.99999999999999 // example 2: harmonic_mean([40, 60], [5, 30]) // returns 2: 56 // example 3: harmonic_mean([0, 10]) // returns 3: 0
const values = assertStatisticsArray(data, 'harmonic_mean').map((value) =>toStatisticNumber(value, 'harmonic_mean')) if (values.length < 1) { thrownewError('harmonic_mean requires at least one data point') }
if (values.length === 1 && weights === undefined) { const value = values[0] ?? 0 if (value < 0) { thrownewError('harmonic mean does not support negative values') } return value }
const errmsg = 'harmonic mean does not support negative values' const weightValues = weights === undefined ? newArray(values.length).fill(1) : assertStatisticsArray(weights, 'harmonic_mean').map((value) =>toStatisticNumber(value, 'harmonic_mean'))
if (weightValues.length !== values.length) { thrownewError('Number of weights does not match data size') }
if (weightValues.some((value) => value < 0) || values.some((value) => value < 0)) { thrownewError(errmsg) }
const sumWeights = weightValues.reduce((sum, value) => sum + value, 0)
try { let total = 0 for (let index = 0; index < values.length; index += 1) { const value = values[index] ?? 0 const weight = weightValues[index] ?? 0 total += weight ? weight / value : 0 } if (total <= 0) { thrownewError('Weighted sum must be positive') } return sumWeights / total } catch (error) { if (error instanceofError && error.message === 'Weighted sum must be positive') { throw error } return0 } }
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.
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.