MathPHP\Statistics\Average::weightedMovingAverage PHP Method

weightedMovingAverage() public static method

Similar to simple n-point moving average, however, each n-point has a weight associated with it, and instead of dividing by n, we divide by the sum of the weights. Each weighted average = ∑(weighted values) / ∑(weights)
public static weightedMovingAverage ( array $numbers, integer $n, array $weights ) : array
$numbers array
$n integer n-point moving average
$weights array Weights for each n points
return array of averages
    public static function weightedMovingAverage(array $numbers, int $n, array $weights) : array
    {
        if (count($weights) !== $n) {
            throw new Exception\BadDataException('Number of weights must equal number of n-points');
        }
        $m = count($numbers);
        $∑w = array_sum($weights);
        $WMA = [];
        for ($i = 0; $i <= $m - $n; $i++) {
            $∑wp = array_sum(Map\Multi::multiply(array_slice($numbers, $i, $n), $weights));
            $WMA[] = $∑wp / $∑w;
        }
        return $WMA;
    }