simple-statistics
Version:
27 lines (23 loc) • 877 B
JavaScript
import quantile from "./quantile.js";
/**
* The [Interquartile range](http://en.wikipedia.org/wiki/Interquartile_range) is
* a measure of statistical dispersion, or how scattered, spread, or
* concentrated a distribution is. It's computed as the difference between
* the third quartile and first quartile.
*
* @param {Array<number>} x sample of one or more numbers
* @returns {number} interquartile range: the span between lower and upper quartile,
* 0.25 and 0.75
* @example
* interquartileRange([0, 1, 2, 3]); // => 2
*/
function interquartileRange(x) {
// Interquartile range is the span between the upper quartile,
// at `0.75`, and lower quartile, `0.25`
const q1 = quantile(x, 0.75);
const q2 = quantile(x, 0.25);
if (typeof q1 === "number" && typeof q2 === "number") {
return q1 - q2;
}
}
export default interquartileRange;