1 /* 2 [The "BSD license"] 3 Copyright (c) 2005-2009 Terence Parr 4 All rights reserved. 5 6 Redistribution and use in source and binary forms, with or without 7 modification, are permitted provided that the following conditions 8 are met: 9 1. Redistributions of source code must retain the above copyright 10 notice, this list of conditions and the following disclaimer. 11 2. Redistributions in binary form must reproduce the above copyright 12 notice, this list of conditions and the following disclaimer in the 13 documentation and/or other materials provided with the distribution. 14 3. The name of the author may not be used to endorse or promote products 15 derived from this software without specific prior written permission. 16 17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 package org.antlr.runtime.misc; 29 30 import java.io.*; 31 import java.util.List; 32 33 /** Stats routines needed by profiler etc... 34 35 // note that these routines return 0.0 if no values exist in the X[] 36 // which is not "correct", but it is useful so I don't generate NaN 37 // in my output 38 39 */ 40 public class Stats { 41 public static final String ANTLRWORKS_DIR = "antlrworks"; 42 43 /** Compute the sample (unbiased estimator) standard deviation following: 44 * 45 * Computing Deviations: Standard Accuracy 46 * Tony F. Chan and John Gregg Lewis 47 * Stanford University 48 * Communications of ACM September 1979 of Volume 22 the ACM Number 9 49 * 50 * The "two-pass" method from the paper; supposed to have better 51 * numerical properties than the textbook summation/sqrt. To me 52 * this looks like the textbook method, but I ain't no numerical 53 * methods guy. 54 */ 55 public static double stddev(int[] X) { 56 int m = X.length; 57 if ( m<=1 ) { 58 return 0; 59 } 60 double xbar = avg(X); 61 double s2 = 0.0; 62 for (int i=0; i<m; i++){ 63 s2 += (X[i] - xbar)*(X[i] - xbar); 64 } 65 s2 = s2/(m-1); 66 return Math.sqrt(s2); 67 } 68 69 /** Compute the sample mean */ 70 public static double avg(int[] X) { 71 double xbar = 0.0; 72 int m = X.length; 73 if ( m==0 ) { 74 return 0; 75 } 76 for (int i=0; i<m; i++){ 77 xbar += X[i]; 78 } 79 if ( xbar>=0.0 ) { 80 return xbar / m; 81 } 82 return 0.0; 83 } 84 85 public static int min(int[] X) { 86 int min = Integer.MAX_VALUE; 87 int m = X.length; 88 if ( m==0 ) { 89 return 0; 90 } 91 for (int i=0; i<m; i++){ 92 if ( X[i] < min ) { 93 min = X[i]; 94 } 95 } 96 return min; 97 } 98 99 public static int max(int[] X) { 100 int max = Integer.MIN_VALUE; 101 int m = X.length; 102 if ( m==0 ) { 103 return 0; 104 } 105 for (int i=0; i<m; i++){ 106 if ( X[i] > max ) { 107 max = X[i]; 108 } 109 } 110 return max; 111 } 112 113 /** Compute the sample mean */ 114 public static double avg(List<Integer> X) { 115 double xbar = 0.0; 116 int m = X.size(); 117 if ( m==0 ) { 118 return 0; 119 } 120 for (int i=0; i<m; i++){ 121 xbar += X.get(i); 122 } 123 if ( xbar>=0.0 ) { 124 return xbar / m; 125 } 126 return 0.0; 127 } 128 129 public static int min(List<Integer> X) { 130 int min = Integer.MAX_VALUE; 131 int m = X.size(); 132 if ( m==0 ) { 133 return 0; 134 } 135 for (int i=0; i<m; i++){ 136 if ( X.get(i) < min ) { 137 min = X.get(i); 138 } 139 } 140 return min; 141 } 142 143 public static int max(List<Integer> X) { 144 int max = Integer.MIN_VALUE; 145 int m = X.size(); 146 if ( m==0 ) { 147 return 0; 148 } 149 for (int i=0; i<m; i++){ 150 if ( X.get(i) > max ) { 151 max = X.get(i); 152 } 153 } 154 return max; 155 } 156 157 public static int sum(int[] X) { 158 int s = 0; 159 int m = X.length; 160 if ( m==0 ) { 161 return 0; 162 } 163 for (int i=0; i<m; i++){ 164 s += X[i]; 165 } 166 return s; 167 } 168 169 public static void writeReport(String filename, String data) throws IOException { 170 String absoluteFilename = getAbsoluteFileName(filename); 171 File f = new File(absoluteFilename); 172 File parent = f.getParentFile(); 173 parent.mkdirs(); // ensure parent dir exists 174 // write file 175 FileOutputStream fos = new FileOutputStream(f, true); // append 176 BufferedOutputStream bos = new BufferedOutputStream(fos); 177 PrintStream ps = new PrintStream(bos); 178 ps.println(data); 179 ps.close(); 180 bos.close(); 181 fos.close(); 182 } 183 184 public static String getAbsoluteFileName(String filename) { 185 return System.getProperty("user.home")+File.separator+ 186 ANTLRWORKS_DIR +File.separator+ 187 filename; 188 } 189 } 190