Example #1
0
 public static double link(double r) {
   if (r < 0.0) {
     double s = Math.exp(r);
     return s / (1.0 + s);
   } else {
     double s = Math.exp(-r);
     return 1.0 / (1.0 + s);
   }
 }
Example #2
0
 public static double sumOfPowerOfDeviations(double[] data, int k, double c) {
   double sum = 0;
   for (int i = 0; i < data.length; i++) {
     sum += Math.pow(data[i] - c, k);
   }
   return sum;
 }
Example #3
0
 public static double skew(double[] data) {
   double sum = 0;
   double sumPD = 0;
   double sumSq = 0;
   for (int i = 0; i < data.length; i++) {
     sum += data[i];
     sumSq += data[i] * data[i];
   }
   double mean = sum / data.length;
   double standardDeviation = Math.sqrt((sumSq - mean * sum) / data.length);
   for (int i = 0; i < data.length; i++) {
     sumPD += Math.pow(data[i] - mean, 3);
   }
   double moment3 = sumPD / data.length;
   return moment3 / (standardDeviation * standardDeviation * standardDeviation);
 }
Example #4
0
 public static double sumOfLogarithms(double[] elements) {
   double logsum = 0;
   for (int i = 0; i < elements.length; i++) {
     logsum += Math.log(elements[i]);
   }
   return logsum;
 }
Example #5
0
 public static double meanDeviation(double[] elements, double mean) {
   int size = elements.length;
   double sum = 0;
   for (int i = size; --i >= 0; ) {
     sum += Math.abs(elements[i] - mean);
   }
   return sum / size;
 }
Example #6
0
 public static double kurtosis(double[] data) {
   double sum = 0;
   double sumPD = 0;
   double sumSq = 0;
   for (int i = 0; i < data.length; i++) {
     sum += data[i];
     sumSq += data[i] * data[i];
   }
   double mean = sum / data.length;
   double standardDeviation = Math.sqrt((--sumSq - mean * sum) / data.length);
   for (int i = 0; i < data.length; i++) {
     sumPD += Math.pow(data[i] - mean, 4);
   }
   double moment4 = sumPD / data.length;
   return -3
       + moment4 / (standardDeviation * standardDeviation * standardDeviation * standardDeviation);
 }
Example #7
0
 public static double logGamma(double xx) {
   double[] cof = {
     76.18009172947146,
     -86.50532032941677,
     24.01409824083091,
     -1.231739572450155,
     0.1208650973866179e-2,
     -0.5395239384953e-5
   };
   double x = xx - 1.0;
   double tmp = x + 5.5;
   tmp -= (x + 0.5) * Math.log(tmp);
   double ser = 1.000000000190015;
   double[] coeff = cof;
   for (int j = 0; j <= 5; j++) {
     x++;
     ser += coeff[j] / x;
   }
   return -tmp + Math.log(2.5066282746310005 * ser);
 }
Example #8
0
 public static double[] standardize(double[] data) {
   double sum = 0;
   double sumSq = 0;
   for (int i = 0; i < data.length; i++) {
     sum += data[i];
     sumSq += data[i] * data[i];
   }
   double mean = sum / data.length;
   double sd = Math.sqrt((sumSq - mean * sum) / data.length);
   for (int i = 0; i < data.length; i++) {
     data[i] = (data[i] - mean) / sd;
   }
   return data;
 }
Example #9
0
  /** Randomly sets K temporary cluster centers */
  private static void setRandomCenters() {

    centers = new ArrayList<Point>();

    int rand;
    for (int i = 0; i < K; ++i) {
      do {
        rand = (int) (Math.random() * points.size());
      } while (centers.contains(points.get(rand)));
      centers.add(points.get(rand));
      clusters.get(i).addCenterPoint(points.get(rand));
    }

    for (Point p : centers) System.out.println(p);
  }
Example #10
0
 public static double[] power(double[] data, int k) {
   for (int i = 0; i < data.length; i++) {
     data[i] = Math.pow(data[i], k);
   }
   return data;
 }
Example #11
0
 public static double geometricPdf(int k, double p) {
   return p * Math.pow(1 - p, k);
 }
Example #12
0
 public static double sampleSkew(int size, double moment3, double sampleVariance) {
   int n = size;
   double s = Math.sqrt(sampleVariance);
   double m3 = moment3 * n;
   return n * m3 / ((n - 1) * (n - 2) * s * s * s);
 }
Example #13
0
 public static double sampleKurtosisStandardError(int size) {
   int n = size;
   return Math.sqrt(24.0 * n * (n-- - 1) * (n - 1) / ((n - 3) * (n - 2) * (n + 3) * (n + 5)));
 }
Example #14
0
  public static void main(String[] args) {

    // TEST MEASURE
    //        Point p1 = new Point(-1d, -1d);
    //        Point p2 = new Point(2d, 3d);
    //        System.out.println(measure.d(p1, p2));
    //        System.out.println(measure.s(p1, p2));
    //        return;

    Double[][] data = FileHandler.readFile(fileName);

    // cannot display points if dimension is > 2
    if (data[0].length != 2) canDisplay = false;

    // build graphic points from coords' array
    buildPointsFromData(data);
    Config.computeBoundingRect(points);

    // init display
    if (canDisplay) {
      disp = new Display();
      disp.setVisible(true);
      for (Point p : points) {
        disp.addObject(p);
      }
    }

    testResults = new double[nbTests];

    for (int t = 0; t < nbTests; ++t) {

      // define K clusters and K temporary centres
      clusters = new ArrayList<Cluster>();
      for (int i = 0; i < K; ++i) {
        clusters.add(new Cluster());
      }
      setRandomCenters();
      for (Cluster c : clusters) {
        System.out.println("center for cluster " + c + ": " + c.getCenter());
      }

      if (canDisplay) pause(1000);

      // variables used in for loops
      double minDist, currDist, diff;
      Double[] prevCoords, newCoords;
      Cluster alloc;
      Point newCenter;

      for (int i = 0; i < maxIter; ++i) {

        if (canDisplay) {
          disp.setLabel("[ iteration #" + (i + 1) + " ]");
        } else {
          System.out.println("------> iteration #" + (i + 1));
        }

        // allocate points to group which center is closest
        for (Point p : points) {

          minDist = Config.MAX;
          alloc = clusters.get(0); // default initialization

          for (Cluster c : clusters) {
            currDist = measure.d(p, c.getCenter());
            if (currDist < minDist) {
              minDist = currDist;
              alloc = c;
            }
          }

          alloc.addPoint(p);
        }

        // recenter: calculate gravity centers for formed groups
        diff = 0;
        prevCoords = null;
        for (Cluster c : clusters) {

          // delete previous center if it not a Point of the Cluster
          if (canDisplay && !c.getPoints().contains(c.getCenter())) {
            disp.removeObject(c.getCenter());
          }

          if (stopOnConverge) {
            prevCoords = c.getCenter().getCoords();
          }

          newCenter = c.makeGravityCenter();

          if (stopOnConverge) {
            newCoords = c.getCenter().getCoords();
            for (int k = 0; k < prevCoords.length; ++k) {
              diff += Math.abs(prevCoords[k] - newCoords[k]);
            }
          }

          if (canDisplay) {
            disp.addObject(newCenter);
          } else {
            // System.out.println("\tcenter for " + c + ": " + c.getCenter());
            System.out.println(c.getCenter());
          }
        } // loop over clusters

        if (canDisplay) {
          disp.repaint();
        }

        // if Clusters' centers don't change anymore, then stop (algorithm converged)
        if (diff == 0 && stopOnConverge) {
          testResults[t] = (double) i;
          if (canDisplay) {
            disp.setLabel("[ Converged at iteration #" + (i) + " ]");
            disp.repaint();
          } else {
            System.out.println("[ Converged at iteration #" + (i) + " ]");
          }
          break;
        }

        pause(100);
      } // loop over iterations

      if (testResults[t] == 0) {
        System.out.println("!!!!!!!!!! Test #" + t + " did not converge.");
        if (stopOnConverge) return;
      }

      // reset display
      if (canDisplay && t + 1 < nbTests) {
        for (Point p : points) p.setCluster(null);
        for (Cluster c : clusters) disp.removeObject(c.getCenter());
      }
    } // loop over tests

    // display test results and compute means
    DescriptiveStatistics stats = new DescriptiveStatistics(testResults);
    System.out.println("=========> Results:");
    for (int t = 0; t < nbTests; ++t) {
      System.out.println("--> [ " + testResults[t] + " ]");
    }
    System.out.println("=========> Means: " + stats.getMean());
    System.out.println("=========> Std dev: " + stats.getStandardDeviation());
  }