stats

Descriptive statistics and math library (mean, median, stddev, percentiles).

v1.0.0Math, time & IDsbundledimport "stats";
Overview Installation Documentation Examples

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
Packagestats
Version1.0.0 (follows the compiler)
Entry filestats.hphp
Sourcepackages/lib/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.

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:

Error Handling

All functions validate their inputs and throw Exception with a clear stats: ... message on invalid input:

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 it
hphp install stats
hphp run main.hphp

Covered by the official test suite in tests/positive/lib_stats.hphp.