DoubleMomentStatistics.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-3.5.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.DoubleConsumer;
027 import java.util.function.ToDoubleFunction;
028 import java.util.stream.Collector;
029 
030 import org.jenetics.internal.math.DoubleAdder;
031 
032 /**
033  * A state object for collecting statistics such as count, min, max, sum, mean,
034  * variance, skewness and kurtosis. The design of this class is similar to the
035  {@link java.util.DoubleSummaryStatistics} class.
036  <p>
037  * This class is designed to work with (though does not require) streams. For
038  * example, you can compute moments-statistics on a stream of doubles with:
039  <pre>{@code
040  * final DoubleStream stream = ...
041  * final DoubleMomentStatistics statistics = stream.collect(
042  *         DoubleMomentStatistics::new,
043  *         DoubleMomentStatistics::accept,
044  *         DoubleMomentStatistics::combine
045  *     );
046  * }</pre>
047  *
048  * For a non double stream, you can use a collector:
049  <pre>{@code
050  * final Stream<SomeObject> stream = ...
051  * final DoubleMomentStatistics statistics = stream
052  *     .collect(toDoubleMomentStatistics(v -> v.doubleValue()));
053  * }</pre>
054  *
055  <p>
056  <b>Implementation note:</b>
057  <i>This implementation is not thread safe. However, it is safe to use
058  {@link #toDoubleMomentStatistics(ToDoubleFunction)}  on a parallel stream,
059  * because the parallel implementation of
060  {@link java.util.stream.Stream#collect Stream.collect()}
061  * provides the necessary partitioning, isolation, and merging of results for
062  * safe and efficient parallel execution.</i>
063  *
064  @see java.util.DoubleSummaryStatistics
065  @see org.jenetics.stat.DoubleMoments
066  @see <a href="http://people.xiph.org/~tterribe/notes/homs.html">
067  *      Computing Higher-Order Moments Online</a>
068  *
069  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
070  @since 3.0
071  @version 3.0
072  */
073 public class DoubleMomentStatistics
074     extends MomentStatistics
075     implements DoubleConsumer
076 {
077 
078     private double _min = Double.POSITIVE_INFINITY;
079     private double _max = Double.NEGATIVE_INFINITY;
080 
081     private final DoubleAdder _sum = new DoubleAdder();
082 
083     /**
084      * Create an empty moments object.
085      */
086     public DoubleMomentStatistics() {
087     }
088 
089     /**
090      * Records a new value into the moments information
091      *
092      @param value the input {@code value}
093      */
094     @Override
095     public void accept(final double value) {
096         super.accept(value);
097         _min = min(_min, value);
098         _max = max(_max, value);
099         _sum.add(value);
100     }
101 
102     /**
103      * Combine two {@code DoubleMoments} statistic objects.
104      *
105      @param other the other {@code DoubleMoments} statistics to combine with
106      *        {@code this} one.
107      @return {@code this} statistics object
108      @throws java.lang.NullPointerException if the other statistical summary
109      *         is {@code null}.
110      */
111     public DoubleMomentStatistics combine(final DoubleMomentStatistics other) {
112         super.combine(other);
113         _min = min(_min, other._min);
114         _max = max(_max, other._max);
115         _sum.add(other._sum);
116 
117         return this;
118     }
119 
120     /**
121      * Return the minimum value recorded, or {@code Double.POSITIVE_INFINITY} if
122      * no values have been recorded.
123      *
124      @return the minimum value, or {@code Double.POSITIVE_INFINITY} if none
125      */
126     public double getMin() {
127         return _min;
128     }
129 
130     /**
131      * Return the maximum value recorded, or {@code Double.NEGATIVE_INFINITY} if
132      * no values have been recorded.
133      *
134      @return the maximum value, or {@code Double.NEGATIVE_INFINITY} if none
135      */
136     public double getMax() {
137         return _max;
138     }
139 
140     /**
141      * Return the sum of values recorded, or zero if no values have been
142      * recorded.
143      *
144      @return the sum of values, or zero if none
145      */
146     public double getSum() {
147         return _sum.doubleValue();
148     }
149 
150     @Override
151     public String toString() {
152         return String.format(
153             "Summary[N=%d, ∧=%s, ∨=%s, Σ=%s, μ=%s, s²=%s, S=%s, K=%s]",
154             getCount(), _min, _max, _sum.doubleValue(),
155             getMean(), getVariance(), getSkewness(), getKurtosis()
156         );
157     }
158 
159     /**
160      * Return a {@code Collector} which applies an double-producing mapping
161      * function to each input element, and returns moments-statistics for the
162      * resulting values.
163      *
164      <pre>{@code
165      * final Stream<SomeObject> stream = ...
166      * final DoubleMomentStatistics statistics = stream
167      *     .collect(toDoubleMomentStatistics(v -> v.doubleValue()));
168      * }</pre>
169      *
170      @param mapper a mapping function to apply to each element
171      @param <T> the type of the input elements
172      @return a {@code Collector} implementing the moments-statistics reduction
173      @throws java.lang.NullPointerException if the given {@code mapper} is
174      *         {@code null}
175      */
176     public static <T> Collector<T, ?, DoubleMomentStatistics>
177     toDoubleMomentStatistics(final ToDoubleFunction<? super T> mapper) {
178         requireNonNull(mapper);
179         return Collector.of(
180             DoubleMomentStatistics::new,
181             (r, t-> r.accept(mapper.applyAsDouble(t)),
182             DoubleMomentStatistics::combine
183         );
184     }
185 
186 }