Example usage for org.apache.commons.math.stat.descriptive.summary SumOfSquares SumOfSquares

List of usage examples for org.apache.commons.math.stat.descriptive.summary SumOfSquares SumOfSquares

Introduction

In this page you can find the example usage for org.apache.commons.math.stat.descriptive.summary SumOfSquares SumOfSquares.

Prototype

public SumOfSquares() 

Source Link

Document

Create a SumOfSquares instance

Usage

From source file:net.sf.jdmf.util.MathCalculator.java

/**
 * Calculates the distance between two points in a nD space (assumes that
 * n = firstPoint.size() = secondPoint.size()). 
 * /*from   w w w.  java 2 s .  co  m*/
 * @param firstPoint the first point
 * @param secondPoint the second point
 * @return the distance between both points
 */
public Double calculateDistance(Vector<Double> firstPoint, Vector<Double> secondPoint) {
    SumOfSquares sumOfSquares = new SumOfSquares();

    for (int i = 0; i < firstPoint.size(); ++i) {
        sumOfSquares.increment(secondPoint.get(i) - firstPoint.get(i));
    }

    return Math.sqrt(sumOfSquares.getResult());
}

From source file:org.NooLab.math3.stat.inference.OneWayAnova.java

/**
 * This method actually does the calculations (except P-value).
 *
 * @param categoryData <code>Collection</code> of <code>double[]</code>
 * arrays each containing data for one category
 * @return computed AnovaStats//from w  w w.ja v a2s  .c  o  m
 * @throws NullArgumentException if <code>categoryData</code> is <code>null</code>
 * @throws DimensionMismatchException if the length of the <code>categoryData</code>
 * array is less than 2 or a contained <code>double[]</code> array does not contain
 * at least two values
 */
private AnovaStats anovaStats(final Collection<double[]> categoryData)
        throws NullArgumentException, DimensionMismatchException {

    if (categoryData == null) {
        throw new NullArgumentException();
    }

    // check if we have enough categories
    if (categoryData.size() < 2) {
        throw new DimensionMismatchException(LocalizedFormats.TWO_OR_MORE_CATEGORIES_REQUIRED,
                categoryData.size(), 2);
    }

    // check if each category has enough data and all is double[]
    for (double[] array : categoryData) {
        if (array.length <= 1) {
            throw new DimensionMismatchException(LocalizedFormats.TWO_OR_MORE_VALUES_IN_CATEGORY_REQUIRED,
                    array.length, 2);
        }
    }

    int dfwg = 0;
    double sswg = 0;
    Sum totsum = new Sum();
    SumOfSquares totsumsq = new SumOfSquares();
    int totnum = 0;

    for (double[] data : categoryData) {

        Sum sum = new Sum();
        SumOfSquares sumsq = new SumOfSquares();
        int num = 0;

        for (int i = 0; i < data.length; i++) {
            double val = data[i];

            // within category
            num++;
            sum.increment(val);
            sumsq.increment(val);

            // for all categories
            totnum++;
            totsum.increment(val);
            totsumsq.increment(val);
        }
        dfwg += num - 1;
        double ss = sumsq.getResult() - sum.getResult() * sum.getResult() / num;
        sswg += ss;
    }
    double sst = totsumsq.getResult() - totsum.getResult() * totsum.getResult() / totnum;
    double ssbg = sst - sswg;
    int dfbg = categoryData.size() - 1;
    double msbg = ssbg / dfbg;
    double mswg = sswg / dfwg;
    double F = msbg / mswg;

    return new AnovaStats(dfbg, dfwg, F);
}