Difference between revisions of "User:Skilgannon/Free Code"

From Robowiki
Jump to navigation Jump to search
(a quick algorithm)
 
m (Using <syntaxhighlight>.)
 
(5 intermediate revisions by one other user not shown)
Line 1: Line 1:
This is a neat method that I made up. It takes array of 'indexes' between 0 and max and returns a double between 0 and 1 of how 'clustered' your array is. 1 if all the values are the same, and 0 if there are infinite values spread perfectly evenly. Note, this is very different from a standard deviation calculation. In this code there can be as many 'dense' points on the graph as you want, and it won't try to accommodate them all from one mean. Instead, it relies on the fact that (d + 1)*(d + 1) is always greater than (d + 1) for any d > 0.
+
This is a neat method that I made up. It takes array of 'indexes' (be it guess factors, or indexes where you are logging hits) between 0 and max and returns a double between 0 and 1 of how 'clustered' your array is. 1 if all the values are the same, and 0 if there are infinite values spread perfectly evenly. Note, this is very different from a standard deviation calculation. In this code there can be as many 'dense' points on the graph as you want, and it won't try to accommodate them all from one mean. Instead, it relies on the fact that  
 +
<math> (d+k)^2 + (d-k)^2 > 2 \times d^2  </math>
  
<pre>  
+
<syntaxhighlight>  
 
       public static double clustering(float[] indexes, float max){
 
       public static double clustering(float[] indexes, float max){
     
 
 
         float[] sorted = new float[indexes.length];
 
         float[] sorted = new float[indexes.length];
 
         System.arraycopy(indexes,0,sorted,0,indexes.length);
 
         System.arraycopy(indexes,0,sorted,0,indexes.length);
Line 9: Line 9:
 
          
 
          
 
         double clustering = sorted[0] + max  
 
         double clustering = sorted[0] + max  
             - sorted[sorted.length - 1] + 1;
+
             - sorted[sorted.length - 1];
 
         clustering *= clustering;
 
         clustering *= clustering;
 
          
 
          
        float min = sorted[0];
 
 
         for(int i = 1; i < sorted.length; i++){
 
         for(int i = 1; i < sorted.length; i++){
             double diff = sorted[i] - min + 1;
+
             double diff = sorted[i] - sorted[i-1];
 
             clustering += diff*diff;
 
             clustering += diff*diff;
            min = sorted[i];
 
 
         }       
 
         }       
         return (clustering - sorted.length + 1)/((max + 1)*(max + 1));
+
         return clustering/(max*max);
 
      
 
      
 
       }
 
       }
</pre>
+
</syntaxhighlight>

Latest revision as of 10:38, 1 July 2010

This is a neat method that I made up. It takes array of 'indexes' (be it guess factors, or indexes where you are logging hits) between 0 and max and returns a double between 0 and 1 of how 'clustered' your array is. 1 if all the values are the same, and 0 if there are infinite values spread perfectly evenly. Note, this is very different from a standard deviation calculation. In this code there can be as many 'dense' points on the graph as you want, and it won't try to accommodate them all from one mean. Instead, it relies on the fact that <math> (d+k)^2 + (d-k)^2 > 2 \times d^2 </math>

 
       public static double clustering(float[] indexes, float max){
         float[] sorted = new float[indexes.length];
         System.arraycopy(indexes,0,sorted,0,indexes.length);
         java.util.Arrays.sort(sorted);
         
         double clustering = sorted[0] + max 
            - sorted[sorted.length - 1];
         clustering *= clustering;
         
         for(int i = 1; i < sorted.length; i++){
            double diff = sorted[i] - sorted[i-1];
            clustering += diff*diff;
         }      
         return clustering/(max*max);
      	
      }