StorelessUnivariateStatistic.java

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

  17. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.stat.descriptive;

  22. import java.util.function.DoubleConsumer;

  23. import org.hipparchus.exception.LocalizedCoreFormats;
  24. import org.hipparchus.exception.MathIllegalArgumentException;
  25. import org.hipparchus.util.MathArrays;
  26. import org.hipparchus.util.MathUtils;

  27. /**
  28.  * Extends the definition of {@link UnivariateStatistic} with
  29.  * {@link #increment} and {@link #incrementAll(double[])} methods for adding
  30.  * values and updating internal state.
  31.  * <p>
  32.  * This interface is designed to be used for calculating statistics that can be
  33.  * computed in one pass through the data without storing the full array of
  34.  * sample values.
  35.  * <p>
  36.  * Note: unless otherwise stated, the {@link #evaluate(double[])} and
  37.  * {@link #evaluate(double[], int, int)} methods do <b>NOT</b> alter the internal
  38.  * state of the respective statistic.
  39.  */
  40. public interface StorelessUnivariateStatistic
  41.     extends UnivariateStatistic, DoubleConsumer {

  42.     /**
  43.      * {@inheritDoc}
  44.      * <p>
  45.      * The default implementation creates a copy of this {@link StorelessUnivariateStatistic}
  46.      * instance, calls {@link #clear} on it, then calls {@link #incrementAll} with the specified
  47.      * portion of the input array, and then uses {@link #getResult} to compute the return value.
  48.      * <p>
  49.      * Note that this implementation does not change the internal state of the statistic.
  50.      * <p>
  51.      * Implementations may override this method with a more efficient and possibly more
  52.      * accurate implementation that works directly with the input array.
  53.      *
  54.      * @param values the input array
  55.      * @param begin the index of the first element to include
  56.      * @param length the number of elements to include
  57.      * @return the value of the statistic applied to the included array entries
  58.      * @throws MathIllegalArgumentException if the array is null or the indices are not valid
  59.      * @see UnivariateStatistic#evaluate(double[], int, int)
  60.      */
  61.     @Override
  62.     default double evaluate(final double[] values, final int begin, final int length)
  63.         throws MathIllegalArgumentException {

  64.         if (MathArrays.verifyValues(values, begin, length)) {
  65.             StorelessUnivariateStatistic stat = copy();
  66.             stat.clear();
  67.             stat.incrementAll(values, begin, length);
  68.             return stat.getResult();
  69.         }
  70.         return Double.NaN;
  71.     }

  72.     /**
  73.      * Updates the internal state of the statistic to reflect the addition of the new value.
  74.      * @param d  the new value.
  75.      */
  76.     void increment(double d);

  77.     /** {@inheritDoc} */
  78.     @Override
  79.     default void accept(double value) {
  80.         increment(value);
  81.     }

  82.     /**
  83.      * Updates the internal state of the statistic to reflect addition of
  84.      * all values in the values array. Does not clear the statistic first --
  85.      * i.e., the values are added <strong>incrementally</strong> to the dataset.
  86.      * <p>
  87.      * The default implementation delegates to
  88.      * <code>incrementAll(double[], int, int)</code> in the natural way.
  89.      *
  90.      * @param values  array holding the new values to add
  91.      * @throws MathIllegalArgumentException if the array is null
  92.      */
  93.     default void incrementAll(double[] values) throws MathIllegalArgumentException {
  94.         MathUtils.checkNotNull(values, LocalizedCoreFormats.INPUT_ARRAY);
  95.         incrementAll(values, 0, values.length);
  96.     }


  97.     /**
  98.      * Updates the internal state of the statistic to reflect addition of
  99.      * the values in the designated portion of the values array.  Does not
  100.      * clear the statistic first -- i.e., the values are added
  101.      * <strong>incrementally</strong> to the dataset.
  102.      * <p>
  103.      * The default implementation just calls {@link #increment} in a loop over
  104.      * the specified portion of the input array.
  105.      *
  106.      * @param values  array holding the new values to add
  107.      * @param start  the array index of the first value to add
  108.      * @param length  the number of elements to add
  109.      * @throws MathIllegalArgumentException if the array is null or the index
  110.      */
  111.     default void incrementAll(double[] values, int start, int length)
  112.         throws MathIllegalArgumentException {

  113.         if (MathArrays.verifyValues(values, start, length)) {
  114.             int k = start + length;
  115.             for (int i = start; i < k; i++) {
  116.                 increment(values[i]);
  117.             }
  118.         }
  119.     }


  120.     /**
  121.      * Returns the current value of the Statistic.
  122.      * @return value of the statistic, <code>Double.NaN</code> if it
  123.      * has been cleared or just instantiated.
  124.      */
  125.     double getResult();

  126.     /**
  127.      * Returns the number of values that have been added.
  128.      * @return the number of values.
  129.      */
  130.     long getN();

  131.     /**
  132.      * Clears the internal state of the Statistic
  133.      */
  134.     void clear();

  135.     /**
  136.      * Returns a copy of the statistic with the same internal state.
  137.      *
  138.      * @return a copy of the statistic
  139.      */
  140.     @Override
  141.     StorelessUnivariateStatistic copy();

  142. }