001/*
002 * Java Genetic Algorithm Library (jenetics-8.0.0).
003 * Copyright (c) 2007-2024 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 */
020package io.jenetics.stat;
021
022import static java.util.Objects.requireNonNull;
023
024import java.util.function.IntConsumer;
025import java.util.function.LongConsumer;
026import java.util.function.ToLongFunction;
027import 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 * {@snippet lang="java":
037 * final LongStream stream = null; // @replace substring='null' replacement="..."
038 * final LongMomentStatistics statistics = stream.collect(
039 *         LongMomentStatistics::new,
040 *         LongMomentStatistics::accept,
041 *         LongMomentStatistics::combine
042 *     );
043 * }
044 *
045 * For a non-long stream, you can use a collector:
046 * {@snippet lang="java":
047 * final Stream<SomeObject> stream = null; // @replace substring='null' replacement="..."
048 * final LongMomentStatistics statistics = stream
049 *     .collect(toLongMomentStatistics(v -> v.longValue()));
050 * }
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 6.0
067 */
068public 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 moment 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 moment 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 maximum value recorded, or {@code Long.MIN_VALUE} if no
136         * values have been recorded.
137         *
138         * @return the maximum value, or {@code Long.MIN_VALUE} if none
139         */
140        public long max() {
141                return _max;
142        }
143
144        /**
145         * Return the sum of values recorded, or zero if no values have been
146         * recorded.
147         *
148         * @return the sum of values, or zero if none
149         */
150        public long sum() {
151                return _sum;
152        }
153
154        /**
155         * Compares the state of two {@code LongMomentStatistics} objects. This is
156         * a replacement for the {@link #equals(Object)} which is not advisable to
157         * implement for this mutable object. If two objects have the same state, it
158         * has still the same state when updated with the same value.
159         * {@snippet lang="java":
160         * final LongMomentStatistics lms1 = null; // @replace substring='null' replacement="..."
161         * final LongMomentStatistics lms2 = null; // @replace substring='null' replacement="..."
162         *
163         * if (lms1.sameState(lms2)) {
164         *     final long value = random.nextInt(1_000_000);
165         *     lms1.accept(value);
166         *     lms2.accept(value);
167         *
168         *     assert lms1.sameState(lms2);
169         *     assert lms2.sameState(lms1);
170         *     assert lms1.sameState(lms1);
171         * }
172         * }
173         *
174         * @since 3.7
175         *
176         * @param other the other object for the test
177         * @return {@code true} the {@code this} and the {@code other} objects have
178         *         the same state, {@code false} otherwise
179         */
180        public boolean sameState(final LongMomentStatistics other) {
181                return this == other ||
182                        _min == other._min &&
183                        _max == other._max &&
184                        _sum == other._sum &&
185                        super.sameState(other);
186        }
187
188        /**
189         * Return a {@code LongMoments} object from the current statistics,
190         *
191         * @since 3.9
192         *
193         * @return a {@code LongMoments} object from the current statistics
194         */
195        public LongMoments toLongMoments() {
196                return LongMoments.of(this);
197        }
198
199        @Override
200        public String toString() {
201                return String.format(
202                        "LongMomentStatistics[N=%d, ∧=%s, ∨=%s, Σ=%s, μ=%s, s²=%s, S=%s, K=%s]",
203                        count(), min(), max(), sum(),
204                        mean(), variance(), skewness(), kurtosis()
205                );
206        }
207
208        /**
209         * Return a {@code Collector} which applies a long-producing mapping
210         * function to each input element, and returns moments-statistics for the
211         * resulting values.
212         *
213         * {@snippet lang="java":
214         * final Stream<SomeObject> stream = null; // @replace substring='null' replacement="..."
215         * final LongMomentStatistics statistics = stream
216         *     .collect(toLongMomentStatistics(v -> v.longValue()));
217         * }
218         *
219         * @param mapper a mapping function to apply to each element
220         * @param <T> the type of the input elements
221         * @return a {@code Collector} implementing the moments-statistics reduction
222         * @throws java.lang.NullPointerException if the given {@code mapper} is
223         *         {@code null}
224         */
225        public static <T> Collector<T, ?, LongMomentStatistics>
226        toLongMomentStatistics(final ToLongFunction<? super T> mapper) {
227                requireNonNull(mapper);
228                return Collector.of(
229                        LongMomentStatistics::new,
230                        (r, t) -> r.accept(mapper.applyAsLong(t)),
231                        LongMomentStatistics::combine
232                );
233        }
234
235}