/******************************************************************************* * Name: T. Shy * Login: jsshy * Precept: P00 * * Compilation: javac PlotTukey.java * Execution: java PlotTukey N * Dependencies: StdDraw.java * * Draws Tukey plots for N random doubles in the range [0., 1.). * N is the first command-line argument to the program. * ******************************************************************************/ public class PlotTukey { //calculate the mean of a[0..n] public static double mean(double[] a, int n) { double sum = 0.0; for (int i = 0; i <= n; i++) sum += a[i]; return sum/(n + 1); } //calculate the std. err. of the mean of a[0..n] public static double sigma(double[] a, int n) { double mean = mean(a, n); double var = 0.0; //one point case if (n <= 0) return 0.0; for (int i = 0; i <= n; i++) { double d = mean - a[i]; var += d*d; } var /= n; return Math.sqrt(var/(n + 1)); } public static void main(String[] args) { int N = Integer.parseInt(args[0]); final double PEN_RADIUS = 0.5/N; double[] a = new double[N]; //initialize "a" with random double values for (int i = 0; i < N; i++) { a[i] = Math.random(); } StdDraw.setXscale(0, N-1); if (N > 0) StdDraw.setPenRadius(PEN_RADIUS); for (int i = 0; i < N; i++) { double mean = mean(a, i); double sig = sigma(a, i); StdDraw.setPenColor(StdDraw.BLACK); StdDraw.point(i, a[i]); //draw the std. dev. lines StdDraw.line(i, mean + sig, i, mean + 2*sig); StdDraw.line(i, mean - sig, i, mean - 2*sig); StdDraw.setPenColor(StdDraw.RED); StdDraw.point(i, mean); } } }