Open fega opened 5 years ago
I support the addition of a buildin function for the arithmetic mean, but I think mean()
is not the best name for it, because there are other useful means as well, and even a generalized mean. Therefore, we should have all of them:
mean(i[, x]*)
= mean(i, args)
generalized mean: 1/n(args)*pow(sum(args.map(x => pow(x, i))), 1/i)
min(args)
= mean(-∞, args)
= quant(0, j, args)
minimumhmean(args)
= mean(-1, args)
harmonic meanghmean(x, y, n)
geometric-harmonic mean):for (var i = 1, g[0] = x, h[0] = y; i <= n && g[i] != h[i]; i++) {g[i] = gmean(g[i-1], h[i-1]); h[i] = hmean(g[i-1], h[i-1]);} return h[n];
gmean(args)
= mean(0, args)
geometric mean, arithmetic-harmonic meanagmean(x, y, n)
arithmetic-geometric mean):for (var i = 1, a[0] = x, g[0] = y; i <= n && a[i] != g[i]; i++) {a[i] = amean(a[i-1], g[i-1]); g[i] = gmean(a[i-1], g[i-1]);} return g[n];
amean(args)
= avg(args)
= mean(1, args)
arithmetic mean, averageqmean(args)
= hypot(args)
= mean(2, args)
quadratic mean = root mean squared (RMS) = hypotenusecmean(args)
= mean(3, args)
cubic meanmax(args)
= mean(+∞, args)
= quant(n(args), j, args)
maximumSince you might want to support truncated means or weighted means out of the box as well, it might make sense to expect the arguments args
as an array (or similar iteratable): mean(int = 1, args = [], trunc = 0, weight = [])
.
Other functions working on a set of values would make sense as well:
range(args)
= max(args) - min(args)
mid(args)
= amean(max(args), min(args))
mid-rangemode(args)
mode, i.e. most frequent valuemedian(args)
= med(args)
= quant(1, 2, args)
= quart(2, args)
median, i. e. central or middle valuequant(i, j, args)
quantile: j groups with equal number of members, i-th boundary; i.e. 0 ≤ i ≤ j
terc(i, args)
= quant(i, 3, args)
tercilequart(i, args)
= quant(i, 4, args)
quartilequint(i, args)
= quant(i, 5, args)
quintilesext(i, args)
= quant(i, 6, args)
sextiledec(i, args)
= quant(i, 10, args)
decileperc(i, args)
= quant(i, 100, args)
percentilen(args)
= count(args)
(= args.length
) number of argumentssum(args)
= Σ(args)
sum, i.e. result of adding all arguments #4product(args)
= Π(args)
product, i.e. result of multiplying all arguments
For rounding #11, I would prefer a module as a second parameter that the result must be a multiple of: mround(1.2345, 0.01)
= 1.23.
Also Standard Deviation may be a good idea
Hello, I really like this proposal, and I'd like two suggestions
Math.mean([1,2,3]) // 2
Math.roundBy(100.1234, 2) // 100.12
I'm proposing a separated method for round, since I think that Math.round could lead to breaking changes