DoubleMomentStatistics.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-5.2.0).
003  * Copyright (c) 2007-2020 Franz Wilhelmstötter
004  *
005  * Licensed under the Apache License, Version 2.0 (the "License");
006  * you may not use this file except in compliance with the License.
007  * 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  * Author:
018  *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
019  */
020 package io.jenetics.stat;
021 
022 import static java.util.Objects.requireNonNull;
023 
024 import java.util.function.DoubleConsumer;
025 import java.util.function.ToDoubleFunction;
026 import java.util.stream.Collector;
027 
028 import io.jenetics.internal.math.DoubleAdder;
029 
030 /**
031  * A state object for collecting statistics such as count, min, max, sum, mean,
032  * variance, skewness and kurtosis. The design of this class is similar to the
033  {@link java.util.DoubleSummaryStatistics} class.
034  <p>
035  * This class is designed to work with (though does not require) streams. For
036  * example, you can compute moments-statistics on a stream of doubles with:
037  <pre>{@code
038  * final DoubleStream stream = ...
039  * final DoubleMomentStatistics statistics = stream.collect(
040  *         DoubleMomentStatistics::new,
041  *         DoubleMomentStatistics::accept,
042  *         DoubleMomentStatistics::combine
043  *     );
044  * }</pre>
045  *
046  * For a non double stream, you can use a collector:
047  <pre>{@code
048  * final Stream<SomeObject> stream = ...
049  * final DoubleMomentStatistics statistics = stream
050  *     .collect(toDoubleMomentStatistics(v -> v.doubleValue()));
051  * }</pre>
052  *
053  * @implNote
054  * This implementation is not thread safe. However, it is safe to use
055  {@link #toDoubleMomentStatistics(ToDoubleFunction)}  on a parallel stream,
056  * because the parallel implementation of
057  {@link java.util.stream.Stream#collect Stream.collect()}
058  * provides the necessary partitioning, isolation, and merging of results for
059  * safe and efficient parallel execution.
060  *
061  @see java.util.DoubleSummaryStatistics
062  @see io.jenetics.stat.DoubleMoments
063  @see <a href="http://people.xiph.org/~tterribe/notes/homs.html">
064  *      Computing Higher-Order Moments Online</a>
065  *
066  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
067  @since 3.0
068  @version 5.2
069  */
070 public class DoubleMomentStatistics
071     extends MomentStatistics
072     implements DoubleConsumer
073 {
074 
075     private double _min = Double.POSITIVE_INFINITY;
076     private double _max = Double.NEGATIVE_INFINITY;
077 
078     private final DoubleAdder _sum = new DoubleAdder();
079 
080     /**
081      * Create an empty moments object.
082      */
083     public DoubleMomentStatistics() {
084     }
085 
086     /**
087      * Records a new value into the moments information
088      *
089      @param value the input {@code value}
090      */
091     @Override
092     public void accept(final double value) {
093         super.accept(value);
094         _min = Math.min(_min, value);
095         _max = Math.max(_max, value);
096         _sum.add(value);
097     }
098 
099     /**
100      * Combine two {@code DoubleMoments} statistic objects.
101      *
102      @param other the other {@code DoubleMoments} statistics to combine with
103      *        {@code this} one.
104      @return {@code this} statistics object
105      @throws java.lang.NullPointerException if the other statistical summary
106      *         is {@code null}.
107      */
108     public DoubleMomentStatistics combine(final DoubleMomentStatistics other) {
109         super.combine(other);
110         _min = Math.min(_min, other._min);
111         _max = Math.max(_max, other._max);
112         _sum.add(other._sum);
113 
114         return this;
115     }
116 
117     /**
118      * Return the minimum value recorded, or {@code Double.POSITIVE_INFINITY} if
119      * no values have been recorded.
120      *
121      @return the minimum value, or {@code Double.POSITIVE_INFINITY} if none
122      */
123     public double min() {
124         return _min;
125     }
126 
127     /**
128      * Return the minimum value recorded, or {@code Double.POSITIVE_INFINITY} if
129      * no values have been recorded.
130      *
131      @return the minimum value, or {@code Double.POSITIVE_INFINITY} if none
132      @deprecated Use {@link #min()} instead
133      */
134     @Deprecated
135     public double getMin() {
136         return _min;
137     }
138 
139     /**
140      * Return the maximum value recorded, or {@code Double.NEGATIVE_INFINITY} if
141      * no values have been recorded.
142      *
143      @return the maximum value, or {@code Double.NEGATIVE_INFINITY} if none
144      */
145     public double max() {
146         return _max;
147     }
148 
149     /**
150      * Return the maximum value recorded, or {@code Double.NEGATIVE_INFINITY} if
151      * no values have been recorded.
152      *
153      @return the maximum value, or {@code Double.NEGATIVE_INFINITY} if none
154      @deprecated Use {@link #max()} instead
155      */
156     @Deprecated
157     public double getMax() {
158         return _max;
159     }
160 
161     /**
162      * Return the sum of values recorded, or zero if no values have been
163      * recorded.
164      *
165      @return the sum of values, or zero if none
166      */
167     public double sum() {
168         return _sum.doubleValue();
169     }
170 
171     /**
172      * Return the sum of values recorded, or zero if no values have been
173      * recorded.
174      *
175      @return the sum of values, or zero if none
176      @deprecated Use {@link #sum()} instead
177      */
178     @Deprecated
179     public double getSum() {
180         return _sum.doubleValue();
181     }
182 
183     /**
184      * Compares the state of two {@code DoubleMomentStatistics} objects. This is
185      * a replacement for the {@link #equals(Object)} which is not advisable to
186      * implement for this mutable object. If two object have the same state, it
187      * has still the same state when updated with the same value.
188      <pre>{@code
189      * final DoubleMomentStatistics ds1 = ...;
190      * final DoubleMomentStatistics ds2 = ...;
191      *
192      * if (ds1.sameState(ds2)) {
193      *     final double value = random.nextDouble();
194      *     ds1.accept(value);
195      *     ds2.accept(value);
196      *
197      *     assert ds1.sameState(ds2);
198      *     assert ds2.sameState(ds1);
199      *     assert ds1.sameState(ds1);
200      * }
201      * }</pre>
202      *
203      @since 3.7
204      *
205      @param other the other object for the test
206      @return {@code true} the {@code this} and the {@code other} objects have
207      *         the same state, {@code false} otherwise
208      */
209     public boolean sameState(final DoubleMomentStatistics other) {
210         return this == other ||
211             Double.compare(_min, other._min== &&
212             Double.compare(_max, other._max== &&
213             _sum.sameState(other._sum&&
214             super.sameState(other);
215     }
216 
217     /**
218      * Return a {@code DoubleMoments} object from the current statistics,
219      *
220      @since 3.9
221      *
222      @return a {@code DoubleMoments} object from the current statistics
223      */
224     public DoubleMoments toDoubleMoments() {
225         return DoubleMoments.of(this);
226     }
227 
228     @Override
229     public String toString() {
230         return String.format(
231             "Summary[N=%d, ∧=%s, ∨=%s, Σ=%s, μ=%s, s²=%s, S=%s, K=%s]",
232             count(), _min, _max, _sum.doubleValue(),
233             mean(), variance(), skewness(), kurtosis()
234         );
235     }
236 
237     /**
238      * Return a {@code Collector} which applies an double-producing mapping
239      * function to each input element, and returns moments-statistics for the
240      * resulting values.
241      *
242      <pre>{@code
243      * final Stream<SomeObject> stream = ...
244      * final DoubleMomentStatistics statistics = stream
245      *     .collect(toDoubleMomentStatistics(v -> v.doubleValue()));
246      * }</pre>
247      *
248      @param mapper a mapping function to apply to each element
249      @param <T> the type of the input elements
250      @return a {@code Collector} implementing the moments-statistics reduction
251      @throws java.lang.NullPointerException if the given {@code mapper} is
252      *         {@code null}
253      */
254     public static <T> Collector<T, ?, DoubleMomentStatistics>
255     toDoubleMomentStatistics(final ToDoubleFunction<? super T> mapper) {
256         requireNonNull(mapper);
257         return Collector.of(
258             DoubleMomentStatistics::new,
259             (r, t-> r.accept(mapper.applyAsDouble(t)),
260             DoubleMomentStatistics::combine
261         );
262     }
263 
264 }