Java Code Examples for com.google.common.math.DoubleMath#fuzzyCompare()

The following examples show how to use com.google.common.math.DoubleMath#fuzzyCompare() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GraphGenerator.java    From ambiverse-nlu with Apache License 2.0 6 votes vote down vote up
/**
 * Prunes candidates, keeping only the top K elements for each mention. K is
 * set in graphSettings.
 *
 * @param mention
 *
 * @return
 */
private Entities pruneCandidates(Mention mention) {
  Entities bestEntities = null;
  if (settings.getGraphSettings().shouldPruneCandidateEntities()) {
    int k = settings.getGraphSettings().getPruneCandidateThreshold();
    Entities candidates = mention.getCandidateEntities();
    if (candidates.size() > k) {
      Ordering<Entity> order = new Ordering<Entity>() {

        @Override public int compare(Entity e1, Entity e2) {
          return DoubleMath.fuzzyCompare(e1.getMentionEntitySimilarity(), e2.getMentionEntitySimilarity(), EPSILON);
        }
      };
      List<Entity> topEntities = order.greatestOf(candidates, k);
      bestEntities = new Entities();
      bestEntities.addAll(topEntities);
    }
  }
  return bestEntities;
}
 
Example 2
Source File: CubeUtils.java    From incubator-pinot with Apache License 2.0 6 votes vote down vote up
/**
 * Flips parent's change ratio if the change ratios of current node and its parent are different.
 *
 * @param baselineValue the baseline value of a node.
 * @param currentValue the current value of a node.
 * @param ratio the (parent) ratio to be flipped.
 *
 * @return the ratio that has the same direction as the change direction of baseline and current value.
 */
public static double ensureChangeRatioDirection(double baselineValue, double currentValue, double ratio) {
  // case: value goes down but parent's value goes up
  if (DoubleMath.fuzzyCompare(baselineValue, currentValue, epsilon) > 0 && DoubleMath.fuzzyCompare(ratio, 1, epsilon) > 0) {
    if (Double.compare(ratio, 2) >= 0) {
      ratio = 2d - (ratio - ((long) ratio - 1));
    } else {
      ratio = 2d - ratio;
    }
    // case: value goes up but parent's value goes down
  } else if (DoubleMath.fuzzyCompare(baselineValue, currentValue, epsilon) < 0 && DoubleMath.fuzzyCompare(ratio, 1, epsilon) < 0) {
    ratio = 2d - ratio;
  }
  // return the original ratio for other cases.
  return ratio;
}
 
Example 3
Source File: RatioCubeNode.java    From incubator-pinot with Apache License 2.0 6 votes vote down vote up
@Override
public boolean side() {
  double currentValue = getCurrentValue();
  double baselineValue = getBaselineValue();
  if (!DoubleMath.fuzzyEquals(currentValue, 0, epsilon) && !DoubleMath.fuzzyEquals(baselineValue, 0, epsilon)) {
    // The most common case is located first in order to reduce performance impact
    return DoubleMath.fuzzyCompare(currentValue, baselineValue, epsilon) >= 0;
  } else {
    if (parent != null) {
      if (DoubleMath.fuzzyEquals(currentValue, 0, epsilon) && DoubleMath.fuzzyEquals(baselineValue, 0, epsilon)) {
        return parent.side();
      } else if (DoubleMath.fuzzyEquals(currentValue, 0, epsilon)) {
        return DoubleMath.fuzzyCompare(baselineValue, parent.getBaselineValue(), epsilon) < 0;
      } else { //if (DoubleMath.fuzzyEquals(baselineValue, 0, epsilon)) {
        return DoubleMath.fuzzyCompare(currentValue, parent.getCurrentValue(), epsilon) >= 0;
      }
    } else {
      return DoubleMath.fuzzyCompare(currentValue, baselineValue, epsilon) >= 0;
    }
  }
}
 
