SampleSummaryPoint.java
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.lang.Double.compare;
023 import static java.lang.String.format;
024 
025 import java.io.Serializable;
026 import java.util.Arrays;
027 
028 /**
029  * Represents a point of the sample summary. The class contains the following
030  * values:
031  <ul>
032  *     <li>mean</li>
033  *     <li>variance</li>
034  *     <li>skewness</li>
035  *     <li>kurtosis</li>
036  *     <li>median</li>
037  *     <li>low (<i>25-percentile</i>)</li>
038  *     <li>high (<i>75-percentile</i>)</li>
039  *     <li>min</li>
040  *     <li>max</li>
041  </ul>
042  *
043  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
044  @version 3.4
045  @since 3.4
046  */
047 public final class SampleSummaryPoint implements Serializable {
048 
049     private static final long serialVersionUID = 1L;
050 
051     /**
052      * Constant with all values set to {@link Double#NaN}.
053      */
054     public static SampleSummaryPoint NaN = of(
055         Double.NaN,
056         Double.NaN,
057         Double.NaN,
058         Double.NaN,
059         Double.NaN,
060         Double.NaN,
061         Double.NaN,
062         Double.NaN,
063         Double.NaN
064     );
065 
066     private final double _mean;
067     private final double _variance;
068     private final double _skewness;
069     private final double _kurtosis;
070     private final double _median;
071     private final double _low;
072     private final double _high;
073     private final double _min;
074     private final double _max;
075 
076     private SampleSummaryPoint(
077         final double mean,
078         final double variance,
079         final double skewness,
080         final double kurtosis,
081         final double median,
082         final double low,
083         final double high,
084         final double min,
085         final double max
086     ) {
087         _mean = mean;
088         _variance = variance;
089         _skewness = skewness;
090         _kurtosis = kurtosis;
091         _median = median;
092         _low = low;
093         _high = high;
094         _min = min;
095         _max = max;
096     }
097 
098     /**
099      * Return the 75-percentile
100      *
101      @return the 75-percentile
102      */
103     public double getHigh() {
104         return _high;
105     }
106 
107     /**
108      * The kurtosis value.
109      *
110      @return the kurtosis value
111      */
112     public double getKurtosis() {
113         return _kurtosis;
114     }
115 
116     /**
117      * The 25-percentile
118      *
119      @return the 25-percentile
120      */
121     public double getLow() {
122         return _low;
123     }
124 
125     /**
126      * The maximum value.
127      *
128      @return the maximum value
129      */
130     public double getMax() {
131         return _max;
132     }
133 
134     /**
135      * The mean value.
136      *
137      @return the mean value
138      */
139     public double getMean() {
140         return _mean;
141     }
142 
143     /**
144      * The median value.
145      *
146      @return the median value
147      */
148     public double getMedian() {
149         return _median;
150     }
151 
152     /**
153      * The minimum value.
154      *
155      @return the minimum value
156      */
157     public double getMin() {
158         return _min;
159     }
160 
161     /**
162      * The skewness value.
163      *
164      @return the skewness value
165      */
166     public double getSkewness() {
167         return _skewness;
168     }
169 
170     /**
171      * The variance value.
172      *
173      @return the variance value
174      */
175     public double getVariance() {
176         return _variance;
177     }
178 
179     /**
180      * Return the double values of this point as array in the following order:
181      <ul>
182      *     <li>mean</li>
183      *     <li>variance</li>
184      *     <li>skewness</li>
185      *     <li>kurtosis</li>
186      *     <li>median</li>
187      *     <li>low (<i>25-percentile</i>)</li>
188      *     <li>high (<i>75-percentile</i>)</li>
189      *     <li>min</li>
190      *     <li>max</li>
191      </ul>
192      *
193      @return the double values of this point as array
194      */
195     public double[] toArray() {
196         return new double[] {
197             _mean, _variance, _skewness, _kurtosis,
198             _median, _low, _high, _min, _max
199         };
200     }
201 
202     @Override
203     public int hashCode() {
204         return Arrays.hashCode(toArray());
205     }
206 
207     @Override
208     public boolean equals(final Object obj) {
209         return obj instanceof SampleSummaryPoint &&
210             compare(_mean, ((SampleSummaryPoint)obj)._mean== &&
211             compare(_variance, ((SampleSummaryPoint)obj)._variance== &&
212             compare(_skewness, ((SampleSummaryPoint)obj)._skewness== &&
213             compare(_kurtosis, ((SampleSummaryPoint)obj)._kurtosis== &&
214             compare(_median, ((SampleSummaryPoint)obj)._median== &&
215             compare(_low, ((SampleSummaryPoint)obj)._low== &&
216             compare(_high, ((SampleSummaryPoint)obj)._high== &&
217             compare(_min, ((SampleSummaryPoint)obj)._min== &&
218             compare(_max, ((SampleSummaryPoint)obj)._max== 0;
219     }
220 
221     @Override
222     public String toString() {
223         return format(
224             "Point[mean=%f, variance=%f, skewness=%f, kurtosis=%f, " +
225             "median=%f, low=%f, high=%f, min=%f, max=%f]",
226             _mean,
227             _variance,
228             _skewness,
229             _kurtosis,
230             _median,
231             _low,
232             _high,
233             _min,
234             _max
235         );
236     }
237 
238     /**
239      * Create a new {@code SampleSummaryPoint} from the given values.
240      *
241      @param mean the mean value
242      @param variance the variance value
243      @param skewness the skewness value
244      @param kurtosis the kurtosis value
245      @param median the median value
246      @param low the 25-percentile
247      @param high the 75-percentile
248      @param min the minimum value
249      @param max the maximum value
250      @return a new {@code SampleSummaryPoint} from the given values
251      */
252     public static SampleSummaryPoint of(
253         final double mean,
254         final double variance,
255         final double skewness,
256         final double kurtosis,
257         final double median,
258         final double low,
259         final double high,
260         final double min,
261         final double max
262     ) {
263         return new SampleSummaryPoint(
264             mean,
265             variance,
266             skewness,
267             kurtosis,
268             median,
269             low,
270             high,
271             min,
272             max
273         );
274     }
275 
276 }