(* univariate-stats.sml * * Some statistical functions on unweighted univariate samples. * * Copyright (c) 2004 by The Fellowship of SML/NJ * * Author: Matthias Blume (blume@tti-c.org) *) structure UnivariateStats :> sig (* We distinguish between two kinds of samples. Only the "heavy" * kind permits calculation of average deviation and median. * It is also considerably more expensive because it keeps an * array of all points while the "light" variety is constant-size. *) type light type heavy type 'a sample (* light or heavy *) type 'a evaluation (* light or heavy *) (* Samples are built incrementally by adding points to an initially * empty sample: *) val lempty : light sample val hempty : unit -> heavy sample val ladd : real * light sample -> light sample (* constant *) val hadd : real * heavy sample -> heavy sample (* amortized constant *) (* Evaluate the sample; this completes all the expensive work except * for things that depend on "heavy" samples: *) val evaluate : 'a sample -> 'a evaluation (* constant *) (* extracting of "cheap" information (constant-time): *) val N : 'a evaluation -> int val n : 'a evaluation -> real (* N as real *) val mean : 'a evaluation -> real val variance : 'a evaluation -> real val standardDeviation : 'a evaluation -> real val skew : 'a evaluation -> real val kurtosis : 'a evaluation -> real (* extracting of "expensive" information: *) val median : heavy evaluation -> real (* randomized linear *) val averageDeviation : heavy evaluation -> real (* linear *) end = struct infix 8 \$ val op \$ = Unsafe.Array.sub infix 3 <- fun (a, i) <- x = Unsafe.Array.update (a, i, x) type light = unit type heavy = real array * int type 'a sample = ('a * int * real * real * real * real) type 'a evaluation = ('a * int * real * real * real * real * real * real) fun insert (x, n, (a, sz)) = let val (a, sz) = if nif i if ini then ds/nr else ad (i+1, ds + abs(a\$i-m)) in ad (0, 0.0) end end
Click to toggle
does not end with </html> tag
does not end with </body> tag
The output has ended thus: k) = let fun ad (i, ds) = if i>ni then ds/nr else ad (i+1, ds + abs(a\$i-m)) in ad (0, 0.0) end end