package com.splicemachine.derby.impl.sql.execute.operations;

import org.apache.derby.agg.Aggregator;
import org.apache.derby.iapi.types.DataValueDescriptor;
import org.apache.derby.iapi.error.StandardException;

public class SpliceUDAVariance<K extends Double>
        implements Aggregator<K,K,SpliceUDAVariance<K>>
{
    int count;
    double sum;
    double variance;

    public SpliceUDAVariance() {

    }

    public void init() {
        count = 0;
        sum = 0;
        variance = 0;
    }

    public void accumulate( K value ) {
        count++;
        double v = value.doubleValue();
        sum += v;
        if (count > 1) {
            double t = count*v - sum;
            variance += (t*t) / ((double)count*(count-1));
        }
    }

    public void merge( SpliceUDAVariance<K> other ) {
        long n = count;
        long m = other.count;

        if (n == 0) {
            variance = other.variance;
            count = other.count;
            sum = other.sum;
        }

        if (m != 0 && n != 0) {
            // Merge the two partials
            double a = sum;
            double b = other.sum;
            count += other.count;
            sum += other.sum;
            double t = (m/(double)n)*a - b;
            variance += other.variance + ((n/(double)m)/((double)n+m)) * t * t;
        }
    }

    public K terminate() {
        Double r = new Double(variance);
        return (K) r;
    }

    public void add (DataValueDescriptor addend) throws StandardException{
        variance = addend.getDouble();
    }

}


@@ -212,16 +212,802 @@
 ion;%0D%0A%0D%0A
+%0D%0A/**%0D%0A * Evaluate the variance using the algorithm described by Chan, Golub, and LeVeque in%0D%0A * %22Algorithms for computing the sample variance: analysis and recommendations%22%0D%0A * The American Statistician, 37 (1983) pp. 242--247.%0D%0A *%0D%0A * variance = variance1 + variance2 + n/(m*(m+n)) * pow(((m/n)*t1 - t2),2)%0D%0A *%0D%0A * where: - variance is sum%5Bx-avg%5E2%5D (this is actually n times the variance)%0D%0A * and is updated at every step. - n is the count of elements in chunk1 - m is%0D%0A * the count of elements in chunk2 - t1 = sum of elements in chunk1, t2 =%0D%0A * sum of elements in chunk2.%0D%0A *%0D%0A * This algorithm was proven to be numerically stable by J.L. Barlow in%0D%0A * %22Error analysis of a pairwise summation algorithm to compute sample variance%22%0D%0A * Numer. Math, 58 (1991) pp. 583--590%0D%0A *%0D%0A */%0D%0A
 public c
@@ -1046,25 +1046,16 @@
  Double%3E
-%0D%0A       
  impleme
@@ -1094,18 +1094,17 @@
 ance%3CK%3E%3E
-%0D%0A
+ 
 %7B%0D%0A    i

Add Documenation