Example 4
Source File: TransverseMercator.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public boolean equals(Object o) {
  if (this == o)
    return true;
  if (o == null || getClass() != o.getClass())
    return false;

  TransverseMercator that = (TransverseMercator) o;
  double tolerance = 1e-6;

  if (DoubleMath.fuzzyCompare(that.earthRadius, earthRadius, tolerance) != 0)
    return false;
  if (DoubleMath.fuzzyCompare(that.falseEasting, falseEasting, tolerance) != 0)
    return false;
  if (DoubleMath.fuzzyCompare(that.falseNorthing, falseNorthing, tolerance) != 0)
    return false;
  if (DoubleMath.fuzzyCompare(that.lat0, lat0, tolerance) != 0)
    return false;
  if (DoubleMath.fuzzyCompare(that.lon0, lon0, tolerance) != 0)
    return false;
  if (DoubleMath.fuzzyCompare(that.scale, scale, tolerance) != 0)
    return false;

  if ((defaultMapArea == null) != (that.defaultMapArea == null))
    return false; // common case is that these are null
  return defaultMapArea == null || that.defaultMapArea.equals(defaultMapArea);

}
 
Example 5
Source File: ProjectionRect.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Returns {@code true} if this bounding box contains {@code point}.
 *
 * @param point a point in projection coordinates.
 * @return {@code true} if this bounding box contains {@code point}.
 */
public boolean contains(ProjectionPoint point) {
  return DoubleMath.fuzzyCompare(point.getX(), getMinX(), 1e-6) >= 0
      && DoubleMath.fuzzyCompare(point.getX(), getMaxX(), 1e-6) <= 0
      && DoubleMath.fuzzyCompare(point.getY(), getMinY(), 1e-6) >= 0
      && DoubleMath.fuzzyCompare(point.getY(), getMaxY(), 1e-6) <= 0;
}
 
Example 6
Source File: GraphGenerator.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
/**
 * Checks if the coherence robustness check fires. If local sim and prior
 * agree on an entity, use it.
 *
 * @param mentionL1s
 * @param currentMention
 * @param isNamedEntity 
 * @return best candidate or null if check failed.
 */
private Entity doCoherenceRobustnessCheck(Mention currentMention, Map<Mention, Double> mentionL1s, boolean isNamedEntity) {
  Entity bestCandidate = null;
  if (settings.getGraphSettings().shouldUseCoherenceRobustnessTest(isNamedEntity)) {
    if (mentionL1s.containsKey(currentMention) &&
        DoubleMath.fuzzyCompare(mentionL1s.get(currentMention),
        settings.getGraphSettings().getCohRobustnessThreshold(isNamedEntity), EPSILON) < 0) {
      bestCandidate = getBestCandidate(currentMention);
    }
  }
  return bestCandidate;
}
 
Example 7
Source File: GraphGenerator.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
/**
 * Checks if the confidence of a mention disambiguation by local sim alone is 
 * high enough to fix it.
 *
 * @param mention
 * @return best candidate or null if check failed.
 */
private Entity doConfidenceThresholdCheck(Mention mention, TIntDoubleHashMap normalizedEntityLocalSims) {
  Entity bestEntity = null;
  if (settings.getGraphSettings().shouldUseConfidenceThresholdTest()) {
    double max = CollectionUtils.getMaxValue(normalizedEntityLocalSims);
    if (DoubleMath.fuzzyCompare(max, settings.getGraphSettings().getConfidenceTestThreshold(), EPSILON) > 0) {
      bestEntity = getBestCandidate(mention);
    }
  }
  return bestEntity;
}
 
Example 8
Source File: FSTCursor.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
public int dicardContinueOrfuzzyMatching(int currentCharacterPosition, double minMatchingRatio) {
  int explored = currentCharacterPosition - characterStart + 1; //The number of characters explored so far
  double current_ratio = matches / (double) explored; //The ratio of hits
  if(matches < 3 || explored - matches > 3) { //If there is less that 2 hits or more than 3 misses
    return -1; //Discard
  }
  if(DoubleMath.fuzzyCompare(current_ratio, minMatchingRatio, 0.0001) >= 0) {
    return 1; //Include
  } else if(DoubleMath.fuzzyCompare(minMatchingRatio, 1, 0.0001) < 1
      && explored - matches <= 2) {
    return 1; //Include
  } else {
    return 0; //continue
  }
}
 
