Skip to main content

Statistics

The 43 definitions of the statistics library, each with its Epsil spelling, its MathJSON name, its signature and its full description.

Data and distributions​

The functions of this library take their data as a collection of numbers: several arguments, a list, a range, or linspace(a, b, n). A matrix is not a collection of numbers here.

[mean(1, 2, 3), mean([1, 2, 3]), mean(1..10)]
// ➔ [2,2,11/2]
mean([[1, 2], [3, 4]])
// ➔ error: Type error: expected `collection<number> | distribution | number`, got `matrix<integer^(2x2)>` at `[[1, 2], [3, 4]]` in `mean([[1, 2], [3, 4]])`

Exact data gives an exact result.

Probability distributions​

A probability distribution is a value, made by one of normalDistribution, binomialDistribution, poissonDistribution, uniformDistribution and exponentialDistribution. It can be assigned, passed as an argument, and queried with pdf, cdf, quantile, mean, variance and standardDeviation. With symbolic parameters, the answers are symbolic.

These functions give exact closed forms: an ordinary expression, which N approximates at machine or arbitrary precision. A float parameter or point gives a float. A discrete cdf reads a float point as its floor, and stays exact.

cdf(normalDistribution(0, 1), x)
// ➔ 1/2 * (Erf(sqrt(2)/2 * x) + 1)
[pdf(normalDistribution(0, 1), 0.5), cdf(poissonDistribution(2), 2.5)]
// ➔ [0.352065326764299477774,5 / e^2]
[mean(normalDistribution(mu, sigma)), variance(binomialDistribution(n, p))]
// ➔ [mu,n * p * (1 - p)]

The error functions and the regularized incomplete gamma and beta functions that these closed forms use are in the Special Functions library.

Definitions​

Each definition is listed under its Epsil spelling (the MathJSON name when it has none), with its signature in the engine's type syntax. The Standard Library page is the one-page index of every category.

betaDistribution​

MathJSON BetaDistribution · (real<0<..>, real<0<..>) -> expression<BetaDistribution>

  • argument 1 — The shape α, positive: the density is proportional to x^(α−1)·(1−x)^(β−1). Sample: 2.
  • argument 2 — The shape β, positive. Sample: 3.

Beta distribution with shape parameters α and β, on the interval [0, 1].

mean(betaDistribution(2, 3))
// ➔ 2/5
cdf(betaDistribution(2, 3), 1/2)
// ➔ 11/16

betaRegularized​

(broadcastable<complex | infinity>, broadcastable<complex | infinity>, broadcastable<complex | infinity>, broadcastable<complex | infinity>?) -> number

  • argument 1 — The point x, in [0, 1] for a value in [0, 1]; with four arguments, the lower point z0. Sample: 1/2.
  • argument 2 — The parameter a, positive; with four arguments, the upper point z1. Sample: 2.
  • argument 3 — The parameter b, positive; with four arguments, the parameter a. Sample: 3.
  • argument 4 — With four arguments, the parameter b: the result is I_z1(a, b) − I_z0(a, b). Sample: 3.

Regularized incomplete beta function I_x(a, b); with four arguments, I_z1(a, b) − I_z0(a, b)

betaRegularized(1/2, 2, 3)
// ➔ 11/16
betaRegularized(0.3, 2, 3)
// ➔ 0.3483
I_x(a, b) = \frac{1}{\operatorname{B}(a, b)}\int_0^x t^{a-1}(1-t)^{b-1} \, dt

The endpoints fold for a numeric a and b: betaRegularized(0, a, b) is 0 and betaRegularized(1, a, b) is 1. For a rational x and positive integers a and b the value is an exact rational. Other exact arguments stay symbolic; N gives their value, at machine or arbitrary precision.

[betaRegularized(0, 2, 3), betaRegularized(1, 2, 3)]
// ➔ [0,1]
betaRegularized(1/2, 2, 3)
// ➔ 11/16
betaRegularized(1/3, 1/2, 2)
// ➔ BetaRegularized(1/3, 1/2, 2)

Outside [0, 1], N gives the real value where it is a finite sum: for positive integers a and b at any real x, and for x > 1 with an integer b up to 30. Elsewhere the call stays unevaluated.

[N(betaRegularized(2, 2, 3)), N(betaRegularized(2, 1/2, 2))]
// ➔ [8,0.707106781186547524401]
N(betaRegularized(-1, 1/2, 2))
// ➔ BetaRegularized(-1, 0.5, 2)

The cumulative distribution function of a binomial distribution evaluates to this function:

\operatorname{CDF}(\operatorname{Binomial}(n, p), k) =
I_{1-p}(n-k, k+1)
.

cdf(binomialDistribution(10, 1/2), 3)
// ➔ 11/64

With four arguments, betaRegularized(z0, z1, a, b) is I_{z_1}(a, b) - I_{z_0}(a, b).

N(betaRegularized(0.2, 0.5, 2, 3))
// ➔ 0.5067

See also: DLMF 8.17.

MathJSON: BetaRegularized

["BetaRegularized", x, a, b] and ["BetaRegularized", z0, z1, a, b].

["N", ["BetaRegularized", 0.5, 2, 3]]
// ➔ 0.6875

["BetaRegularized", ["Rational", 1, 2], 2, 3]
// ➔ ["Rational", 11, 16]

binCounts​

(xs: collection<number>, bins: list<number> | number) -> list<number>

  • xs — The data, finite real numbers; an empty collection stays unevaluated. Sample: [1, 2, 2, 3].
  • bins — A positive integer number of bins of equal width from the least to the greatest value, or a list of bin edges; a bin holds its lower edge, the last bin also its upper edge, and a value outside the edges is not counted. A number that is not a positive integer stays unevaluated. Sample: 3.

