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.util.Objects.requireNonNull;
023 import static org.jenetics.internal.util.Equality.eq;
024
025 import java.io.Serializable;
026 import java.util.function.ToLongFunction;
027 import java.util.stream.Collector;
028
029 import org.jenetics.internal.util.Hash;
030
031 /**
032 * <i>Value</i> objects which contains statistical moments.
033 *
034 * @see org.jenetics.stat.LongMomentStatistics
035 *
036 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
037 * @since 3.0
038 * @version 3.0
039 */
040 public final class LongMoments implements Serializable {
041
042 private static final long serialVersionUID = 1L;
043
044 private final long _count;
045 private final long _min;
046 private final long _max;
047 private final long _sum;
048 private final double _mean;
049 private final double _variance;
050 private final double _skewness;
051 private final double _kurtosis;
052
053
054 /**
055 * Create an immutable object which contains statistical values.
056 *
057 * @param count the count of values recorded
058 * @param min the minimum value
059 * @param max the maximum value
060 * @param sum the sum of the recorded values
061 * @param mean the arithmetic mean of values
062 * @param variance the variance of values
063 * @param skewness the skewness of values
064 * @param kurtosis the kurtosis of values
065 */
066 private LongMoments(
067 final long count,
068 final long min,
069 final long max,
070 final long sum,
071 final double mean,
072 final double variance,
073 final double skewness,
074 final double kurtosis
075 ) {
076 _count = count;
077 _min = min;
078 _max = max;
079 _sum = sum;
080 _mean = mean;
081 _variance = variance;
082 _skewness = skewness;
083 _kurtosis = kurtosis;
084 }
085
086 /**
087 * Returns the count of values recorded.
088 *
089 * @return the count of recorded values
090 */
091 public long getCount() {
092 return _count;
093 }
094
095 /**
096 * Return the minimum value recorded, or {@code Long.MAX_VALUE} if no
097 * values have been recorded.
098 *
099 * @return the minimum value, or {@code Long.MAX_VALUE} if none
100 */
101 public long getMin() {
102 return _min;
103 }
104
105 /**
106 * Return the maximum value recorded, or {@code Long.MIN_VALUE} if no
107 * values have been recorded.
108 *
109 * @return the maximum value, or {@code Long.MIN_VALUE} if none
110 */
111 public long getMax() {
112 return _max;
113 }
114
115 /**
116 * Return the sum of values recorded, or zero if no values have been
117 * recorded.
118 *
119 * @return the sum of values, or zero if none
120 */
121 public long getSum() {
122 return _sum;
123 }
124
125 /**
126 * Return the arithmetic mean of values recorded, or zero if no values have
127 * been recorded.
128 *
129 * @return the arithmetic mean of values, or zero if none
130 */
131 public double getMean() {
132 return _mean;
133 }
134
135 /**
136 * Return the variance of values recorded, or {@code Double.NaN} if no
137 * values have been recorded.
138 *
139 * @return the variance of values, or {@code NaN} if none
140 */
141 public double getVariance() {
142 return _variance;
143 }
144
145 /**
146 * Return the skewness of values recorded, or {@code Double.NaN} if less
147 * than two values have been recorded.
148 *
149 * @see <a href="https://en.wikipedia.org/wiki/Skewness">Skewness</a>
150 *
151 * @return the skewness of values, or {@code NaN} if less than two values
152 * have been recorded
153 */
154 public double getSkewness() {
155 return _skewness;
156 }
157
158 /**
159 * Return the kurtosis of values recorded, or {@code Double.NaN} if less
160 * than four values have been recorded.
161 *
162 * @see <a href="https://en.wikipedia.org/wiki/Kurtosis">Kurtosis</a>
163 *
164 * @return the kurtosis of values, or {@code NaN} if less than four values
165 * have been recorded
166 */
167 public double getKurtosis() {
168 return _kurtosis;
169 }
170
171 @Override
172 public int hashCode() {
173 return Hash.of(LongMoments.class)
174 .and(_count)
175 .and(_sum)
176 .and(_min)
177 .and(_max)
178 .and(_mean)
179 .and(_variance)
180 .and(_skewness)
181 .and(_kurtosis).value();
182 }
183
184 @Override
185 public boolean equals(final Object obj) {
186 return obj instanceof LongMoments &&
187 eq(_count, ((LongMoments)obj)._count) &&
188 eq(_sum, ((LongMoments)obj)._sum) &&
189 eq(_min, ((LongMoments)obj)._min) &&
190 eq(_max, ((LongMoments)obj)._max) &&
191 eq(_mean, ((LongMoments)obj)._mean) &&
192 eq(_variance, ((LongMoments)obj)._variance) &&
193 eq(_skewness, ((LongMoments)obj)._skewness) &&
194 eq(_kurtosis, ((LongMoments)obj)._kurtosis);
195 }
196
197 @Override
198 public String toString() {
199 return String.format(
200 "IntMoments[N=%d, ∧=%s, ∨=%s, Σ=%s, μ=%s, s²=%s, S=%s, K=%s]",
201 getCount(), getMin(), getMax(), getSum(),
202 getMean(), getVariance(), getSkewness(), getKurtosis()
203 );
204 }
205
206 /**
207 * Create an immutable object which contains statistical values.
208 *
209 * @param count the count of values recorded
210 * @param min the minimum value
211 * @param max the maximum value
212 * @param sum the sum of the recorded values
213 * @param mean the arithmetic mean of values
214 * @param variance the variance of values
215 * @param skewness the skewness of values
216 * @param kurtosis the kurtosis of values
217 * @return an immutable object which contains statistical values
218 */
219 public static LongMoments of(
220 final long count,
221 final long min,
222 final long max,
223 final long sum,
224 final double mean,
225 final double variance,
226 final double skewness,
227 final double kurtosis
228 ) {
229 return new LongMoments(
230 count,
231 min,
232 max,
233 sum,
234 mean,
235 variance,
236 skewness,
237 kurtosis
238 );
239 }
240
241 /**
242 * Return a new value object of the statistical moments, currently
243 * represented by the {@code statistics} object.
244 *
245 * @param statistics the creating (mutable) statistics class
246 * @return the statistical moments
247 */
248 public static LongMoments of(final LongMomentStatistics statistics) {
249 return new LongMoments(
250 statistics.getCount(),
251 statistics.getMin(),
252 statistics.getMax(),
253 statistics.getSum(),
254 statistics.getMean(),
255 statistics.getVariance(),
256 statistics.getSkewness(),
257 statistics.getKurtosis()
258 );
259 }
260
261 /**
262 * Return a {@code Collector} which applies an long-producing mapping
263 * function to each input element, and returns moments-statistics for the
264 * resulting values.
265 *
266 * <pre>{@code
267 * final Stream<SomeObject> stream = ...
268 * final LongMoments moments = stream
269 * .collect(toLongMoments(v -> v.longValue()));
270 * }</pre>
271 *
272 * @param mapper a mapping function to apply to each element
273 * @param <T> the type of the input elements
274 * @return a {@code Collector} implementing the moments-statistics reduction
275 * @throws java.lang.NullPointerException if the given {@code mapper} is
276 * {@code null}
277 */
278 public static <T> Collector<T, ?, LongMoments>
279 toLongMoments(final ToLongFunction<? super T> mapper) {
280 requireNonNull(mapper);
281 return Collector.of(
282 LongMomentStatistics::new,
283 (a, b) -> a.accept(mapper.applyAsLong(b)),
284 LongMomentStatistics::combine,
285 LongMoments::of
286 );
287 }
288
289 }
|