alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Commons Math example source code file (StatUtils.java)

This example Commons Math source code file (StatUtils.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Commons Math tags/keywords

geometricmean, illegalargumentexception, illegalargumentexception, max, mean, percentile, percentile, product, statutils, sum, univariatestatistic, univariatestatistic, variance, variance

The Commons Math StatUtils.java source code

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.commons.math.stat;

import org.apache.commons.math.MathRuntimeException;
import org.apache.commons.math.stat.descriptive.UnivariateStatistic;
import org.apache.commons.math.stat.descriptive.moment.GeometricMean;
import org.apache.commons.math.stat.descriptive.moment.Mean;
import org.apache.commons.math.stat.descriptive.moment.Variance;
import org.apache.commons.math.stat.descriptive.rank.Max;
import org.apache.commons.math.stat.descriptive.rank.Min;
import org.apache.commons.math.stat.descriptive.rank.Percentile;
import org.apache.commons.math.stat.descriptive.summary.Product;
import org.apache.commons.math.stat.descriptive.summary.Sum;
import org.apache.commons.math.stat.descriptive.summary.SumOfLogs;
import org.apache.commons.math.stat.descriptive.summary.SumOfSquares;

/**
 * StatUtils provides static methods for computing statistics based on data
 * stored in double[] arrays.
 *
 * @version $Revision: 811685 $ $Date: 2009-09-05 13:36:48 -0400 (Sat, 05 Sep 2009) $
 */
public final class StatUtils {

    /** sum */
    private static final UnivariateStatistic SUM = new Sum();

    /** sumSq */
    private static final UnivariateStatistic SUM_OF_SQUARES = new SumOfSquares();

    /** prod */
    private static final UnivariateStatistic PRODUCT = new Product();

    /** sumLog */
    private static final UnivariateStatistic SUM_OF_LOGS = new SumOfLogs();

    /** min */
    private static final UnivariateStatistic MIN = new Min();

    /** max */
    private static final UnivariateStatistic MAX = new Max();

    /** mean */
    private static final UnivariateStatistic MEAN = new Mean();

    /** variance */
    private static final Variance VARIANCE = new Variance();

    /** percentile */
    private static final Percentile PERCENTILE = new Percentile();

    /** geometric mean */
    private static final GeometricMean GEOMETRIC_MEAN = new GeometricMean();

    /**
     * Private Constructor
     */
    private StatUtils() {
    }

    /**
     * Returns the sum of the values in the input array, or
     * <code>Double.NaN if the array is empty.
     * <p>
     * Throws <code>IllegalArgumentException if the input array
     * is null.</p>
     *
     * @param values  array of values to sum
     * @return the sum of the values or <code>Double.NaN if the array
     * is empty
     * @throws IllegalArgumentException if the array is null
     */
    public static double sum(final double[] values) {
        return SUM.evaluate(values);
    }

    /**
     * Returns the sum of the entries in the specified portion of
     * the input array, or <code>Double.NaN if the designated subarray
     * is empty.
     * <p>
     * Throws <code>IllegalArgumentException if the array is null.

* * @param values the input array * @param begin index of the first array element to include * @param length the number of elements to include * @return the sum of the values or Double.NaN if length = 0 * @throws IllegalArgumentException if the array is null or the array index * parameters are not valid */ public static double sum(final double[] values, final int begin, final int length) { return SUM.evaluate(values, begin, length); } /** * Returns the sum of the squares of the entries in the input array, or * <code>Double.NaN if the array is empty. * <p> * Throws <code>IllegalArgumentException if the array is null.

* * @param values input array * @return the sum of the squared values or <code>Double.NaN if the * array is empty * @throws IllegalArgumentException if the array is null */ public static double sumSq(final double[] values) { return SUM_OF_SQUARES.evaluate(values); } /** * Returns the sum of the squares of the entries in the specified portion of * the input array, or <code>Double.NaN if the designated subarray * is empty. * <p> * Throws <code>IllegalArgumentException if the array is null.

* * @param values the input array * @param begin index of the first array element to include * @param length the number of elements to include * @return the sum of the squares of the values or Double.NaN if length = 0 * @throws IllegalArgumentException if the array is null or the array index * parameters are not valid */ public static double sumSq(final double[] values, final int begin, final int length) { return SUM_OF_SQUARES.evaluate(values, begin, length); } /** * Returns the product of the entries in the input array, or * <code>Double.NaN if the array is empty. * <p> * Throws <code>IllegalArgumentException if the array is null.

* * @param values the input array * @return the product of the values or Double.NaN if the array is empty * @throws IllegalArgumentException if the array is null */ public static double product(final double[] values) { return PRODUCT.evaluate(values); } /** * Returns the product of the entries in the specified portion of * the input array, or <code>Double.NaN if the designated subarray * is empty. * <p> * Throws <code>IllegalArgumentException if the array is null.

* * @param values the input array * @param begin index of the first array element to include * @param length the number of elements to include * @return the product of the values or Double.NaN if length = 0 * @throws IllegalArgumentException if the array is null or the array index * parameters are not valid */ public static double product(final double[] values, final int begin, final int length) { return PRODUCT.evaluate(values, begin, length); } /** * Returns the sum of the natural logs of the entries in the input array, or * <code>Double.NaN if the array is empty. * <p> * Throws <code>IllegalArgumentException if the array is null.

* <p> * See {@link org.apache.commons.math.stat.descriptive.summary.SumOfLogs}. * </p> * * @param values the input array * @return the sum of the natural logs of the values or Double.NaN if * the array is empty * @throws IllegalArgumentException if the array is null */ public static double sumLog(final double[] values) { return SUM_OF_LOGS.evaluate(values); } /** * Returns the sum of the natural logs of the entries in the specified portion of * the input array, or <code>Double.NaN if the designated subarray * is empty. * <p> * Throws <code>IllegalArgumentException if the array is null.

* <p> * See {@link org.apache.commons.math.stat.descriptive.summary.SumOfLogs}. * </p> * * @param values the input array * @param begin index of the first array element to include * @param length the number of elements to include * @return the sum of the natural logs of the values or Double.NaN if * length = 0 * @throws IllegalArgumentException if the array is null or the array index * parameters are not valid */ public static double sumLog(final double[] values, final int begin, final int length) { return SUM_OF_LOGS.evaluate(values, begin, length); } /** * Returns the arithmetic mean of the entries in the input array, or * <code>Double.NaN if the array is empty. * <p> * Throws <code>IllegalArgumentException if the array is null.

* <p> * See {@link org.apache.commons.math.stat.descriptive.moment.Mean} for * details on the computing algorithm.</p> * * @param values the input array * @return the mean of the values or Double.NaN if the array is empty * @throws IllegalArgumentException if the array is null */ public static double mean(final double[] values) { return MEAN.evaluate(values); } /** * Returns the arithmetic mean of the entries in the specified portion of * the input array, or <code>Double.NaN if the designated subarray * is empty. * <p> * Throws <code>IllegalArgumentException if the array is null.

* <p> * See {@link org.apache.commons.math.stat.descriptive.moment.Mean} for * details on the computing algorithm.</p> * * @param values the input array * @param begin index of the first array element to include * @param length the number of elements to include * @return the mean of the values or Double.NaN if length = 0 * @throws IllegalArgumentException if the array is null or the array index * parameters are not valid */ public static double mean(final double[] values, final int begin, final int length) { return MEAN.evaluate(values, begin, length); } /** * Returns the geometric mean of the entries in the input array, or * <code>Double.NaN if the array is empty. * <p> * Throws <code>IllegalArgumentException if the array is null.

* <p> * See {@link org.apache.commons.math.stat.descriptive.moment.GeometricMean} * for details on the computing algorithm.</p> * * @param values the input array * @return the geometric mean of the values or Double.NaN if the array is empty * @throws IllegalArgumentException if the array is null */ public static double geometricMean(final double[] values) { return GEOMETRIC_MEAN.evaluate(values); } /** * Returns the geometric mean of the entries in the specified portion of * the input array, or <code>Double.NaN if the designated subarray * is empty. * <p> * Throws <code>IllegalArgumentException if the array is null.

* <p> * See {@link org.apache.commons.math.stat.descriptive.moment.GeometricMean} * for details on the computing algorithm.</p> * * @param values the input array * @param begin index of the first array element to include * @param length the number of elements to include * @return the geometric mean of the values or Double.NaN if length = 0 * @throws IllegalArgumentException if the array is null or the array index * parameters are not valid */ public static double geometricMean(final double[] values, final int begin, final int length) { return GEOMETRIC_MEAN.evaluate(values, begin, length); } /** * Returns the variance of the entries in the input array, or * <code>Double.NaN if the array is empty. * <p> * See {@link org.apache.commons.math.stat.descriptive.moment.Variance} for * details on the computing algorithm.</p> * <p> * Returns 0 for a single-value (i.e. length = 1) sample.</p> * <p> * Throws <code>IllegalArgumentException if the array is null.

* * @param values the input array * @return the variance of the values or Double.NaN if the array is empty * @throws IllegalArgumentException if the array is null */ public static double variance(final double[] values) { return VARIANCE.evaluate(values); } /** * Returns the variance of the entries in the specified portion of * the input array, or <code>Double.NaN if the designated subarray * is empty. * <p> * See {@link org.apache.commons.math.stat.descriptive.moment.Variance} for * details on the computing algorithm.</p> * <p> * Returns 0 for a single-value (i.e. length = 1) sample.</p> * <p> * Throws <code>IllegalArgumentException if the array is null or the * array index parameters are not valid.</p> * * @param values the input array * @param begin index of the first array element to include * @param length the number of elements to include * @return the variance of the values or Double.NaN if length = 0 * @throws IllegalArgumentException if the array is null or the array index * parameters are not valid */ public static double variance(final double[] values, final int begin, final int length) { return VARIANCE.evaluate(values, begin, length); } /** * Returns the variance of the entries in the specified portion of * the input array, using the precomputed mean value. Returns * <code>Double.NaN if the designated subarray is empty. * <p> * See {@link org.apache.commons.math.stat.descriptive.moment.Variance} for * details on the computing algorithm.</p> * <p> * The formula used assumes that the supplied mean value is the arithmetic * mean of the sample data, not a known population parameter. This method * is supplied only to save computation when the mean has already been * computed.</p> * <p> * Returns 0 for a single-value (i.e. length = 1) sample.</p> * <p> * Throws <code>IllegalArgumentException if the array is null or the * array index parameters are not valid.</p> * * @param values the input array * @param mean the precomputed mean value * @param begin index of the first array element to include * @param length the number of elements to include * @return the variance of the values or Double.NaN if length = 0 * @throws IllegalArgumentException if the array is null or the array index * parameters are not valid */ public static double variance(final double[] values, final double mean, final int begin, final int length) { return VARIANCE.evaluate(values, mean, begin, length); } /** * Returns the variance of the entries in the input array, using the * precomputed mean value. Returns <code>Double.NaN if the array * is empty. * <p> * See {@link org.apache.commons.math.stat.descriptive.moment.Variance} for * details on the computing algorithm.</p> * <p> * The formula used assumes that the supplied mean value is the arithmetic * mean of the sample data, not a known population parameter. This method * is supplied only to save computation when the mean has already been * computed.</p> * <p> * Returns 0 for a single-value (i.e. length = 1) sample.</p> * <p> * Throws <code>IllegalArgumentException if the array is null.

* * @param values the input array * @param mean the precomputed mean value * @return the variance of the values or Double.NaN if the array is empty * @throws IllegalArgumentException if the array is null */ public static double variance(final double[] values, final double mean) { return VARIANCE.evaluate(values, mean); } /** * Returns the maximum of the entries in the input array, or * <code>Double.NaN if the array is empty. * <p> * Throws <code>IllegalArgumentException if the array is null.

* <p> * <ul> * <li>The result is NaN iff all values are NaN * (i.e. <code>NaN values have no impact on the value of the statistic). * <li>If any of the values equals Double.POSITIVE_INFINITY, * the result is <code>Double.POSITIVE_INFINITY. * </ul>

* * @param values the input array * @return the maximum of the values or Double.NaN if the array is empty * @throws IllegalArgumentException if the array is null */ public static double max(final double[] values) { return MAX.evaluate(values); } /** * Returns the maximum of the entries in the specified portion of * the input array, or <code>Double.NaN if the designated subarray * is empty. * <p> * Throws <code>IllegalArgumentException if the array is null or * the array index parameters are not valid.</p> * <p> * <ul> * <li>The result is NaN iff all values are NaN * (i.e. <code>NaN values have no impact on the value of the statistic). * <li>If any of the values equals Double.POSITIVE_INFINITY, * the result is <code>Double.POSITIVE_INFINITY. * </ul>

* * @param values the input array * @param begin index of the first array element to include * @param length the number of elements to include * @return the maximum of the values or Double.NaN if length = 0 * @throws IllegalArgumentException if the array is null or the array index * parameters are not valid */ public static double max(final double[] values, final int begin, final int length) { return MAX.evaluate(values, begin, length); } /** * Returns the minimum of the entries in the input array, or * <code>Double.NaN if the array is empty. * <p> * Throws <code>IllegalArgumentException if the array is null.

* <p> * <ul> * <li>The result is NaN iff all values are NaN * (i.e. <code>NaN values have no impact on the value of the statistic). * <li>If any of the values equals Double.NEGATIVE_INFINITY, * the result is <code>Double.NEGATIVE_INFINITY. * </ul>

* * @param values the input array * @return the minimum of the values or Double.NaN if the array is empty * @throws IllegalArgumentException if the array is null */ public static double min(final double[] values) { return MIN.evaluate(values); } /** * Returns the minimum of the entries in the specified portion of * the input array, or <code>Double.NaN if the designated subarray * is empty. * <p> * Throws <code>IllegalArgumentException if the array is null or * the array index parameters are not valid.</p> * <p> * <ul> * <li>The result is NaN iff all values are NaN * (i.e. <code>NaN values have no impact on the value of the statistic). * <li>If any of the values equals Double.NEGATIVE_INFINITY, * the result is <code>Double.NEGATIVE_INFINITY. * </ul>

* * @param values the input array * @param begin index of the first array element to include * @param length the number of elements to include * @return the minimum of the values or Double.NaN if length = 0 * @throws IllegalArgumentException if the array is null or the array index * parameters are not valid */ public static double min(final double[] values, final int begin, final int length) { return MIN.evaluate(values, begin, length); } /** * Returns an estimate of the <code>pth percentile of the values * in the <code>values array. * <p> * <ul> * <li>Returns Double.NaN if values has length * <code>0

* <li>Returns (for any value of p) values[0] * if <code>values has length 1 * <li>Throws IllegalArgumentException if values * is null or p is not a valid quantile value (p must be greater than 0 * and less than or equal to 100)</li> * </ul>

* <p> * See {@link org.apache.commons.math.stat.descriptive.rank.Percentile} for * a description of the percentile estimation algorithm used.</p> * * @param values input array of values * @param p the percentile value to compute * @return the percentile value or Double.NaN if the array is empty * @throws IllegalArgumentException if <code>values is null * or p is invalid */ public static double percentile(final double[] values, final double p) { return PERCENTILE.evaluate(values,p); } /** * Returns an estimate of the <code>pth percentile of the values * in the <code>values array, starting with the element in (0-based) * position <code>begin in the array and including length * values. * <p> * <ul> * <li>Returns Double.NaN if length = 0 * <li>Returns (for any value of p) values[begin] * if <code>length = 1 * <li>Throws IllegalArgumentException if values * is null , <code>begin or length is invalid, or * <code>p is not a valid quantile value (p must be greater than 0 * and less than or equal to 100)</li> * </ul>

* <p> * See {@link org.apache.commons.math.stat.descriptive.rank.Percentile} for * a description of the percentile estimation algorithm used.</p> * * @param values array of input values * @param p the percentile to compute * @param begin the first (0-based) element to include in the computation * @param length the number of array elements to include * @return the percentile value * @throws IllegalArgumentException if the parameters are not valid or the * input array is null */ public static double percentile(final double[] values, final int begin, final int length, final double p) { return PERCENTILE.evaluate(values, begin, length, p); } /** * Returns the sum of the (signed) differences between corresponding elements of the * input arrays -- i.e., sum(sample1[i] - sample2[i]). * * @param sample1 the first array * @param sample2 the second array * @return sum of paired differences * @throws IllegalArgumentException if the arrays do not have the same * (positive) length */ public static double sumDifference(final double[] sample1, final double[] sample2) throws IllegalArgumentException { int n = sample1.length; if ((n != sample2.length) || (n < 1)) { throw MathRuntimeException.createIllegalArgumentException( "input arrays must have the same positive length ({0} and {1})", n, sample2.length); } double result = 0; for (int i = 0; i < n; i++) { result += sample1[i] - sample2[i]; } return result; } /** * Returns the mean of the (signed) differences between corresponding elements of the * input arrays -- i.e., sum(sample1[i] - sample2[i]) / sample1.length. * * @param sample1 the first array * @param sample2 the second array * @return mean of paired differences * @throws IllegalArgumentException if the arrays do not have the same * (positive) length */ public static double meanDifference(final double[] sample1, final double[] sample2) throws IllegalArgumentException { return sumDifference(sample1, sample2) / sample1.length; } /** * Returns the variance of the (signed) differences between corresponding elements of the * input arrays -- i.e., var(sample1[i] - sample2[i]). * * @param sample1 the first array * @param sample2 the second array * @param meanDifference the mean difference between corresponding entries * @see #meanDifference(double[],double[]) * @return variance of paired differences * @throws IllegalArgumentException if the arrays do not have the same * length or their common length is less than 2. */ public static double varianceDifference(final double[] sample1, final double[] sample2, double meanDifference) throws IllegalArgumentException { double sum1 = 0d; double sum2 = 0d; double diff = 0d; int n = sample1.length; if (n < 2 || n != sample2.length) { throw MathRuntimeException.createIllegalArgumentException( "input arrays must have the same length and at least two elements ({0} and {1})", n, sample2.length); } for (int i = 0; i < n; i++) { diff = sample1[i] - sample2[i]; sum1 += (diff - meanDifference) *(diff - meanDifference); sum2 += diff - meanDifference; } return (sum1 - (sum2 * sum2 / n)) / (n - 1); } }

Other Commons Math examples (source code examples)

Here is a short list of links related to this Commons Math StatUtils.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.