Count the number of elements falling into each bin.

binCounts([1, 2, 2, 3], 3)
// ➔ [1,2,1]
binCounts([1, 2, 2, 3, 4, 5, 5, 5], 3)
// ➔ [3,1,4]
MathJSON: BinCounts

["BinCounts", collection, bins].

binomialDistribution​

(n: integer<0..>, p: real<0..1>) -> expression<BinomialDistribution>

  • n — The number of trials. Sample: 10.
  • p — The probability of success of each trial. Sample: 1/2.

Binomial distribution: number of successes in n independent trials, each with success probability p.

pdf(binomialDistribution(10, 1/2), 5)
// ➔ 63/256
mean(binomialDistribution(10, 0.3))
// ➔ 3

The number of successes in n ≥ 0 independent trials, each succeeding with probability 0 ≤ p ≤ 1.

MathJSON: BinomialDistribution

["BinomialDistribution", n, p].

cdf​

(distribution, real | signed_infinity) -> nan | real<0..1>

  • argument 1 — The distribution. Sample: normalDistribution(0, 1).
  • argument 2 — The point x; for a discrete distribution, a point that is not an integer is read as its floor. Sample: 1.

Cumulative distribution function P(X ≤ x) of a distribution.

cdf(normalDistribution(0, 1), 0)
// ➔ 1/2
cdf(binomialDistribution(10, 1/2), 5)
// ➔ 319/512

The cumulative distribution function: the probability that a value is at most x. The result is exact: a normal CDF is written with erf, and a discrete one with the regularized incomplete gamma or beta function. Use N for a numeric value.

N(cdf(normalDistribution(0, 1), 1))
// ➔ 0.841344746068542948585

D and integrate read the closed form of pdf and cdf of a continuous distribution:

[D(cdf(normalDistribution(0, 1), x), x), integrate(pdf(normalDistribution(0, 1), x), x)]
// ➔ [(sqrt(2) * e^(-1/2 * x^2)) / (2sqrt(pi)),1/2 * Erf(sqrt(2)/2 * x)]
integrate(pdf(normalDistribution(0, 1), x), (x, -oo, oo))
// ➔ 1
MathJSON: CDF

["CDF", distribution, x].

chiSquareDistribution​

MathJSON ChiSquareDistribution · (real<0<..>) -> expression<ChiSquareDistribution>

  • argument 1 — The number of degrees of freedom k, positive. Sample: 4.

Chi-square distribution with k degrees of freedom (k > 0, not necessarily an integer).

cdf(chiSquareDistribution(2), 1)
// ➔ 1 - sqrt(1 / e)
mean(chiSquareDistribution(4))
// ➔ 4

correlation​

(xs: collection<number | tuple>, ys: collection<number>?) -> nan | real<-1..1>

  • xs — The x values, or the (x, y) pairs when ys is omitted; at least two points are needed, and data with no variance is an error. Sample: [1, 2, 3].
  • ys — The y values, as many as the x values. Sample: [2, 4, 7].

Pearson's correlation coefficient of paired data, given as two equal-length collections or one collection of (x, y) pairs.

correlation([1, 2, 3], [2, 4, 7])
// ➔ 5/38 * sqrt(57)
correlation([1, 2, 3], [3, 2, 1])
// ➔ -1

The Pearson correlation coefficient of two data sets, between -1 and 1: the strength of their linear relationship, r = \frac{\operatorname{cov}(x, y)}{\sigma_x \sigma_y}.

correlation([1, 2, 3, 4, 5], [2, 4, 5, 4, 5])
// ➔ sqrt(15)/5
MathJSON: Correlation

["Correlation", xs, ys], ["Correlation", points].

covariance​

(xs: collection<number | tuple>, ys: collection<number>?) -> nan | real

  • xs — The x values, or the (x, y) pairs when ys is omitted; at least two points are needed. Sample: [1, 2, 3].
  • ys — The y values, as many as the x values. Sample: [2, 4, 7].

Sample covariance (n − 1 denominator) of paired data, given as two equal-length collections or one collection of (x, y) pairs.

covariance([1, 2, 3], [2, 4, 7])
// ➔ 5/2
covariance([(1, 2), (2, 4), (3, 7)])
// ➔ 5/2

The sample covariance of two data sets: two collections of the same length, or one collection of (x, y) pairs. \operatorname{cov}(x, y) = \frac{1}{n-1} \sum_{i=1}^n (x_i - \bar{x})(y_i - \bar{y}). populationCovariance has the denominator n.

[covariance([1, 2, 3, 4, 5], [2, 4, 5, 4, 5]), covariance([(1, 2), (2, 4), (3, 5)])]
// ➔ [3/2,3/2]
populationCovariance([1, 2, 3, 4, 5], [2, 4, 5, 4, 5])
// ➔ 6/5
MathJSON: Covariance

["Covariance", xs, ys], ["Covariance", points].

erf​

(broadcastable<complex | signed_infinity>, broadcastable<complex | signed_infinity>?) -> complex

  • argument 1 — The point z; with two arguments, the lower point z0. Sample: 1/2.
  • argument 2 — The upper point z1: the result is Erf(z1) − Erf(z0). Sample: 1.

Gauss error function; with two arguments, Erf(z1) − Erf(z0)

erf(0.5)
// ➔ 0.520499877813046537683
erf(0, 1.0)
// ➔ 0.842700792949714869341

The error function is used in statistics to compute probabilities of normally distributed events. For a complex z,

\operatorname{erf} z = \frac{2}{\sqrt{\pi}} \int_0^z e^{-t^2} dt

It is an odd function, \operatorname{erf}(-z) = -\operatorname{erf} z: simplify applies this.

