math-php
|
Statistical averages. More...
Static Public Member Functions | |
static | mean (array $numbers) |
Calculate the mean average of a list of numbers. More... | |
static | weightedMean (array $numbers, array $weights) |
Calculate the weighted mean average of a list of numbers https://en.wikipedia.org/wiki/Weighted_arithmetic_mean. More... | |
static | median (array $numbers) |
Calculate the median average of a list of numbers. More... | |
static | kthSmallest (array $numbers, int $k) |
Return the kth smallest value in an array Uses a linear-time algorithm: O(n) time in worst case. More... | |
static | mode (array $numbers) |
Calculate the mode average of a list of numbers If multiple modes (bimodal, trimodal, etc.), all modes will be returned. More... | |
static | geometricMean (array $numbers) |
Geometric mean A type of mean which indicates the central tendency or typical value of a set of numbers by using the product of their values (as opposed to the arithmetic mean which uses their sum). More... | |
static | harmonicMean (array $numbers) |
Harmonic mean (subcontrary mean) The harmonic mean can be expressed as the reciprocal of the arithmetic mean of the reciprocals. More... | |
static | contraharmonicMean (array $numbers) |
Contraharmonic mean A function complementary to the harmonic mean. More... | |
static | rootMeanSquare (array $numbers) |
Root mean square (quadratic mean) The square root of the arithmetic mean of the squares of a set of numbers. More... | |
static | quadraticMean (array $numbers) |
Quadradic mean (root mean square) Convenience function for rootMeanSquare. More... | |
static | trimean (array $numbers) |
Trimean (TM, or Tukey's trimean) A measure of a probability distribution's location defined as a weighted average of the distribution's median and its two quartiles. More... | |
static | interquartileMean (array $numbers) |
Interquartile mean (IQM) A measure of central tendency based on the truncated mean of the interquartile range. More... | |
static | iqm (array $numbers) |
IQM (Interquartile mean) Convenience function for interquartileMean. More... | |
static | cubicMean (array $numbers) |
Cubic mean https://en.wikipedia.org/wiki/Cubic_mean / 1 n x cubic = ³/ - ∑ xᵢ³ √ n ⁱ⁼¹ More... | |
static | truncatedMean (array $numbers, int $trim_percent) |
Truncated mean (trimmed mean) The mean after discarding given parts of a probability distribution or sample at the high and low end, and typically discarding an equal amount of both. More... | |
static | lehmerMean (array $numbers, $p) |
Lehmer mean https://en.wikipedia.org/wiki/Lehmer_mean. More... | |
static | generalizedMean (array $numbers, float $p) |
Generalized mean (power mean, Hölder mean) https://en.wikipedia.org/wiki/Generalized_mean. More... | |
static | powerMean (array $numbers, float $p) |
Power mean (generalized mean) Convenience method for generalizedMean. More... | |
static | simpleMovingAverage (array $numbers, int $n) |
Simple n-point moving average SMA The unweighted mean of the previous n data. More... | |
static | cumulativeMovingAverage (array $numbers) |
Cumulative moving average (CMA) More... | |
static | weightedMovingAverage (array $numbers, int $n, array $weights) |
Weighted n-point moving average (WMA) More... | |
static | exponentialMovingAverage (array $numbers, int $n) |
Exponential moving average (EMA) More... | |
static | arithmeticGeometricMean (float $x, float $y) |
Arithmetic-Geometric mean. More... | |
static | agm (float $x, float $y) |
Convenience method for arithmeticGeometricMean. More... | |
static | logarithmicMean (float $x, float $y) |
Logarithmic mean A function of two non-negative numbers which is equal to their difference divided by the logarithm of their quotient. More... | |
static | heronianMean (float $A, float $B) |
Heronian mean https://en.wikipedia.org/wiki/Heronian_mean __ H = ⅓(A + √AB + B) More... | |
static | identricMean (float $x, float $y) |
Identric mean https://en.wikipedia.org/wiki/Identric_mean 1 / xˣ I(x,y) = - ˣ⁻ʸ/ – ℯ √ yʸ More... | |
static | describe (array $numbers) |
Get a report of all the averages over a list of numbers Includes mean, median mode, geometric mean, harmonic mean, quardratic mean. More... | |
Statistical averages.
|
static |
Convenience method for arithmeticGeometricMean.
float | $x | |
float | $y |
|
static |
Arithmetic-Geometric mean.
First, compute the arithmetic and geometric means of x and y, calling them a₁ and g₁ respectively. Then, use iteration, with a₁ taking the place of x and g₁ taking the place of y. Both a and g will converge to the same mean. https://en.wikipedia.org/wiki/Arithmetic%E2%80%93geometric_mean
x and y ≥ 0 If x or y = 0, then agm = 0 If x or y < 0, then NaN
float | $x | |
float | $y |
|
static |
Contraharmonic mean A function complementary to the harmonic mean.
A special case of the Lehmer mean, L₂(x), where p = 2. https://en.wikipedia.org/wiki/Contraharmonic_mean
array | $numbers |
|
static |
Cubic mean https://en.wikipedia.org/wiki/Cubic_mean
/ 1 n x cubic = ³/ - ∑ xᵢ³ √ n ⁱ⁼¹
array | $numbers |
|
static |
Cumulative moving average (CMA)
Base case for initial average: x₀ CMA₀ = – 1
Standard case: xᵢ + (i * CMAᵢ₋₁) CMAᵢ = --------------— i + 1
array | $numbers |
|
static |
Get a report of all the averages over a list of numbers Includes mean, median mode, geometric mean, harmonic mean, quardratic mean.
array | $numbers |
Exception |
|
static |
Exponential moving average (EMA)
The start of the EPA is seeded with the first data point. Then each day after that: EMAtoday = α⋅xtoday + (1-α)EMAyesterday
where α: coefficient that represents the degree of weighting decrease, a constant smoothing factor between 0 and 1.
array | $numbers | |
int | $n | Length of the EPA |
|
static |
Generalized mean (power mean, Hölder mean) https://en.wikipedia.org/wiki/Generalized_mean.
/ 1 n \ 1/p Mp(x) = | - ∑ xᵢᵖ| \ n ⁱ⁼¹ /
Special cases: M-∞(x) is min(x) M₋₁(x) is the harmonic mean M₀(x) is the geometric mean M₁(x) is the arithmetic mean M₂(x) is the quadratic mean M₃(x) is the cubic mean M∞(x) is max(X)
array | $numbers | |
float | $p |
|
static |
Geometric mean A type of mean which indicates the central tendency or typical value of a set of numbers by using the product of their values (as opposed to the arithmetic mean which uses their sum).
https://en.wikipedia.org/wiki/Geometric_mean
Geometric mean = ⁿ√a₀a₁a₂ ⋯
array | $numbers |
|
static |
Harmonic mean (subcontrary mean) The harmonic mean can be expressed as the reciprocal of the arithmetic mean of the reciprocals.
Appropriate for situations when the average of rates is desired. https://en.wikipedia.org/wiki/Harmonic_mean
array | $numbers |
Exception |
|
static |
Heronian mean https://en.wikipedia.org/wiki/Heronian_mean __ H = ⅓(A + √AB + B)
float | $A | |
float | $B |
|
static |
Identric mean https://en.wikipedia.org/wiki/Identric_mean
1 / xˣ I(x,y) = - ˣ⁻ʸ/ – ℯ √ yʸ
float | $x | |
float | $y |
Exception |
|
static |
Interquartile mean (IQM) A measure of central tendency based on the truncated mean of the interquartile range.
Only the data in the second and third quartiles is used (as in the interquartile range), and the lowest 25% and the highest 25% of the scores are discarded. https://en.wikipedia.org/wiki/Interquartile_mean
array | $numbers |
Exception |
|
static |
IQM (Interquartile mean) Convenience function for interquartileMean.
array | $numbers |
Exception |
|
static |
Return the kth smallest value in an array Uses a linear-time algorithm: O(n) time in worst case.
if $a = [1,2,3,4,6,7]
kthSmallest($a, 4) = 6
Algorithm: 1) If n is small, just sort and return 2) Otherwise, group into 5-element subsets and mind the median 3) Find the median of the medians 4) Find L and U sets
array | $numbers | |
int | $k | zero indexed |
|
static |
Lehmer mean https://en.wikipedia.org/wiki/Lehmer_mean.
∑xᵢᵖ Lp(x) = ---— ∑xᵢᵖ⁻¹
Special cases: L-∞(x) is the min(x) L₀(x) is the harmonic mean L½(x₀, x₁) is the geometric mean if computed against two numbers L₁(x) is the arithmetic mean L₂(x) is the contraharmonic mean L∞(x) is the max(x)
array | $numbers | |
float | $p |
|
static |
Logarithmic mean A function of two non-negative numbers which is equal to their difference divided by the logarithm of their quotient.
https://en.wikipedia.org/wiki/Logarithmic_mean
Mlm(x, y) = 0 if x = 0 or y = 0 x if x = y otherwise:
ln y - ln x
float | $x | |
float | $y |
|
static |
Calculate the mean average of a list of numbers.
∑⟮xᵢ⟯ x̄ = --— n
array | $numbers |
|
static |
Calculate the median average of a list of numbers.
array | $numbers |
|
static |
Calculate the mode average of a list of numbers If multiple modes (bimodal, trimodal, etc.), all modes will be returned.
Always returns an array, even if only one mode.
array | $numbers |
|
static |
Power mean (generalized mean) Convenience method for generalizedMean.
array | $numbers | |
float | $p |
|
static |
Quadradic mean (root mean square) Convenience function for rootMeanSquare.
array | $numbers |
|
static |
Root mean square (quadratic mean) The square root of the arithmetic mean of the squares of a set of numbers.
https://en.wikipedia.org/wiki/Root_mean_square
/x₁+²x₂²+ ⋯ x rms = / --------— √ n
array | $numbers |
|
static |
Simple n-point moving average SMA The unweighted mean of the previous n data.
First calculate initial average: ⁿ⁻¹ ∑ xᵢ ᵢ₌₀
To calculating successive values, a new value comes into the sum and an old value drops out: SMAtoday = SMAyesterday + NewNumber/N - DropNumber/N
array | $numbers | |
int | $n | n-point moving average |
|
static |
Trimean (TM, or Tukey's trimean) A measure of a probability distribution's location defined as a weighted average of the distribution's median and its two quartiles.
https://en.wikipedia.org/wiki/Trimean
Q₁ + 2Q₂ + Q₃
TM = ----------— 4
array | $numbers |
|
static |
Truncated mean (trimmed mean) The mean after discarding given parts of a probability distribution or sample at the high and low end, and typically discarding an equal amount of both.
This number of points to be discarded is given as a percentage of the total number of points. https://en.wikipedia.org/wiki/Truncated_mean
Trim count = floor( (trim percent / 100) * sample size )
For example: [8, 3, 7, 1, 3, 9] with a trim of 20% First sort the list: [1, 3, 3, 7, 8, 9] Sample size = 6 Then determine trim count: floot(20/100 * 6 ) = 1 Trim the list by removing 1 from each end: [3, 3, 7, 8] Finally, find the mean: 5.2
array | $numbers | |
int | $trim_percent | Percent between 0-99 |
Exception |
|
static |
Calculate the weighted mean average of a list of numbers https://en.wikipedia.org/wiki/Weighted_arithmetic_mean.
∑⟮xᵢwᵢ⟯ x̄ = --— ∑⟮wᵢ⟯
array | $numbers | |
array | $weights |
Exception |
|
static |
Weighted n-point moving average (WMA)
Similar to simple n-point moving average, however, each n-point has a weight associated with it, and instead of dividing by n, we divide by the sum of the weights.
Each weighted average = ∑(weighted values) / ∑(weights)
array | $numbers | |
int | $n | n-point moving average |
array | $weights | Weights for each n points |
Exception |