StdStats.java
16.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/******************************************************************************
* Compilation: javac StdStats.java
* Execution: java StdStats < input.txt
* Dependencies: StdOut.java
*
* Library of statistical functions.
*
* The test client reads an array of real numbers from standard
* input, and computes the minimum, mean, maximum, and
* standard deviation.
*
* The functions all throw a java.lang.IllegalArgumentException
* if the array passed in as an argument is null.
*
* The floating-point functions all return NaN if any input is NaN.
*
* Unlike Math.min() and Math.max(), the min() and max() functions
* do not differentiate between -0.0 and 0.0.
*
* % more tiny.txt
* 5
* 3.0 1.0 2.0 5.0 4.0
*
* % java StdStats < tiny.txt
* min 1.000
* mean 3.000
* max 5.000
* std dev 1.581
*
* Should these funtions use varargs instead of array arguments?
*
******************************************************************************/
package edu.princeton.cs.algs4;
/**
* The {@code StdStats} class provides static methods for computing
* statistics such as min, max, mean, sample standard deviation, and
* sample variance.
* <p>
* For additional documentation, see
* <a href="http://introcs.cs.princeton.edu/22library">Section 2.2</a> of
* <i>Computer Science: An Interdisciplinary Approach</i>
* by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public final class StdStats {
private StdStats() { }
/**
* Returns the maximum value in the specified array.
*
* @param a the array
* @return the maximum value in the array {@code a[]};
* {@code Double.NEGATIVE_INFINITY} if no such value
*/
public static double max(double[] a) {
validateNotNull(a);
double max = Double.NEGATIVE_INFINITY;
for (int i = 0; i < a.length; i++) {
if (Double.isNaN(a[i])) return Double.NaN;
if (a[i] > max) max = a[i];
}
return max;
}
/**
* Returns the maximum value in the specified subarray.
*
* @param a the array
* @param lo the left endpoint of the subarray (inclusive)
* @param hi the right endpoint of the subarray (inclusive)
* @return the maximum value in the subarray {@code a[lo..hi]};
* {@code Double.NEGATIVE_INFINITY} if no such value
*/
public static double max(double[] a, int lo, int hi) {
validateNotNull(a);
validateSubarrayIndices(lo, hi, a.length);
double max = Double.NEGATIVE_INFINITY;
for (int i = lo; i <= hi; i++) {
if (Double.isNaN(a[i])) return Double.NaN;
if (a[i] > max) max = a[i];
}
return max;
}
/**
* Returns the maximum value in the specified array.
*
* @param a the array
* @return the maximum value in the array {@code a[]};
* {@code Integer.MIN_VALUE} if no such value
*/
public static int max(int[] a) {
validateNotNull(a);
int max = Integer.MIN_VALUE;
for (int i = 0; i < a.length; i++) {
if (a[i] > max) max = a[i];
}
return max;
}
/**
* Returns the minimum value in the specified array.
*
* @param a the array
* @return the minimum value in the array {@code a[]};
* {@code Double.POSITIVE_INFINITY} if no such value
*/
public static double min(double[] a) {
validateNotNull(a);
double min = Double.POSITIVE_INFINITY;
for (int i = 0; i < a.length; i++) {
if (Double.isNaN(a[i])) return Double.NaN;
if (a[i] < min) min = a[i];
}
return min;
}
/**
* Returns the minimum value in the specified subarray.
*
* @param a the array
* @param lo the left endpoint of the subarray (inclusive)
* @param hi the right endpoint of the subarray (inclusive)
* @return the maximum value in the subarray {@code a[lo..hi]};
* {@code Double.POSITIVE_INFINITY} if no such value
*/
public static double min(double[] a, int lo, int hi) {
validateNotNull(a);
validateSubarrayIndices(lo, hi, a.length);
double min = Double.POSITIVE_INFINITY;
for (int i = lo; i <= hi; i++) {
if (Double.isNaN(a[i])) return Double.NaN;
if (a[i] < min) min = a[i];
}
return min;
}
/**
* Returns the minimum value in the specified array.
*
* @param a the array
* @return the minimum value in the array {@code a[]};
* {@code Integer.MAX_VALUE} if no such value
*/
public static int min(int[] a) {
validateNotNull(a);
int min = Integer.MAX_VALUE;
for (int i = 0; i < a.length; i++) {
if (a[i] < min) min = a[i];
}
return min;
}
/**
* Returns the average value in the specified array.
*
* @param a the array
* @return the average value in the array {@code a[]};
* {@code Double.NaN} if no such value
*/
public static double mean(double[] a) {
validateNotNull(a);
if (a.length == 0) return Double.NaN;
double sum = sum(a);
return sum / a.length;
}
/**
* Returns the average value in the specified subarray.
*
* @param a the array
* @param lo the left endpoint of the subarray (inclusive)
* @param hi the right endpoint of the subarray (inclusive)
* @return the average value in the subarray {@code a[lo..hi]};
* {@code Double.NaN} if no such value
*/
public static double mean(double[] a, int lo, int hi) {
validateNotNull(a);
validateSubarrayIndices(lo, hi, a.length);
if (a.length == 0) return Double.NaN;
double sum = sum(a, lo, hi);
return sum / a.length;
}
/**
* Returns the average value in the specified array.
*
* @param a the array
* @return the average value in the array {@code a[]};
* {@code Double.NaN} if no such value
*/
public static double mean(int[] a) {
validateNotNull(a);
if (a.length == 0) return Double.NaN;
int sum = sum(a);
return 1.0 * sum / a.length;
}
/**
* Returns the sample variance in the specified array.
*
* @param a the array
* @return the sample variance in the array {@code a[]};
* {@code Double.NaN} if no such value
*/
public static double var(double[] a) {
validateNotNull(a);
if (a.length == 0) return Double.NaN;
double avg = mean(a);
double sum = 0.0;
for (int i = 0; i < a.length; i++) {
sum += (a[i] - avg) * (a[i] - avg);
}
return sum / (a.length - 1);
}
/**
* Returns the sample variance in the specified subarray.
*
* @param a the array
* @param lo the left endpoint of the subarray (inclusive)
* @param hi the right endpoint of the subarray (inclusive)
* @return the sample variance in the subarray {@code a[lo..hi]};
* {@code Double.NaN} if no such value
*/
public static double var(double[] a, int lo, int hi) {
validateNotNull(a);
validateSubarrayIndices(lo, hi, a.length);
if (a.length == 0) return Double.NaN;
double avg = mean(a, lo, hi);
double sum = 0.0;
for (int i = lo; i <= hi; i++) {
sum += (a[i] - avg) * (a[i] - avg);
}
return sum / (a.length - 1);
}
/**
* Returns the sample variance in the specified array.
*
* @param a the array
* @return the sample variance in the array {@code a[]};
* {@code Double.NaN} if no such value
*/
public static double var(int[] a) {
validateNotNull(a);
if (a.length == 0) return Double.NaN;
double avg = mean(a);
double sum = 0.0;
for (int i = 0; i < a.length; i++) {
sum += (a[i] - avg) * (a[i] - avg);
}
return sum / (a.length - 1);
}
/**
* Returns the population variance in the specified array.
*
* @param a the array
* @return the population variance in the array {@code a[]};
* {@code Double.NaN} if no such value
*/
public static double varp(double[] a) {
validateNotNull(a);
if (a.length == 0) return Double.NaN;
double avg = mean(a);
double sum = 0.0;
for (int i = 0; i < a.length; i++) {
sum += (a[i] - avg) * (a[i] - avg);
}
return sum / a.length;
}
/**
* Returns the population variance in the specified subarray.
*
* @param a the array
* @param lo the left endpoint of the subarray (inclusive)
* @param hi the right endpoint of the subarray (inclusive)
* @return the population variance in the subarray {@code a[lo..hi]};
* {@code Double.NaN} if no such value
*/
public static double varp(double[] a, int lo, int hi) {
validateNotNull(a);
validateSubarrayIndices(lo, hi, a.length);
if (a.length == 0) return Double.NaN;
double avg = mean(a, lo, hi);
double sum = 0.0;
for (int i = lo; i <= hi; i++) {
sum += (a[i] - avg) * (a[i] - avg);
}
return sum / a.length;
}
/**
* Returns the sample standard deviation in the specified array.
*
* @param a the array
* @return the sample standard deviation in the array {@code a[]};
* {@code Double.NaN} if no such value
*/
public static double stddev(double[] a) {
validateNotNull(a);
return Math.sqrt(var(a));
}
/**
* Returns the sample standard deviation in the specified array.
*
* @param a the array
* @return the sample standard deviation in the array {@code a[]};
* {@code Double.NaN} if no such value
*/
public static double stddev(int[] a) {
validateNotNull(a);
return Math.sqrt(var(a));
}
/**
* Returns the sample standard deviation in the specified subarray.
*
* @param a the array
* @param lo the left endpoint of the subarray (inclusive)
* @param hi the right endpoint of the subarray (inclusive)
* @return the sample standard deviation in the subarray {@code a[lo..hi]};
* {@code Double.NaN} if no such value
*/
public static double stddev(double[] a, int lo, int hi) {
validateNotNull(a);
validateSubarrayIndices(lo, hi, a.length);
return Math.sqrt(var(a, lo, hi));
}
/**
* Returns the population standard deviation in the specified array.
*
* @param a the array
* @return the population standard deviation in the array;
* {@code Double.NaN} if no such value
*/
public static double stddevp(double[] a) {
validateNotNull(a);
return Math.sqrt(varp(a));
}
/**
* Returns the population standard deviation in the specified subarray.
*
* @param a the array
* @param lo the left endpoint of the subarray (inclusive)
* @param hi the right endpoint of the subarray (inclusive)
* @return the population standard deviation in the subarray {@code a[lo..hi]};
* {@code Double.NaN} if no such value
*/
public static double stddevp(double[] a, int lo, int hi) {
validateNotNull(a);
validateSubarrayIndices(lo, hi, a.length);
return Math.sqrt(varp(a, lo, hi));
}
/**
* Returns the sum of all values in the specified array.
*
* @param a the array
* @return the sum of all values in the array {@code a[]};
* {@code 0.0} if no such value
*/
private static double sum(double[] a) {
validateNotNull(a);
double sum = 0.0;
for (int i = 0; i < a.length; i++) {
sum += a[i];
}
return sum;
}
/**
* Returns the sum of all values in the specified subarray.
*
* @param a the array
* @param lo the left endpoint of the subarray (inclusive)
* @param hi the right endpoint of the subarray (inclusive)
* @return the sum of all values in the subarray {@code a[lo..hi]};
* {@code 0.0} if no such value
*/
private static double sum(double[] a, int lo, int hi) {
validateNotNull(a);
validateSubarrayIndices(lo, hi, a.length);
double sum = 0.0;
for (int i = lo; i <= hi; i++) {
sum += a[i];
}
return sum;
}
/**
* Returns the sum of all values in the specified array.
*
* @param a the array
* @return the sum of all values in the array {@code a[]};
* {@code 0.0} if no such value
*/
private static int sum(int[] a) {
validateNotNull(a);
int sum = 0;
for (int i = 0; i < a.length; i++) {
sum += a[i];
}
return sum;
}
/**
* Plots the points (0, <em>a</em><sub>0</sub>), (1, <em>a</em><sub>1</sub>), ...,
* (<em>n</em>−1, <em>a</em><sub><em>n</em>−1</sub>) to standard draw.
*
* @param a the array of values
*/
public static void plotPoints(double[] a) {
validateNotNull(a);
int n = a.length;
StdDraw.setXscale(-1, n);
StdDraw.setPenRadius(1.0 / (3.0 * n));
for (int i = 0; i < n; i++) {
StdDraw.point(i, a[i]);
}
}
/**
* Plots the line segments connecting
* (<em>i</em>, <em>a</em><sub><em>i</em></sub>) to
* (<em>i</em>+1, <em>a</em><sub><em>i</em>+1</sub>) for
* each <em>i</em> to standard draw.
*
* @param a the array of values
*/
public static void plotLines(double[] a) {
validateNotNull(a);
int n = a.length;
StdDraw.setXscale(-1, n);
StdDraw.setPenRadius();
for (int i = 1; i < n; i++) {
StdDraw.line(i-1, a[i-1], i, a[i]);
}
}
/**
* Plots bars from (0, <em>a</em><sub><em>i</em></sub>) to
* (<em>a</em><sub><em>i</em></sub>) for each <em>i</em>
* to standard draw.
*
* @param a the array of values
*/
public static void plotBars(double[] a) {
validateNotNull(a);
int n = a.length;
StdDraw.setXscale(-1, n);
for (int i = 0; i < n; i++) {
StdDraw.filledRectangle(i, a[i]/2, 0.25, a[i]/2);
}
}
// throw an IllegalArgumentException if x is null
private static void validateNotNull(Object x) {
if (x == null)
throw new IllegalArgumentException("argument is null");
}
// throw an exception unless 0 <= lo <= hi < length
private static void validateSubarrayIndices(int lo, int hi, int length) {
if (lo < 0 || hi >= length || lo > hi)
throw new IndexOutOfBoundsException("subarray indices out of bounds");
}
/**
* Unit tests {@code StdStats}.
* Convert command-line arguments to array of doubles and call various methods.
*
* @param args the command-line arguments
*/
public static void main(String[] args) {
double[] a = StdArrayIO.readDouble1D();
StdOut.printf(" min %10.3f\n", min(a));
StdOut.printf(" mean %10.3f\n", mean(a));
StdOut.printf(" max %10.3f\n", max(a));
StdOut.printf(" stddev %10.3f\n", stddev(a));
StdOut.printf(" var %10.3f\n", var(a));
StdOut.printf(" stddevp %10.3f\n", stddevp(a));
StdOut.printf(" varp %10.3f\n", varp(a));
}
}
/******************************************************************************
* Copyright 2002-2016, Robert Sedgewick and Kevin Wayne.
*
* This file is part of algs4.jar, which accompanies the textbook
*
* Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,
* Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.
* http://algs4.cs.princeton.edu
*
*
* algs4.jar is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* algs4.jar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with algs4.jar. If not, see http://www.gnu.org/licenses.
******************************************************************************/