PHP Class MathPHP\Statistics\Average

ファイルを表示 Open project: markrogoyski/math-php Class Usage Examples

Public Methods

Method Description
agm ( number $x, number $y ) : float Convenience method for arithmeticGeometricMean
arithmeticGeometricMean ( number $x, number $y ) : float Arithmetic-Geometric mean
contraharmonicMean ( array $numbers ) : number Contraharmonic mean A function complementary to the harmonic mean.
cubicMean ( array $numbers ) : number Cubic mean https://en.wikipedia.org/wiki/Cubic_mean _________ 1 n x cubic = ³/ - ∑ xᵢ³ √ n ⁱ⁼¹
cumulativeMovingAverage ( array $numbers ) : array Cumulative moving average (CMA)
describe ( array $numbers ) : array Get a report of all the averages over a list of numbers Includes mean, median mode, geometric mean, harmonic mean, quardratic mean
exponentialMovingAverage ( array $numbers, integer $n ) : array Exponential moving average (EMA)
generalizedMean ( array $numbers, number $p ) : number Generalized mean (power mean, Hölder mean) https://en.wikipedia.org/wiki/Generalized_mean
geometricMean ( array $numbers ) : number 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).
harmonicMean ( array $numbers ) : number Harmonic mean (subcontrary mean) The harmonic mean can be expressed as the reciprocal of the arithmetic mean of the reciprocals.
heronianMean ( number $A, number $B ) : number Heronian mean https://en.wikipedia.org/wiki/Heronian_mean __ H = ⅓(A + √AB + B)
identricMean ( number $x, number $y ) : number Identric mean https://en.wikipedia.org/wiki/Identric_mean ____ 1 / xˣ I(x,y) = - ˣ⁻ʸ/ -- ℯ √ yʸ
interquartileMean ( array $numbers ) : number Interquartile mean (IQM) A measure of central tendency based on the truncated mean of the interquartile range.
iqm ( array $numbers ) : number IQM (Interquartile mean) Convenience function for interquartileMean
kthSmallest ( array $numbers, integer $k ) : number Return the kth smallest value in an array Uses a linear-time algorithm: O(n) time in worst case.
lehmerMean ( array $numbers, number $p ) : number Lehmer mean https://en.wikipedia.org/wiki/Lehmer_mean
logarithmicMean ( number $x, number $y ) : number Logarithmic mean A function of two non-negative numbers which is equal to their difference divided by the logarithm of their quotient.
mean ( array $numbers ) : number Calculate the mean average of a list of numbers
median ( array $numbers ) : number Calculate the median average of a list of numbers
mode ( array $numbers ) : array Calculate the mode average of a list of numbers If multiple modes (bimodal, trimodal, etc.), all modes will be returned.
powerMean ( array $numbers, number $p ) : number Power mean (generalized mean) Convenience method for generalizedMean
quadraticMean ( array $numbers ) : number Quadradic mean (root mean square) Convenience function for rootMeanSquare
rootMeanSquare ( array $numbers ) : number Root mean square (quadratic mean) The square root of the arithmetic mean of the squares of a set of numbers.
simpleMovingAverage ( array $numbers, integer $n ) : array Simple n-point moving average SMA The unweighted mean of the previous n data.
trimean ( array $numbers ) : number 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.
truncatedMean ( array $numbers, integer $trim_percent ) : number 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.
weightedMovingAverage ( array $numbers, integer $n, array $weights ) : array Weighted n-point moving average (WMA)

Private Methods

Method Description
splitAtValue ( array $numbers, integer $value ) : array Given an array and a value, separate the array into two groups, those values which are greater than the value, and those that are less than the value. Also, tell how many times the value appears in the array.

Method Details

agm() public static method

Convenience method for arithmeticGeometricMean
public static agm ( number $x, number $y ) : float
$x number
$y number
return float

arithmeticGeometricMean() public static method

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
public static arithmeticGeometricMean ( number $x, number $y ) : float
$x number
$y number
return float

contraharmonicMean() public static method

A special case of the Lehmer mean, L₂(x), where p = 2. https://en.wikipedia.org/wiki/Contraharmonic_mean
public static contraharmonicMean ( array $numbers ) : number
$numbers array
return number

cubicMean() public static method

Cubic mean https://en.wikipedia.org/wiki/Cubic_mean _________ 1 n x cubic = ³/ - ∑ xᵢ³ √ n ⁱ⁼¹
public static cubicMean ( array $numbers ) : number
$numbers array
return number

cumulativeMovingAverage() public static method

