Example #1
0
  /**
   * Compare approximate and exact diameter computation. Approximate is very fast but provides only
   * a rough upper bound. For a discretized pairwise distance distribution, this is absolutely
   * sufficient.
   */
  public void testMaxPhaseSpaceDiameter() {

    int tsLen = 25000;
    TimeSeriesGenerator tsg = new TimeSeriesGenerator(1, tsLen, "lorenz");
    double[][][][] trajectories = tsg.getAllTrajectories();
    double[][] ts = trajectories[4][0];
    System.out.println("Computing max phase space diameter.");
    long tic = System.currentTimeMillis();
    double diam = PhaseSpaceDistribution.maxPhaseSpaceDiameter(ts, ts);
    System.out.println("diam = " + diam);
    long toc = System.currentTimeMillis();
    System.out.println(toc - tic);

    System.out.println("Computing max phase space diameter, approximate.");
    tic = System.currentTimeMillis();
    double diamApprox = PhaseSpaceDistribution.maxPhaseSpaceDiameterApproximate(ts);
    System.out.println("diamApprox = " + diamApprox);
    toc = System.currentTimeMillis();
    System.out.println(toc - tic);
    System.out.println("Relative error: " + (Math.abs(diamApprox - diam) / diam));

    //        GeometryFactory factory = new GeometryFactory(new
    // PrecisionModel(PrecisionModel.FIXED));
    //        Point[] points = new Point[tsLen];
    //        for (int i = 0; i < tsLen; i++) {
    //            Coordinate[] coordinates = new Coordinate[ts.length];
    //            for (int j = 0; j < ts.length; j++) {
    //                coordinates[j] = new Co
    //            }
    //            points[i] = new Point(new CoordinateArraySequence(coordinates), factory);
    //        }
    //        MultiPoint data = new MultiPoint(points, factory);
    //        MinimumDiameter minimumDiameter = new MinimumDiameter(geom);

  }
Example #2
0
  public void testPhaseSpaceApproxDistribution() {

    int tsLen = 500;
    TimeSeriesGenerator tsg = new TimeSeriesGenerator(1, tsLen, null);
    double[][][][] trajectories = tsg.getAllTrajectories();
    for (int systemIdx = 0; systemIdx < 9; systemIdx++) {
      double[][] ts = trajectories[systemIdx][0];
      long[] dist = PhaseSpaceDistribution.approximateDistanceDistribution(ts);
      System.out.println(Arrays.toString(dist));
    }
  }
  private static String processFile(File file) throws IOException {

    final String absolutePath = file.getAbsolutePath();
    final String filename = file.getName();

    System.out.println(String.format("Processing: %s", absolutePath));

    double[][] trajectory = BaseExecutable.readFile(absolutePath, ",");
    double[][] noisyTrajectory = TimeSeriesGenerator.addNoise(trajectory, 0.05, 0.1);

    PhaseSpaceReconstructed phaseSpaceReconstructed =
        new PhaseSpaceReconstructed(noisyTrajectory[0], 1);

    return String.format(
        "%s\t%s\t%s\t%s\t%s",
        filename,
        filename.split("\\.")[0].subSequence(6, filename.split("\\.")[0].length() - 2),
        phaseSpaceReconstructed.embedding_dimension,
        phaseSpaceReconstructed.embedding_delay,
        phaseSpaceReconstructed.min_embedding_delay);
  }
  public void testClustering() {

    final int numberOfTimeSeriesPerClass = 100;
    final int timeSeriesLength = 1500;
    final int sample_size = (9 * numberOfTimeSeriesPerClass) / 2;
    final int numberOfResamples = 100;
    final int maxIterations = 1000;
    final int numberOfInitializations = 100;

    // ------------------------------------
    // compute DRQA measures
    // ------------------------------------

    TimeSeriesGenerator timeSeriesGenerator =
        new TimeSeriesGenerator(numberOfTimeSeriesPerClass, timeSeriesLength, null);
    ArrayList<DoublePoint> rqa_measures =
        new ArrayList<DoublePoint>(9 * numberOfTimeSeriesPerClass);

    System.out.println("Computing DRQA measures");
    double[][][][] trajectories = timeSeriesGenerator.getAllTrajectories();
    for (int system_idx = 0; system_idx < trajectories.length; system_idx++) {
      System.out.println(String.format("System: %s", TimeSeriesGenerator.system_names[system_idx]));
      double[][][] trajectory1 = trajectories[system_idx];
      for (double[][] trajectory : trajectory1) {
        DRQA drqa = new DRQA(trajectory, trajectory, 0.05);
        drqa.computeRQA(2, 2, 2);
        //                rqa_measures[system_idx * numberOfTimeSeriesPerClass + i] = new
        // DoublePoint(drqa.allRQAMeasures(DRQA.HistogramStatistic.values()));
        rqa_measures.add(new DoublePoint(drqa.standardRQAMeasures()));
      }
    }
    System.out.println(
        String.format(
            "Standard RQA of first point:\n%s", Arrays.toString(rqa_measures.get(0).getPoint())));

    // ------------------------------------
    // Cluster
    // ------------------------------------

    long before = System.currentTimeMillis();

    RandomDataGenerator randomDataGenerator = new RandomDataGenerator();
    final EuclideanDistance euclideanDistance = new EuclideanDistance();
    ClusterEvaluator<DoublePoint> evaluator =
        new SumOfClusterVariances<DoublePoint>(euclideanDistance);

    ArrayList<DoublePoint> sample = new ArrayList<DoublePoint>(sample_size);
    System.out.println(String.format("sample_size: %s", sample_size));

    // generate random subsamples of the data set
    for (int resample_idx = 0; resample_idx < numberOfResamples; resample_idx++) {

      System.out.println(String.format("Resample: %s", resample_idx + 1));

      // generate random sample of half size
      Object[] randomSample = randomDataGenerator.nextSample(rqa_measures, sample_size);
      sample.clear();
      for (Object point : randomSample) sample.add((DoublePoint) point);

      int bestK = 0;
      double bestScore = Double.POSITIVE_INFINITY;
      // cluster using different numbers of clusters
      for (int k = 2; k < 17; k++) {

        // seed the algorithm several times to have a better chance to escape local maxima
        // this can also be done using
        // org.apache.commons.math3.ml.clustering.MultiKMeansPlusPlusClusterer,
        // but it doesn't return the actual scores, which is important in determining the number of
        // clusters.
        double bestInitScore = Double.POSITIVE_INFINITY;
        for (int init_num = 0; init_num < numberOfInitializations; init_num++) {
          KMeansPlusPlusClusterer<DoublePoint> clusterer =
              new KMeansPlusPlusClusterer<DoublePoint>(
                  k, maxIterations, euclideanDistance, new ISAACRandom(init_num));
          List<CentroidCluster<DoublePoint>> result = clusterer.cluster(sample);

          final double score = evaluator.score(result);
          bestInitScore = Math.min(bestInitScore, score);
        } // for initializations
        if (bestInitScore < bestScore) {
          bestScore = bestInitScore;
          bestK = k;
        }
        //                for(CentroidCluster<DoublePoint> cc : result)
        // System.out.println(Arrays.toString(cc.getCenter().getPoint()));
      } // for number of clusters
      System.out.println(String.format("Best k=%s, score=%s", bestK, bestScore));
    } // for resampling

    System.out.println(
        String.format("Time for clustering: %s", System.currentTimeMillis() - before));
  }