001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
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
package stdlib;

/* ***********************************************************************
 *  Compilation:  javac StdStats.java
 *  Execution:    java StdStats < input.txt
 *
 *  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 NullPointerException if the array
 *  passed in is null.

 *  % 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
 *
 *************************************************************************/

/**
 *  <i>Standard statistics</i>. This class provides 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>Introduction to Programming in Java: An Interdisciplinary Approach</i>
 *  by Robert Sedgewick and Kevin Wayne.
 */
public final class StdStats {

  private StdStats() { }

  /**
   * Return maximum value in array, -infinity if no such value.
   */
  public static double max(double[] a) {
    double max = Double.NEGATIVE_INFINITY;
    for (int i = 0; i < a.length; i++) {
      if (a[i] > max) max = a[i];
    }
    return max;
  }

  /**
   * Return maximum value in subarray a[lo..hi], -infinity if no such value.
   */
  public static double max(double[] a, int lo, int hi) {
    if (lo < 0 || hi >= a.length || lo > hi)
      throw new RuntimeException("Subarray indices out of bounds");
    double max = Double.NEGATIVE_INFINITY;
    for (int i = lo; i <= hi; i++) {
      if (a[i] > max) max = a[i];
    }
    return max;
  }

  /**
   * Return maximum value of array, Integer.MIN_VALUE if no such value
   */
  public static int max(int[] a) {
    int max = Integer.MIN_VALUE;
    for (int i = 0; i < a.length; i++) {
      if (a[i] > max) max = a[i];
    }
    return max;
  }

  /**
   * Return minimum value in array, +infinity if no such value.
   */
  public static double min(double[] a) {
    double min = Double.POSITIVE_INFINITY;
    for (int i = 0; i < a.length; i++) {
      if (a[i] < min) min = a[i];
    }
    return min;
  }

  /**
   * Return minimum value in subarray a[lo..hi], +infinity if no such value.
   */
  public static double min(double[] a, int lo, int hi) {
    if (lo < 0 || hi >= a.length || lo > hi)
      throw new RuntimeException("Subarray indices out of bounds");
    double min = Double.POSITIVE_INFINITY;
    for (int i = lo; i <= hi; i++) {
      if (a[i] < min) min = a[i];
    }
    return min;
  }

  /**
   * Return minimum value of array, Integer.MAX_VALUE if no such value
   */
  public static int min(int[] a) {
    int min = Integer.MAX_VALUE;
    for (int i = 0; i < a.length; i++) {
      if (a[i] < min) min = a[i];
    }
    return min;
  }

  /**
   * Return average value in array, NaN if no such value.
   */
  public static double mean(double[] a) {
    if (a.length == 0) return Double.NaN;
    double sum = sum(a);
    return sum / a.length;
  }

  /**
   * Return average value in subarray a[lo..hi], NaN if no such value.
   */
  public static double mean(double[] a, int lo, int hi) {
    int length = hi - lo + 1;
    if (lo < 0 || hi >= a.length || lo > hi)
      throw new RuntimeException("Subarray indices out of bounds");
    if (length == 0) return Double.NaN;
    double sum = sum(a, lo, hi);
    return sum / length;
  }

  /**
   * Return average value in array, NaN if no such value.
   */
  public static double mean(int[] a) {
    if (a.length == 0) return Double.NaN;
    double sum = 0.0;
    for (int i = 0; i < a.length; i++) {
      sum = sum + a[i];
    }
    return sum / a.length;
  }

  /**
   * Return sample variance of array, NaN if no such value.
   */
  public static double var(double[] 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);
  }

  /**
   * Return sample variance of subarray a[lo..hi], NaN if no such value.
   */
  public static double var(double[] a, int lo, int hi) {
    int length = hi - lo + 1;
    if (lo < 0 || hi >= a.length || lo > hi)
      throw new RuntimeException("Subarray indices out of bounds");
    if (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 / (length - 1);
  }

  /**
   * Return sample variance of array, NaN if no such value.
   */
  public static double var(int[] 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);
  }

  /**
   * Return population variance of array, NaN if no such value.
   */
  public static double varp(double[] 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;
  }

  /**
   * Return population variance of subarray a[lo..hi],  NaN if no such value.
   */
  public static double varp(double[] a, int lo, int hi) {
    int length = hi - lo + 1;
    if (lo < 0 || hi >= a.length || lo > hi)
      throw new RuntimeException("Subarray indices out of bounds");
    if (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 / length;
  }


  /**
   * Return sample standard deviation of array, NaN if no such value.
   */
  public static double stddev(double[] a) {
    return Math.sqrt(var(a));
  }

  /**
   * Return sample standard deviation of subarray a[lo..hi], NaN if no such value.
   */
  public static double stddev(double[] a, int lo, int hi) {
    return Math.sqrt(var(a, lo, hi));
  }

  /**
   * Return sample standard deviation of array, NaN if no such value.
   */
  public static double stddev(int[] a) {
    return Math.sqrt(var(a));
  }

  /**
   * Return population standard deviation of array, NaN if no such value.
   */
  public static double stddevp(double[] a) {
    return Math.sqrt(varp(a));
  }

  /**
   * Return population standard deviation of subarray a[lo..hi], NaN if no such value.
   */
  public static double stddevp(double[] a, int lo, int hi) {
    return Math.sqrt(varp(a, lo, hi));
  }

  /**
   * Return sum of all values in array.
   */
  public static double sum(double[] a) {
    double sum = 0.0;
    for (int i = 0; i < a.length; i++) {
      sum += a[i];
    }
    return sum;
  }

  /**
   * Return sum of all values in subarray a[lo..hi].
   */
  public static double sum(double[] a, int lo, int hi) {
    if (lo < 0 || hi >= a.length || lo > hi)
      throw new RuntimeException("Subarray indices out of bounds");
    double sum = 0.0;
    for (int i = lo; i <= hi; i++) {
      sum += a[i];
    }
    return sum;
  }

  /**
   * Return sum of all values in array.
   */
  public static int sum(int[] a) {
    int sum = 0;
    for (int i = 0; i < a.length; i++) {
      sum += a[i];
    }
    return sum;
  }

  /**
   * Plot points (i, a[i]) to standard draw.
   */
  public static void plotPoints(double[] a) {
    int N = a.length;
    StdDraw.setXscale(0, N-1);
    StdDraw.setPenRadius(1.0 / (3.0 * N));
    for (int i = 0; i < N; i++) {
      StdDraw.point(i, a[i]);
    }
  }

  /**
   * Plot line segments connecting points (i, a[i]) to standard draw.
   */
  public static void plotLines(double[] a) {
    int N = a.length;
    StdDraw.setXscale(0, N-1);
    StdDraw.setPenRadius();
    for (int i = 1; i < N; i++) {
      StdDraw.line(i-1, a[i-1], i, a[i]);
    }
  }

  /**
   * Plot bars from (0, a[i]) to (i, a[i]) to standard draw.
   */
  public static void plotBars(double[] a) {
    int N = a.length;
    StdDraw.setXscale(0, N-1);
    for (int i = 0; i < N; i++) {
      StdDraw.filledRectangle(i, a[i]/2, .25, a[i]/2);
    }
  }


  /**
   * Test client.
   * Convert command-line arguments to array of doubles and call various methods.
   */
  public static void main(String[] args) {
    double[] a = ArrayGenerator.readDouble1D(new In ());
    StdOut.format("       min %7.3f\n", min(a));
    StdOut.format("      mean %7.3f\n", mean(a));
    StdOut.format("       max %7.3f\n", max(a));
    StdOut.format("   std dev %7.3f\n", stddev(a));
  }
}