[erf(0), erf(-x), simplify(erf(-x))]
// ➔ [0,Erf(-x),-Erf(x)]
N(erf(1 + i))
// ➔ (1.3161512816979477 + 0.19045346923783446i)

With two arguments, erf(z0, z1) is the generalized error function \operatorname{erf} z_1 - \operatorname{erf} z_0.

N(erf(0.1, 0.5))
// ➔ 0.408036961794761645479
MathJSON: Erf

["Erf", z] and ["Erf", z0, z1].

["N", ["Erf", 0.1, 0.5]]
// ➔ 0.408036961794761645479

erfInv​

(broadcastable<complex | infinity>, broadcastable<complex | infinity>?) -> number

  • argument 1 — The value z, in [−1, 1] for a real result (±1 give ±∞); with two arguments, the point z0. Sample: 1/2.
  • argument 2 — The value z: the result is the x with Erf(z0, x) = z. Sample: 1/2.

Inverse of the error function; with two arguments, the x with Erf(z0, x) = z, that is ErfInv(z + Erf(z0))

erfInv(0.5)
// ➔ 0.476936276204469873381
erfInv(1)
// ➔ +oo

erfInv(x) is the inverse error function, \operatorname{erf}(\operatorname{erf}^{-1} x) = x, of a real number -1 < x < 1. At \pm 1 it is \pm\infty. Outside that interval the value is not defined: erfInv(+oo) is NaN, and erfInv(2) stays unevaluated, also with N.

[erfInv(0), erfInv(1), erfInv(-1), N(erfInv(1/2))]
// ➔ [0,+oo,-oo,0.476936276204469873381]
[N(erfInv(2)), erfInv(+oo)]
// ➔ [ErfInv(2),NaN]

With two arguments, erfInv(z0, z) is the x with \operatorname{erf} x - \operatorname{erf} z_0 = z, that is erfInv(z + erf(z0)).

N(erfInv(0.5, 0.2))
// ➔ 0.764695648017466677088
MathJSON: ErfInv

["ErfInv", x] and ["ErfInv", z0, z].

["N", ["ErfInv", 0.5, 0.2]]
// ➔ 0.764695648017466677088

erfc​

(broadcastable<complex | signed_infinity>) -> complex

  • argument 1 — The point x. Sample: 1/2.

Complementary error function: 1 - Erf(x)

erfc(0.5)
// ➔ 0.479500122186953462317
erfc(-oo)
// ➔ 2

\operatorname{erfc} z = 1 - \operatorname{erf} z, for a complex z.

[erfc(0), N(erfc(1)), 1 - N(erf(1))]
// ➔ [1,0.157299207050285130659,0.157299207050285130659]
MathJSON: Erfc

["Erfc", z].

["N", ["Erfc", 1]]
// ➔ 0.157299207050285130659

erfi​

MathJSON Erfi · (broadcastable<complex | signed_infinity>) -> complex | signed_infinity

  • argument 1 — The point x. Sample: 1/2.

Imaginary error function: -i·Erf(i·x)

erfi(0.5)
// ➔ 0.61495209469651098084

exponentialDistribution​

(real<0<..>) -> expression<ExponentialDistribution>

  • argument 1 — The rate λ, positive: the mean of the distribution is 1/λ. Sample: 2.

Exponential distribution with rate parameter λ.

cdf(exponentialDistribution(2), 1)
// ➔ 1 - e^(-2)
variance(exponentialDistribution(2))
// ➔ 1/4

The waiting time between events that occur at a mean rate lambda > 0. This is the rate parameterization: the mean is 1/lambda.

mean(exponentialDistribution(lambda))
// ➔ 1 / lambda
MathJSON: ExponentialDistribution

["ExponentialDistribution", lambda].

fRatioDistribution​

MathJSON FRatioDistribution · (real<0<..>, real<0<..>) -> expression<FRatioDistribution>

  • argument 1 — The degrees of freedom d₁ of the numerator, positive. Sample: 2.
  • argument 2 — The degrees of freedom d₂ of the denominator, positive. Sample: 5.

Snedecor's F distribution with d₁ degrees of freedom in the numerator and d₂ in the denominator.

mean(fRatioDistribution(2, 5))
// ➔ 5/3
cdf(fRatioDistribution(2, 3), 1.0)
// ➔ 0.535241998455109973778

findFit​

MathJSON FindFit · (data: any, model: any, params: any, vars: any, options: dictionary?) -> dictionary

  • data — The data: a list of (x…, y) tuples, or a plain list of y values at x = 1, 2, 3, …; a list of datasets for a list of models. Sample: [(0, 1), (1, 3), (2, 5)].
  • model — The model, an expression in the variables and the parameters, or a list of models that share parameters. Sample: a * x + b.
  • params — The parameters, each a symbol, (a, a0) with the start value a0, or (a, a0, lo, hi) with bounds. Sample: [a, b].
  • vars — The variables, a symbol or a list of symbols. Sample: [x].
  • options — The options of the fit. Sample: {global -> {a -> [0, 5]}}.
    • global (dictionary<list<real>>) — The parameters (at most two) searched over a range [lo, hi] or [lo, hi, step] for the lowest sum of squared residuals, instead of the local minimum nearest the start. Sample: {a -> [0, 5]}.

