IntMomentStatistics.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-3.7.0).
003  * Copyright (c) 2007-2016 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@gmx.at)
019  */
020 package org.jenetics.stat;
021 
022 import static java.lang.Math.max;
023 import static java.lang.Math.min;
024 import static java.util.Objects.requireNonNull;
025 
026 import java.util.function.IntConsumer;
027 import java.util.function.ToIntFunction;
028 import java.util.stream.Collector;
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  * design of the {@link java.util.IntSummaryStatistics} 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 ints with:
037  <pre>{@code
038  * final IntStream stream = ...
039  * final IntMomentStatistics statistics = stream.collect(
040  *         IntMomentStatistics::new,
041  *         IntMomentStatistics::accept,
042  *         IntMomentStatistics::combine
043  *     );
044  * }</pre>
045  *
046  * For a non int stream, you can use a collector:
047  <pre>{@code
048  * final Stream<SomeObject> stream = ...
049  * final IntMomentStatistics statistics = stream
050  *     .collect(toIntMomentStatistics(v -> v.intValue()));
051  * }</pre>
052  *
053  <p>
054  <b>Implementation note:</b>
055  <i>This implementation is not thread safe. However, it is safe to use
056  {@link #toIntMomentStatistics(ToIntFunction)}  on a parallel stream, because the parallel
057  * implementation of {@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.</i>
060  *
061  @see java.util.IntSummaryStatistics
062  @see org.jenetics.stat.IntMoments
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@gmx.at">Franz Wilhelmstötter</a>
067  @since 3.0
068  @version 3.7
069  */
070 public class IntMomentStatistics
071     extends MomentStatistics
072     implements IntConsumer
073 {
074 
075     private int _min = Integer.MAX_VALUE;
076     private int _max = Integer.MIN_VALUE;
077     private long _sum = 0L;
078 
079     /**
080      * Create an empty moments object.
081      */
082     public IntMomentStatistics() {
083     }
084 
085     /**
086      * Records a new value into the moments information
087      *
088      @param value the input {@code value}
089      */
090     @Override
091     public void accept(final int value) {
092         super.accept(value);
093         _min = min(_min, value);
094         _max = max(_max, value);
095         _sum += value;
096     }
097 
098     /**
099      * Combine two {@code IntMoments} statistic objects.
100      *
101      @param other the other {@code IntMoments} statistics to combine with
102      *        {@code this} one.
103      @return {@code this} statistics object
104      @throws java.lang.NullPointerException if the other statistical summary
105      *         is {@code null}.
106      */
107     public IntMomentStatistics combine(final IntMomentStatistics other) {
108         super.combine(other);
109         _min = min(_min, other._min);
110         _max = max(_max, other._max);
111         _sum += other._sum;
112 
113         return this;
114     }
115 
116     /**
117      * Return the minimum value recorded, or {@code Integer.MAX_VALUE} if no
118      * values have been recorded.
119      *
120      @return the minimum value, or {@code Integer.MAX_VALUE} if none
121      */
122     public int getMin() {
123         return _min;
124     }
125 
126     /**
127      * Return the maximum value recorded, or {@code Integer.MIN_VALUE} if no
128      * values have been recorded.
129      *
130      @return the maximum value, or {@code Integer.MIN_VALUE} if none
131      */
132     public int getMax() {
133         return _max;
134     }
135 
136     /**
137      * Return the sum of values recorded, or zero if no values have been
138      * recorded.
139      *
140      @return the sum of values, or zero if none
141      */
142     public long getSum() {
143         return _sum;
144     }
145 
146     /**
147      * Compares the state of two {@code IntMomentStatistics} objects. This is
148      * a replacement for the {@link #equals(Object)} which is not advisable to
149      * implement for this mutable object. If two object have the same state, it
150      * has still the same state when updated with the same value.
151      <pre>{@code
152      * final IntMomentStatistics ims1 = ...;
153      * final IntMomentStatistics ims2 = ...;
154      *
155      * if (ims1.sameState(ims2)) {
156      *     final int value = random.nextInt(1_000_000);
157      *     ims1.accept(value);
158      *     ims2.accept(value);
159      *
160      *     assert ims1.sameState(ims2);
161      *     assert ims2.sameState(ims1);
162      *     assert ims1.sameState(ims1);
163      * }
164      * }</pre>
165      *
166      @since 3.7
167      *
168      @param other the other object for the test
169      @return {@code true} the {@code this} and the {@code other} objects have
170      *         the same state, {@code false} otherwise
171      */
172     public boolean sameState(final IntMomentStatistics other) {
173         return _min == other._min &&
174             _max == other._max &&
175             _sum == other._sum &&
176             super.sameState(other);
177     }
178 
179     @Override
180     public String toString() {
181         return String.format(
182             "IntMomentStatistics[N=%d, ∧=%s, ∨=%s, Σ=%s, μ=%s, s²=%s, S=%s, K=%s]",
183             getCount(), getMin(), getMax(), getSum(),
184             getMean(), getVariance(), getSkewness(), getKurtosis()
185         );
186     }
187 
188     /**
189      * Return a {@code Collector} which applies an int-producing mapping
190      * function to each input element, and returns moments-statistics for the
191      * resulting values.
192      *
193      <pre>{@code
194      * final Stream<SomeObject> stream = ...
195      * final IntMomentStatistics statistics = stream
196      *     .collect(toIntMomentStatistics(v -> v.intValue()));
197      * }</pre>
198      *
199      @param mapper a mapping function to apply to each element
200      @param <T> the type of the input elements
201      @return a {@code Collector} implementing the moments-statistics reduction
202      @throws java.lang.NullPointerException if the given {@code mapper} is
203      *         {@code null}
204      */
205     public static <T> Collector<T, ?, IntMomentStatistics>
206     toIntMomentStatistics(final ToIntFunction<? super T> mapper) {
207         requireNonNull(mapper);
208         return Collector.of(
209             IntMomentStatistics::new,
210             (r, t-> r.accept(mapper.applyAsInt(t)),
211             IntMomentStatistics::combine
212         );
213     }
214 
215 }