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