Nonlinear least-squares fit of a model to data. FindFit(data, model, params, vars): fit model (an expression in vars and the parameters) to data, a list of (x…, y) tuples or a plain list of y values. Each parameter spec is a bare symbol, (a, a0), or (a, a0, lo, hi) with box constraints. Returns a record {parameters, converged, residualNorm, iterations}. converged is true at a stationary point of the sum of squared residuals (a minimum of the misfit, which is not zero for noisy data), and at an exact fit: every residual within the engine's tolerance of the terms its own row computes. The joint form takes a list of models and matching datasets sharing parameters. An optional fifth argument is a dictionary of options: {global: {b: [lo, hi]}} searches the named parameters (at most two) over the given range for the lowest sum of squared residuals instead of returning the local minimum nearest the start; iterations is then the total over every solver run. The search samples each parameter on a grid: a parameter that scales the phase of a periodic function of a variable (b in cos(b x + c)) gets a point in every basin of the residual, about π / (span of x) apart; {global: {b: [lo, hi, step]}} sets the step of the grid instead (when the model is linear in the other parameters; otherwise the solver runs from each point of a coarse grid of 24 points, or 6 × 6). A step on the phase c of a cos(b x + c) keeps it on the grid; without a step the phase is solved by least squares. With more than 300 data rows the grid is scanned on a stratified sample of 300 rows, and the best grid points are then solved again and refined on every row. With a kernel pool on the engine (ce.workers), evaluateAsync() scans the grid on the workers and gives the record of evaluate(); the solver runs stay on the calling thread. The record of evaluateAsync() also has route: "pool" when the workers scanned the grid, or "calling-thread" with a reason ("no-pool", "pool-unusable", "no-global-search", "not-separable", "not-compilable", "pool-declined", "no-points-to-ship", "step-budget", "setup-timeout"). On the calling thread evaluateAsync() gives the event loop a turn about every 8 ms, and it keeps the limit of a withTimeLimit span that encloses the call.

findFit([(0, 1), (1, 3), (2, 5)], a * x + b, [a, b], [x])
// ➔ {"parameters" -> {"a" -> 1.9999999983899441, "b" -> 1.0000000020700244}, "converged" -> true, "residualNorm" -> 2.4123168032968065e-9, "iterations" -> 3}
findFit([1, 3, 5], a * x + b, [(a, 1), b], [x])
// ➔ {"parameters" -> {"a" -> 1.9999999999572358, "b" -> -0.9999999999076185}, "converged" -> true, "residualNorm" -> 6.163184317732835e-11, "iterations" -> 4}

gammaDistribution​

MathJSON GammaDistribution · (shape: real<0<..>, scale: real<0<..>) -> expression<GammaDistribution>

  • shape — The shape α, positive. Sample: 2.
  • scale — The scale θ, positive: the mean of the distribution is α·θ. Sample: 3.

Gamma distribution with shape α and scale θ: the density is x^(α−1)·e^(−x/θ)/(Γ(α)·θ^α) for x > 0.

mean(gammaDistribution(2, 3))
// ➔ 6
cdf(gammaDistribution(2, 1), 1)
// ➔ 1 - 2 / e

gammaRegularized​

(broadcastable<complex | infinity>, broadcastable<complex | infinity>, broadcastable<complex | infinity>?) -> number

  • argument 1 — The parameter a; for a > 0 and z ≥ 0 the value is in [0, 1]. Sample: 2.
  • argument 2 — The lower limit z of the integral; with three arguments, the first limit z0. Sample: 1.
  • argument 3 — The second limit z1: the result is Q(a, z0) − Q(a, z1). Sample: 3.

Regularized upper incomplete gamma function Q(a, z) = Γ(a, z)/Γ(a); with three arguments, Q(a, z0) − Q(a, z1)

gammaRegularized(2, 1)
// ➔ 2 / e
gammaRegularized(2, 1.0)
// ➔ 0.735758882342884643192
Q(a, z) = \frac{\Gamma(a, z)}{\Gamma(a)} = \frac{1}{\Gamma(a)}\int_z^\infty t^{a-1}e^{-t} \, dt

Special values fold: for a numeric a, gammaRegularized(a, 0) is 1, and gammaRegularized(1, z) is e^{-z}. For a positive integer a and an exact z the value is a closed form. Other exact arguments stay symbolic; N gives their value, at machine or arbitrary precision.

[gammaRegularized(2, 0), gammaRegularized(1, z), gammaRegularized(3, 5)]
// ➔ [1,e^(-z),37 / (2e^5)]
[gammaRegularized(1/2, 2), N(gammaRegularized(1/2, 2))]
// ➔ [GammaRegularized(1/2, 2),0.0455002638963584144006]

The cumulative distribution function of a Poisson distribution evaluates to this function:

\operatorname{CDF}(\operatorname{Poisson}(\lambda), k) =
Q(k+1, \lambda)
.

cdf(poissonDistribution(lambda), k)
// ➔ Which(k < 0, 0, true, GammaRegularized(floor(k) + 1, lambda))
cdf(poissonDistribution(2), 3)
// ➔ 19 / (3e^2)

With three arguments, gammaRegularized(a, z0, z1) is Q(a, z_0) - Q(a, z_1).

N(gammaRegularized(3, 1, 5))
// ➔ 0.795046583445524662701

See also: DLMF 8.2.

MathJSON: GammaRegularized

["GammaRegularized", a, z] and ["GammaRegularized", a, z0, z1].

["N", ["GammaRegularized", 3, 5]]
// ➔ 0.124652019483081141288

geometricDistribution​

MathJSON GeometricDistribution · (real<0..1>) -> expression<GeometricDistribution>

  • argument 1 — The probability p of success of each trial; p = 0 is refused. Sample: 1/4.

Geometric distribution: the number of failures before the first success, each trial a success with probability p (0 < p ≤ 1). The support is {0, 1, 2, …}.

pdf(geometricDistribution(1/2), 2)
// ➔ 1/8
mean(geometricDistribution(1/4))
// ➔ 3

histogram​

(xs: collection<number>, bins: list<number> | number) -> list<tuple<number, integer>>

  • xs — The data, finite real numbers; an empty collection stays unevaluated. Sample: [1, 2, 2, 3].
  • bins — A positive integer number of bins of equal width from the least to the greatest value, or a list of bin edges; a bin holds its lower edge, the last bin also its upper edge, and a value outside the edges is not counted. A number that is not a positive integer stays unevaluated. Sample: 3.

