Return-Path: X-Original-To: apmail-commons-commits-archive@minotaur.apache.org Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 6073B176C2 for ; Mon, 13 Apr 2015 20:16:26 +0000 (UTC) Received: (qmail 91849 invoked by uid 500); 13 Apr 2015 20:16:25 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 91598 invoked by uid 500); 13 Apr 2015 20:16:25 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 91582 invoked by uid 99); 13 Apr 2015 20:16:25 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 13 Apr 2015 20:16:25 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 9FFBEDFFE6; Mon, 13 Apr 2015 20:16:25 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: tn@apache.org To: commits@commons.apache.org Date: Mon, 13 Apr 2015 20:16:26 -0000 Message-Id: <861e59f973054ea7bdc2f8116f042d64@git.apache.org> In-Reply-To: <670246bbee10440aaa77946e6d9daea5@git.apache.org> References: <670246bbee10440aaa77946e6d9daea5@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [2/3] [math] [MATH-1205] Major refactoring of the descriptive statistics package. [MATH-1205] Major refactoring of the descriptive statistics package. Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/845e1d54 Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/845e1d54 Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/845e1d54 Branch: refs/heads/master Commit: 845e1d54231d3ff3fb04bdbf5dc5f6b631d9b01e Parents: e31fde8 Author: Thomas Neidhart Authored: Mon Apr 13 22:11:35 2015 +0200 Committer: Thomas Neidhart Committed: Mon Apr 13 22:11:35 2015 +0200 ---------------------------------------------------------------------- src/changes/changes.xml | 25 ++ .../AbstractStorelessUnivariateStatistic.java | 90 ++--- .../AbstractUnivariateStatistic.java | 182 ++-------- .../StorelessUnivariateStatistic.java | 7 +- .../stat/descriptive/UnivariateStatistic.java | 1 - .../stat/descriptive/moment/FirstMoment.java | 5 +- .../stat/descriptive/moment/FourthMoment.java | 5 +- .../stat/descriptive/moment/GeometricMean.java | 24 +- .../math4/stat/descriptive/moment/Kurtosis.java | 37 +- .../math4/stat/descriptive/moment/Mean.java | 23 +- .../stat/descriptive/moment/SecondMoment.java | 10 +- .../stat/descriptive/moment/SemiVariance.java | 344 +++++++++---------- .../math4/stat/descriptive/moment/Skewness.java | 22 +- .../descriptive/moment/StandardDeviation.java | 22 +- .../stat/descriptive/moment/ThirdMoment.java | 10 +- .../math4/stat/descriptive/moment/Variance.java | 53 ++- .../math4/stat/descriptive/rank/Max.java | 14 +- .../math4/stat/descriptive/rank/Median.java | 3 +- .../math4/stat/descriptive/rank/Min.java | 14 +- .../descriptive/rank/PSquarePercentile.java | 41 +-- .../math4/stat/descriptive/rank/Percentile.java | 48 ++- .../math4/stat/descriptive/summary/Product.java | 24 +- .../math4/stat/descriptive/summary/Sum.java | 26 +- .../stat/descriptive/summary/SumOfLogs.java | 16 +- .../stat/descriptive/summary/SumOfSquares.java | 16 +- .../AbstractUnivariateStatisticTest.java | 103 ------ .../descriptive/DescriptiveStatisticsTest.java | 15 +- .../MixedListUnivariateImplTest.java | 30 +- .../MultivariateSummaryStatisticsTest.java | 9 + ...torelessUnivariateStatisticAbstractTest.java | 23 +- .../UnivariateStatisticAbstractTest.java | 14 +- .../stat/descriptive/moment/KurtosisTest.java | 1 - .../stat/descriptive/moment/SkewnessTest.java | 1 - .../moment/StandardDeviationTest.java | 2 +- .../stat/descriptive/moment/VarianceTest.java | 2 +- .../math4/stat/descriptive/rank/MaxTest.java | 3 +- .../math4/stat/descriptive/rank/MedianTest.java | 18 +- .../math4/stat/descriptive/rank/MinTest.java | 3 +- .../descriptive/rank/PSquarePercentileTest.java | 9 +- .../stat/descriptive/summary/ProductTest.java | 11 +- .../stat/descriptive/summary/SumLogTest.java | 5 +- .../stat/descriptive/summary/SumSqTest.java | 6 +- .../math4/stat/descriptive/summary/SumTest.java | 11 +- 43 files changed, 545 insertions(+), 783 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-math/blob/845e1d54/src/changes/changes.xml ---------------------------------------------------------------------- diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 39fe6fe..8eaca42 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -54,6 +54,31 @@ If the output is not quite correct, check for invisible trailing spaces! + + Removed methods "test(...)" from "AbstractUnivariateStatistic". + The already existing methods "MathArrays#verifyValues(...)" shall + be used instead. + + + The abstract class "AbstractStorelessUnivariateStatistic" does not + extend anymore from "AbstractUnivariateStatistic". + + + Default implementation of + "AbstractStorelessUnivariateStatistic#equals(Object)" + will only return true if both instances have the same type. Previously + different statistics were considered to be equal if their current state + happened to be equal. + + + Default implementations of "AbstractStorelessUnivariateStatistic#evaluate(...)" + do not alter the internal state anymore. Instead a temporary copy of + the statistic is created for evaluation purposes. + + + Methods "evaluate(...)" of class "Variance" changed the internal state + although it was stated differently in the javadoc. + Fixed ignored method parameters in QRDecomposition protected methods. http://git-wip-us.apache.org/repos/asf/commons-math/blob/845e1d54/src/main/java/org/apache/commons/math4/stat/descriptive/AbstractStorelessUnivariateStatistic.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/stat/descriptive/AbstractStorelessUnivariateStatistic.java b/src/main/java/org/apache/commons/math4/stat/descriptive/AbstractStorelessUnivariateStatistic.java index 197f93f..599188f 100644 --- a/src/main/java/org/apache/commons/math4/stat/descriptive/AbstractStorelessUnivariateStatistic.java +++ b/src/main/java/org/apache/commons/math4/stat/descriptive/AbstractStorelessUnivariateStatistic.java @@ -19,37 +19,34 @@ package org.apache.commons.math4.stat.descriptive; import org.apache.commons.math4.exception.MathIllegalArgumentException; import org.apache.commons.math4.exception.NullArgumentException; import org.apache.commons.math4.exception.util.LocalizedFormats; +import org.apache.commons.math4.util.MathArrays; import org.apache.commons.math4.util.MathUtils; import org.apache.commons.math4.util.Precision; /** - * - * Abstract implementation of the {@link StorelessUnivariateStatistic} interface. + * Abstract base class for implementations of the + * {@link StorelessUnivariateStatistic} interface. *

- * Provides default evaluate() and incrementAll(double[]) - * implementations.

+ * Provides default {@code evaluate(double[],...)} and {@code incrementAll(double[])} + * implementations. *

- * Note that these implementations are not synchronized.

