예제 #1
0
    /**
     * Removes the point with the given index.
     *
     * @param index the index to remove
     */
    public void removePoint(int index) {
      distanceMatrix.remove(index);

      for (List<Pair<Integer, Double>> distances : distanceMatrix) {
        ListIterator<Pair<Integer, Double>> iterator = distances.listIterator();

        while (iterator.hasNext()) {
          Pair<Integer, Double> point = iterator.next();

          if (point.getFirst() == index) {
            iterator.remove();
          } else if (point.getFirst() > index) {
            // decrement the index so it stays aligned with the
            // index in distanceMatrix
            iterator.set(new Pair<Integer, Double>(point.getFirst() - 1, point.getSecond()));
          }
        }
      }
    }
  private Collection<List<TimeSample>> collectUserTimeSamples(Pair<User, User> pair) {
    Collection<List<TimeSample>> datasets = new ArrayList<>();
    // for (User user : users) {
    UserData userData = this.usersData.get(pair.getFirst());
    datasets.add(new ArrayList<>(userData.getBtData()));
    datasets.add(new ArrayList<>(userData.getActivityData()));
    datasets.add(new ArrayList<>(userData.getWifiData()));
    datasets.add(new ArrayList<>(userData.getAudioData()));

    userData = this.usersData.get(pair.getSecond());
    datasets.add(new ArrayList<>(userData.getBtData()));
    datasets.add(new ArrayList<>(userData.getActivityData()));
    datasets.add(new ArrayList<>(userData.getWifiData()));
    datasets.add(new ArrayList<>(userData.getAudioData()));
    //	}
    return datasets;
  }
예제 #3
0
  /**
   * Computes the Kendall's Tau rank correlation coefficient between the two arrays.
   *
   * @param xArray first data array
   * @param yArray second data array
   * @return Returns Kendall's Tau rank correlation coefficient for the two arrays
   * @throws DimensionMismatchException if the arrays lengths do not match
   */
  public double correlation(final double[] xArray, final double[] yArray)
      throws DimensionMismatchException {

    if (xArray.length != yArray.length) {
      throw new DimensionMismatchException(xArray.length, yArray.length);
    }

    final int n = xArray.length;
    final long numPairs = n * (n - 1l) / 2l;

    @SuppressWarnings("unchecked")
    Pair<Double, Double>[] pairs = new Pair[n];
    for (int i = 0; i < n; i++) {
      pairs[i] = new Pair<Double, Double>(xArray[i], yArray[i]);
    }

    Arrays.sort(
        pairs,
        new Comparator<Pair<Double, Double>>() {
          public int compare(Pair<Double, Double> pair1, Pair<Double, Double> pair2) {
            int compareFirst = pair1.getFirst().compareTo(pair2.getFirst());
            return compareFirst != 0
                ? compareFirst
                : pair1.getSecond().compareTo(pair2.getSecond());
          }
        });

    int tiedXPairs = 0;
    int tiedXYPairs = 0;
    int consecutiveXTies = 1;
    int consecutiveXYTies = 1;
    Pair<Double, Double> prev = pairs[0];
    for (int i = 1; i < n; i++) {
      final Pair<Double, Double> curr = pairs[i];
      if (curr.getFirst().equals(prev.getFirst())) {
        consecutiveXTies++;
        if (curr.getSecond().equals(prev.getSecond())) {
          consecutiveXYTies++;
        } else {
          tiedXYPairs += consecutiveXYTies * (consecutiveXYTies - 1) / 2;
          consecutiveXYTies = 1;
        }
      } else {
        tiedXPairs += consecutiveXTies * (consecutiveXTies - 1) / 2;
        consecutiveXTies = 1;
        tiedXYPairs += consecutiveXYTies * (consecutiveXYTies - 1) / 2;
        consecutiveXYTies = 1;
      }
      prev = curr;
    }
    tiedXPairs += consecutiveXTies * (consecutiveXTies - 1) / 2;
    tiedXYPairs += consecutiveXYTies * (consecutiveXYTies - 1) / 2;

    int swaps = 0;
    @SuppressWarnings("unchecked")
    Pair<Double, Double>[] pairsDestination = new Pair[n];
    for (int segmentSize = 1; segmentSize < n; segmentSize <<= 1) {
      for (int offset = 0; offset < n; offset += 2 * segmentSize) {
        int i = offset;
        final int iEnd = FastMath.min(i + segmentSize, n);
        int j = iEnd;
        final int jEnd = FastMath.min(j + segmentSize, n);

        int copyLocation = offset;
        while (i < iEnd || j < jEnd) {
          if (i < iEnd) {
            if (j < jEnd) {
              if (pairs[i].getSecond().compareTo(pairs[j].getSecond()) <= 0) {
                pairsDestination[copyLocation] = pairs[i];
                i++;
              } else {
                pairsDestination[copyLocation] = pairs[j];
                j++;
                swaps += iEnd - i;
              }
            } else {
              pairsDestination[copyLocation] = pairs[i];
              i++;
            }
          } else {
            pairsDestination[copyLocation] = pairs[j];
            j++;
          }
          copyLocation++;
        }
      }
      final Pair<Double, Double>[] pairsTemp = pairs;
      pairs = pairsDestination;
      pairsDestination = pairsTemp;
    }

    int tiedYPairs = 0;
    int consecutiveYTies = 1;
    prev = pairs[0];
    for (int i = 1; i < n; i++) {
      final Pair<Double, Double> curr = pairs[i];
      if (curr.getSecond().equals(prev.getSecond())) {
        consecutiveYTies++;
      } else {
        tiedYPairs += consecutiveYTies * (consecutiveYTies - 1) / 2;
        consecutiveYTies = 1;
      }
      prev = curr;
    }
    tiedYPairs += consecutiveYTies * (consecutiveYTies - 1) / 2;

    int concordantMinusDiscordant = numPairs - tiedXPairs - tiedYPairs + tiedXYPairs - 2 * swaps;
    return concordantMinusDiscordant
        / FastMath.sqrt((numPairs - tiedXPairs) * (numPairs - tiedYPairs));
  }
 private void loadFeaturesData(Pair<User, User> pair) {
   usersData.put(pair.getFirst(), new UserFeaturesDataLoader(pair.getFirst(), interval).get());
   usersData.put(pair.getSecond(), new UserFeaturesDataLoader(pair.getSecond(), interval).get());
 }