Compute a histogram of the values in a collection. Returns a list of (bin start, count) tuples.

histogram([1, 2, 2, 3], 3)
// ➔ [(1, 1),(1.6666666666666665, 2),(2.333333333333333, 1)]

histogram(data, bins) groups the data into bins and counts the elements of each: a list of (start, count) pairs. bins is a number of bins, an integer, or a list of the edges of the bins. A number of bins that is not an integer leaves the histogram unevaluated: write a bin width as a list of edges.

histogram([1, 2, 2, 3, 4, 5, 5, 5], 3)
// ➔ [(1, 3),(2.333333333333333, 1),(3.6666666666666665, 4)]
histogram([1, 2, 3], 1.5)
// ➔ Histogram([1,2,3], 1.5)
MathJSON: Histogram

["Histogram", collection, bins]. In LaTeX, \operatorname{histogram} parses to Histogram, and \operatorname{pdf} and \operatorname{cdf} to PDF and CDF.

hypergeometricDistribution​

MathJSON HypergeometricDistribution · (draws: integer<0..>, successes: integer<0..>, population: integer<0..>) -> expression<HypergeometricDistribution>

  • draws — The number n of draws, at most the population. Sample: 5.
  • successes — The number K of successes in the population, at most the population. Sample: 10.
  • population — The size N of the population. Sample: 20.

Hypergeometric distribution: the number of successes in n draws without replacement from a population of N items of which K are successes (0 ≤ n ≤ N, 0 ≤ K ≤ N).

pdf(hypergeometricDistribution(5, 10, 20), 2)
// ➔ 225/646
mean(hypergeometricDistribution(5, 10, 20))
// ➔ 5/2

interquartileRange​

((collection<number> | number)+) -> +oo | nan | real<0..>

  • arguments 1, … — The data, as collections or single numbers that form one sample; Q1 and Q3 are the medians of the lower and upper halves, without the median of an odd count, and an empty collection gives NaN. Sample: [1, 2, 3, 4, 5, 6, 7, 8].

Interquartile range (Q3 - Q1) of a collection.

interquartileRange([1, 2, 3, 4, 5, 6, 7, 8])
// ➔ 4
MathJSON: InterquartileRange

["InterquartileRange", collection].

kurtosis​

((collection<number> | number)+) -> nan | real

  • arguments 1, … — The data, as collections or single numbers that form one sample; the result is the population kurtosis m₄/m₂² (3 for a normal distribution, not the excess), and NaN when every value is the same. Sample: [1, 2, 3, 4, 10].

Kurtosis of a collection of numbers.

kurtosis([1, 2, 3, 4, 10])
// ➔ 697/250

A measure of the weight of the tails of the data: \frac{1}{n} \sum_{i=1}^n \left(\frac{x_i - \mu}{\sigma}\right)^4.

kurtosis([1, 2, 3, 10])
// ➔ 1394/625
MathJSON: Kurtosis

["Kurtosis", collection].

linearRegression​

(any+) -> tuple<number, number>

  • arguments 1, … — The data, as two collections of x and y values or one collection of (x, y) pairs with at least two points, then optionally the variable of the fitted line. Sample: [1, 2, 3].

Least-squares linear fit b0 + b1·x. Returns Tuple(b0, b1), or the fitted expression if a trailing variable symbol is given.

linearRegression([1, 2, 3], [2, 4, 7])
// ➔ (-2/3, 5/2)
linearRegression([1, 2, 3], [2, 4, 7], x)
// ➔ 5/2 * x - 2/3

The least-squares line y = b0 + b1 x through the data, as the tuple (b0, b1). With a variable as last argument, the result is the expression of the line in that variable, ready to plot with the data.

linearRegression([1, 2, 3, 4, 5], [2, 4, 5, 4, 5])
// ➔ (11/5, 3/5)
linearRegression([1, 2, 3, 4, 5], [2, 4, 5, 4, 5], x)
// ➔ 3/5 * x + 11/5
MathJSON: LinearRegression

["LinearRegression", xs, ys], ["LinearRegression", points], with an optional variable.

mean​

((collection<number> | distribution | number)+) -> number

  • arguments 1, … — The data, as collections or single numbers that form one sample, or a single distribution, whose mean is returned; no data gives NaN. Sample: [1, 2, 3, 4].

Arithmetic mean (average) of a collection of numbers.

mean([1, 2, 3, 4])
// ➔ 5/2
mean(normalDistribution(3, 2))
// ➔ 3

The arithmetic mean, \bar{x} = \frac{1}{n} \sum_{i=1}^n x_i. When the collection holds complex infinity, the mean is NaN: a value with no direction has no mean, although the sum of the same collection is complex infinity.

mean([7, 8, 3.1, 12, 77])
// ➔ 21.42
[mean([1, oo * i]), sum([1, oo * i])]
// ➔ [NaN,~oo]
MathJSON: Mean • LaTeX: mean

["Mean", collection], ["Mean", distribution]. The collection can also be several arguments, a Range or a Linspace.

["Mean", ["List", 7, 8, 3.1, 12, 77]]
// ➔ 21.42
["Mean", ["NormalDistribution", "mu", "sigma"]]
// ➔ "mu"

median​

((collection<number> | number)+) -> nan | real | signed_infinity

  • arguments 1, … — The data, as collections or single numbers that form one sample; an even count gives the mean of the two middle values, and no data gives NaN. Sample: [3, 1, 4, 2].

Median of a collection of numbers.

median([3, 1, 4, 2])
// ➔ 5/2

The value that separates the higher half of the data from the lower half: the middle element of the sorted data, or the mean of the two middle elements when there is an even number of them.

