001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.math.stat.descriptive;
018    
019    import org.apache.commons.math.MathRuntimeException;
020    
021    /**
022     * Abstract base class for all implementations of the
023     * {@link UnivariateStatistic} interface.
024     * <p>
025     * Provides a default implementation of <code>evaluate(double[]),</code>
026     * delegating to <code>evaluate(double[], int, int)</code> in the natural way.
027     * </p>
028     * <p>
029     * Also includes a <code>test</code> method that performs generic parameter
030     * validation for the <code>evaluate</code> methods.</p>
031     *
032     * @version $Revision: 894705 $ $Date: 2009-12-30 15:24:54 -0500 (Wed, 30 Dec 2009) $
033     */
034    public abstract class AbstractUnivariateStatistic
035        implements UnivariateStatistic {
036    
037        /**
038         * {@inheritDoc}
039         */
040        public double evaluate(final double[] values) {
041            test(values, 0, 0);
042            return evaluate(values, 0, values.length);
043        }
044    
045        /**
046         * {@inheritDoc}
047         */
048        public abstract double evaluate(final double[] values, final int begin, final int length);
049    
050        /**
051         * {@inheritDoc}
052         */
053        public abstract UnivariateStatistic copy();
054    
055        /**
056         * This method is used by <code>evaluate(double[], int, int)</code> methods
057         * to verify that the input parameters designate a subarray of positive length.
058         * <p>
059         * <ul>
060         * <li>returns <code>true</code> iff the parameters designate a subarray of
061         * positive length</li>
062         * <li>throws <code>IllegalArgumentException</code> if the array is null or
063         * or the indices are invalid</li>
064         * <li>returns <code>false</li> if the array is non-null, but
065         * <code>length</code> is 0.
066         * </ul></p>
067         *
068         * @param values the input array
069         * @param begin index of the first array element to include
070         * @param length the number of elements to include
071         * @return true if the parameters are valid and designate a subarray of positive length
072         * @throws IllegalArgumentException if the indices are invalid or the array is null
073         */
074        protected boolean test(
075            final double[] values,
076            final int begin,
077            final int length) {
078    
079            if (values == null) {
080                throw MathRuntimeException.createIllegalArgumentException("input values array is null");
081            }
082    
083            if (begin < 0) {
084                throw MathRuntimeException.createIllegalArgumentException(
085                      "start position cannot be negative ({0})", begin);
086            }
087    
088            if (length < 0) {
089                throw MathRuntimeException.createIllegalArgumentException(
090                      "length cannot be negative ({0})", length);
091            }
092    
093            if (begin + length > values.length) {
094                throw MathRuntimeException.createIllegalArgumentException(
095                      "subarray ends after array end");
096            }
097    
098            if (length == 0) {
099                return false;
100            }
101    
102            return true;
103    
104        }
105    
106        /**
107         * This method is used by <code>evaluate(double[], double[], int, int)</code> methods
108         * to verify that the begin and length parameters designate a subarray of positive length
109         * and the weights are all non-negative, non-NaN, finite, and not all zero.
110         * <p>
111         * <ul>
112         * <li>returns <code>true</code> iff the parameters designate a subarray of
113         * positive length and the weights array contains legitimate values.</li>
114         * <li>throws <code>IllegalArgumentException</code> if any of the following are true:
115         * <ul><li>the values array is null</li>
116         *     <li>the weights array is null</li>
117         *     <li>the weights array does not have the same length as the values array</li>
118         *     <li>the weights array contains one or more infinite values</li>
119         *     <li>the weights array contains one or more NaN values</li>
120         *     <li>the weights array contains negative values</li>
121         *     <li>the start and length arguments do not determine a valid array</li></ul>
122         * </li>
123         * <li>returns <code>false</li> if the array is non-null, but
124         * <code>length</code> is 0.
125         * </ul></p>
126         *
127         * @param values the input array
128         * @param weights the weights array
129         * @param begin index of the first array element to include
130         * @param length the number of elements to include
131         * @return true if the parameters are valid and designate a subarray of positive length
132         * @throws IllegalArgumentException if the indices are invalid or the array is null
133         * @since 2.1
134         */
135        protected boolean test(
136            final double[] values,
137            final double[] weights,
138            final int begin,
139            final int length) {
140    
141            if (weights == null) {
142                throw MathRuntimeException.createIllegalArgumentException("input weights array is null");
143            }
144    
145            if (weights.length !=  values.length) {
146                throw MathRuntimeException.createIllegalArgumentException(
147                      "Different number of weights and values");
148            }
149    
150            boolean containsPositiveWeight = false;
151            for (int i = begin; i < begin + length; i++) {
152                if (Double.isNaN(weights[i])) {
153                    throw MathRuntimeException.createIllegalArgumentException(
154                            "NaN weight at index {0}", i);
155                }
156                if (Double.isInfinite(weights[i])) {
157                    throw MathRuntimeException.createIllegalArgumentException(
158                            "Infinite weight at index {0}", i);
159                }
160                if (weights[i] < 0) {
161                    throw MathRuntimeException.createIllegalArgumentException(
162                          "negative weight {0} at index {1} ", weights[i], i);
163                }
164                if (!containsPositiveWeight && weights[i] > 0.0) {
165                    containsPositiveWeight = true;
166                }
167            }
168    
169            if (!containsPositiveWeight) {
170                throw MathRuntimeException.createIllegalArgumentException(
171                        "weight array must contain at least one non-zero value");
172            }
173    
174            return test(values, begin, length);
175        }
176    }
177