package stat; import java.awt.*; import java.io.*; import parameters.Parameter; import parameters.ParameterSet; import parameters.ParameterSetArray; /** Saves the cross-correlation coefficient between each two parameters in the wheel plot. At the bottom of the file, also prints the largest x correlation coefficients where x is set in the code (currently set to 20). Does not test for significance yet. */ public class CrossCorrStat extends Stat implements Runnable { public CrossCorrStat() {} public void run() { int i, j, k, l; int num_largest = 20; String [] largest_names = new String[num_largest]; float [] largest_values = new float[num_largest]; for(k = 0; k < num_largest; k++) largest_values[k] = 0; if(paramSets == null) { System.out.println("No Cam loaded - Stat did nothing"); return; } FileDialog fd = new FileDialog(new Frame(), "Save into file:", FileDialog.SAVE); fd.show(); if(fd.getFile() == null) return; try { PrintWriter ps = new PrintWriter(new FileOutputStream(fd.getDirectory() + fd.getFile())); double [] mat = makeParameterMatrix(); if(mat == null) { System.out.println("Problem making matrix of parameters"); ps.close(); return; } if(numSets < 2) { System.out.println("You need at least two sets of parameters in the cam in order to get a correlation"); ps.close(); return; } // First print out labels ps.print("\t"); printParameterLabels(prototype, ps); ps.println(""); for(i = 0; i < prototype.getNumParams(); i++) { ps.print(prototype.getName(i) + "\t"); // Print the param label for this row for(j = 0; j <= i; j++) { float val = getCorrelation(mat, i, j); ps.print(val + "\t"); if(i != j) { k = 0; while(k < num_largest && Math.abs(val) < Math.abs(largest_values[k])) k++; if(k < num_largest) { // Move everything else down for(l = num_largest - 1; l > k; l--) { largest_names[l] = largest_names[l - 1]; largest_values[l] = largest_values[l - 1]; } largest_names[k] = new String(prototype.getName(i) + "-" + prototype.getName(j)); largest_values[k] = val; } } } ps.println(""); } for(k = 0; k < num_largest; k++) { System.out.println(largest_names[k] + "\t" + largest_values[k]); if(Math.abs(largest_values[k]) > -1) ps.println(largest_names[k] + "\t" + largest_values[k]); } } catch(Exception e) { System.out.println("Error in CrossCorrStat: " + e.toString()); } System.out.println("Finished calculating cross correlations"); } /** Returns the correlation coefficient between two parameters given the data in the input matrix. Not sure what stats book I pulled this formula from. */ float getCorrelation(double [] mat, int param1, int param2) { double sum1 = 0, sum2 = 0, sum_sqr1 = 0, sum_sqr2 = 0; int i, num_params = prototype.getNumParams(); for(i = 0; i < numSets; i++) { sum1 += mat[i * num_params + param1]; sum_sqr1 += mat[i * num_params + param1] * mat[i * num_params + param1]; sum2 += mat[i * num_params + param2]; sum_sqr2 += mat[i * num_params + param2] * mat[i * num_params + param2]; } double var1 = Math.sqrt((sum_sqr1 - (sum1 * sum1 / numSets)) / numSets); double var2 = Math.sqrt((sum_sqr2 - (sum2 * sum2 / numSets)) / numSets); sum1 /= numSets; sum2 /= numSets; float p = 0; for(i = 0; i < numSets; i++) { p += (mat[i * num_params + param1] - sum1) * (mat[i * num_params + param2] - sum2); } p /= numSets * var1 * var2; return p; } }