{ "name": "Mean", "summary": "The arithmetic mean of a list of numbers.", "tags": [ "statistics", "summation" ], "sources": [ { "title": "NIST/SEMATECH e-Handbook of Statistical Methods, section 1.3.5.1, Measures of location: the mean is the sum of the data points divided by their number", "url": "https://www.itl.nist.gov/div898/handbook/eda/section3/eda351.htm" } ], "cases": [ { "call": "Mean([1.0, 2.0, 3.0, 4.0])" }, { "call": "Mean([0.1, 0.1, 0.1])" }, { "call": "Mean([-5.0, 5.0])" }, { "call": "Mean([1.0e300, 1.0e300])" }, { "call": "Mean([2.5])" } ], "references": [ { "lang": "python", "level": "plain", "file": "ref/python/plain.py", "note": "The same loop: one running total, added to in order." }, { "lang": "python", "level": "fsum", "file": "ref/python/fsum.py", "note": "math.fsum rounds the sum once, exactly; the plain loop rounds at every step. On these cases the two differ at most in the last digit. Under cancellation they can differ in every digit (Mean([1.0e16, 1.0, -1.0e16]) is 0.0 by the loop and 0.3333333333333333 by fsum), so the tolerance holds for cases like these only.", "rel": 1e-15 }, { "lang": "c", "level": "plain", "file": "ref/c/plain.c", "note": "The same loop in C. Without -ffast-math the compiler keeps the additions in order; the check builds it at -O0 and -O2." }, { "lang": "mojo", "level": "plain", "file": "ref/mojo/plain.mojo", "note": "The same loop in Mojo." }, { "lang": "mojo", "level": "unrolled", "file": "ref/mojo/unrolled.mojo", "note": "Four running totals, the shape a vectorizing compiler gives the loop. The additions happen in a different order, so on these cases the last digit can differ; under cancellation every digit can (Mean([1.0e16, 1.0, 1.0, 1.0, -1.0e16, 0.0, 0.0, 0.0]) is 0.0 by the plain loop and 0.375 unrolled).", "rel": 1e-15 } ], "proof": { "lang": "dafny", "file": "proof/mean.dfy", "note": "Over real numbers: the loop computes Sum(X) / |X|, and the mean lies between the smallest and largest value (rounding can push the FLOAT result just outside, as Mean([0.1, 0.1, 0.1]) shows)." } }