(* For computing average and variance without using too much memory... See http://www.cs.umd.edu/~austinjp for a proof of the algorithm's correctness. I guarantee nothing about the quality, effectiveness, compilability, etc of this code. @author Austin Parker *) (* average, variance, and number of samples *) type sample_stats = float*float*int let null_stats = (0.0,0.0,0) let updateSampleStats (ss : sample_stats) nextVal = let (avg,var,num) = ss in let newNum = num+1 in let newAvg = (nextVal +. (avg*.(float_of_int num))) /. (float_of_int newNum) in let inter1 = (nextVal-.avg) /. (float_of_int newNum) in let inter2 = nextVal-.newAvg in let newVar = ((float_of_int num)*.var+.(float_of_int num)*.inter1*.inter1+.inter2*.inter2)/. (float_of_int newNum) in (newAvg,newVar,newNum) let updateSampleStatsWithStats (ss1 : sample_stats) (ss2: sample_stats) = let (a1,v1,n1) = ss1 in let (a2,v2,n2) = ss2 in let n3 = n1+n2 in let a3 = ((a1*.(float_of_int n1))+.(a2*.(float_of_int n2))) /. (float_of_int (n1+n2)) in let v3 = 0.0 (*unimplemented...*) in (a3,v3,n3) let sample_stats_to_string (ss : sample_stats) = let (avg,var,num) = ss in "Average: "^(string_of_float avg)^" Variance: "^(string_of_float var)^" Samples: "^(string_of_int num)