sanmai/pipeline
sanmai/pipeline is a lightweight PHP pipeline library to process data through a chain of stages. Compose reusable, testable transformations with clear input/output flow, and plug in custom middleware-like steps for flexible processing in any app.
These methods are used for performing statistical analysis on pipeline data.
finalVariance()Calculates a comprehensive set of statistics for numeric data in the pipeline.
Signature: finalVariance(?callable $castFunc = null, ?RunningVariance $variance = null): RunningVariance
$castFunc: A function to convert pipeline values to floats. Defaults to floatval. Return null to skip non-numeric values.$variance: An optional, pre-initialized RunningVariance object to continue calculations from.Behavior:
RunningVariance object.RunningVariance object contains methods to get the mean, variance, standard deviation, min, max, and count.$castFunc returns as null are not included in the statistics.Examples:
use Pipeline\Helper\RunningVariance;
// Basic statistics
$stats = take([1, 2, 3, 4, 5])->finalVariance();
echo $stats->getCount(); // 5
echo $stats->getMean(); // 3.0
echo $stats->getVariance(); // 2.5
echo $stats->getStandardDeviation(); // ~1.58
echo $stats->getMin(); // 1.0
echo $stats->getMax(); // 5.0
// Statistics for a specific field
$stats = take($users)->finalVariance(fn($user) => $user['age']);
// Handling mixed data (skip non-numeric values)
$stats = take(['1', 'abc', 2, null, 3.5])
->finalVariance(fn($x) => is_numeric($x) ? (float)$x : null);
echo $stats->getCount(); // 3 (only numeric values counted)
// Continuing from existing statistics
$initialStats = take($firstBatch)->finalVariance();
$combinedStats = take($secondBatch)->finalVariance(null, $initialStats);
runningVariance()Observes values as they pass through the pipeline, calculating statistics without consuming the pipeline.
Signature: runningVariance(?RunningVariance &$variance, ?callable $castFunc = null): self
&$variance: A reference to a RunningVariance object, which will be updated with the statistics. It will be created if null.$castFunc: A function to convert pipeline values to floats.Behavior:
Examples:
$stats = null;
$processedData = take([1, 2, 3, 4, 5])
->runningVariance($stats)
->map(fn($x) => $x * 2)
->toList();
echo $stats->getMean(); // 3.0
RunningVariance Helper ClassThe Pipeline\Helper\RunningVariance class provides a powerful way to work with statistics. It uses Welford's online algorithm to calculate variance and other metrics in a single pass, which is highly efficient.
RunningVariance MethodsgetCount(): int: The number of observations.getMean(): float: The arithmetic mean.getVariance(): float: The sample variance.getStandardDeviation(): float: The sample standard deviation.getMin(): float: The minimum value.getMax(): float: The maximum value.You can merge RunningVariance instances, which is useful for parallel processing or combining historical and current data.
// Merge stats from two different sources
$stats1 = take($source1)->finalVariance();
$stats2 = take($source2)->finalVariance();
$combinedStats = new RunningVariance($stats1, $stats2);
How can I help you explore Laravel packages today?