- * + * Note that these implementations are not synchronized. */ public abstract class AbstractStorelessUnivariateStatistic - extends AbstractUnivariateStatistic implements StorelessUnivariateStatistic { /** - * This default implementation calls {@link #clear}, then invokes - * {@link #increment} in a loop over the the input array, and then uses - * {@link #getResult} to compute the return value. + * This default implementation creates a copy of this {@link StorelessUnivariateStatistic} + * instance, calls {@link #clear} on it, then calls {@link #incrementAll} with the specified + * portion of the input array, and then uses {@link #getResult} to compute the return value. *

- * Note that this implementation changes the internal state of the - * statistic. Its side effects are the same as invoking {@link #clear} and - * then {@link #incrementAll(double[])}.

+ * Note that this implementation does not change the internal state of the statistic. *

- * Implementations may override this method with a more efficient and - * possibly more accurate implementation that works directly with the - * input array.

+ * Implementations may override this method with a more efficient and possibly more + * accurate implementation that works directly with the input array. *

- * If the array is null, a MathIllegalArgumentException is thrown.

+ * If the array is null, a MathIllegalArgumentException is thrown. + * * @param values input array * @return the value of the statistic applied to the input array * @throws MathIllegalArgumentException if values is null @@ -64,20 +61,18 @@ public abstract class AbstractStorelessUnivariateStatistic } /** - * This default implementation calls {@link #clear}, then invokes - * {@link #increment} in a loop over the specified portion of the input - * array, and then uses {@link #getResult} to compute the return value. + * This default implementation creates a copy of this {@link StorelessUnivariateStatistic} + * instance, calls {@link #clear} on it, then calls {@link #incrementAll} with the specified + * portion of the input array, and then uses {@link #getResult} to compute the return value. *

- * Note that this implementation changes the internal state of the - * statistic. Its side effects are the same as invoking {@link #clear} and - * then {@link #incrementAll(double[], int, int)}.

+ * Note that this implementation does not change the internal state of the statistic. *

- * Implementations may override this method with a more efficient and - * possibly more accurate implementation that works directly with the - * input array.

+ * Implementations may override this method with a more efficient and possibly more + * accurate implementation that works directly with the input array. *

* If the array is null or the index parameters are not valid, an - * MathIllegalArgumentException is thrown.

+ * MathIllegalArgumentException is thrown. + * * @param values the input array * @param begin the index of the first element to include * @param length the number of elements to include @@ -86,13 +81,16 @@ public abstract class AbstractStorelessUnivariateStatistic * @see org.apache.commons.math4.stat.descriptive.UnivariateStatistic#evaluate(double[], int, int) */ @Override - public double evaluate(final double[] values, final int begin, - final int length) throws MathIllegalArgumentException { - if (test(values, begin, length)) { - clear(); - incrementAll(values, begin, length); + public double evaluate(final double[] values, final int begin, final int length) + throws MathIllegalArgumentException { + + if (MathArrays.verifyValues(values, begin, length)) { + final StorelessUnivariateStatistic stat = copy(); + stat.clear(); + stat.incrementAll(values, begin, length); + return stat.getResult(); } - return getResult(); + return Double.NaN; } /** @@ -123,11 +121,11 @@ public abstract class AbstractStorelessUnivariateStatistic * This default implementation just calls {@link #increment} in a loop over * the input array. *

- * Throws IllegalArgumentException if the input values array is null.

+ * Throws IllegalArgumentException if the input values array is null. * * @param values values to add * @throws MathIllegalArgumentException if values is null - * @see org.apache.commons.math4.stat.descriptive.StorelessUnivariateStatistic#incrementAll(double[]) + * @see StorelessUnivariateStatistic#incrementAll(double[]) */ @Override public void incrementAll(double[] values) throws MathIllegalArgumentException { @@ -141,17 +139,17 @@ public abstract class AbstractStorelessUnivariateStatistic * This default implementation just calls {@link #increment} in a loop over * the specified portion of the input array. *

- * Throws IllegalArgumentException if the input values array is null.

+ * Throws IllegalArgumentException if the input values array is null. * * @param values array holding values to add * @param begin index of the first array element to add * @param length number of array elements to add * @throws MathIllegalArgumentException if values is null - * @see org.apache.commons.math4.stat.descriptive.StorelessUnivariateStatistic#incrementAll(double[], int, int) + * @see StorelessUnivariateStatistic#incrementAll(double[], int, int) */ @Override public void incrementAll(double[] values, int begin, int length) throws MathIllegalArgumentException { - if (test(values, begin, length)) { + if (MathArrays.verifyValues(values, begin, length)) { int k = begin + length; for (int i = begin; i < k; i++) { increment(values[i]); @@ -160,9 +158,11 @@ public abstract class AbstractStorelessUnivariateStatistic } /** - * Returns true iff object is an - * AbstractStorelessUnivariateStatistic returning the same - * values as this for getResult() and getN() + * Returns true iff object is the same type of + * {@link StorelessUnivariateStatistic} (the object's class equals this + * instance) returning the same values as this for getResult() + * and getN(). + * * @param object object to test equality against. * @return true if object returns the same value as this */ @@ -171,22 +171,22 @@ public abstract class AbstractStorelessUnivariateStatistic if (object == this ) { return true; } - if (object instanceof AbstractStorelessUnivariateStatistic == false) { + if (object == null || object.getClass() != this.getClass()) { return false; } - AbstractStorelessUnivariateStatistic stat = (AbstractStorelessUnivariateStatistic) object; + StorelessUnivariateStatistic stat = (StorelessUnivariateStatistic) object; return Precision.equalsIncludingNaN(stat.getResult(), this.getResult()) && Precision.equalsIncludingNaN(stat.getN(), this.getN()); } /** - * Returns hash code based on getResult() and getN() + * Returns hash code based on getResult() and getN(). * * @return hash code */ @Override public int hashCode() { - return 31* (31 + MathUtils.hash(getResult())) + MathUtils.hash(getN()); + return 31 * (31 + MathUtils.hash(getResult())) + MathUtils.hash(getN()); } } http://git-wip-us.apache.org/repos/asf/commons-math/blob/845e1d54/src/main/java/org/apache/commons/math4/stat/descriptive/AbstractUnivariateStatistic.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/stat/descriptive/AbstractUnivariateStatistic.java b/src/main/java/org/apache/commons/math4/stat/descriptive/AbstractUnivariateStatistic.java index ab8e02d..e953aed 100644 --- a/src/main/java/org/apache/commons/math4/stat/descriptive/AbstractUnivariateStatistic.java +++ b/src/main/java/org/apache/commons/math4/stat/descriptive/AbstractUnivariateStatistic.java @@ -24,16 +24,10 @@ import org.apache.commons.math4.exception.util.LocalizedFormats; import org.apache.commons.math4.util.MathArrays; /** - * Abstract base class for all implementations of the - * {@link UnivariateStatistic} interface. + * Abstract base class for implementations of the {@link UnivariateStatistic} interface. *

* Provides a default implementation of evaluate(double[]), * delegating to evaluate(double[], int, int) in the natural way. - *

- *

- * Also includes a test method that performs generic parameter - * validation for the evaluate methods.

- * */ public abstract class AbstractUnivariateStatistic implements UnivariateStatistic { @@ -42,6 +36,28 @@ public abstract class AbstractUnivariateStatistic private double[] storedData; /** + * {@inheritDoc} + */ + @Override + public double evaluate(final double[] values) throws MathIllegalArgumentException { + MathArrays.verifyValues(values, 0, 0); + return evaluate(values, 0, values.length); + } + + /** + * {@inheritDoc} + */ + @Override + public abstract double evaluate(final double[] values, final int begin, final int length) + throws MathIllegalArgumentException; + + /** + * {@inheritDoc} + */ + @Override + public abstract UnivariateStatistic copy(); + + /** * Set the data array. *

* The stored value is a copy of the parameter array, not the array itself. @@ -80,7 +96,7 @@ public abstract class AbstractUnivariateStatistic * @see #evaluate() */ public void setData(final double[] values, final int begin, final int length) - throws MathIllegalArgumentException { + throws MathIllegalArgumentException { if (values == null) { throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY); } @@ -113,154 +129,4 @@ public abstract class AbstractUnivariateStatistic return evaluate(storedData); } - /** - * {@inheritDoc} - */ - @Override - public double evaluate(final double[] values) throws MathIllegalArgumentException { - test(values, 0, 0); - return evaluate(values, 0, values.length); - } - - /** - * {@inheritDoc} - */ - @Override - public abstract double evaluate(final double[] values, final int begin, final int length) - throws MathIllegalArgumentException; - - /** - * {@inheritDoc} - */ - @Override - public abstract UnivariateStatistic copy(); - - /** - * This method is used by evaluate(double[], int, int) methods - * to verify that the input parameters designate a subarray of positive length. - *

- *

    - *
  • returns true iff the parameters designate a subarray of - * positive length
  • - *
  • throws MathIllegalArgumentException if the array is null or - * or the indices are invalid
  • - *
  • returns false
  • if the array is non-null, but - * length is 0. - *

- * - * @param values the input array - * @param begin index of the first array element to include - * @param length the number of elements to include - * @return true if the parameters are valid and designate a subarray of positive length - * @throws MathIllegalArgumentException if the indices are invalid or the array is null - */ - protected boolean test( - final double[] values, - final int begin, - final int length) throws MathIllegalArgumentException { - return MathArrays.verifyValues(values, begin, length, false); - } - - /** - * This method is used by evaluate(double[], int, int) methods - * to verify that the input parameters designate a subarray of positive length. - *

- *

    - *
  • returns true iff the parameters designate a subarray of - * non-negative length
  • - *
  • throws IllegalArgumentException if the array is null or - * or the indices are invalid
  • - *
  • returns false
  • if the array is non-null, but - * length is 0 unless allowEmpty is true - *

- * - * @param values the input array - * @param begin index of the first array element to include - * @param length the number of elements to include - * @param allowEmpty if true then zero length arrays are allowed - * @return true if the parameters are valid - * @throws MathIllegalArgumentException if the indices are invalid or the array is null - * @since 3.0 - */ - protected boolean test(final double[] values, final int begin, - final int length, final boolean allowEmpty) throws MathIllegalArgumentException { - return MathArrays.verifyValues(values, begin, length, allowEmpty); - } - - /** - * This method is used by evaluate(double[], double[], int, int) methods - * to verify that the begin and length parameters designate a subarray of positive length - * and the weights are all non-negative, non-NaN, finite, and not all zero. - *

- *

    - *
  • returns true iff the parameters designate a subarray of - * positive length and the weights array contains legitimate values.
  • - *
  • throws IllegalArgumentException if any of the following are true: - *
    • the values array is null
    • - *
    • the weights array is null
    • - *
    • the weights array does not have the same length as the values array
    • - *
    • the weights array contains one or more infinite values
    • - *
    • the weights array contains one or more NaN values
    • - *
    • the weights array contains negative values
    • - *
    • the start and length arguments do not determine a valid array
    - *
  • - *
  • returns false
  • if the array is non-null, but - * length is 0. - *

- * - * @param values the input array - * @param weights the weights array - * @param begin index of the first array element to include - * @param length the number of elements to include - * @return true if the parameters are valid and designate a subarray of positive length - * @throws MathIllegalArgumentException if the indices are invalid or the array is null - * @since 2.1 - */ - protected boolean test( - final double[] values, - final double[] weights, - final int begin, - final int length) throws MathIllegalArgumentException { - return MathArrays.verifyValues(values, weights, begin, length, false); - } - - /** - * This method is used by evaluate(double[], double[], int, int) methods - * to verify that the begin and length parameters designate a subarray of positive length - * and the weights are all non-negative, non-NaN, finite, and not all zero. - *

- *

    - *
  • returns true iff the parameters designate a subarray of - * non-negative length and the weights array contains legitimate values.
  • - *
  • throws MathIllegalArgumentException if any of the following are true: - *
    • the values array is null
    • - *
    • the weights array is null
    • - *
    • the weights array does not have the same length as the values array
    • - *
    • the weights array contains one or more infinite values
    • - *
    • the weights array contains one or more NaN values
    • - *
    • the weights array contains negative values
    • - *
    • the start and length arguments do not determine a valid array
    - *
  • - *
  • returns false
  • if the array is non-null, but - * length is 0 unless allowEmpty is true. - *

- * - * @param values the input array. - * @param weights the weights array. - * @param begin index of the first array element to include. - * @param length the number of elements to include. - * @param allowEmpty if {@code true} than allow zero length arrays to pass. - * @return {@code true} if the parameters are valid. - * @throws NullArgumentException if either of the arrays are null - * @throws MathIllegalArgumentException if the array indices are not valid, - * the weights array contains NaN, infinite or negative elements, or there - * are no positive weights. - * @since 3.0 - */ - protected boolean test(final double[] values, final double[] weights, - final int begin, final int length, final boolean allowEmpty) throws MathIllegalArgumentException { - - return MathArrays.verifyValues(values, weights, begin, length, allowEmpty); - } } - http://git-wip-us.apache.org/repos/asf/commons-math/blob/845e1d54/src/main/java/org/apache/commons/math4/stat/descriptive/StorelessUnivariateStatistic.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/stat/descriptive/StorelessUnivariateStatistic.java b/src/main/java/org/apache/commons/math4/stat/descriptive/StorelessUnivariateStatistic.java index 938e093..e3db965 100644 --- a/src/main/java/org/apache/commons/math4/stat/descriptive/StorelessUnivariateStatistic.java +++ b/src/main/java/org/apache/commons/math4/stat/descriptive/StorelessUnivariateStatistic.java @@ -25,8 +25,11 @@ import org.apache.commons.math4.exception.MathIllegalArgumentException; *

* This interface is designed to be used for calculating statistics that can be * computed in one pass through the data without storing the full array of - * sample values.

- * + * sample values. + *

+ * Note: unless otherwise stated, the {@link #evaluate(double[])} and + * {@link #evaluate(double[], int, int)} methods do NOT alter the internal + * state of the respective statistic. */ public interface StorelessUnivariateStatistic extends UnivariateStatistic { http://git-wip-us.apache.org/repos/asf/commons-math/blob/845e1d54/src/main/java/org/apache/commons/math4/stat/descriptive/UnivariateStatistic.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/stat/descriptive/UnivariateStatistic.java b/src/main/java/org/apache/commons/math4/stat/descriptive/UnivariateStatistic.java index 03228be..3a07a92 100644 --- a/src/main/java/org/apache/commons/math4/stat/descriptive/UnivariateStatistic.java +++ b/src/main/java/org/apache/commons/math4/stat/descriptive/UnivariateStatistic.java @@ -22,7 +22,6 @@ import org.apache.commons.math4.util.MathArrays; /** * Base interface implemented by all statistics. - * */ public interface UnivariateStatistic extends MathArrays.Function { /** http://git-wip-us.apache.org/repos/asf/commons-math/blob/845e1d54/src/main/java/org/apache/commons/math4/stat/descriptive/moment/FirstMoment.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/stat/descriptive/moment/FirstMoment.java b/src/main/java/org/apache/commons/math4/stat/descriptive/moment/FirstMoment.java index 00d8513..a1eb340 100644 --- a/src/main/java/org/apache/commons/math4/stat/descriptive/moment/FirstMoment.java +++ b/src/main/java/org/apache/commons/math4/stat/descriptive/moment/FirstMoment.java @@ -46,14 +46,12 @@ import org.apache.commons.math4.util.MathUtils; * multiple threads access an instance of this class concurrently, and at least * one of the threads invokes the increment() or * clear() method, it must be synchronized externally.

- * */ class FirstMoment extends AbstractStorelessUnivariateStatistic implements Serializable { /** Serializable version identifier */ - private static final long serialVersionUID = 6112755307178490473L; - + private static final long serialVersionUID = 20150412L; /** Count of values that have been added */ protected long n; @@ -161,7 +159,6 @@ class FirstMoment extends AbstractStorelessUnivariateStatistic throws NullArgumentException { MathUtils.checkNotNull(source); MathUtils.checkNotNull(dest); - dest.setData(source.getDataRef()); dest.n = source.n; dest.m1 = source.m1; dest.dev = source.dev; http://git-wip-us.apache.org/repos/asf/commons-math/blob/845e1d54/src/main/java/org/apache/commons/math4/stat/descriptive/moment/FourthMoment.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/stat/descriptive/moment/FourthMoment.java b/src/main/java/org/apache/commons/math4/stat/descriptive/moment/FourthMoment.java index d7d07df..d005762 100644 --- a/src/main/java/org/apache/commons/math4/stat/descriptive/moment/FourthMoment.java +++ b/src/main/java/org/apache/commons/math4/stat/descriptive/moment/FourthMoment.java @@ -52,12 +52,11 @@ import org.apache.commons.math4.util.MathUtils; * multiple threads access an instance of this class concurrently, and at least * one of the threads invokes the increment() or * clear() method, it must be synchronized externally.

- * */ class FourthMoment extends ThirdMoment implements Serializable{ /** Serializable version identifier */ - private static final long serialVersionUID = 4763990447117157611L; + private static final long serialVersionUID = 20150412L; /** fourth moment of values that have been added */ private double m4; @@ -72,7 +71,7 @@ class FourthMoment extends ThirdMoment implements Serializable{ /** * Copy constructor, creates a new {@code FourthMoment} identical - * to the {@code original} + * to the {@code original}. * * @param original the {@code FourthMoment} instance to copy * @throws NullArgumentException if original is null http://git-wip-us.apache.org/repos/asf/commons-math/blob/845e1d54/src/main/java/org/apache/commons/math4/stat/descriptive/moment/GeometricMean.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/stat/descriptive/moment/GeometricMean.java b/src/main/java/org/apache/commons/math4/stat/descriptive/moment/GeometricMean.java index ab628de..938930b 100644 --- a/src/main/java/org/apache/commons/math4/stat/descriptive/moment/GeometricMean.java +++ b/src/main/java/org/apache/commons/math4/stat/descriptive/moment/GeometricMean.java @@ -48,19 +48,17 @@ import org.apache.commons.math4.util.MathUtils; * multiple threads access an instance of this class concurrently, and at least * one of the threads invokes the increment() or * clear() method, it must be synchronized externally.

- * - * */ public class GeometricMean extends AbstractStorelessUnivariateStatistic implements Serializable { /** Serializable version identifier */ - private static final long serialVersionUID = -8178734905303459453L; + private static final long serialVersionUID = 20150412L; /** Wrapped SumOfLogs instance */ private StorelessUnivariateStatistic sumOfLogs; /** - * Create a GeometricMean instance + * Create a GeometricMean instance. */ public GeometricMean() { sumOfLogs = new SumOfLogs(); @@ -68,7 +66,7 @@ public class GeometricMean extends AbstractStorelessUnivariateStatistic implemen /** * Copy constructor, creates a new {@code GeometricMean} identical - * to the {@code original} + * to the {@code original}. * * @param original the {@code GeometricMean} instance to copy * @throws NullArgumentException if original is null @@ -80,7 +78,7 @@ public class GeometricMean extends AbstractStorelessUnivariateStatistic implemen /** * Create a GeometricMean instance using the given SumOfLogs instance - * @param sumOfLogs sum of logs instance to use for computation + * @param sumOfLogs sum of logs instance to use for computation. */ public GeometricMean(SumOfLogs sumOfLogs) { this.sumOfLogs = sumOfLogs; @@ -142,11 +140,9 @@ public class GeometricMean extends AbstractStorelessUnivariateStatistic implemen * index parameters are not valid */ @Override - public double evaluate( - final double[] values, final int begin, final int length) - throws MathIllegalArgumentException { - return FastMath.exp( - sumOfLogs.evaluate(values, begin, length) / length); + public double evaluate(final double[] values, final int begin, final int length) + throws MathIllegalArgumentException { + return FastMath.exp(sumOfLogs.evaluate(values, begin, length) / length); } /** @@ -169,13 +165,13 @@ public class GeometricMean extends AbstractStorelessUnivariateStatistic implemen * (i.e if n > 0) */ public void setSumLogImpl(StorelessUnivariateStatistic sumLogImpl) - throws MathIllegalStateException { + throws MathIllegalStateException { checkEmpty(); this.sumOfLogs = sumLogImpl; } /** - * Returns the currently configured sum of logs implementation + * Returns the currently configured sum of logs implementation. * * @return the StorelessUnivariateStatistic implementing the log sum */ @@ -195,11 +191,9 @@ public class GeometricMean extends AbstractStorelessUnivariateStatistic implemen throws NullArgumentException { MathUtils.checkNotNull(source); MathUtils.checkNotNull(dest); - dest.setData(source.getDataRef()); dest.sumOfLogs = source.sumOfLogs.copy(); } - /** * Throws MathIllegalStateException if n > 0. * @throws MathIllegalStateException if data has been added to this statistic http://git-wip-us.apache.org/repos/asf/commons-math/blob/845e1d54/src/main/java/org/apache/commons/math4/stat/descriptive/moment/Kurtosis.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/stat/descriptive/moment/Kurtosis.java b/src/main/java/org/apache/commons/math4/stat/descriptive/moment/Kurtosis.java index 2164ed9..8e5f280 100644 --- a/src/main/java/org/apache/commons/math4/stat/descriptive/moment/Kurtosis.java +++ b/src/main/java/org/apache/commons/math4/stat/descriptive/moment/Kurtosis.java @@ -22,6 +22,7 @@ import org.apache.commons.math4.exception.MathIllegalArgumentException; import org.apache.commons.math4.exception.NullArgumentException; import org.apache.commons.math4.stat.descriptive.AbstractStorelessUnivariateStatistic; import org.apache.commons.math4.util.FastMath; +import org.apache.commons.math4.util.MathArrays; import org.apache.commons.math4.util.MathUtils; @@ -29,27 +30,26 @@ import org.apache.commons.math4.util.MathUtils; * Computes the Kurtosis of the available values. *

* We use the following (unbiased) formula to define kurtosis:

- *

- * kurtosis = { [n(n+1) / (n -1)(n - 2)(n-3)] sum[(x_i - mean)^4] / std^4 } - [3(n-1)^2 / (n-2)(n-3)] - *

- * where n is the number of values, mean is the {@link Mean} and std is the + *

+ * kurtosis = { [n(n+1) / (n -1)(n - 2)(n-3)] sum[(x_i - mean)^4] / std^4 } - [3(n-1)^2 / (n-2)(n-3)] + *

+ * where n is the number of values, mean is the {@link Mean} and std is the * {@link StandardDeviation}

*

- * Note that this statistic is undefined for n < 4. Double.Nan - * is returned when there is not sufficient data to compute the statistic. - * Note that Double.NaN may also be returned if the input includes NaN - * and / or infinite values.

+ * Note that this statistic is undefined for n < 4. Double.Nan + * is returned when there is not sufficient data to compute the statistic. + * Note that Double.NaN may also be returned if the input includes NaN + * and / or infinite values.

*

* Note that this implementation is not synchronized. If * multiple threads access an instance of this class concurrently, and at least * one of the threads invokes the increment() or * clear() method, it must be synchronized externally.

- * */ public class Kurtosis extends AbstractStorelessUnivariateStatistic implements Serializable { /** Serializable version identifier */ - private static final long serialVersionUID = 2784465764798260919L; + private static final long serialVersionUID = 20150412L; /**Fourth Moment on which this statistic is based */ protected FourthMoment moment; @@ -59,11 +59,11 @@ public class Kurtosis extends AbstractStorelessUnivariateStatistic implements S *

* Statistics based on (constructed from) external moments cannot * be incremented or cleared.

- */ + */ protected boolean incMoment; /** - * Construct a Kurtosis + * Construct a Kurtosis. */ public Kurtosis() { incMoment = true; @@ -71,7 +71,7 @@ public class Kurtosis extends AbstractStorelessUnivariateStatistic implements S } /** - * Construct a Kurtosis from an external moment + * Construct a Kurtosis from an external moment. * * @param m4 external Moment */ @@ -82,7 +82,7 @@ public class Kurtosis extends AbstractStorelessUnivariateStatistic implements S /** * Copy constructor, creates a new {@code Kurtosis} identical - * to the {@code original} + * to the {@code original}. * * @param original the {@code Kurtosis} instance to copy * @throws NullArgumentException if original is null @@ -161,13 +161,13 @@ public class Kurtosis extends AbstractStorelessUnivariateStatistic implements S * index parameters are not valid */ @Override - public double evaluate(final double[] values,final int begin, final int length) - throws MathIllegalArgumentException { + public double evaluate(final double[] values, final int begin, final int length) + throws MathIllegalArgumentException { + // Initialize the kurtosis double kurt = Double.NaN; - if (test(values, begin, length) && length > 3) { - + if (MathArrays.verifyValues(values, begin, length) && length > 3) { // Compute the mean and standard deviation Variance variance = new Variance(); variance.incrementAll(values, begin, length); @@ -219,7 +219,6 @@ public class Kurtosis extends AbstractStorelessUnivariateStatistic implements S throws NullArgumentException { MathUtils.checkNotNull(source); MathUtils.checkNotNull(dest); - dest.setData(source.getDataRef()); dest.moment = source.moment.copy(); dest.incMoment = source.incMoment; } http://git-wip-us.apache.org/repos/asf/commons-math/blob/845e1d54/src/main/java/org/apache/commons/math4/stat/descriptive/moment/Mean.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/stat/descriptive/moment/Mean.java b/src/main/java/org/apache/commons/math4/stat/descriptive/moment/Mean.java index 46a4551..4a23b0e 100644 --- a/src/main/java/org/apache/commons/math4/stat/descriptive/moment/Mean.java +++ b/src/main/java/org/apache/commons/math4/stat/descriptive/moment/Mean.java @@ -23,11 +23,12 @@ import org.apache.commons.math4.exception.NullArgumentException; import org.apache.commons.math4.stat.descriptive.AbstractStorelessUnivariateStatistic; import org.apache.commons.math4.stat.descriptive.WeightedEvaluation; import org.apache.commons.math4.stat.descriptive.summary.Sum; +import org.apache.commons.math4.util.MathArrays; import org.apache.commons.math4.util.MathUtils; /** - *

Computes the arithmetic mean of a set of values. Uses the definitional - * formula:

+ * Computes the arithmetic mean of a set of values. Uses the definitional + * formula: *

* mean = sum(x_i) / n *

@@ -58,13 +59,12 @@ import org.apache.commons.math4.util.MathUtils; * multiple threads access an instance of this class concurrently, and at least * one of the threads invokes the increment() or * clear() method, it must be synchronized externally. - * */ public class Mean extends AbstractStorelessUnivariateStatistic implements Serializable, WeightedEvaluation { /** Serializable version identifier */ - private static final long serialVersionUID = -1296043746617791564L; + private static final long serialVersionUID = 20150412L; /** First moment on which this statistic is based. */ protected FirstMoment moment; @@ -95,7 +95,7 @@ public class Mean extends AbstractStorelessUnivariateStatistic /** * Copy constructor, creates a new {@code Mean} identical - * to the {@code original} + * to the {@code original}. * * @param original the {@code Mean} instance to copy * @throws NullArgumentException if original is null @@ -160,9 +160,10 @@ public class Mean extends AbstractStorelessUnivariateStatistic * parameters are not valid */ @Override - public double evaluate(final double[] values,final int begin, final int length) - throws MathIllegalArgumentException { - if (test(values, begin, length)) { + public double evaluate(final double[] values, final int begin, final int length) + throws MathIllegalArgumentException { + + if (MathArrays.verifyValues(values, begin, length)) { Sum sum = new Sum(); double sampleSize = length; @@ -211,7 +212,7 @@ public class Mean extends AbstractStorelessUnivariateStatistic @Override public double evaluate(final double[] values, final double[] weights, final int begin, final int length) throws MathIllegalArgumentException { - if (test(values, weights, begin, length)) { + if (MathArrays.verifyValues(values, weights, begin, length)) { Sum sum = new Sum(); // Compute initial estimate using definitional formula @@ -254,7 +255,7 @@ public class Mean extends AbstractStorelessUnivariateStatistic */ @Override public double evaluate(final double[] values, final double[] weights) - throws MathIllegalArgumentException { + throws MathIllegalArgumentException { return evaluate(values, weights, 0, values.length); } @@ -269,7 +270,6 @@ public class Mean extends AbstractStorelessUnivariateStatistic return result; } - /** * Copies source to dest. *

Neither source nor dest can be null.

@@ -282,7 +282,6 @@ public class Mean extends AbstractStorelessUnivariateStatistic throws NullArgumentException { MathUtils.checkNotNull(source); MathUtils.checkNotNull(dest); - dest.setData(source.getDataRef()); dest.incMoment = source.incMoment; dest.moment = source.moment.copy(); } http://git-wip-us.apache.org/repos/asf/commons-math/blob/845e1d54/src/main/java/org/apache/commons/math4/stat/descriptive/moment/SecondMoment.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/stat/descriptive/moment/SecondMoment.java b/src/main/java/org/apache/commons/math4/stat/descriptive/moment/SecondMoment.java index e617ada..6cab875 100644 --- a/src/main/java/org/apache/commons/math4/stat/descriptive/moment/SecondMoment.java +++ b/src/main/java/org/apache/commons/math4/stat/descriptive/moment/SecondMoment.java @@ -44,18 +44,17 @@ import org.apache.commons.math4.util.MathUtils; * multiple threads access an instance of this class concurrently, and at least * one of the threads invokes the increment() or * clear() method, it must be synchronized externally.

- * */ public class SecondMoment extends FirstMoment implements Serializable { /** Serializable version identifier */ - private static final long serialVersionUID = 3942403127395076445L; + private static final long serialVersionUID = 20150412L; /** second moment of values that have been added */ protected double m2; /** - * Create a SecondMoment instance + * Create a SecondMoment instance. */ public SecondMoment() { super(); @@ -64,13 +63,12 @@ public class SecondMoment extends FirstMoment implements Serializable { /** * Copy constructor, creates a new {@code SecondMoment} identical - * to the {@code original} + * to the {@code original}. * * @param original the {@code SecondMoment} instance to copy * @throws NullArgumentException if original is null */ - public SecondMoment(SecondMoment original) - throws NullArgumentException { + public SecondMoment(SecondMoment original) throws NullArgumentException { super(original); this.m2 = original.m2; } http://git-wip-us.apache.org/repos/asf/commons-math/blob/845e1d54/src/main/java/org/apache/commons/math4/stat/descriptive/moment/SemiVariance.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/stat/descriptive/moment/SemiVariance.java b/src/main/java/org/apache/commons/math4/stat/descriptive/moment/SemiVariance.java index 10c8659..2c8f840 100644 --- a/src/main/java/org/apache/commons/math4/stat/descriptive/moment/SemiVariance.java +++ b/src/main/java/org/apache/commons/math4/stat/descriptive/moment/SemiVariance.java @@ -22,6 +22,7 @@ import java.io.Serializable; import org.apache.commons.math4.exception.MathIllegalArgumentException; import org.apache.commons.math4.exception.NullArgumentException; import org.apache.commons.math4.stat.descriptive.AbstractUnivariateStatistic; +import org.apache.commons.math4.util.MathArrays; import org.apache.commons.math4.util.MathUtils; /** @@ -66,11 +67,11 @@ public class SemiVariance extends AbstractUnivariateStatistic implements Seriali public static final Direction DOWNSIDE_VARIANCE = Direction.DOWNSIDE; /** Serializable version identifier */ - private static final long serialVersionUID = -2653430366886024994L; + private static final long serialVersionUID = 20150412L; /** * Determines whether or not bias correction is applied when computing the - * value of the statisic. True means that bias is corrected. + * value of the statistic. True means that bias is corrected. */ private boolean biasCorrected = true; @@ -98,7 +99,6 @@ public class SemiVariance extends AbstractUnivariateStatistic implements Seriali this.biasCorrected = biasCorrected; } - /** * Constructs a SemiVariance with the specified Direction property * and default (true) biasCorrected property @@ -110,7 +110,6 @@ public class SemiVariance extends AbstractUnivariateStatistic implements Seriali this.varianceDirection = direction; } - /** * Constructs a SemiVariance with the specified isBiasCorrected * property and the specified Direction property. @@ -127,10 +126,9 @@ public class SemiVariance extends AbstractUnivariateStatistic implements Seriali this.varianceDirection = direction; } - /** * Copy constructor, creates a new {@code SemiVariance} identical - * to the {@code original} + * to the {@code original}. * * @param original the {@code SemiVariance} instance to copy * @throws NullArgumentException if original is null @@ -139,7 +137,6 @@ public class SemiVariance extends AbstractUnivariateStatistic implements Seriali copy(original, this); } - /** * {@inheritDoc} */ @@ -151,7 +148,6 @@ public class SemiVariance extends AbstractUnivariateStatistic implements Seriali return result; } - /** * Copies source to dest. *

Neither source nor dest can be null.

@@ -164,84 +160,81 @@ public class SemiVariance extends AbstractUnivariateStatistic implements Seriali throws NullArgumentException { MathUtils.checkNotNull(source); MathUtils.checkNotNull(dest); - dest.setData(source.getDataRef()); dest.biasCorrected = source.biasCorrected; dest.varianceDirection = source.varianceDirection; } /** - *

Returns the {@link SemiVariance} of the designated values against the mean, using - * instance properties varianceDirection and biasCorrection.

+ *

Returns the {@link SemiVariance} of the designated values against the mean, using + * instance properties varianceDirection and biasCorrection.

+ * + *

Returns NaN if the array is empty and throws + * IllegalArgumentException if the array is null.

+ * + * @param values the input array + * @param start index of the first array element to include + * @param length the number of elements to include + * @return the SemiVariance + * @throws MathIllegalArgumentException if the parameters are not valid + * + */ + @Override + public double evaluate(final double[] values, final int start, final int length) + throws MathIllegalArgumentException { + double m = (new Mean()).evaluate(values, start, length); + return evaluate(values, m, varianceDirection, biasCorrected, 0, values.length); + } + + /** + * This method calculates {@link SemiVariance} for the entire array against the mean, using + * the current value of the biasCorrection instance property. + * + * @param values the input array + * @param direction the {@link Direction} of the semivariance + * @return the SemiVariance + * @throws MathIllegalArgumentException if values is null + * + */ + public double evaluate(final double[] values, Direction direction) + throws MathIllegalArgumentException { + double m = (new Mean()).evaluate(values); + return evaluate(values, m, direction, biasCorrected, 0, values.length); + } + + /** + *

Returns the {@link SemiVariance} of the designated values against the cutoff, using + * instance properties variancDirection and biasCorrection.

* *

Returns NaN if the array is empty and throws - * IllegalArgumentException if the array is null.

+ * MathIllegalArgumentException if the array is null.

* * @param values the input array - * @param start index of the first array element to include - * @param length the number of elements to include + * @param cutoff the reference point * @return the SemiVariance - * @throws MathIllegalArgumentException if the parameters are not valid - * + * @throws MathIllegalArgumentException if values is null */ - @Override - public double evaluate(final double[] values, final int start, final int length) - throws MathIllegalArgumentException { - double m = (new Mean()).evaluate(values, start, length); - return evaluate(values, m, varianceDirection, biasCorrected, 0, values.length); - } - - - /** - * This method calculates {@link SemiVariance} for the entire array against the mean, using - * the current value of the biasCorrection instance property. - * - * @param values the input array - * @param direction the {@link Direction} of the semivariance - * @return the SemiVariance - * @throws MathIllegalArgumentException if values is null - * - */ - public double evaluate(final double[] values, Direction direction) - throws MathIllegalArgumentException { - double m = (new Mean()).evaluate(values); - return evaluate (values, m, direction, biasCorrected, 0, values.length); - } - - /** - *

Returns the {@link SemiVariance} of the designated values against the cutoff, using - * instance properties variancDirection and biasCorrection.

- * - *

Returns NaN if the array is empty and throws - * MathIllegalArgumentException if the array is null.

- * - * @param values the input array - * @param cutoff the reference point - * @return the SemiVariance - * @throws MathIllegalArgumentException if values is null - */ - public double evaluate(final double[] values, final double cutoff) - throws MathIllegalArgumentException { - return evaluate(values, cutoff, varianceDirection, biasCorrected, 0, values.length); - } - - /** - *

Returns the {@link SemiVariance} of the designated values against the cutoff in the - * given direction, using the current value of the biasCorrection instance property.

- * - *

Returns NaN if the array is empty and throws - * MathIllegalArgumentException if the array is null.

- * - * @param values the input array - * @param cutoff the reference point - * @param direction the {@link Direction} of the semivariance - * @return the SemiVariance - * @throws MathIllegalArgumentException if values is null - */ - public double evaluate(final double[] values, final double cutoff, final Direction direction) - throws MathIllegalArgumentException { - return evaluate(values, cutoff, direction, biasCorrected, 0, values.length); - } + public double evaluate(final double[] values, final double cutoff) + throws MathIllegalArgumentException { + return evaluate(values, cutoff, varianceDirection, biasCorrected, 0, values.length); + } + /** + *

Returns the {@link SemiVariance} of the designated values against the cutoff in the + * given direction, using the current value of the biasCorrection instance property.

+ * + *

Returns NaN if the array is empty and throws + * MathIllegalArgumentException if the array is null.

+ * + * @param values the input array + * @param cutoff the reference point + * @param direction the {@link Direction} of the semivariance + * @return the SemiVariance + * @throws MathIllegalArgumentException if values is null + */ + public double evaluate(final double[] values, final double cutoff, final Direction direction) + throws MathIllegalArgumentException { + return evaluate(values, cutoff, direction, biasCorrected, 0, values.length); + } /** *

Returns the {@link SemiVariance} of the designated values against the cutoff @@ -260,110 +253,111 @@ public class SemiVariance extends AbstractUnivariateStatistic implements Seriali * @throws MathIllegalArgumentException if the parameters are not valid * */ - public double evaluate (final double[] values, final double cutoff, final Direction direction, - final boolean corrected, final int start, final int length) throws MathIllegalArgumentException { - - test(values, start, length); - if (values.length == 0) { - return Double.NaN; - } else { - if (values.length == 1) { - return 0.0; - } else { - final boolean booleanDirection = direction.getDirection(); - - double dev = 0.0; - double sumsq = 0.0; - for (int i = start; i < length; i++) { - if ((values[i] > cutoff) == booleanDirection) { - dev = values[i] - cutoff; - sumsq += dev * dev; - } - } - - if (corrected) { - return sumsq / (length - 1.0); - } else { - return sumsq / length; - } - } - } - } + public double evaluate (final double[] values, final double cutoff, final Direction direction, + final boolean corrected, final int start, final int length) + throws MathIllegalArgumentException { + + MathArrays.verifyValues(values, start, length); + if (values.length == 0) { + return Double.NaN; + } else { + if (values.length == 1) { + return 0.0; + } else { + final boolean booleanDirection = direction.getDirection(); + + double dev = 0.0; + double sumsq = 0.0; + for (int i = start; i < length; i++) { + if ((values[i] > cutoff) == booleanDirection) { + dev = values[i] - cutoff; + sumsq += dev * dev; + } + } + + if (corrected) { + return sumsq / (length - 1.0); + } else { + return sumsq / length; + } + } + } + } - /** - * Returns true iff biasCorrected property is set to true. - * - * @return the value of biasCorrected. - */ - public boolean isBiasCorrected() { - return biasCorrected; - } + /** + * Returns true iff biasCorrected property is set to true. + * + * @return the value of biasCorrected. + */ + public boolean isBiasCorrected() { + return biasCorrected; + } - /** - * Sets the biasCorrected property. - * - * @param biasCorrected new biasCorrected property value - */ - public void setBiasCorrected(boolean biasCorrected) { - this.biasCorrected = biasCorrected; - } + /** + * Sets the biasCorrected property. + * + * @param biasCorrected new biasCorrected property value + */ + public void setBiasCorrected(boolean biasCorrected) { + this.biasCorrected = biasCorrected; + } - /** - * Returns the varianceDirection property. - * - * @return the varianceDirection - */ - public Direction getVarianceDirection () { - return varianceDirection; - } + /** + * Returns the varianceDirection property. + * + * @return the varianceDirection + */ + public Direction getVarianceDirection () { + return varianceDirection; + } - /** - * Sets the variance direction - * - * @param varianceDirection the direction of the semivariance - */ - public void setVarianceDirection(Direction varianceDirection) { - this.varianceDirection = varianceDirection; - } + /** + * Sets the variance direction + * + * @param varianceDirection the direction of the semivariance + */ + public void setVarianceDirection(Direction varianceDirection) { + this.varianceDirection = varianceDirection; + } - /** - * The direction of the semivariance - either upside or downside. The direction - * is represented by boolean, with true corresponding to UPSIDE semivariance. - */ - public enum Direction { - /** - * The UPSIDE Direction is used to specify that the observations above the - * cutoff point will be used to calculate SemiVariance - */ - UPSIDE (true), - - /** - * The DOWNSIDE Direction is used to specify that the observations below - * the cutoff point will be used to calculate SemiVariance - */ - DOWNSIDE (false); - - /** - * boolean value UPSIDE <-> true - */ - private boolean direction; - - /** - * Create a Direction with the given value. - * - * @param b boolean value representing the Direction. True corresponds to UPSIDE. - */ - Direction (boolean b) { - direction = b; - } - - /** - * Returns the value of this Direction. True corresponds to UPSIDE. - * - * @return true if direction is UPSIDE; false otherwise - */ - boolean getDirection () { - return direction; - } - } + /** + * The direction of the semivariance - either upside or downside. The direction + * is represented by boolean, with true corresponding to UPSIDE semivariance. + */ + public enum Direction { + /** + * The UPSIDE Direction is used to specify that the observations above the + * cutoff point will be used to calculate SemiVariance + */ + UPSIDE (true), + + /** + * The DOWNSIDE Direction is used to specify that the observations below + * the cutoff point will be used to calculate SemiVariance + */ + DOWNSIDE (false); + + /** + * boolean value UPSIDE <-> true + */ + private boolean direction; + + /** + * Create a Direction with the given value. + * + * @param b boolean value representing the Direction. True corresponds to UPSIDE. + */ + Direction (boolean b) { + direction = b; + } + + /** + * Returns the value of this Direction. True corresponds to UPSIDE. + * + * @return true if direction is UPSIDE; false otherwise + */ + boolean getDirection () { + return direction; + } + } } http://git-wip-us.apache.org/repos/asf/commons-math/blob/845e1d54/src/main/java/org/apache/commons/math4/stat/descriptive/moment/Skewness.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/stat/descriptive/moment/Skewness.java b/src/main/java/org/apache/commons/math4/stat/descriptive/moment/Skewness.java index 3b488c9..8dca668 100644 --- a/src/main/java/org/apache/commons/math4/stat/descriptive/moment/Skewness.java +++ b/src/main/java/org/apache/commons/math4/stat/descriptive/moment/Skewness.java @@ -22,6 +22,7 @@ import org.apache.commons.math4.exception.MathIllegalArgumentException; import org.apache.commons.math4.exception.NullArgumentException; import org.apache.commons.math4.stat.descriptive.AbstractStorelessUnivariateStatistic; import org.apache.commons.math4.util.FastMath; +import org.apache.commons.math4.util.MathArrays; import org.apache.commons.math4.util.MathUtils; /** @@ -38,12 +39,11 @@ import org.apache.commons.math4.util.MathUtils; * multiple threads access an instance of this class concurrently, and at least * one of the threads invokes the increment() or * clear() method, it must be synchronized externally.

- * */ public class Skewness extends AbstractStorelessUnivariateStatistic implements Serializable { /** Serializable version identifier */ - private static final long serialVersionUID = 7101857578996691352L; + private static final long serialVersionUID = 20150412L; /** Third moment on which this statistic is based */ protected ThirdMoment moment = null; @@ -57,7 +57,7 @@ public class Skewness extends AbstractStorelessUnivariateStatistic implements Se protected boolean incMoment; /** - * Constructs a Skewness + * Constructs a Skewness. */ public Skewness() { incMoment = true; @@ -65,7 +65,7 @@ public class Skewness extends AbstractStorelessUnivariateStatistic implements Se } /** - * Constructs a Skewness with an external moment + * Constructs a Skewness with an external moment. * @param m3 external moment */ public Skewness(final ThirdMoment m3) { @@ -75,7 +75,7 @@ public class Skewness extends AbstractStorelessUnivariateStatistic implements Se /** * Copy constructor, creates a new {@code Skewness} identical - * to the {@code original} + * to the {@code original}. * * @param original the {@code Skewness} instance to copy * @throws NullArgumentException if original is null @@ -139,7 +139,7 @@ public class Skewness extends AbstractStorelessUnivariateStatistic implements Se } /** - * Returns the Skewness of the entries in the specifed portion of the + * Returns the Skewness of the entries in the specified portion of the * input array. *

* See {@link Skewness} for the definition used in the computation.

@@ -149,19 +149,18 @@ public class Skewness extends AbstractStorelessUnivariateStatistic implements Se * @param values the input array * @param begin the index of the first array element to include * @param length the number of elements to include - * @return the skewness of the values or Double.NaN if length is less than - * 3 + * @return the skewness of the values or Double.NaN if length is less than 3 * @throws MathIllegalArgumentException if the array is null or the array index * parameters are not valid */ @Override - public double evaluate(final double[] values,final int begin, - final int length) throws MathIllegalArgumentException { + public double evaluate(final double[] values,final int begin, final int length) + throws MathIllegalArgumentException { // Initialize the skewness double skew = Double.NaN; - if (test(values, begin, length) && length > 2 ){ + if (MathArrays.verifyValues(values, begin, length) && length > 2 ) { Mean mean = new Mean(); // Get the mean and the standard deviation double m = mean.evaluate(values, begin, length); @@ -217,7 +216,6 @@ public class Skewness extends AbstractStorelessUnivariateStatistic implements Se throws NullArgumentException { MathUtils.checkNotNull(source); MathUtils.checkNotNull(dest); - dest.setData(source.getDataRef()); dest.moment = new ThirdMoment(source.moment.copy()); dest.incMoment = source.incMoment; } http://git-wip-us.apache.org/repos/asf/commons-math/blob/845e1d54/src/main/java/org/apache/commons/math4/stat/descriptive/moment/StandardDeviation.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/stat/descriptive/moment/StandardDeviation.java b/src/main/java/org/apache/commons/math4/stat/descriptive/moment/StandardDeviation.java index 0d86be2..d2f5e8b 100644 --- a/src/main/java/org/apache/commons/math4/stat/descriptive/moment/StandardDeviation.java +++ b/src/main/java/org/apache/commons/math4/stat/descriptive/moment/StandardDeviation.java @@ -38,13 +38,12 @@ import org.apache.commons.math4.util.MathUtils; * multiple threads access an instance of this class concurrently, and at least * one of the threads invokes the increment() or * clear() method, it must be synchronized externally.

- * */ public class StandardDeviation extends AbstractStorelessUnivariateStatistic implements Serializable { /** Serializable version identifier */ - private static final long serialVersionUID = 5728716329662425188L; + private static final long serialVersionUID = 20150412L; /** Wrapped Variance instance */ private Variance variance = null; @@ -68,7 +67,7 @@ public class StandardDeviation extends AbstractStorelessUnivariateStatistic /** * Copy constructor, creates a new {@code StandardDeviation} identical - * to the {@code original} + * to the {@code original}. * * @param original the {@code StandardDeviation} instance to copy * @throws NullArgumentException if original is null @@ -78,7 +77,7 @@ public class StandardDeviation extends AbstractStorelessUnivariateStatistic } /** - * Contructs a StandardDeviation with the specified value for the + * Constructs a StandardDeviation with the specified value for the * isBiasCorrected property. If this property is set to * true, the {@link Variance} used in computing results will * use the bias-corrected, or "sample" formula. See {@link Variance} for @@ -92,7 +91,7 @@ public class StandardDeviation extends AbstractStorelessUnivariateStatistic } /** - * Contructs a StandardDeviation with the specified value for the + * Constructs a StandardDeviation with the specified value for the * isBiasCorrected property and the supplied external moment. * If isBiasCorrected is set to true, the * {@link Variance} used in computing results will use the bias-corrected, @@ -177,8 +176,8 @@ public class StandardDeviation extends AbstractStorelessUnivariateStatistic */ @Override public double evaluate(final double[] values, final int begin, final int length) - throws MathIllegalArgumentException { - return FastMath.sqrt(variance.evaluate(values, begin, length)); + throws MathIllegalArgumentException { + return FastMath.sqrt(variance.evaluate(values, begin, length)); } /** @@ -206,7 +205,7 @@ public class StandardDeviation extends AbstractStorelessUnivariateStatistic * parameters are not valid */ public double evaluate(final double[] values, final double mean, - final int begin, final int length) throws MathIllegalArgumentException { + final int begin, final int length) throws MathIllegalArgumentException { return FastMath.sqrt(variance.evaluate(values, mean, begin, length)); } @@ -232,7 +231,7 @@ public class StandardDeviation extends AbstractStorelessUnivariateStatistic * @throws MathIllegalArgumentException if the array is null */ public double evaluate(final double[] values, final double mean) - throws MathIllegalArgumentException { + throws MathIllegalArgumentException { return FastMath.sqrt(variance.evaluate(values, mean)); } @@ -261,7 +260,6 @@ public class StandardDeviation extends AbstractStorelessUnivariateStatistic return result; } - /** * Copies source to dest. *

Neither source nor dest can be null.

@@ -270,11 +268,9 @@ public class StandardDeviation extends AbstractStorelessUnivariateStatistic * @param dest StandardDeviation to copy to * @throws NullArgumentException if either source or dest is null */ - public static void copy(StandardDeviation source, StandardDeviation dest) - throws NullArgumentException { + public static void copy(StandardDeviation source, StandardDeviation dest) throws NullArgumentException { MathUtils.checkNotNull(source); MathUtils.checkNotNull(dest); - dest.setData(source.getDataRef()); dest.variance = source.variance.copy(); } http://git-wip-us.apache.org/repos/asf/commons-math/blob/845e1d54/src/main/java/org/apache/commons/math4/stat/descriptive/moment/ThirdMoment.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/stat/descriptive/moment/ThirdMoment.java b/src/main/java/org/apache/commons/math4/stat/descriptive/moment/ThirdMoment.java index ba74055..b26b77f 100644 --- a/src/main/java/org/apache/commons/math4/stat/descriptive/moment/ThirdMoment.java +++ b/src/main/java/org/apache/commons/math4/stat/descriptive/moment/ThirdMoment.java @@ -51,12 +51,12 @@ import org.apache.commons.math4.util.MathUtils; class ThirdMoment extends SecondMoment implements Serializable { /** Serializable version identifier */ - private static final long serialVersionUID = -7818711964045118679L; + private static final long serialVersionUID = 20150412L; /** third moment of values that have been added */ protected double m3; - /** + /** * Square of deviation of most recently added value from previous first * moment, normalized by previous sample size. Retained to prevent * repeated computation in higher order moments. nDevSq = nDev * nDev. @@ -64,7 +64,7 @@ class ThirdMoment extends SecondMoment implements Serializable { protected double nDevSq; /** - * Create a FourthMoment instance + * Create a FourthMoment instance. */ public ThirdMoment() { super(); @@ -74,10 +74,10 @@ class ThirdMoment extends SecondMoment implements Serializable { /** * Copy constructor, creates a new {@code ThirdMoment} identical - * to the {@code original} + * to the {@code original}. * * @param original the {@code ThirdMoment} instance to copy - * @throws NullArgumentException if orginal is null + * @throws NullArgumentException if original is null */ public ThirdMoment(ThirdMoment original) throws NullArgumentException { copy(original, this); http://git-wip-us.apache.org/repos/asf/commons-math/blob/845e1d54/src/main/java/org/apache/commons/math4/stat/descriptive/moment/Variance.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/stat/descriptive/moment/Variance.java b/src/main/java/org/apache/commons/math4/stat/descriptive/moment/Variance.java index c7602ae..d88f952 100644 --- a/src/main/java/org/apache/commons/math4/stat/descriptive/moment/Variance.java +++ b/src/main/java/org/apache/commons/math4/stat/descriptive/moment/Variance.java @@ -23,6 +23,7 @@ import org.apache.commons.math4.exception.NullArgumentException; import org.apache.commons.math4.exception.util.LocalizedFormats; import org.apache.commons.math4.stat.descriptive.AbstractStorelessUnivariateStatistic; import org.apache.commons.math4.stat.descriptive.WeightedEvaluation; +import org.apache.commons.math4.util.MathArrays; import org.apache.commons.math4.util.MathUtils; /** @@ -64,12 +65,11 @@ import org.apache.commons.math4.util.MathUtils; * multiple threads access an instance of this class concurrently, and at least * one of the threads invokes the increment() or * clear() method, it must be synchronized externally.

- * */ public class Variance extends AbstractStorelessUnivariateStatistic implements Serializable, WeightedEvaluation { /** Serializable version identifier */ - private static final long serialVersionUID = -9111962718267217978L; + private static final long serialVersionUID = 20150412L; /** SecondMoment is used in incremental calculation of Variance*/ protected SecondMoment moment = null; @@ -100,13 +100,13 @@ public class Variance extends AbstractStorelessUnivariateStatistic implements Se /** * Constructs a Variance based on an external second moment. + *

* When this constructor is used, the statistic may only be * incremented via the moment, i.e., {@link #increment(double)} * does nothing; whereas {@code m2.increment(value)} increments * both {@code m2} and the Variance instance constructed from it. * - * @param m2 the SecondMoment (Third or Fourth moments work - * here as well.) + * @param m2 the SecondMoment (Third or Fourth moments work here as well.) */ public Variance(final SecondMoment m2) { incMoment = false; @@ -115,7 +115,7 @@ public class Variance extends AbstractStorelessUnivariateStatistic implements Se /** * Constructs a Variance with the specified isBiasCorrected - * property + * property. * * @param isBiasCorrected setting for bias correction - true means * bias will be corrected and is equivalent to using the argumentless @@ -143,7 +143,7 @@ public class Variance extends AbstractStorelessUnivariateStatistic implements Se /** * Copy constructor, creates a new {@code Variance} identical - * to the {@code original} + * to the {@code original}. * * @param original the {@code Variance} instance to copy * @throws NullArgumentException if original is null @@ -177,17 +177,17 @@ public class Variance extends AbstractStorelessUnivariateStatistic implements Se */ @Override public double getResult() { - if (moment.n == 0) { - return Double.NaN; - } else if (moment.n == 1) { - return 0d; + if (moment.n == 0) { + return Double.NaN; + } else if (moment.n == 1) { + return 0d; + } else { + if (isBiasCorrected) { + return moment.m2 / (moment.n - 1d); } else { - if (isBiasCorrected) { - return moment.m2 / (moment.n - 1d); - } else { - return moment.m2 / (moment.n); - } + return moment.m2 / (moment.n); } + } } /** @@ -255,12 +255,11 @@ public class Variance extends AbstractStorelessUnivariateStatistic implements Se */ @Override public double evaluate(final double[] values, final int begin, final int length) - throws MathIllegalArgumentException { + throws MathIllegalArgumentException { double var = Double.NaN; - if (test(values, begin, length)) { - clear(); + if (MathArrays.verifyValues(values, begin, length)) { if (length == 1) { var = 0.0; } else if (length > 1) { @@ -320,8 +319,7 @@ public class Variance extends AbstractStorelessUnivariateStatistic implements Se double var = Double.NaN; - if (test(values, weights,begin, length)) { - clear(); + if (MathArrays.verifyValues(values, weights,begin, length)) { if (length == 1) { var = 0.0; } else if (length > 1) { @@ -373,7 +371,7 @@ public class Variance extends AbstractStorelessUnivariateStatistic implements Se */ @Override public double evaluate(final double[] values, final double[] weights) - throws MathIllegalArgumentException { + throws MathIllegalArgumentException { return evaluate(values, weights, 0, values.length); } @@ -404,11 +402,11 @@ public class Variance extends AbstractStorelessUnivariateStatistic implements Se * parameters are not valid */ public double evaluate(final double[] values, final double mean, - final int begin, final int length) throws MathIllegalArgumentException { + final int begin, final int length) throws MathIllegalArgumentException { double var = Double.NaN; - if (test(values, begin, length)) { + if (MathArrays.verifyValues(values, begin, length)) { if (length == 1) { var = 0.0; } else if (length > 1) { @@ -506,12 +504,12 @@ public class Variance extends AbstractStorelessUnivariateStatistic implements Se * @since 2.1 */ public double evaluate(final double[] values, final double[] weights, - final double mean, final int begin, final int length) - throws MathIllegalArgumentException { + final double mean, final int begin, final int length) + throws MathIllegalArgumentException { double var = Double.NaN; - if (test(values, weights, begin, length)) { + if (MathArrays.verifyValues(values, weights, begin, length)) { if (length == 1) { var = 0.0; } else if (length > 1) { @@ -581,7 +579,7 @@ public class Variance extends AbstractStorelessUnivariateStatistic implements Se * @since 2.1 */ public double evaluate(final double[] values, final double[] weights, final double mean) - throws MathIllegalArgumentException { + throws MathIllegalArgumentException { return evaluate(values, weights, mean, 0, values.length); } @@ -622,7 +620,6 @@ public class Variance extends AbstractStorelessUnivariateStatistic implements Se throws NullArgumentException { MathUtils.checkNotNull(source); MathUtils.checkNotNull(dest); - dest.setData(source.getDataRef()); dest.moment = source.moment.copy(); dest.isBiasCorrected = source.isBiasCorrected; dest.incMoment = source.incMoment; http://git-wip-us.apache.org/repos/asf/commons-math/blob/845e1d54/src/main/java/org/apache/commons/math4/stat/descriptive/rank/Max.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/stat/descriptive/rank/Max.java b/src/main/java/org/apache/commons/math4/stat/descriptive/rank/Max.java index 60efe1b..502d2d4 100644 --- a/src/main/java/org/apache/commons/math4/stat/descriptive/rank/Max.java +++ b/src/main/java/org/apache/commons/math4/stat/descriptive/rank/Max.java @@ -21,6 +21,7 @@ import java.io.Serializable; import org.apache.commons.math4.exception.MathIllegalArgumentException; import org.apache.commons.math4.exception.NullArgumentException; import org.apache.commons.math4.stat.descriptive.AbstractStorelessUnivariateStatistic; +import org.apache.commons.math4.util.MathArrays; import org.apache.commons.math4.util.MathUtils; /** @@ -37,12 +38,11 @@ import org.apache.commons.math4.util.MathUtils; * multiple threads access an instance of this class concurrently, and at least * one of the threads invokes the increment() or * clear() method, it must be synchronized externally.

- * */ public class Max extends AbstractStorelessUnivariateStatistic implements Serializable { /** Serializable version identifier */ - private static final long serialVersionUID = -5593383832225844641L; + private static final long serialVersionUID = 20150412L; /** Number of values that have been added */ private long n; @@ -51,7 +51,7 @@ public class Max extends AbstractStorelessUnivariateStatistic implements Seriali private double value; /** - * Create a Max instance + * Create a Max instance. */ public Max() { n = 0; @@ -60,7 +60,7 @@ public class Max extends AbstractStorelessUnivariateStatistic implements Seriali /** * Copy constructor, creates a new {@code Max} identical - * to the {@code original} + * to the {@code original}. * * @param original the {@code Max} instance to copy * @throws NullArgumentException if original is null @@ -129,9 +129,10 @@ public class Max extends AbstractStorelessUnivariateStatistic implements Seriali */ @Override public double evaluate(final double[] values, final int begin, final int length) - throws MathIllegalArgumentException { + throws MathIllegalArgumentException { + double max = Double.NaN; - if (test(values, begin, length)) { + if (MathArrays.verifyValues(values, begin, length)) { max = values[begin]; for (int i = begin; i < begin + length; i++) { if (!Double.isNaN(values[i])) { @@ -165,7 +166,6 @@ public class Max extends AbstractStorelessUnivariateStatistic implements Seriali throws NullArgumentException { MathUtils.checkNotNull(source); MathUtils.checkNotNull(dest); - dest.setData(source.getDataRef()); dest.n = source.n; dest.value = source.value; } http://git-wip-us.apache.org/repos/asf/commons-math/blob/845e1d54/src/main/java/org/apache/commons/math4/stat/descriptive/rank/Median.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/stat/descriptive/rank/Median.java b/src/main/java/org/apache/commons/math4/stat/descriptive/rank/Median.java index 4aa10bf..d2a62cc 100644 --- a/src/main/java/org/apache/commons/math4/stat/descriptive/rank/Median.java +++ b/src/main/java/org/apache/commons/math4/stat/descriptive/rank/Median.java @@ -32,12 +32,11 @@ import org.apache.commons.math4.util.KthSelector; * multiple threads access an instance of this class concurrently, and at least * one of the threads invokes the increment() or * clear() method, it must be synchronized externally.

- * */ public class Median extends Percentile implements Serializable { /** Serializable version identifier */ - private static final long serialVersionUID = -3961477041290915687L; + private static final long serialVersionUID = 20150412L; /** Fixed quantile. */ private static final double FIXED_QUANTILE_50 = 50.0; http://git-wip-us.apache.org/repos/asf/commons-math/blob/845e1d54/src/main/java/org/apache/commons/math4/stat/descriptive/rank/Min.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/stat/descriptive/rank/Min.java b/src/main/java/org/apache/commons/math4/stat/descriptive/rank/Min.java index 0152e48..72eaef4 100644 --- a/src/main/java/org/apache/commons/math4/stat/descriptive/rank/Min.java +++ b/src/main/java/org/apache/commons/math4/stat/descriptive/rank/Min.java @@ -21,6 +21,7 @@ import java.io.Serializable; import org.apache.commons.math4.exception.MathIllegalArgumentException; import org.apache.commons.math4.exception.NullArgumentException; import org.apache.commons.math4.stat.descriptive.AbstractStorelessUnivariateStatistic; +import org.apache.commons.math4.util.MathArrays; import org.apache.commons.math4.util.MathUtils; /** @@ -37,12 +38,11 @@ import org.apache.commons.math4.util.MathUtils; * multiple threads access an instance of this class concurrently, and at least * one of the threads invokes the increment() or * clear() method, it must be synchronized externally.

- * */ public class Min extends AbstractStorelessUnivariateStatistic implements Serializable { /** Serializable version identifier */ - private static final long serialVersionUID = -2941995784909003131L; + private static final long serialVersionUID = 20150412L; /**Number of values that have been added */ private long n; @@ -51,7 +51,7 @@ public class Min extends AbstractStorelessUnivariateStatistic implements Seriali private double value; /** - * Create a Min instance + * Create a Min instance. */ public Min() { n = 0; @@ -60,7 +60,7 @@ public class Min extends AbstractStorelessUnivariateStatistic implements Seriali /** * Copy constructor, creates a new {@code Min} identical - * to the {@code original} + * to the {@code original}. * * @param original the {@code Min} instance to copy * @throws NullArgumentException if original is null @@ -129,9 +129,10 @@ public class Min extends AbstractStorelessUnivariateStatistic implements Seriali */ @Override public double evaluate(final double[] values,final int begin, final int length) - throws MathIllegalArgumentException { + throws MathIllegalArgumentException { + double min = Double.NaN; - if (test(values, begin, length)) { + if (MathArrays.verifyValues(values, begin, length)) { min = values[begin]; for (int i = begin; i < begin + length; i++) { if (!Double.isNaN(values[i])) { @@ -165,7 +166,6 @@ public class Min extends AbstractStorelessUnivariateStatistic implements Seriali throws NullArgumentException { MathUtils.checkNotNull(source); MathUtils.checkNotNull(dest); - dest.setData(source.getDataRef()); dest.n = source.n; dest.value = source.value; }