Overview
Zero-dependency descriptive statistics and math library for HolyPHP.
bundled — works offline
zero dependencies
math
statistics
Quick start
import "stats";
$data = [2, 4, 4, 4, 5, 5, 7, 9];
println("Mean: " . stats_mean($data)); // 5
println("Median: " . stats_median($data)); // 4.5
println("Mode: " . stats_mode($data)); // 4
println("StdDev: " . stats_stddev($data)); // 2
println("P90: " . stats_percentile($data, 90)); // 7.6
$reg = stats_linear_regression([1, 2, 3, 4], [3, 5, 7, 9]);
println("y = " . $reg["slope"] . "x + " . $reg["intercept"]); // y = 2x + 1
Installation
In a project
Inside a project (a folder with hphp.yaml, see Projects & packages):
hphp install stats
This adds the library to hphp.yaml, installs it into .hphp/packages/stats/ and pins the exact version in hphp.lock:
require:
stats: "^1.0"
Use it
import "stats";
stats is bundled with the compiler, so import "stats"; also works in a single file without installing anything, and hphp install works offline.
Outside a project
hphp install stats # for your user, shared by all scripts
hphp create myapp # or start a project, then: hphp install stats
Documentation
API Reference
stats_sum(array $values): float
Returns the sum of all numeric elements in $values.
stats_mean(array $values): float
Returns the arithmetic mean of $values.
stats_median(array $values): float
Returns the median of $values (does not mutate the input array).
stats_mode(array $values): float
Returns the most frequently occurring value in $values. Ties are broken by returning the smallest value.
stats_min(array $values): float
Returns the minimum value in $values.
stats_max(array $values): float
Returns the maximum value in $values.
stats_variance(array $values, bool $sample = false): float
Returns the variance of $values.
$sample = false (default): population variance (divides by n).
$sample = true: sample variance (divides by n - 1; requires at least 2 values).
stats_stddev(array $values, bool $sample = false): float
Returns the standard deviation (sqrt(stats_variance($values, $sample))).
stats_percentile(array $values, float $p): float
Returns the $p-th percentile (0 <= $p <= 100) using linear interpolation between closest ranks.
stats_correlation(array $xs, array $ys): float
Returns the Pearson correlation coefficient r in [-1, 1] between $xs and $ys.
stats_linear_regression(array $xs, array $ys): array
Computes ordinary least squares linear regression y = slope * x + intercept and returns an associative array:
"slope" (float): slope of the fitted line
"intercept" (float): y-intercept of the fitted line
"r2" (float): coefficient of determination (R²)
Error Handling
All functions validate their inputs and throw Exception with a clear stats: ... message on invalid input:
- Empty array:
stats: stats_mean() requires a non-empty array
- Non-numeric elements:
stats: stats_sum() expects numeric values, got non-numeric at index 1
- Out-of-range percentile:
stats: stats_percentile() requires percentile between 0 and 100, got 105
- Sample variance/stddev with < 2 items:
stats: stats_variance() with sample=true requires at least 2 values
- Mismatched array lengths:
stats: stats_correlation() requires arrays of equal length
- Zero variance:
stats: stats_correlation() is undefined when either series has zero variance
Examples
01Quick start
Bundled with the compiler — import "stats"; works offline in any file, or add it to a project with hphp install stats.
import "stats";
$data = [2, 4, 4, 4, 5, 5, 7, 9];
println("Mean: " . stats_mean($data)); // 5
println("Median: " . stats_median($data)); // 4.5
println("Mode: " . stats_mode($data)); // 4
println("StdDev: " . stats_stddev($data)); // 2
println("P90: " . stats_percentile($data, 90)); // 7.6
$reg = stats_linear_regression([1, 2, 3, 4], [3, 5, 7, 9]);
println("y = " . $reg["slope"] . "x + " . $reg["intercept"]); // y = 2x + 1
Try ithphp install stats
hphp run main.hphp
Covered by the official test suite in tests/positive/lib_stats.hphp.