Version 5.0.13

This commit is contained in:
Neo committed 2025-12-19 19:04:57 -08:00
1 parent b89858b2ff
commit 5efe744c5f
1407 files changed
+69690 -30177

No files matched your search

+49
View File
@@ -540,6 +540,55 @@ class Number implements JsonSerializable
return $result;
}
/**
* Get the moving average series from a series of numbers
*
* @param number[] $numbers
* @param int $degree How many elements of the source $numbers to average over
* @param float $weightIncrement How much to scale the weight of each value in the moving average as it approaches the current date. The weight starts at 1 and will be multiplied by this number
* @return (number|null)[] If the input array contains non-numeric values, these will be omitted from the moving average. If there are no numbers in the average period, null is added to the output array instead of a number
*/
public static function movingAverage( array $numbers, int $degree=5, float $weightIncrement=1 ) : array
{
if ( count( $numbers ) <= 1 OR $degree <= 1 )
{
return [...$numbers];
}
$final = [];
$values = array_values( $numbers );
$keys = array_keys( $numbers );
for ( $i = 1; $i < count( $numbers ); $i++ )
{
$start = max(0, $i - $degree);
$slice = array_slice( $values, $start, $i + 1 - $start );
$sum = 0;
$count = 0;
$weight = 1;
foreach ( $slice as $number )
{
if ( !is_numeric( $number ) )
{
continue;
}
$number = ( is_int( $number ) or is_float( $number ) ) ? $number : (float) $number;
$sum += $number * $weight;
$count += $weight;
$weight *= $weightIncrement;
}
if ( $count === 0 )
{
$final[$keys[$i]] = null;
continue;
}
$final[$keys[$i]] = $sum / $count;
}
return $final;
}
/**
* Get product of numbers
*