[median([1, 2, 3, 4, 5]), median([1, 2, 3, 4])]
// ➔ [3,5/2]
MathJSON: Median • LaTeX: median

["Median", collection].

mode​

((collection<number> | number)+) -> nan | real | signed_infinity

  • arguments 1, … — The data, as collections or single numbers that form one sample; a tie gives the smallest of the most frequent values, and no data gives NaN. Sample: [1, 2, 2, 3].

Most frequently occurring value in a collection.

mode([1, 2, 2, 3])
// ➔ 2

The value that occurs most often. When several values occur most often (the data is bimodal or multimodal), the result is the smallest of them.

[mode([1, 2, 2, 3, 4, 4, 5, 5]), mode([2, 5, 5, 3, 2])]
// ➔ [2,2]
MathJSON: Mode

["Mode", collection].

negativeBinomialDistribution​

MathJSON NegativeBinomialDistribution · (n: integer<1..>, p: real<0..1>) -> expression<NegativeBinomialDistribution>

  • n — The number of successes to reach, at least 1. Sample: 3.
  • p — The probability of success of each trial, greater than 0 and at most 1. Sample: 1/2.

Negative binomial distribution: the number of failures before the n-th success (n a positive integer), each trial a success with probability p (0 < p ≤ 1). The support is {0, 1, 2, …}.

pdf(negativeBinomialDistribution(3, 1/2), 2)
// ➔ 3/16
mean(negativeBinomialDistribution(3, 1/2))
// ➔ 3

normalDistribution​

(mu: real, sigma: real<0<..>) -> expression<NormalDistribution>

  • mu — The mean, the center of the bell curve. Sample: 0.
  • sigma — The standard deviation, greater than 0. Sample: 1.

Normal (Gaussian) distribution with mean μ and standard deviation σ.

pdf(normalDistribution(0, 1), 0)
// ➔ sqrt(1 / (2pi))
cdf(normalDistribution(0, 1), 1.96)
// ➔ 0.975002104851779565864

The normal (Gaussian) distribution with mean mu and standard deviation sigma > 0. The second parameter is the standard deviation, not the variance, as in most computer algebra and statistics systems.

pdf(normalDistribution(mu, sigma), x)
// ➔ (sqrt(2) * e^(-((-mu + x)^2) / (2sigma^2))) / (2sigma * sqrt(pi))
MathJSON: NormalDistribution

["NormalDistribution", mean, standardDeviation].

pdf​

(distribution, real | signed_infinity) -> nan | real<0..>

  • argument 1 — The distribution. Sample: normalDistribution(0, 1).
  • argument 2 — The point x; outside the support, and off the integers for a discrete distribution, the value is 0. Sample: 1.

Probability density (continuous) or mass (discrete) function of a distribution, evaluated at x.

pdf(normalDistribution(0, 1), 0)
// ➔ sqrt(1 / (2pi))
pdf(poissonDistribution(2), 3)
// ➔ 4 / (3e^2)

The probability density function of the distribution at x. For a discrete distribution, it is the probability mass function, which is 0 at a point that is not an integer. With a symbolic x, the result is the formula of the density.

[pdf(binomialDistribution(4, 1/2), 2), pdf(binomialDistribution(4, 1/2), 2.5)]
// ➔ [3/8,0]
MathJSON: PDF

["PDF", distribution, x].

poissonDistribution​

(real<0<..>) -> expression<PoissonDistribution>

  • argument 1 — The rate λ, the mean number of events, greater than 0. Sample: 3.

Poisson distribution with rate parameter λ.

pdf(poissonDistribution(2), 3)
// ➔ 4 / (3e^2)
variance(poissonDistribution(3))
// ➔ 3

The number of events in a fixed interval, when events occur independently at a mean rate lambda > 0.

MathJSON: PoissonDistribution

["PoissonDistribution", lambda].

polynomialFit​

(any+) -> list<number>

  • arguments 1, … — The data, as two collections of x and y values or one collection of (x, y) pairs, then the degree, an integer from 0 to 12 and less than the number of points, then optionally the variable of the fitted polynomial. Sample: [0, 1, 2, 3].

Least-squares polynomial fit of the given degree. Returns the ascending coefficient List(c0, …, c_deg), or the fitted expression if a trailing variable symbol is given.

polynomialFit([0, 1, 2, 3], [1, 2, 5, 10], 2)
// ➔ [1,0,1]
polynomialFit([0, 1, 2, 3], [1, 2, 5, 10], 2, x)
// ➔ x^2 + 1

The least-squares polynomial of the given degree, as the list of its coefficients, the constant term first. Points that lie on a polynomial give it exactly. With a variable as last argument, the result is the polynomial in that variable. The degree is at most the number of points minus 1, and at most 12.

polynomialFit([(0, 1), (1, 2), (2, 5), (3, 10)], 2)
// ➔ [1,0,1]
polynomialFit([(0, 1), (1, 2), (2, 5), (3, 10)], 2, x)
// ➔ x^2 + 1
polynomialFit([(0, 1), (1, 2)], 2)
// ➔ error: Runtime error: unexpected argument
MathJSON: PolynomialFit

["PolynomialFit", xs, ys, degree], ["PolynomialFit", points, degree], with an optional variable.

populationCovariance​

(xs: collection<number | tuple>, ys: collection<number>?) -> nan | real

  • xs — The x values, or the (x, y) pairs when ys is omitted; at least two points are needed. Sample: [1, 2, 3].
  • ys — The y values, as many as the x values. Sample: [2, 4, 7].

Population covariance (n denominator) of paired data, given as two equal-length collections or one collection of (x, y) pairs.