Base case for initial average: x₀ CMA₀ = -- 1 Standard case: xᵢ + (i * CMAᵢ₋₁) CMAᵢ = ----------------- i + 1
public static cumulativeMovingAverage ( array $numbers ) : array
$numbers array
return array of cumulative averages

describe() public static method

Get a report of all the averages over a list of numbers Includes mean, median mode, geometric mean, harmonic mean, quardratic mean
public static describe ( array $numbers ) : array
$numbers array
return array [ mean, median, mode, geometric_mean, harmonic_mean, contraharmonic_mean, quadratic_mean, trimean, iqm, cubic_mean ]

exponentialMovingAverage() public static method

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.
public static exponentialMovingAverage ( array $numbers, integer $n ) : array
$numbers array
$n integer Length of the EPA
return array of exponential moving averages

generalizedMean() public static method

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)
public static generalizedMean ( array $numbers, number $p ) : number
$numbers array
$p number
return number

geometricMean() public static method

https://en.wikipedia.org/wiki/Geometric_mean __________ Geometric mean = ⁿ√a₀a₁a₂ ⋯
public static geometricMean ( array $numbers ) : number
$numbers array
return number

harmonicMean() public static method

Appropriate for situations when the average of rates is desired. https://en.wikipedia.org/wiki/Harmonic_mean
public static harmonicMean ( array $numbers ) : number
$numbers array
return number

heronianMean() public static method

Heronian mean https://en.wikipedia.org/wiki/Heronian_mean __ H = ⅓(A + √AB + B)
public static heronianMean ( number $A, number $B ) : number
$A number
$B number
return number

identricMean() public static method

Identric mean https://en.wikipedia.org/wiki/Identric_mean ____ 1 / xˣ I(x,y) = - ˣ⁻ʸ/ -- ℯ √ yʸ
public static identricMean ( number $x, number $y ) : number
$x number
$y number
return number

interquartileMean() public static method

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
public static interquartileMean ( array $numbers ) : number
$numbers array
return number

iqm() public static method

IQM (Interquartile mean) Convenience function for interquartileMean
public static iqm ( array $numbers ) : number
$numbers array
return number

kthSmallest() public static method

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 - L is numbers lower than the median of medians - U is numbers higher than the median of medians 5) Recursive step - if k is the median of medians, return that - Otherwise, recusively search in smaller group.
public static kthSmallest ( array $numbers, integer $k ) : number
$numbers array
$k integer zero indexed
return number

lehmerMean() public static method

∑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)
public static lehmerMean ( array $numbers, number $p ) : number
$numbers array
$p number
return number

logarithmicMean() public static method

https://en.wikipedia.org/wiki/Logarithmic_mean Mlm(x, y) = 0 if x = 0 or y = 0 x if x = y otherwise: y - x ----------- ln y - ln x
public static logarithmicMean ( number $x, number $y ) : number
$x number
$y number
return number

mean() public static method

∑⟮xᵢ⟯ x̄ = ----- n
public static mean ( array $numbers ) : number
$numbers array
return number

median() public static method

Calculate the median average of a list of numbers
public static median ( array $numbers ) : number
$numbers array
return number

mode() public static method

Always returns an array, even if only one mode.
public static mode ( array $numbers ) : array
$numbers array
return array of mode(s)

powerMean() public static method

Power mean (generalized mean) Convenience method for generalizedMean
public static powerMean ( array $numbers, number $p ) : number
$numbers array
$p number
return number

quadraticMean() public static method

Quadradic mean (root mean square) Convenience function for rootMeanSquare
public static quadraticMean ( array $numbers ) : number
$numbers array
return number

rootMeanSquare() public static method

https://en.wikipedia.org/wiki/Root_mean_square ___________ x₁+²x₂²+ ⋯ x rms = / ----------- √ n
public static rootMeanSquare ( array $numbers ) : number
$numbers array
return number

simpleMovingAverage() public static method

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
public static simpleMovingAverage ( array $numbers, integer $n ) : array
$numbers array
$n integer n-point moving average
return array of averages for each n-point time period

trimean() public static method

https://en.wikipedia.org/wiki/Trimean Q₁ + 2Q₂ + Q₃ TM = ------------- 4
public static trimean ( array $numbers ) : number
$numbers array
return number

truncatedMean() public static method

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
public static truncatedMean ( array $numbers, integer $trim_percent ) : number
$numbers array
$trim_percent integer Percent between 0-99
return number

weightedMovingAverage() public static method

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)
public static weightedMovingAverage ( array $numbers, integer $n, array $weights ) : array
$numbers array
$n integer n-point moving average
$weights array Weights for each n points
return array of averages