Statistics and optimization functions
Descriptive statistics, density estimation, interpolation, the ECDF, and one-dimensional optimization. Missing values, weights, and errors follow the shared rules.
Summaries
stat_mean()
Arithmetic mean.
nullandNANare missing:missing: 'raise'rejects them, andmissing: 'drop'removes them, pairwise for two arrays.- Infinite input, or arithmetic that overflows, throws.
Phit\stat_mean( array $values, string $missing = 'raise',): float| Parameter | Type | Default | Description |
|---|---|---|---|
$values |
array |
required | Data; null and NAN are missing |
$missing |
string |
'raise' |
'raise' rejects null and NAN; 'drop' removes them, pairwise for aligned inputs |
Returns float
Errors Exception: code 1001, 1002, or 1003 for an input error.
See also Details
stat_median()
Median: the type 7 quantile at 0.5.
nullandNANare missing:missing: 'raise'rejects them, andmissing: 'drop'removes them, pairwise for two arrays.- Infinite input, or arithmetic that overflows, throws.
Phit\stat_median( array $values, string $missing = 'raise',): float| Parameter | Type | Default | Description |
|---|---|---|---|
$values |
array |
required | Data; null and NAN are missing |
$missing |
string |
'raise' |
'raise' rejects null and NAN; 'drop' removes them, pairwise for aligned inputs |
Returns float
Errors Exception: code 1001, 1002, or 1003 for an input error.
See also Details
stat_var()
Variance: type: 'sample' divides by n − 1, and 'population' by n.
nullandNANare missing:missing: 'raise'rejects them, andmissing: 'drop'removes them, pairwise for two arrays.- Infinite input, or arithmetic that overflows, throws.
Phit\stat_var( array $values, string $type = 'sample', string $missing = 'raise',): float| Parameter | Type | Default | Description |
|---|---|---|---|
$values |
array |
required | Data; null and NAN are missing |
$type |
string |
'sample' |
'sample' divides by n − 1, 'population' by n |
$missing |
string |
'raise' |
'raise' rejects null and NAN; 'drop' removes them, pairwise for aligned inputs |
Returns float
Errors Exception: code 1001, 1002, or 1003 for an input error.
See also Details
stat_sd()
Standard deviation: the square root of stat_var().
nullandNANare missing:missing: 'raise'rejects them, andmissing: 'drop'removes them, pairwise for two arrays.- Infinite input, or arithmetic that overflows, throws.
Phit\stat_sd( array $values, string $type = 'sample', string $missing = 'raise',): float| Parameter | Type | Default | Description |
|---|---|---|---|
$values |
array |
required | Data; null and NAN are missing |
$type |
string |
'sample' |
'sample' divides by n − 1, 'population' by n |
$missing |
string |
'raise' |
'raise' rejects null and NAN; 'drop' removes them, pairwise for aligned inputs |
Returns float
Errors Exception: code 1001, 1002, or 1003 for an input error.
See also Details
stat_quantile()
Sample quantile by R’s default type 7, linear interpolation.
nullandNANare missing:missing: 'raise'rejects them, andmissing: 'drop'removes them, pairwise for two arrays.- Infinite input, or arithmetic that overflows, throws.
Phit\stat_quantile( array $values, float $p, string $missing = 'raise',): float| Parameter | Type | Default | Description |
|---|---|---|---|
$values |
array |
required | Data; null and NAN are missing |
$p |
float |
required | Probability in [0, 1] |
$missing |
string |
'raise' |
'raise' rejects null and NAN; 'drop' removes them, pairwise for aligned inputs |
Returns float
Errors Exception: code 1001, 1002, or 1003 for an input error.
See also Details
stat_quantile_array()
Several type 7 quantiles from one sort.
nullandNANare missing:missing: 'raise'rejects them, andmissing: 'drop'removes them, pairwise for two arrays.- Infinite input, or arithmetic that overflows, throws.
Phit\stat_quantile_array( array $values, array $probabilities, string $missing = 'raise',): array| Parameter | Type | Default | Description |
|---|---|---|---|
$values |
array |
required | Data; null and NAN are missing |
$probabilities |
array |
required | Probabilities in [0, 1] |
$missing |
string |
'raise' |
'raise' rejects null and NAN; 'drop' removes them, pairwise for aligned inputs |
Returns list<float>
Errors Exception: code 1001, 1002, or 1003 for an input error.
See also Details
stat_weighted_mean()
Weighted mean: sum(w·x) / sum(w).
- Weights must be nonnegative with a positive sum. A negative weight is rejected before missing values are removed.
nullandNANare missing:missing: 'raise'rejects them, andmissing: 'drop'removes them, pairwise for two arrays.- Infinite input, or arithmetic that overflows, throws.
Phit\stat_weighted_mean( array $values, array $weights, string $missing = 'raise',): float| Parameter | Type | Default | Description |
|---|---|---|---|
$values |
array |
required | Data; null and NAN are missing |
$weights |
array |
required | Nonnegative weights with a positive sum, aligned with the values |
$missing |
string |
'raise' |
'raise' rejects null and NAN; 'drop' removes them, pairwise for aligned inputs |
Returns float
Errors Exception: code 1001, 1002, or 1003 for an input error.
See also Details
stat_weighted_var()
Weighted variance: type: 'frequency' divides by W − 1, 'reliability' by W − sum(w²)/W, and 'population' by W, with W = sum(w).
- A negative weight is rejected before missing values are removed.
nullandNANare missing:missing: 'raise'rejects them, andmissing: 'drop'removes them, pairwise for two arrays.- Infinite input, or arithmetic that overflows, throws.
Phit\stat_weighted_var( array $values, array $weights, string $type = 'frequency', string $missing = 'raise',): float| Parameter | Type | Default | Description |
|---|---|---|---|
$values |
array |
required | Data; null and NAN are missing |
$weights |
array |
required | Nonnegative weights with a positive sum, aligned with the values |
$type |
string |
'frequency' |
'frequency' divides by W − 1, 'reliability' by W − sum(w²)/W, 'population' by W, with W = sum(w) |
$missing |
string |
'raise' |
'raise' rejects null and NAN; 'drop' removes them, pairwise for aligned inputs |
Returns float
Errors Exception: code 1001, 1002, or 1003 for an input error.
See also Details
stat_cov()
Covariance of two aligned arrays: type: 'sample' divides by n − 1, and 'population' by n.
nullandNANare missing:missing: 'raise'rejects them, andmissing: 'drop'removes them, pairwise for two arrays.- Infinite input, or arithmetic that overflows, throws.
Phit\stat_cov( array $x, array $y, string $type = 'sample', string $missing = 'raise',): float| Parameter | Type | Default | Description |
|---|---|---|---|
$x |
array |
required | Data; null and NAN are missing |
$y |
array |
required | Second variable, aligned with the first; null and NAN are missing |
$type |
string |
'sample' |
'sample' divides by n − 1, 'population' by n |
$missing |
string |
'raise' |
'raise' rejects null and NAN; 'drop' removes them, pairwise for aligned inputs |
Returns float
Errors Exception: code 1001, 1002, or 1003 for an input error.
See also Details
stat_cor()
Pearson correlation of two aligned arrays.
- Returns
NANwhen either input is constant. nullandNANare missing:missing: 'raise'rejects them, andmissing: 'drop'removes them, pairwise for two arrays.- Infinite input, or arithmetic that overflows, throws.
Phit\stat_cor( array $x, array $y, string $missing = 'raise',): float| Parameter | Type | Default | Description |
|---|---|---|---|
$x |
array |
required | Data; null and NAN are missing |
$y |
array |
required | Second variable, aligned with the first; null and NAN are missing |
$missing |
string |
'raise' |
'raise' rejects null and NAN; 'drop' removes them, pairwise for aligned inputs |
Returns float
Errors Exception: code 1001, 1002, or 1003 for an input error.
See also Details
Smoothing and interpolation
stat_density()
Kernel density estimate, like R’s density().
- Bins the data linearly, convolves with the kernel by FFT, and interpolates onto
npoints fromfromtoto. bwisnrd0,nrd, or a positive number, multiplied byadjust.- Weights are used as given; as in R, weights that sum to one are renormalized after missing values are removed.
nullandNANare missing:missing: 'raise'rejects them, andmissing: 'drop'removes them, pairwise for two arrays.- Infinite input, or arithmetic that overflows, throws.
Phit\stat_density( array $x, string|float $bw = 'nrd0', float $adjust = 1.0, string $kernel = 'gaussian', int $n = 512, ?float $from = NULL, ?float $to = NULL, float $cut = 3.0, ?array $weights = NULL, string $missing = 'raise',): array| Parameter | Type | Default | Description |
|---|---|---|---|
$x |
array |
required | Data; null and NAN are missing |
$bw |
string\|float |
'nrd0' |
'nrd0', 'nrd', or a positive bandwidth |
$adjust |
float |
1.0 |
Multiplier applied to the bandwidth |
$kernel |
string |
'gaussian' |
'gaussian', 'epanechnikov', 'rectangular', 'triangular', 'biweight', 'cosine', or 'optcosine' |
$n |
int |
512 |
Number of output points |
$from |
?float |
NULL |
Left end of the output grid; null extends the data range by cut bandwidths |
$to |
?float |
NULL |
Right end of the output grid; null extends the data range by cut bandwidths |
$cut |
float |
3.0 |
Bandwidths beyond the data range for the default grid |
$weights |
?array |
NULL |
Nonnegative weights aligned with x, used as given; null weights all points equally |
$missing |
string |
'raise' |
'raise' rejects null and NAN; 'drop' removes them, pairwise for aligned inputs |
Returns array{x: list<float>, y: list<float>, bw: float, n: int}
Errors Exception: code 1001, 1002, or 1003 for an input error.
See also Details
stat_bw_nrd0()
Silverman’s rule-of-thumb bandwidth, like R’s bw.nrd0(): 0.9 · min(sd, IQR/1.34) · n^(−1/5), with R’s fallbacks when that is zero.
nullandNANare missing:missing: 'raise'rejects them, andmissing: 'drop'removes them, pairwise for two arrays.- Infinite input, or arithmetic that overflows, throws.
Phit\stat_bw_nrd0( array $x, string $missing = 'raise',): float| Parameter | Type | Default | Description |
|---|---|---|---|
$x |
array |
required | Data; null and NAN are missing |
$missing |
string |
'raise' |
'raise' rejects null and NAN; 'drop' removes them, pairwise for aligned inputs |
Returns float
Errors Exception: code 1001, 1002, or 1003 for an input error.
See also Details
stat_bw_nrd()
Scott’s variation of the rule-of-thumb bandwidth, like R’s bw.nrd(): 1.06 · min(sd, IQR/1.34) · n^(−1/5).
nullandNANare missing:missing: 'raise'rejects them, andmissing: 'drop'removes them, pairwise for two arrays.- Infinite input, or arithmetic that overflows, throws.
Phit\stat_bw_nrd( array $x, string $missing = 'raise',): float| Parameter | Type | Default | Description |
|---|---|---|---|
$x |
array |
required | Data; null and NAN are missing |
$missing |
string |
'raise' |
'raise' rejects null and NAN; 'drop' removes them, pairwise for aligned inputs |
Returns float
Errors Exception: code 1001, 1002, or 1003 for an input error.
See also Details
stat_approx()
Interpolates the points (x, y) at xout, like R’s approx().
method: 'linear'interpolates linearly;'constant'uses a step function in whichfweights the right-hand value.- Outside [min(x), max(x)],
rule: 1returnsNANandrule: 2the value at the nearer end. - Tied x values collapse by
ties:'mean','min', or'max'. - A
nullorNANinxoutreturnsNAN. nullandNANare missing:missing: 'raise'rejects them, andmissing: 'drop'removes them, pairwise for two arrays.- Infinite input, or arithmetic that overflows, throws.
Phit\stat_approx( array $x, array $y, array $xout, string $method = 'linear', int $rule = 1, string $ties = 'mean', float $f = 0.0, string $missing = 'raise',): array| Parameter | Type | Default | Description |
|---|---|---|---|
$x |
array |
required | Abscissae of the points; null and NAN are missing |
$y |
array |
required | Ordinates, aligned with x; null and NAN are missing |
$xout |
array |
required | Evaluation points; null or NAN gives NAN |
$method |
string |
'linear' |
'linear' or 'constant' |
$rule |
int |
1 |
1 returns NAN outside the data range; 2 returns the value at the nearer end |
$ties |
string |
'mean' |
'mean', 'min', or 'max' for tied x values |
$f |
float |
0.0 |
For method 'constant', the weight of the right-hand value, in [0, 1] |
$missing |
string |
'raise' |
'raise' rejects null and NAN; 'drop' removes them, pairwise for aligned inputs |
Returns list<float>
Errors Exception: code 1001, 1002, or 1003 for an input error.
See also Details
stat_ecdf()
Empirical CDF of x at each q: the proportion of x at or below q, as R’s ecdf(x)(q).
- A
nullorNANinqreturnsNAN. nullandNANare missing:missing: 'raise'rejects them, andmissing: 'drop'removes them, pairwise for two arrays.- Infinite input, or arithmetic that overflows, throws.
Phit\stat_ecdf( array $x, array $q, string $missing = 'raise',): array| Parameter | Type | Default | Description |
|---|---|---|---|
$x |
array |
required | Data; null and NAN are missing |
$q |
array |
required | Evaluation points; null or NAN gives NAN |
$missing |
string |
'raise' |
'raise' rejects null and NAN; 'drop' removes them, pairwise for aligned inputs |
Returns list<float>
Errors Exception: code 1001, 1002, or 1003 for an input error.
See also Details
Optimization and root finding
optimize()
Minimizes f on [lower, upper], or maximizes it with maximum: true, like R’s optimize().
- Uses Brent’s golden-section and parabolic search, step for step as R does.
- Returns
minimum(ormaximum) andobjective, the value offthere. ftakes one float and returns an int or float. An exception thrown byfpropagates.- A nonfinite value during the search is replaced by the largest finite value, as in R, with one warning.
Phit\optimize( callable $f, float $lower, float $upper, ?float $tol = NULL, bool $maximum = false,): array| Parameter | Type | Default | Description |
|---|---|---|---|
$f |
callable |
required | Objective: takes one float and returns an int or float |
$lower |
float |
required | Lower end of the search interval |
$upper |
float |
required | Upper end of the search interval |
$tol |
?float |
NULL |
Default PHP_FLOAT_EPSILON ** 0.25, as R |
$maximum |
bool |
false |
Maximize instead of minimize |
Returns array{minimum: float, objective: float}\|array{maximum: float, objective: float}
Errors Exception: code 1001 for an input error.
See also Details
uniroot()
Finds a root of f in [lower, upper], like R’s uniroot().
- Uses Brent’s method, step for step as R does.
f(lower)andf(upper)must not have the same sign. - Returns
root,f_root,iter, andestim_prec. When it runs out of iterations it warns, setsitertomaxiter, and returns its last estimate. ftakes one float and returns an int or float. An exception thrown byfpropagates.- A nonfinite value during the search is replaced by the largest finite value, as in R, with one warning.
Phit\uniroot( callable $f, float $lower, float $upper, ?float $tol = NULL, int $maxiter = 1000,): array| Parameter | Type | Default | Description |
|---|---|---|---|
$f |
callable |
required | Function whose root is sought: takes one float and returns an int or float |
$lower |
float |
required | Lower end of the search interval |
$upper |
float |
required | Upper end of the search interval |
$tol |
?float |
NULL |
Default PHP_FLOAT_EPSILON ** 0.25, as R |
$maxiter |
int |
1000 |
Largest number of iterations |
Returns array{root: float, f_root: float, iter: int, estim_prec: float}
Errors Exception: code 1001 for an input error.
See also Details