populationCovariance([1, 2, 3], [2, 4, 7])
// ➔ 5/3
populationCovariance([(1, 2), (2, 4), (3, 7)])
// ➔ 5/3

\frac{1}{n} \sum_{i=1}^n (x_i - \bar{x})(y_i - \bar{y}).

MathJSON: PopulationCovariance

["PopulationCovariance", xs, ys], ["PopulationCovariance", points].

populationStandardDeviation​

((collection<number> | number)+) -> nan | real<0..>

  • arguments 1, … — The data, as collections or single numbers that form one sample; the divisor is n, so one value gives 0 and no data gives NaN. Sample: [2, 4, 4, 4, 5, 5, 7, 9].

Population Standard Deviation of a collection of numbers.

populationStandardDeviation([2, 4, 4, 4, 5, 5, 7, 9])
// ➔ 2

\sqrt{\frac{1}{N} \sum_{i=1}^N (x_i - \mu)^2}, where N is the size of the population.

MathJSON: PopulationStandardDeviation

["PopulationStandardDeviation", collection].

populationVariance​

((collection<number> | number)+) -> nan | real<0..>

  • arguments 1, … — The data, as collections or single numbers that form one sample; the divisor is n, so one value gives 0 and no data gives NaN. Sample: [1, 2, 3, 4].

Population variance of a collection of numbers.

populationVariance([1, 2, 3, 4])
// ➔ 5/4

The variance with the denominator n, the size of the population: \frac{1}{N} \sum_{i=1}^N (x_i - \mu)^2.

MathJSON: PopulationVariance

["PopulationVariance", collection].

quantile​

(collection<any> | distribution, real<0..1>) -> nan | real | signed_infinity

  • argument 1 — The distribution, or a collection of numbers whose empirical quantile agrees with Quartiles at 1/4, 1/2 and 3/4. Sample: normalDistribution(0, 1).
  • argument 2 — The probability p, from 0 to 1; 0 and 1 give the ends of the support, or the least and the greatest datum. Sample: 0.975.

Quantile (inverse CDF): the least x with CDF(x) ≥ p, for p in [0, 1]. The first argument may also be a data collection, in which case the empirical quantile is returned.

quantile(normalDistribution(0, 1), 0.975)
// ➔ 1.95996398454005423553
quantile([1, 2, 3, 4, 5], 1/4)
// ➔ 3/2

The quantile of a distribution, its inverse CDF: the smallest x such that cdf(x) ≥ p, for 0 ≤ p ≤ 1. quantile(d, 1/2) is the median. For a continuous distribution, the result is a closed form. For a discrete one with numeric parameters, it is found by an exact search of the support.

quantile(normalDistribution(mu, sigma), p)
// ➔ mu + sqrt(2) * sigma * ErfInv(2p - 1)
[quantile(poissonDistribution(9), 0.95), quantile(binomialDistribution(10, 1/2), 1/2)]
// ➔ [14,5]

Of a collection, it is the empirical quantile, which interpolates the sorted data. It agrees with quartiles and median: p = 1/4, 1/2 and 3/4 give the three quartiles, and p = 0 and p = 1 the minimum and the maximum.

[quantile([1, 2, 3, 4, 5, 6, 7, 8], 1/4), quantile([1, 2, 3, 4, 5, 6, 7, 8], 1)]
// ➔ [5/2,8]
MathJSON: Quantile

["Quantile", distribution, p], ["Quantile", collection, p].

["Quantile", ["PoissonDistribution", 9], 0.95]

quartiles​

((collection<number> | number)+) -> tuple<lower: nan | real | signed_infinity, mid: nan | real | signed_infinity, upper: nan | real | signed_infinity>

  • arguments 1, … — The data, as collections or single numbers that form one sample; no data gives a triple of NaN. Sample: [1, 2, 3, 4, 5].

Lower quartile, median, and upper quartile of a collection. Uses the Moore–McCabe (exclusive-hinges) convention: the sample is split at its median, and Q1/Q3 are the medians of the lower/upper halves with the overall median excluded from both halves when the sample size is odd.

quartiles([1, 2, 3, 4, 5])
// ➔ (3/2, 3, 9/2)

The three values that divide the sorted data into four groups of the same size. interquartileRange is the third quartile minus the first.

[quartiles([1, 2, 3, 4, 5, 6, 7, 8]), interquartileRange([1, 2, 3, 4, 5, 6, 7, 8])]
// ➔ [(5/2, 9/2, 13/2),4]
MathJSON: Quartiles

["Quartiles", collection].

randomSample​

((xs: T, count: number) random -> T where T: string) & ((xs: indexed_collection, count: number) random -> list)

  • xs — The collection to draw from; a string gives a string of its characters. Sample: [1, 2, 3, 4, 5].
  • count — The number of elements to draw, from 0 to the length of the collection; it is rounded to an integer. Sample: 2.

RandomSample(xs, count): a list of count elements drawn from the indexed collection xs, without replacement. "Without replacement" is over POSITIONS, not values: on a multiset, repeats are expected — RandomSample([1, 1, 2], 2) can return [1, 1]. Sampling a string yields a string. Wrap the call in WithRandomSeed(seed, ...) to make it deterministic.

withRandomSeed(42, randomSample([1, 2, 3, 4, 5], 2))
// ➔ [4,3]

randomSample(xs, k) is a list of k elements drawn from the indexed collection xs without replacement. That is over positions, not values: each position is drawn at most once, but repeated values of a multiset can both be drawn, so randomSample([1, 1, 2], 2) is [1, 1] in about a quarter of the draws.

Unlike randomChoice, the count may not be more than the size of the collection: there are only that many positions to draw. A count equal to the size gives a permutation. The count is rounded, so a computed count needs no rounding. The collection must be indexed: an interval or a set is a type error; draw from it with randomChoice. The collection is not built: a sparse Fisher–Yates shuffle of the indexes draws exactly k elements, so randomSample(1..1000000, 3) is cheap.

