001 /*
002 * Java Genetic Algorithm Library (jenetics-4.0.0).
003 * Copyright (c) 2007-2017 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 * <p>
054 * <b>Implementation note:</b>
055 * <i>This implementation is not thread safe. However, it is safe to use
056 * {@link #toDoubleMomentStatistics(ToDoubleFunction)} on a parallel stream,
057 * because the parallel implementation of
058 * {@link java.util.stream.Stream#collect Stream.collect()}
059 * provides the necessary partitioning, isolation, and merging of results for
060 * safe and efficient parallel execution.</i>
061 *
062 * @see java.util.DoubleSummaryStatistics
063 * @see io.jenetics.stat.DoubleMoments
064 * @see <a href="http://people.xiph.org/~tterribe/notes/homs.html">
065 * Computing Higher-Order Moments Online</a>
066 *
067 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
068 * @since 3.0
069 * @version 4.0
070 */
071 public class DoubleMomentStatistics
072 extends MomentStatistics
073 implements DoubleConsumer
074 {
075
076 private double _min = Double.POSITIVE_INFINITY;
077 private double _max = Double.NEGATIVE_INFINITY;
078
079 private final DoubleAdder _sum = new DoubleAdder();
080
081 /**
082 * Create an empty moments object.
083 */
084 public DoubleMomentStatistics() {
085 }
086
087 /**
088 * Records a new value into the moments information
089 *
090 * @param value the input {@code value}
091 */
092 @Override
093 public void accept(final double value) {
094 super.accept(value);
095 _min = Math.min(_min, value);
096 _max = Math.max(_max, value);
097 _sum.add(value);
098 }
099
100 /**
101 * Combine two {@code DoubleMoments} statistic objects.
102 *
103 * @param other the other {@code DoubleMoments} statistics to combine with
104 * {@code this} one.
105 * @return {@code this} statistics object
106 * @throws java.lang.NullPointerException if the other statistical summary
107 * is {@code null}.
108 */
109 public DoubleMomentStatistics combine(final DoubleMomentStatistics other) {
110 super.combine(other);
111 _min = Math.min(_min, other._min);
112 _max = Math.max(_max, other._max);
113 _sum.add(other._sum);
114
115 return this;
116 }
117
118 /**
119 * Return the minimum value recorded, or {@code Double.POSITIVE_INFINITY} if
120 * no values have been recorded.
121 *
122 * @return the minimum value, or {@code Double.POSITIVE_INFINITY} if none
123 */
124 public double getMin() {
125 return _min;
126 }
127
128 /**
129 * Return the maximum value recorded, or {@code Double.NEGATIVE_INFINITY} if
130 * no values have been recorded.
131 *
132 * @return the maximum value, or {@code Double.NEGATIVE_INFINITY} if none
133 */
134 public double getMax() {
135 return _max;
136 }
137
138 /**
139 * Return the sum of values recorded, or zero if no values have been
140 * recorded.
141 *
142 * @return the sum of values, or zero if none
143 */
144 public double getSum() {
145 return _sum.doubleValue();
146 }
147
148 /**
149 * Compares the state of two {@code DoubleMomentStatistics} objects. This is
150 * a replacement for the {@link #equals(Object)} which is not advisable to
151 * implement for this mutable object. If two object have the same state, it
152 * has still the same state when updated with the same value.
153 * <pre>{@code
154 * final DoubleMomentStatistics ds1 = ...;
155 * final DoubleMomentStatistics ds2 = ...;
156 *
157 * if (ds1.sameState(ds2)) {
158 * final double value = random.nextDouble();
159 * ds1.accept(value);
160 * ds2.accept(value);
161 *
162 * assert ds1.sameState(ds2);
163 * assert ds2.sameState(ds1);
164 * assert ds1.sameState(ds1);
165 * }
166 * }</pre>
167 *
168 * @since 3.7
169 *
170 * @param other the other object for the test
171 * @return {@code true} the {@code this} and the {@code other} objects have
172 * the same state, {@code false} otherwise
173 */
174 public boolean sameState(final DoubleMomentStatistics other) {
175 return Double.compare(_min, other._min) == 0 &&
176 Double.compare(_max, other._max) == 0 &&
177 _sum.sameState(other._sum) &&
178 super.sameState(other);
179 }
180
181 /**
182 * Return a {@code DoubleMoments} object from the current statistics,
183 *
184 * @since 3.9
185 *
186 * @return a {@code DoubleMoments} object from the current statistics
187 */
188 public DoubleMoments toDoubleMoments() {
189 return DoubleMoments.of(this);
190 }
191
192 @Override
193 public String toString() {
194 return String.format(
195 "Summary[N=%d, ∧=%s, ∨=%s, Σ=%s, μ=%s, s²=%s, S=%s, K=%s]",
196 getCount(), _min, _max, _sum.doubleValue(),
197 getMean(), getVariance(), getSkewness(), getKurtosis()
198 );
199 }
200
201 /**
202 * Return a {@code Collector} which applies an double-producing mapping
203 * function to each input element, and returns moments-statistics for the
204 * resulting values.
205 *
206 * <pre>{@code
207 * final Stream<SomeObject> stream = ...
208 * final DoubleMomentStatistics statistics = stream
209 * .collect(toDoubleMomentStatistics(v -> v.doubleValue()));
210 * }</pre>
211 *
212 * @param mapper a mapping function to apply to each element
213 * @param <T> the type of the input elements
214 * @return a {@code Collector} implementing the moments-statistics reduction
215 * @throws java.lang.NullPointerException if the given {@code mapper} is
216 * {@code null}
217 */
218 public static <T> Collector<T, ?, DoubleMomentStatistics>
219 toDoubleMomentStatistics(final ToDoubleFunction<? super T> mapper) {
220 requireNonNull(mapper);
221 return Collector.of(
222 DoubleMomentStatistics::new,
223 (r, t) -> r.accept(mapper.applyAsDouble(t)),
224 DoubleMomentStatistics::combine
225 );
226 }
227
228 }
|