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.tool.trial;
021
022 import static java.util.Objects.requireNonNull;
023
024 import java.io.Serializable;
025 import java.util.stream.Collector;
026 import java.util.stream.IntStream;
027 import java.util.stream.Stream;
028
029 import org.jenetics.internal.util.require;
030
031 import org.jenetics.stat.DoubleMomentStatistics;
032 import org.jenetics.util.ISeq;
033
034 /**
035 * Summary of a given set of {@link Sample} objects.
036 *
037 * @see Data
038 * @see Sample
039 * @see SampleSummaryPoint
040 * @see SampleSummaryStatistics
041 *
042 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
043 * @version 3.4
044 * @since 3.4
045 */
046 public final class SampleSummary implements Serializable {
047
048 private static final long serialVersionUID = 1L;
049
050 private final ISeq<SampleSummaryPoint> _points;
051
052 private SampleSummary(final ISeq<SampleSummaryPoint> points) {
053 _points = requireNonNull(points);
054 }
055
056 /**
057 * Return the number of {@link SampleSummaryPoint} this summary, one for
058 * every parameter.
059 *
060 * @return the number of parameters
061 */
062 public int parameterCount() {
063 return _points.size();
064 }
065
066 /**
067 * Return the summary points.
068 *
069 * @return the summary points
070 */
071 public ISeq<SampleSummaryPoint> getPoints() {
072 return _points;
073 }
074
075 /**
076 * Return the summary points as {@link Stream}.
077 *
078 * @return the summary points as {@link Stream}
079 */
080 public Stream<SampleSummaryPoint> stream() {
081 return _points.stream();
082 }
083
084 @Override
085 public int hashCode() {
086 return _points.hashCode();
087 }
088
089 @Override
090 public boolean equals(final Object obj) {
091 return obj instanceof SampleSummary &&
092 _points.equals(((SampleSummary)obj)._points);
093 }
094
095 /**
096 * Create a new {@code SampleSummary} object from the given sample points.
097 *
098 * @param points the summary points
099 * @return a new {@code SampleSummary} instance
100 * @throws NullPointerException if the argument is {@code null}
101 */
102 public static SampleSummary of(final ISeq<SampleSummaryPoint> points) {
103 return new SampleSummary(points);
104 }
105
106 /**
107 * Return a new {@code SampleSummary} for the given
108 * {@link SampleSummaryStatistics}.
109 *
110 * @param statistics the summary statistics object
111 * @return a new {@code SampleSummary} instance
112 * @throws NullPointerException if the argument is {@code null}
113 */
114 public static SampleSummary of(final SampleSummaryStatistics statistics) {
115 final ISeq<DoubleMomentStatistics> moments = statistics.getMoments();
116 final ISeq<ExactQuantile> quantiles = statistics.getQuantiles();
117
118 return of(
119 IntStream.range(0, moments.size())
120 .mapToObj(i -> toPoint(moments.get(i), quantiles.get(i)))
121 .collect(ISeq.toISeq())
122 );
123 }
124
125 private static SampleSummaryPoint toPoint(
126 final DoubleMomentStatistics moment,
127 final ExactQuantile quantile
128 ) {
129 return SampleSummaryPoint.of(
130 moment.getMean(),
131 moment.getVariance(),
132 moment.getSkewness(),
133 moment.getKurtosis(),
134 quantile.quantile(0.5),
135 quantile.quantile(0.25),
136 quantile.quantile(0.75),
137 moment.getMin(),
138 moment.getMax()
139 );
140 }
141
142 /**
143 * Return a {@link Collector} for creating a {@code SampleSummary} object.
144 *
145 * @param parameterCount the number of parameters of the samples
146 * @return a {@code SampleSummary} {@link Collector}
147 * @throws IllegalArgumentException if the given {@code parameterCount} is
148 * smaller than one
149 */
150 public static Collector<Sample, ?, SampleSummary>
151 toSampleSummary(final int parameterCount) {
152 require.positive(parameterCount);
153
154 return Collector.of(
155 () -> new SampleSummaryStatistics(parameterCount),
156 SampleSummaryStatistics::accept,
157 SampleSummaryStatistics::combine,
158 SampleSummary::of
159 );
160 }
161
162 }
|