FileDocCategorySizeDatePackage
StatsLib.javaAPI DocApache Poi 3.0.14988Sun Mar 11 12:59:30 GMT 2007org.apache.poi.hssf.record.formula.functions

StatsLib

public final class StatsLib extends Object
author
Amol S. Deshmukh < amolweb at ya hoo dot com > Library for common statistics functions

Fields Summary
Constructors Summary
private StatsLib()

Methods Summary
public static doubleavedev(double[] v)
returns the mean of deviations from mean.

param
v
return

        double r = 0;
        double m = 0;
        double s = 0;
        for (int i=0, iSize=v.length; i<iSize; i++) {
            s += v[i];
        }
        m = s / v.length;
        s = 0;
        for (int i=0, iSize=v.length; i<iSize; i++) {
            s += Math.abs(v[i]-m);
        }
        r = s / v.length;
        return r;
    
public static doubledevsq(double[] v)

        double r = Double.NaN;
        if (v!=null && v.length >= 1) {
            double m = 0;
            double s = 0;
            int n = v.length;
            for (int i=0; i<n; i++) {
                s += v[i];
            }
            m = s / n;
            s = 0;
            for (int i=0; i<n; i++) {
                s += (v[i]- m) * (v[i] - m);
            }
            
            r = (n == 1)
                    ? 0
                    : s;
        }
        return r;
    
public static doublekthLargest(double[] v, int k)
returns the kth largest element in the array. Duplicates are considered as distinct values. Hence, eg. for array {1,2,4,3,3} & k=2, returned value is 3.
k <= 0 & k >= v.length and null or empty arrays will result in return value Double.NaN

param
v
param
k
return

        double r = Double.NaN;
        k--; // since arrays are 0-based
        if (v!=null && v.length > k && k >= 0) {
            Arrays.sort(v);
            r = v[v.length-k-1];
        }
        return r;
    
public static doublekthSmallest(double[] v, int k)
returns the kth smallest element in the array. Duplicates are considered as distinct values. Hence, eg. for array {1,1,2,4,3,3} & k=2, returned value is 1.
k <= 0 & k >= v.length or null array or empty array will result in return value Double.NaN

param
v
param
k
return

        double r = Double.NaN;
        k--; // since arrays are 0-based
        if (v!=null && v.length > k && k >= 0) {
            Arrays.sort(v);
            r = v[k];
        }
        return r;
    
public static doublemedian(double[] v)

        double r = Double.NaN;
        
        if (v!=null && v.length >= 1) {
            int n = v.length;
            Arrays.sort(v);
            r = (n % 2 == 0)
                ? (v[n / 2] + v[n / 2 - 1]) / 2
                : v[n / 2];
        }
        
        return r;
    
public static doublemode(double[] v)
if v is zero length or contains no duplicates, return value is Double.NaN. Else returns the value that occurs most times and if there is a tie, returns the first such value.

param
v
return

        double r = Double.NaN;
        
        // very naive impl, may need to be optimized
        if (v!=null && v.length > 1) {
            int[] counts = new int[v.length]; 
            Arrays.fill(counts, 1);
            for (int i=0, iSize=v.length; i<iSize; i++) {
                for (int j=i+1, jSize=v.length; j<jSize; j++) {
                    if (v[i] == v[j]) counts[i]++;
                }
            }
            double maxv = 0;
            int maxc = 0;
            for (int i=0, iSize=counts.length; i<iSize; i++) {
                if (counts[i] > maxc) {
                    maxv = v[i];
                    maxc = counts[i];
                }
            }
            r = (maxc > 1) ? maxv : Double.NaN; // "no-dups" check
        }
        return r;
    
public static doublestdev(double[] v)

        double r = Double.NaN;
        if (v!=null && v.length > 1) {
            r = Math.sqrt( devsq(v) / (v.length - 1) );
        }
        return r;