API reference
Every public class and method of the statistics library.
Every class is in the PHPMath\Statistics namespace. All classes are final and readonly, and no method changes the instance it is called on. Exceptions are in PHPMath\Statistics\Exception.
Sample#
PHPMath\Statistics\Sample is a final readonly, countable, non-empty list of finite floats with descriptive statistics. See Core concepts and Descriptive statistics.
__construct()#
public function __construct(array $values)
Creates a sample. Only int and float values are accepted, so strings (also numeric ones), null and booleans are rejected. Integers are converted to floats and the order is kept.
- Parameters:
$valuesa list of at least one finiteintorfloat - Throws: EmptySampleException if no values are given
- Throws: NonNumericValueException if a value is not an
intor afloat, with the position in the message - Throws: NonFiniteValueException if a value is
NANor infinite
toArray()#
public function toArray(): array
Returns the values in their original order.
- Returns:
list<float>
count()#
public function count(): int
Returns the number of values. This implements Countable.
- Returns:
int
sum()#
public function sum(): float
Returns the sum, computed with compensated (Neumaier) summation. The result is not checked: it is infinity when the sum exceeds the largest float.
- Returns:
float
mean()#
public function mean(): float
Returns the arithmetic mean, computed with Welford's running algorithm, so it does not overflow for finite values.
- Returns:
float
median()#
public function median(): float
Returns the middle value of the sorted data, or the mean of the two middle values when the count is even.
- Returns:
float
mode()#
public function mode(): array
Returns every value that occurs most often, in ascending order. When all values are unique, every value is returned.
- Returns:
list<float>
populationVariance()#
public function populationVariance(): float
Returns the population variance: the sum of squared deviations divided by n, computed with Welford's running algorithm. A sample with one value gives 0.0. The result is infinity when the true variance exceeds the largest float.
- Returns:
float
sampleVariance()#
public function sampleVariance(): float
Returns the sample variance: the sum of squared deviations divided by n - 1 (Bessel's correction).
- Returns:
float - Throws: InsufficientDataException if the sample has fewer than two values
populationStandardDeviation()#
public function populationStandardDeviation(): float
Returns the square root of the population variance.
- Returns:
float
sampleStandardDeviation()#
public function sampleStandardDeviation(): float
Returns the square root of the sample variance.
- Returns:
float - Throws: InsufficientDataException if the sample has fewer than two values
percentile()#
public function percentile(float $p): float
Returns the percentile using linear interpolation between closest ranks (Hyndman and Fan type 7, the default of R and NumPy, and the same as Excel's PERCENTILE.INC).
- Parameters:
$pa percentile from 0 to 100, inclusive - Returns:
float - Throws: InvalidPercentileException if the percentile is outside 0 to 100, or
NAN
quartiles()#
public function quartiles(): Quartiles
Returns the first, second and third quartile, using the interpolation of percentile().
- Returns:
Quartiles
interquartileRange()#
public function interquartileRange(): float
Returns the third quartile minus the first quartile.
- Returns:
float
skewness()#
public function skewness(): float
Returns the population skewness (Fisher-Pearson coefficient g1): the third central moment divided by the cube of the population standard deviation.
- Returns:
float - Throws: ZeroVarianceException if all values are equal
populationCovariance()#
public function populationCovariance(self $other): float
Returns the population covariance with another sample, dividing by n. The values are paired by position. Samples with one value give 0.0.
- Parameters:
$othera sample of the same size - Returns:
float - Throws: SampleSizeMismatchException if the samples differ in size
sampleCovariance()#
public function sampleCovariance(self $other): float
Returns the sample covariance with another sample, dividing by n - 1.
- Parameters:
$othera sample of the same size - Returns:
float - Throws: SampleSizeMismatchException if the samples differ in size
- Throws: InsufficientDataException if the samples have fewer than two values
correlation()#
public function correlation(self $other): float
Returns the Pearson correlation coefficient with another sample. The result is clamped to the range -1 to 1.
- Parameters:
$othera sample of the same size - Returns:
float - Throws: SampleSizeMismatchException if the samples differ in size
- Throws: ZeroVarianceException if either sample has all values equal
zScores()#
public function zScores(): self
Returns a new sample with the standard scores (x - mean) / population standard deviation.
- Returns:
Sample - Throws: ZeroVarianceException if all values are equal
map()#
public function map(callable $callback): self
Returns a new sample with the callback applied to every value.
- Parameters:
$callbackacallable(float): float - Returns:
Sample - Throws: NonFiniteValueException if the callback returns
NANor infinity - Throws: NonNumericValueException if the callback returns something other than an
intor afloat
plus()#
public function plus(float $amount): self
Returns a new sample with the amount added to every value.
- Parameters:
$amountthe number to add - Returns:
Sample - Throws: NonFiniteValueException if a result is infinite
times()#
public function times(float $factor): self
Returns a new sample with every value multiplied by the factor.
- Parameters:
$factorthe multiplier - Returns:
Sample - Throws: NonFiniteValueException if a result is infinite or
NAN
sorted()#
public function sorted(): self
Returns a new sample with the values in ascending order.
- Returns:
Sample
Quartiles#
PHPMath\Statistics\Quartiles is a final readonly value object that holds the three quartiles of a sample. Create it with Sample::quartiles(). See Percentiles and quartiles.
__construct()#
public function __construct(
public float $first,
public float $second,
public float $third,
)
Creates a quartiles object. The values are exposed as public readonly properties.
- Parameters:
$firstthe 25th percentile,$secondthe median,$thirdthe 75th percentile - Throws: NonFiniteValueException if a value is
NANor infinite - Throws: InvalidQuartilesException if the values do not satisfy
first <= second <= third
NormalDistribution#
PHPMath\Statistics\NormalDistribution is a final readonly normal (Gaussian) distribution. The properties mean and standardDeviation are public and readonly. See Normal distribution.
__construct()#
public function __construct(
public float $mean,
public float $standardDeviation,
)
Creates a distribution.
- Parameters:
$meanthe centre of the curve,$standardDeviationthe width of the curve, greater than zero - Throws: NonFiniteValueException if the mean is
NANor infinite - Throws: InvalidStandardDeviationException if the standard deviation is not finite and greater than zero
pdf()#
public function pdf(float $x): float
Returns the probability density at x. Infinite values are allowed and give 0.0.
- Parameters:
$xthe value to evaluate - Returns:
float - Throws: NonFiniteValueException if
xisNAN
cdf()#
public function cdf(float $x): float
Returns the cumulative probability P(X <= x). The tails are accurate to about 1e-14 relative. Infinite values are allowed: -INF gives 0.0 and INF gives 1.0.
- Parameters:
$xthe value to evaluate - Returns:
float - Throws: NonFiniteValueException if
xisNAN
quantile()#
public function quantile(float $probability): float
Returns the value x for which P(X <= x) equals the probability. It is found by bisection on cdf().
- Parameters:
$probabilitya probability strictly between 0 and 1 - Returns:
float - Throws: InvalidProbabilityException if the probability is not strictly between 0 and 1
LinearRegression#
PHPMath\Statistics\LinearRegression is a final readonly simple linear regression (ordinary least squares) y = intercept + slope * x. The properties slope, intercept and rSquared are public and readonly. The constructor is private, so use fit(). See Linear regression.
fit()#
public static function fit(Sample $x, Sample $y): self
Fits the line to paired values. When y is constant, the line is horizontal and rSquared is 1.0.
- Parameters:
$xthe x values,$ythe y values, of the same size - Returns:
LinearRegression - Throws: SampleSizeMismatchException if the samples differ in size
- Throws: ZeroVarianceException if all x values are equal
predict()#
public function predict(float $x): float
Returns intercept + slope * x.
- Parameters:
$xthe x value to predict for - Returns:
float