randomSample([1, 2, 3], 4)
// ➔ error: Runtime error: out of range: a count in 0..3, 4
randomSample([1, 1, 2], 2) in [[1, 1], [1, 2], [2, 1]]
// ➔ true
withRandomSeed(42, randomSample(1..100, 3)) == withRandomSeed(42, randomSample(1..100, 3))
// ➔ true
randomSample(interval(0, 1), 2)
// ➔ error: Type error: expected `indexed_collection`, got `set<real>` at `interval(0, 1)` in `randomSample(interval(0, 1), 2)`

Sample, with a seed argument, was the previous name: wrap randomSample in withRandomSeed instead.

MathJSON: RandomSample

["RandomSample", xs, k].

skewness​

((collection<number> | number)+) -> nan | real

  • arguments 1, … — The data, as collections or single numbers that form one sample; the skewness is the third central moment over the cube of the population standard deviation, and constant data or no data gives NaN. Sample: [1, 2, 3, 10].

Skewness of a collection of numbers.

skewness([1, 2, 3, 10])
// ➔ 18/25 * sqrt(2)

A measure of the asymmetry of the data about its mean, positive or negative: \frac{1}{n} \sum_{i=1}^n \left(\frac{x_i - \mu}{\sigma}\right)^3.

skewness([1, 2, 3, 10])
// ➔ 18/25 * sqrt(2)
MathJSON: Skewness

["Skewness", collection].

slidingWindow​

((xs: S, count: integer, step: integer?) -> list<string> where S: string) & ((xs: collection, count: integer, step: integer?) -> list<list>)

  • xs — The collection to read; a string gives windows that are strings. Sample: [1, 2, 3, 4].
  • count — The number of elements in each window, at least 1; a count larger than the collection gives no window. Sample: 2.
  • step — The distance between the starts of two windows, at least 1, by default 1; only complete windows are kept. Sample: 2.

Return overlapping sliding windows of fixed size over the collection.

slidingWindow([1, 2, 3, 4], 2)
// ➔ [[1,2],[2,3],[3,4]]
slidingWindow("abcd", 2)
// ➔ ["ab","bc","cd"]
slidingWindow([1, 2, 3, 4, 5], 3)
// ➔ [[1,2,3],[2,3,4],[3,4,5]]
MathJSON: SlidingWindow

["SlidingWindow", collection, size].

standardDeviation​

((collection<number> | distribution | number)+) -> nan | real<0..>

  • arguments 1, … — The data, as collections or single numbers that form one sample, or a single distribution, whose standard deviation is returned; the divisor is n − 1, so fewer than two values give NaN. Sample: [1, 3, 5].

Sample Standard Deviation of a collection of numbers.

standardDeviation([1, 3, 5])
// ➔ 2
standardDeviation(normalDistribution(0, 3))
// ➔ 3

The sample standard deviation, the square root of the sample variance: \sqrt{\frac{1}{n-1} \sum_{i=1}^n (x_i - \bar{x})^2}. populationStandardDeviation is the square root of the population variance.

[standardDeviation([1, 2, 3, 4]), populationStandardDeviation([1, 2, 3, 4])]
// ➔ [sqrt(15)/3,sqrt(5)/2]
standardDeviation(normalDistribution(mu, sigma))
// ➔ sigma
MathJSON: StandardDeviation

["StandardDeviation", collection], ["StandardDeviation", distribution].

studentTDistribution​

MathJSON StudentTDistribution · (real<0<..>) -> expression<StudentTDistribution>

  • argument 1 — The degrees of freedom ν, greater than 0 and not necessarily an integer. Sample: 5.

Student's t distribution with ν degrees of freedom (ν > 0, not necessarily an integer).

pdf(studentTDistribution(1), 0)
// ➔ 1 / pi
variance(studentTDistribution(5))
// ➔ 5/3

uniformDistribution​

(lower: real, upper: real) -> expression<UniformDistribution>

  • lower — The lower end a of the interval. Sample: 0.
  • upper — The upper end b of the interval, greater than lower. Sample: 4.

Continuous uniform distribution on the interval [a, b]. uniformDistribution([a, b]) is the same, and uniformDistribution() is on [0, 1].

pdf(uniformDistribution(0, 4), 1)
// ➔ 1/4
mean(uniformDistribution(2, 6))
// ➔ 4

The continuous uniform distribution on [a, b], with a < b. uniformDistribution([a, b]) is uniformDistribution(a, b), and uniformDistribution() is on [0, 1].

[mean(uniformDistribution([2, 4])), variance(uniformDistribution())]
// ➔ [3,1/12]
MathJSON: UniformDistribution

["UniformDistribution", a, b], ["UniformDistribution", ["List", a, b]], ["UniformDistribution"].

variance​

((collection<number> | distribution | number)+) -> nan | real<0..>

  • arguments 1, … — The data, as collections or single numbers that form one sample, or a single distribution, whose variance is returned; the divisor is n − 1, so fewer than two values give NaN. Sample: [1, 2, 3, 4].

Sample variance of a collection of numbers.

variance([1, 2, 3, 4])
// ➔ 5/3
variance(poissonDistribution(3))
// ➔ 3

The sample variance, with the denominator n - 1: \frac{1}{n-1} \sum_{i=1}^n (x_i - \bar{x})^2. populationVariance has the denominator n. Of a distribution, it is the variance of the distribution.

[variance([1, 2, 3, 4]), populationVariance([1, 2, 3, 4])]
// ➔ [5/3,5/4]
variance(poissonDistribution(lambda))
// ➔ lambda
MathJSON: Variance

["Variance", collection], ["Variance", distribution].