Example 9
Source File: TsdbResult.java    From splicer with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a DataPoint[] -sorted by timestamp - of the reciprocal of points
 * in the Points map eg 1 / value (needed for division)
 * @return
 * @throws Exception
 */
public DataPoint[] getDataPointsFromTreeMapReciprocal() throws Exception {
	TreeMap<String, Object> treeMap = new TreeMap(map);
	DataPoint[] dps = new DataPoint[treeMap.size()];
	int index = 0;
	for(Map.Entry<String, Object> entry: treeMap.entrySet()) {
		MutableDataPoint dp = new MutableDataPoint();
		Object val = entry.getValue();
		long timestamp = Long.valueOf(entry.getKey());
		if(val instanceof Double) {
			double doubleVal = ((Double) val).doubleValue();
			if (DoubleMath.fuzzyCompare(doubleVal, 0, 1E-7) != 0) {
				dp.reset(timestamp, 1 / doubleVal);
			} else {
				dp.reset(timestamp, Double.POSITIVE_INFINITY);
			}
		} else if(val instanceof Long) {
			long longVal = ((Long) val).longValue();
			if(longVal != 0) {
				dp.reset(timestamp, 1 / longVal);
			} else {
				dp.reset(timestamp, Double.POSITIVE_INFINITY);
			}
		} else {
			throw new Exception("Unexpected type in map: " + val.getClass());
		}
		dps[index] = dp;
		index++;
	}

	return dps;
}
 
Example 10
Source File: BrooklynImageChooser.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
protected static int compare(double left, double right) {
    return DoubleMath.fuzzyCompare(left, right, 0.00000001);
}
 
Example 11
Source File: RatioCostFunction.java    From incubator-pinot with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the cost that consider change difference, change changeRatio, and node size.
 *
 * In brief, this function uses this formula to compute the cost:
 *   change difference * log(contribution percentage * change changeRatio)
 *
 * In addition, if a node size to overall data is smaller than 1%, then the cost is always zero.
 *
 * @param parentChangeRatio the changeRatio between baseline and current value of parent node.
 * @param baselineValue the baseline value of the current node.
 * @param currentValue the current value of the current node.
 * @param baselineSize the size of baseline node.
 * @param currentSize the size of current node.
 * @param topBaselineValue the baseline value of the top node.
 * @param topCurrentValue the current value of the top node.
 * @param topBaselineSize the size of baseline data cube .
 * @param topCurrentSize the size of current data cube .
 *
 * @return the cost that consider change difference, change changeRatio, and node size.
 */
@Override
public double computeCost(double parentChangeRatio, double baselineValue, double currentValue, double baselineSize,
    double currentSize, double topBaselineValue, double topCurrentValue, double topBaselineSize,
    double topCurrentSize) {

  // Contribution is the size of the node
  double sizeFactor = (baselineSize + currentSize) / (topBaselineSize + topCurrentSize);
  // Ignore <1% nodes
  if (DoubleMath.fuzzyCompare(sizeFactor, minSizeFactor, epsilon) < 0) {
    return 0d;
  }
  Preconditions.checkState(DoubleMath.fuzzyCompare(sizeFactor,0, epsilon) >= 0, "Contribution {} is smaller than 0.", sizeFactor);
  Preconditions.checkState(DoubleMath.fuzzyCompare(sizeFactor,1, epsilon) <= 0, "Contribution {} is larger than 1", sizeFactor);
  // The cost function considers change difference, change changeRatio, and node size (i.e., sizeFactor)
  return fillEmptyValuesAndGetError(baselineValue, currentValue, parentChangeRatio, sizeFactor);
}
 
Example 12
Source File: GuavaDoubleMathUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void whenFuzzyCompareDouble_shouldReturnZeroIfInRange() {
    int result = DoubleMath.fuzzyCompare(4, 4.05, 0.6);
    assertEquals(0, result);
}
 
Example 13
Source File: GuavaDoubleMathUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void whenFuzzyCompareDouble_shouldReturnNonZeroIfNotInRange() {
    int result = DoubleMath.fuzzyCompare(4, 5, 0.1);
    assertEquals(-1, result);
}