package Experiment; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Scanner; public class ResultsTable { private String name; private ArrayList table; private ArrayList errors; private ResultsRow header; int rows; int cols; public ResultsTable(String name) { this.name = name; table = new ArrayList(); header = new ResultsRow(); rows = 0; cols = 0; } public void addHeader(Object o) { header.add(o); for (ResultsRow row : table) row.add(null); cols++; } public void setEntry(int row, int col, Object o) { if (row < rows && col < cols) { table.get(row).set(col, o); } } public void SystemPrintTable() { System.out.println("Name: " + name + " || Rows : " + rows + " || Cols : " + cols); header.SystemPrintRow(); for (ResultsRow row : table) { row.SystemPrintRow(); } } public void castToDouble() { for (ResultsRow row : table) { row.castToDouble(); } } public void computeResults(String output) throws IOException { castToDouble(); ErrorRow tempE; double actual; errors = new ArrayList(); ArrayList mre = new ArrayList(); for (int i = 0; i < cols - 1; i++) mre.add(0.0); for (ResultsRow row : table) { tempE = new ErrorRow(cols - 1); actual = row.getDouble(0); for (int i = 1; i < row.getSize(); i++) { //Magnitude of Relative Error mre.set(i - 1, mre.get(i - 1) + Math.abs((actual - row.getDouble(i))/actual)); tempE.set(i - 1, Math.abs((actual - row.getDouble(i))/actual)); } errors.add(tempE); } ArrayList mww = computeMWW(); ArrayList results = new ArrayList(); for (int i = 0; i < cols - 1; i++) results.add(new Doublet(mww.get(i),mre.get(i) / rows)); Collections.sort(results); FileWriter writer = new FileWriter(output); for (int i = 0; i < cols - 1; i++) { String[] s = ((String) header.get(i + 1)).split("_"); writer.write(s[0] + "," + s[1] + "," + results.get(i).getOne() + "," + results.get(i).getTwo() + "\n"); } writer.close(); } public ArrayList computeMWW() { ArrayList mwwWins = new ArrayList(); ArrayList mwwLosses = new ArrayList(); ArrayList mwwTies = new ArrayList(); for (int i = 0; i < cols - 1; i++) { mwwWins.add(0.0); mwwLosses.add(0.0); mwwTies.add(0.0); } ArrayList temp; ArrayList rankings; Double[] tempD = new Double[2]; Double count; Double w; Double oW; Double zScore; Double xSum; Double ySum; for (int i = 0; i < cols - 2; i++) { for (int i2 = i; i2 < cols - 1; i2++) { temp = new ArrayList(); rankings = new ArrayList(); xSum = 0.0; ySum = 0.0; for (int j = 0; j < rows; j++) { // Find the difference between the two methods temp.add(errors.get(j).get(i) - errors.get(j).get(i2)); xSum += errors.get(j).get(i); ySum += errors.get(j).get(i2); } // Sort by the absolute values of this difference Collections.sort(temp, new AbsoluteComparator()); // Convert to absolute values and save ranking count = 1.0; for (Double d : temp) { if (Double.compare(d, 0.0) != 0) { tempD[0] = count; tempD[1] = Double.valueOf((double) Double.compare(d, 0.0)); rankings.add(tempD); count++; } } w = 0.0; // Calculate W for (Double[] d : rankings) { w = w + (d[1]*d[0]); } // Calculate oW oW = Math.sqrt((rankings.size() * (rankings.size() + 1) * ((2 * rankings.size())+1))/6); // Calculate zScore zScore = (w - 0.5) / oW; // Test significance at 0.95 level if (zScore > 1.645 & xSum/rows > ySum/rows) { mwwWins.set(i2, mwwWins.get(i2) + 1); mwwLosses.set(i, mwwWins.get(i) + 1); } else if (zScore < 1.645 & xSum/rows < ySum/rows) { mwwWins.set(i, mwwWins.get(i) + 1); mwwLosses.set(i2, mwwWins.get(i2) + 1); } else { mwwTies.set(i, mwwWins.get(i) + 1); mwwTies.set(i2, mwwWins.get(i2) + 1); } } } ArrayList percentLosses = new ArrayList(); for (int i = 0; i < mwwLosses.size(); i++) { percentLosses.add(mwwLosses.get(i)/(mwwLosses.get(i) + mwwWins.get(i) + mwwTies.get(i))); } return percentLosses; } public void collectSplit(long split, String results, String colName) throws FileNotFoundException { int colPos = header.colExists(colName); if (colPos < 0) { this.addHeader(colName); colPos = cols - 1; } ArrayList tempEntries = new ArrayList(); ArrayList tempActual = new ArrayList(); File f = new File(results); Scanner reader = new Scanner(f); String[] temp; while (reader.hasNext()) { temp = reader.next().split(","); if (temp.length >= 1) { tempActual.add(temp[0]); tempEntries.add(temp[temp.length - 1]); } } reader.close(); if (rows < (split + 1) * tempEntries.size()) { ResultsRow r; for (Object o : tempActual) { r = new ResultsRow(cols); r.set(0, o); table.add(r); rows++; } } int rowPos = (int) split * tempEntries.size() ; for (Object o : tempEntries) { this.setEntry(rowPos, colPos, o); rowPos++; } } class ResultsRow { private ArrayList row; private int size; public ResultsRow() { row = new ArrayList(); } public ResultsRow(int size) { row = new ArrayList(); for (int i = 0; i < size; i++) row.add(null); this.size = size; } public void add(Object o) { row.add(o); size++; } public void set(int col, Object o) { row.set(col, o); } public void SystemPrintRow() { String s = ""; for (int i = 0; i < size; i++) { if (i < row.size() - 1) s += row.get(i) + ","; else s += row.get(i); } s += "\n"; System.out.print(s); } public int colExists(String colName) { for (int i = 0; i < size; i++) { if (row.get(i).equals(colName)) return i; } return -1; } public void castToDouble() { for(Object o : row) { o = Double.valueOf((String) o); } } public double getDouble(int pos) { return Double.valueOf((String) row.get(pos)); } public Object get(int pos) { return row.get(pos); } public int getSize() { return size;} } class ErrorRow { private ArrayList errors; int size; public ErrorRow(int size) { errors = new ArrayList(); for (int i = 0; i < size; i++) errors.add(0.0); this.size = size; } public void set(int col, double d) { errors.set(col, d); } public void SystemPrintRow() { String s = ""; for (int i = 0; i < size; i++) { if (i < errors.size() - 1) s += errors.get(i) + ","; else s += errors.get(i); } s += "\n"; System.out.print(s); } public double get(int pos) { return errors.get(pos); } } public class Doublet implements Comparable { private double one; private double two; public Doublet(double one, double two) { this.one = one; this.two = two; } public int compare(Doublet d1, Doublet d2) { return Double.compare(d1.one, d2.one); } @Override public int compareTo(Object o) { return Double.compare(this.one, ((Doublet) o).one); } public String getOne() { return Double.toString(one);} public String getTwo() { return Double.toString(two);} } public class AbsoluteComparator implements Comparator { @Override public int compare(Double o1, Double o2) { return Double.compare(Math.abs(o1), Math.abs(o2)); } } }