Skip to content

Statistics and special functions

AI assistant

Build this with Claude Code, Cursor or Copilot

Copy a Tessero-tuned prompt for Statistics and special functions, grounded in 16 real API signatures , into your IDE's AI. No chatbot, just exact context.

Three classes follow scipy.special, scipy.stats and NumPy's statistics functions name for name. Method names are the SciPy/NumPy names in camelCase (ttest_ind → ttestInd, log_ndtr → logNdtr), and keyword arguments are PHP named arguments. Arguments can be PHP numbers, PHP arrays or NDArrays, with NumPy broadcasting.

FFI package Extension Follows
Tessero\Special Tessero\Ext\Special scipy.special
Tessero\Stats Tessero\Ext\Stats scipy.stats
Tessero\Np Tessero\Ext\Np NumPy statistics, set and search functions

Both backends run the same kernel code, so they return the same bits. Every function is checked against values generated by NumPy/SciPy. The [coverage page(https://github.com/DMJ-CV-91913/tessero/blob/main/docs/project/numpy-scipy-coverage.md) lists what exists, and the [reference deviations(https://github.com/DMJ-CV-91913/tessero/blob/main/docs/project/reference-deviations.md) page lists where a result deliberately differs from SciPy's.

Distributions

A distribution call returns a frozen distribution, as in SciPy:

use Tessero\Stats;
use Tessero\Random\Generator;

$z = Stats::norm();
$z->cdf(1.96);                     // 0.9750021048517795
$z->ppf(0.975);                    // 1.959963984540054

$g = Stats::gamma(2.0, scale: 3.0);          // shape parameters, then loc and scale
$g->mean();                        // 6.0
$g->var();                         // 18.0
$g->median();                      // 5.035040970049984
$g->interval(0.9);                 // [1.066084532095986, 14.231593555171731]
$g->stats(moments: 'mv');          // ['mean' => 6.0, 'var' => 18.0]
$g->rvs(size: 3, randomState: Generator::defaultRng(7));   // == gamma(2, scale=3).rvs(3, random_state=default_rng(7))

Stats::poisson(4.5)->pmf(3);       // 0.168717884924555
Stats::binom(10, 0.3)->sf(4);      // 0.15026833259999992

The methods are pdf/pmf, logpdf/logpmf, cdf, logcdf, sf, logsf, ppf, isf, stats, mean, var, std, median, entropy, moment, support, interval and rvs. They behave as SciPy's public methods do, including outside the support and for invalid parameters (NaN). Where SciPy computes something numerically (moments by integration, quantiles by root finding), Tessero runs the same algorithm: SciPy's QUADPACK and brentq, vendored. rvs draws from the Tessero Generator passed as randomState, through NumPy's own sampling code, so the draws equal SciPy's.

Tests, correlation and descriptive statistics

Functions that return a SciPy result object return an associative array with the same field names:

$x = [2.1, 3.4, 1.9, 5.6, 4.4, 3.3, 2.8];
$y = [3.9, 4.1, 5.2, 6.0, 4.8, 5.5, 6.3];

Stats::ttestInd($x, $y);           // ['statistic' => -2.9318974619301956, 'pvalue' => 0.012559627601277113, 'df' => 12.0]
Stats::pearsonr($x, $y);           // ['statistic' => 0.3295765529354113, 'pvalue' => 0.47037875019519426]
Stats::describe($x);               // keys nobs, minmax, mean, variance, skewness, kurtosis

axis, keepdims and nan_policy (as nanPolicy) work as in SciPy.

NumPy statistics

use Tessero\Np;

Np::median($x);                              // 3.3
Np::percentile($x, 90);                      // 4.880000000000001
Np::quantile($x, 0.25, method: 'nearest');   // 2.8

Also available: the nan-reductions (nanmean, nanmedian, nanstd, ...), average, ptp, histogram, bincount, digitize, cov, corrcoef, unique* and the set operations, partition, lexsort, searchsorted, correlate and convolve.

Special functions

use Tessero\Special;

Special::gammainc(2.0, 1.5);       // 0.4421745996289252
Special::erfinv(0.5);              // 0.4769362762044699
Special::jv(1.0, 2.5);             // 0.4970941024642741

The special functions run the code SciPy itself runs: xsf, Boost.Math and SciPy's own C and C++ glue. They are real-valued; complex arguments are not supported yet.

Edit this page on GitHub