Skip to content

Instantly share code, notes, and snippets.

@jmc734
Last active December 19, 2015 10:19
Show Gist options
  • Save jmc734/5939417 to your computer and use it in GitHub Desktop.
Save jmc734/5939417 to your computer and use it in GitHub Desktop.
Basic Moving Aggregation Methods
<?php
class Aggregate {
public static function avg($value, $index, &$avg) {
$avg = ($value + $index * $avg) / ($index + 1);
}
public static function sum($value, $index, &$sum) {
$sum += $value;
}
public static function max($value, $index, &$max) {
if($value > $max) $max = $value;
}
public static function min($value, $index, &$min) {
if($value < $min) $min = $value;
}
public static function stddev($value, $index, &$stddev) {
$stddev = sqrt((pow($stddev, 2) * $index + pow($value, 2)) / ($index + 1));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment