UNPKG

1.02 kBJavaScriptView Raw
1import sumSimple from "./sum_simple";
2
3/**
4 * The mean, _also known as average_,
5 * is the sum of all values over the number of values.
6 * This is a [measure of central tendency](https://en.wikipedia.org/wiki/Central_tendency):
7 * a method of finding a typical or central value of a set of numbers.
8 *
9 * The simple mean uses the successive addition method internally
10 * to calculate it's result. Errors in floating-point addition are
11 * not accounted for, so if precision is required, the standard {@link mean}
12 * method should be used instead.
13 *
14 * This runs in `O(n)`, linear time, with respect to the length of the array.
15 *
16 *
17 * @param {Array<number>} x sample of one or more data points
18 * @throws {Error} if the the length of x is less than one
19 * @returns {number} mean
20 * @example
21 * mean([0, 10]); // => 5
22 */
23function meanSimple(x) {
24 if (x.length === 0) {
25 throw new Error("meanSimple requires at least one data point");
26 }
27
28 return sumSimple(x) / x.length;
29}
30
31export default meanSimple;