Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Suggestion] Math.mean and Math.roundBy #13

Open
fega opened this issue Feb 20, 2019 · 2 comments
Open

[Suggestion] Math.mean and Math.roundBy #13

fega opened this issue Feb 20, 2019 · 2 comments

Comments

@fega
Copy link

fega commented Feb 20, 2019

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

@fega fega changed the title [Suggestion] Math.mean and Math.roundByy [Suggestion] Math.mean and Math.roundBy Feb 20, 2019
@Crissov
Copy link

Crissov commented May 10, 2019

Means

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) minimum
  • hmean(args) = mean(-1, args) harmonic mean
  • (ghmean(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 mean
  • (agmean(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, average
  • qmean(args) = hypot(args) = mean(2, args) quadratic mean = root mean squared (RMS) = hypotenuse
  • cmean(args) = mean(3, args) cubic mean
  • max(args) = mean(+∞, args) = quant(n(args), j, args) maximum

Since 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

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-range
  • mode(args) mode, i.e. most frequent value
  • median(args) = med(args) = quant(1, 2, args) = quart(2, args) median, i. e. central or middle value
  • quant(i, j, args) quantile: j groups with equal number of members, i-th boundary; i.e. 0 ≤ ij
    • terc(i, args) = quant(i, 3, args) tercile
    • quart(i, args) = quant(i, 4, args) quartile
    • quint(i, args) = quant(i, 5, args) quintile
    • sext(i, args) = quant(i, 6, args) sextile
    • dec(i, args) = quant(i, 10, args) decile
    • perc(i, args) = quant(i, 100, args) percentile
  • n(args) = count(args) (= args.length) number of arguments
  • sum(args) = Σ(args) sum, i.e. result of adding all arguments Math.sum? #4
  • product(args) = Π(args) product, i.e. result of multiplying all arguments

Rounding

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.

@Rudxain
Copy link

Rudxain commented May 19, 2022

Also Standard Deviation may be a good idea

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants