# constSD.pl # ---------- # # Code by Austin Parker # He is a grad student, not a coder. Expect nothing # from this code -- it is just here because I like # the pretty colors source code makes in a syntax # highlighted editor (that was a ``no warenty'' thing). # # If you use this, you should thank me -- particularlly # if you're writing a paper of some kind. # # based on proof located at # http://www.cs.umd.edu/~austinjp/constSD.pdf # takes as input current mean, current variance, # current number of samples and the next sample # and returns the new mean, variance, and number # of samples. # # usage: updateSD( mean, var, num, new_sample ) # returns: ( new_mean, new_var, num+1 ) # sub updateSD { my $mean = shift; my $var = shift; my $n = shift; my $new = shift; my $new_n = $n+1; my $new_mean = ($n*$mean + $new) / $new_n; my $new_var = ($n*$var + $n*(($new-$mean)/$new_n)**2 + ($new - $new_mean)**2)/$new_n; return ($new_mean, $new_var, $new_n); } 1;