1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package ubic.basecode.math;
20
21 import java.util.HashSet;
22 import java.util.Set;
23
24 import cern.colt.list.DoubleArrayList;
25 import cern.jet.stat.Descriptive;
26
27
28
29
30
31
32
33
34
35
36
37 public class Stats {
38
39
40
41
42
43
44
45
46
47 public static DoubleArrayList cdf(DoubleArrayList x) {
48 return cumulateRight(normalize(x));
49 }
50
51
52
53
54
55
56
57
58 public static DoubleArrayList cumulate(DoubleArrayList x) {
59 if (x.size() == 0) {
60 return new DoubleArrayList(0);
61 }
62
63 DoubleArrayList r = new DoubleArrayList();
64
65 double sum = 0.0;
66 for (int i = 0; i < x.size(); i++) {
67 sum += x.get(i);
68 r.add(sum);
69 }
70 return r;
71 }
72
73
74
75
76
77
78
79
80
81 public static DoubleArrayList cumulateRight(DoubleArrayList x) {
82 if (x.size() == 0) {
83 return new DoubleArrayList(0);
84 }
85
86 DoubleArrayList r = new DoubleArrayList(new double[x.size()]);
87
88 double sum = 0.0;
89 for (int i = x.size() - 1; i >= 0; i--) {
90 sum += x.get(i);
91 r.set(i, sum);
92 }
93 return r;
94 }
95
96
97
98
99
100
101
102
103
104
105 public static double cv(DoubleArrayList data) {
106 double mean = DescriptiveWithMissing.mean(data);
107
108 double sampleVariance = DescriptiveWithMissing.sampleVariance(data, mean);
109
110 if (sampleVariance == 0.0) return 0.0;
111
112 if (mean == 0.0) {
113 return 0.0;
114 }
115
116 return Math.sqrt(sampleVariance) / Math.abs(mean);
117 }
118
119
120
121
122
123
124
125 public static boolean isValidFraction(double value) {
126 if (value > 1.0 || value < 0.0) {
127 return false;
128 }
129 return true;
130 }
131
132
133
134
135
136
137
138
139
140
141
142 public static double meanAboveQuantile(int index, double[] array, int effectiveSize) {
143
144 double[] temp = new double[effectiveSize];
145 double median;
146 double returnvalue = 0.0;
147 int k = 0;
148
149 temp = array;
150 median = quantile(index, array, effectiveSize);
151
152 for (int i = 0; i < effectiveSize; i++) {
153 if (temp[i] > median) {
154 returnvalue += temp[i];
155 k++;
156 }
157 }
158 return returnvalue / k;
159 }
160
161
162
163
164
165
166
167 public static DoubleArrayList normalize(DoubleArrayList x) {
168 return normalize(x, Descriptive.sum(x));
169 }
170
171
172
173
174
175
176
177
178 public static DoubleArrayList normalize(DoubleArrayList x, double normfactor) {
179 if (x.size() == 0) {
180 return new DoubleArrayList(0);
181 }
182
183 DoubleArrayList r = new DoubleArrayList();
184
185 for (int i = 0; i < x.size(); i++) {
186 r.add(x.get(i) / normfactor);
187 }
188 return r;
189
190 }
191
192
193
194
195
196
197
198 public static Integer numberofDistinctValues(DoubleArrayList array, double tolerance) {
199
200 Set<Double> distinct = new HashSet<>();
201 int r = 1;
202 if (tolerance > 0.0) {
203 r = (int) Math.ceil(1.0 / tolerance);
204 }
205 for (int i = 0; i < array.size(); i++) {
206 double v = array.get(i);
207 if (tolerance > 0) {
208
209 distinct.add((double) Math.round(v * r) / r);
210 } else {
211 distinct.add(v);
212 }
213 }
214 return Math.max(0, distinct.size());
215
216 }
217
218
219
220
221
222
223 public static Integer numberofDistinctValuesNonNA(DoubleArrayList array, double tolerance) {
224
225 Set<Double> distinct = new HashSet<>();
226 int r = 1;
227 if (tolerance > 0.0) {
228 r = (int) Math.ceil(1.0 / tolerance);
229 }
230 for (int i = 0; i < array.size(); i++) {
231 double v = array.get(i);
232 if (Double.isNaN(v)) {
233 continue;
234 }
235 if (tolerance > 0) {
236
237 distinct.add((double) Math.round(v * r) / r);
238 } else {
239 distinct.add(v);
240 }
241 }
242 return Math.max(0, distinct.size());
243
244 }
245
246
247
248
249
250
251
252
253 public static Double fractionDistinctValuesNonNA(DoubleArrayList array, double tolerance) {
254 double numNonNA = (double) numNonMissing(array);
255 if (numNonNA == 0) return 0.0;
256 return (double) numberofDistinctValuesNonNA(array, tolerance) / numNonNA;
257 }
258
259 private static Integer numNonMissing(DoubleArrayList array) {
260 int nm = 0;
261 for (int i = 0; i < array.size(); i++) {
262 if (Double.isNaN(array.get(i))) continue;
263 nm++;
264 }
265 return nm;
266 }
267
268
269
270
271
272
273
274
275
276
277
278
279 public static double quantile(int index, double[] values, int effectiveSize) {
280 double pivot = -1.0;
281 if (index == 0) {
282 double ans = values[0];
283 for (int i = 1; i < effectiveSize; i++) {
284 if (ans > values[i]) {
285 ans = values[i];
286 }
287 }
288 return ans;
289 }
290
291 double[] temp = new double[effectiveSize];
292
293 for (int i = 0; i < effectiveSize; i++) {
294 temp[i] = values[i];
295 }
296
297 pivot = temp[0];
298
299 double[] smaller = new double[effectiveSize];
300 double[] bigger = new double[effectiveSize];
301 int itrSm = 0;
302 int itrBg = 0;
303 for (int i = 1; i < effectiveSize; i++) {
304 if (temp[i] <= pivot) {
305 smaller[itrSm] = temp[i];
306 itrSm++;
307 } else if (temp[i] > pivot) {
308 bigger[itrBg] = temp[i];
309 itrBg++;
310 }
311 }
312 if (itrSm > index) {
313 return quantile(index, smaller, itrSm);
314 } else if (itrSm < index) {
315 return quantile(index - itrSm - 1, bigger, itrBg);
316 } else {
317 return pivot;
318 }
319
320 }
321
322
323
324
325
326
327
328 public static double range(DoubleArrayList data) {
329 return DescriptiveWithMissing.max(data) - DescriptiveWithMissing.min(data);
330 }
331
332 private Stats() {
333 }
334
335 }