$darkmode
DENOPTIM
StatUtils.java
Go to the documentation of this file.
1/*
2 * DENOPTIM
3 * Copyright (C) 2019 Vishwesh Venkatraman <vishwesh.venkatraman@ntnu.no>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package denoptim.utils;
20
25public final class StatUtils
26{
27
28//------------------------------------------------------------------------------
29
36 public static double sum(double[] numbers)
37 {
38 double sum = 0;
39 for (double number : numbers) {
40 sum += number;
41 }
42 return sum;
43 }
44
45//------------------------------------------------------------------------------
46
53 public static double mean(double[] numbers)
54 {
55 double sum = sum(numbers);
56 return sum / numbers.length;
57 }
58
59//------------------------------------------------------------------------------
60
67 public static double min(double[] numbers)
68 {
69 double min = Integer.MAX_VALUE;
70 for (double number : numbers) {
71 if (number < min)
72 min = number;
73 }
74 return min;
75 }
76
77//------------------------------------------------------------------------------
78
85 public static double max(double[] numbers)
86 {
87 double max = Integer.MIN_VALUE;
88 for (double number : numbers) {
89 if (number > max)
90 max = number;
91 }
92 return max;
93 }
94
95 //------------------------------------------------------------------------------
96
107 public static double stddev(double[] numbers, boolean biasCorrected)
108 {
109 double stddev = Double.NaN;
110 int n = numbers.length;
111 if (n > 0)
112 {
113 if (n > 1)
114 {
115 stddev = Math.sqrt(var(numbers, biasCorrected));
116 }
117 else
118 {
119 stddev = 0.0;
120 }
121 }
122 return stddev;
123 }
124
125//------------------------------------------------------------------------------
126
145 public static double var(double[] numbers, boolean biasCorrected)
146 {
147 int n = numbers.length;
148 if (n == 0)
149 return Double.NaN;
150 else if (n == 1)
151 return 0d;
152
153 double mean = mean(numbers);
154 double[] squares = new double[numbers.length];
155 for (int i=0; i<numbers.length; i++)
156 {
157 double XminMean = numbers[i] - mean;
158 squares[i] = Math.pow(XminMean, 2);
159 }
160 double sum = sum(squares);
161 return sum / (biasCorrected ? (n - 1) : n);
162 }
163
164//------------------------------------------------------------------------------
165
172 public static double median(double[] m)
173 {
174 int middle = m.length / 2; // subscript of middle element
175 if (m.length % 2 == 1)
176 {
177 // Odd number of elements -- return the middle one.
178 return m[middle];
179 }
180 else
181 {
182 // Even number -- return average of middle two
183 // Must cast the numbers to double before dividing.
184 return (m[middle-1] + m[middle]) / 2.0;
185 }
186 }
187
188 //------------------------------------------------------------------------------
189
205 public static double kurtosis(double[] m, boolean biasCorrected)
206 {
207 double value = Double.NaN;
208 int N = m.length;
209 if (N > 3)
210 {
211 double variance = var(m, biasCorrected);
212 if (N <= 3 || variance < 10E-20)
213 {
214 value = 0.0;
215 }
216 else
217 {
218 double n = N;
219 double f1 = (n*(n+1)) /((n -1) * (n - 2) * (n - 3));
220 double avg = mean(m);
221 double f2 = 0, f3 = 0;
222 for (int i=0; i<N; i++)
223 {
224 f2 += Math.pow((m[i] - avg), 4);
225 f3 += Math.pow((m[i] - avg), 2);
226 }
227
228 double f4 = (3 * (n-1) * (n -1)) / ((n-2)*(n-3));
229
230 value = (f1 * (Math.pow(f2, 4)/Math.pow(f3, 2))) - f4;
231 }
232 }
233 return value;
234 }
235
236//------------------------------------------------------------------------------
237
246 public static double skewness(double[] m, boolean biasCorrected)
247 {
248 double n = m.length;
249 if (n <= 3) {
250 return Double.NaN;
251 }
252 double sampleSizeCorrectionTerm = Math.sqrt(n * (n - 1)) / (n - 2);
253 double biasedStandardDeviation = Math.sqrt(n / (n - 1))
254 * stddev(m, biasCorrected);
255 double accDeviationFromMean = 0.0;
256 double mean = mean(m);
257 for (double v : m) {
258 accDeviationFromMean += v - mean;
259 }
260 return sampleSizeCorrectionTerm
261 * (accDeviationFromMean / (n * biasedStandardDeviation));
262 }
263
264//------------------------------------------------------------------------------
265
266}
Utilities for calculating basic statistics.
Definition: StatUtils.java:26
static double mean(double[] numbers)
Returns the mean number in the numbers list.
Definition: StatUtils.java:53
static double sum(double[] numbers)
Returns the sum number in the numbers list.
Definition: StatUtils.java:36
static double kurtosis(double[] m, boolean biasCorrected)
Computes the Kurtosis of the available values.
Definition: StatUtils.java:205
static double stddev(double[] numbers, boolean biasCorrected)
Returns the standard deviation of the numbers.
Definition: StatUtils.java:107
static double skewness(double[] m, boolean biasCorrected)
Computes the skewness of the available values.
Definition: StatUtils.java:246
static double median(double[] m)
Calculates median value of a sorted list.
Definition: StatUtils.java:172
static double var(double[] numbers, boolean biasCorrected)
Computes the variance of the available values.
Definition: StatUtils.java:145
static double min(double[] numbers)
Returns the minimum value among the numbers .
Definition: StatUtils.java:67
static double max(double[] numbers)
Returns the maximum value among the numbers .
Definition: StatUtils.java:85