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.

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

min()#

public function min(): float

Returns the smallest value.

  • Returns: float

max()#

public function max(): float

Returns the largest value.

  • Returns: float

range()#

public function range(): float

Returns max() - min().

  • 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).

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.

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: $p a 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.

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.

sampleCovariance()#

public function sampleCovariance(self $other): float

Returns the sample covariance with another sample, dividing by n - 1.

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.

zScores()#

public function zScores(): self

Returns a new sample with the standard scores (x - mean) / population standard deviation.

map()#

public function map(callable $callback): self

Returns a new sample with the callback applied to every value.

plus()#

public function plus(float $amount): self

Returns a new sample with the amount added to every value.

times()#

public function times(float $factor): self

Returns a new sample with every value multiplied by the factor.

  • Parameters: $factor the 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.

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.

pdf()#

public function pdf(float $x): float

Returns the probability density at x. Infinite values are allowed and give 0.0.

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.

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: $probability a 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.

predict()#

public function predict(float $x): float

Returns intercept + slope * x.

  • Parameters: $x the x value to predict for
  • Returns: float