Write two functions over an array of numbers.
summarize(values) returns { count, mean, median, min, max }:
countis the number of values andmeanis their sum divided by the count.medianis the middle value when the values are put in ascending numeric order. For an even count it is the mean of the two middle values.minandmaxare the smallest and largest values.
percentile(values, p) returns the nearest-rank percentile: sort the values in ascending numeric order, let rank = ceil(p / 100 * count), and return the value at 1-based position rank. When p is 0 the rank is 1 (the smallest value). p must be a finite number from 0 to 100 inclusive.
Errors, checked in this order by both functions:
{ kind: "empty" }whenvalueshas no elements.{ kind: "not_finite", index }when a value isNaN,Infinityor-Infinity.indexis the position of the first such value in the input.{ kind: "invalid_percentile" }(percentileonly) whenpis not a finite number in [0, 100].
Neither function may modify the array it is given.