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

The following examples show how to use com.google.common.math.DoubleMath#log2() . 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: AbstractIdentifierRenamings.java    From naturalize with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public SortedSet<Renaming> calculateScores(
		final Multiset<NGram<String>> ngrams,
		final Set<String> alternatives, final Scope scope) {
	final SortedSet<Renaming> scoreMap = Sets.newTreeSet();

	for (final String identifierName : alternatives) {
		double score = 0;
		for (final Entry<NGram<String>> ngram : ngrams.entrySet()) {
			try {
				final NGram<String> identNGram = NGram.substituteTokenWith(
						ngram.getElement(), WILDCARD_TOKEN, identifierName);
				final double ngramScore = scoreNgram(identNGram);
				score += DoubleMath.log2(ngramScore) * ngram.getCount();
			} catch (final Throwable e) {
				LOGGER.warning(ExceptionUtils.getFullStackTrace(e));
			}
		}
		scoreMap.add(new Renaming(identifierName, (addScopePriors(
				identifierName, scope) - score) / ngrams.size(), ngrams
				.size() / ngramLM.getN(), scope));
	}

	return scoreMap;
}
 
Example 2
Source File: FormattingRenamings.java    From naturalize with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Predict the max-likelihood tokens given the ngrams.
 * 
 * @param ngrams
 * @param alternatives
 * @return
 */
@Override
public SortedSet<Renaming> calculateScores(
		final Multiset<NGram<String>> ngrams,
		final Set<String> alternatives, final Scope scope) {
	final SortedSet<Renaming> suggestions = Sets.newTreeSet();

	for (final String alternative : alternatives) {
		double score = 0;
		for (final NGram<String> ngram : ngrams) {
			score += DoubleMath.log2(getNgramLM().getProbabilityFor(
					NGram.substituteTokenWith(ngram, WILDCARD_TOKEN,
							alternative)));
		}
		suggestions.add(new Renaming(alternative, -score, 1, null));
	}
	return suggestions;
}
 
Example 3
Source File: StatsUtil.java    From api-mining with GNU General Public License v3.0 5 votes vote down vote up
public static double log2SumOfExponentials(final double... values) {
	if (values.length == 1) {
		return values[0];
	}
	final double max = max(values);
	double sum = 0.0;
	for (final double value : values) {
		if (value != Double.NEGATIVE_INFINITY) {
			sum += Math.pow(2, value - max);
		}
	}
	return max + DoubleMath.log2(sum);
}
 
Example 4
Source File: SnippetScorer.java    From naturalize with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Get the probability of a specific renaming happening (taking at least
 * one of the suggestions).
 * 
 * @param suggestions
 * @return
 */
public double getLogProbOfNotRenaming() {
	double logProbNotRename = 0;
	for (final Suggestion sg : suggestions) {
		final double pNotRename = sg.getProbNotRename();
		checkArgument(pNotRename <= 1, pNotRename
				+ " should be in the probability range");
		checkArgument(pNotRename >= 0, pNotRename
				+ " should be in the probability range");
		logProbNotRename += DoubleMath.log2(pNotRename);
	}
	return logProbNotRename;
}
 
Example 5
Source File: AllPriorIdentifierRenaming.java    From naturalize with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private double addGrammarPrior(final String identifierName,
		final Scope scope) {
	if (!USE_GRAMMAR) {
		return 0;
	}
	final double prob = gp.getMLProbability(identifierName,
			Pair.create(scope.astNodeType, scope.astParentNodeType));
	if (prob > 0) {
		return -DoubleMath.log2(prob);
	} else if (!this.isTrueUNK(identifierName)) {
		return 6;
	} else {
		return 0;
	}
}
 
Example 6
Source File: AllPriorIdentifierRenaming.java    From naturalize with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private double addTypePrior(final String identifierName, final Scope scope) {
	if (!USE_TYPES) {
		return 0;
	}
	final double prob = tp.getMLProbability(identifierName, scope.type);
	if (prob > 0) {
		return -DoubleMath.log2(prob);
	} else if (!this.isTrueUNK(identifierName)) {
		return 6;
	} else {
		return 0;
	}
}
 
Example 7
Source File: GrammarPriorIdentifierRenaming.java    From naturalize with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected double addScopePriors(final String identifierName,
		final Scope scope) {
	final double prob = gp.getMLProbability(identifierName,
			Pair.create(scope.astNodeType, scope.astParentNodeType));
	if (prob > 0) {
		return -DoubleMath.log2(prob);
	} else if (!this.isTrueUNK(identifierName)) {
		return 100;
	} else {
		return 0;
	}
}
 
Example 8
Source File: LocalTypedIdentifierRenamings.java    From naturalize with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public double addScopePriors(final String identifierName, final Scope scope) {
	final double prob = tp.getMLProbability(identifierName, scope.type);
	if (prob > 0) {
		return -DoubleMath.log2(prob);
	} else if (!this.isTrueUNK(identifierName)) {
		return 100;
	} else {
		return 0;
	}
}
 
Example 9
Source File: StatsUtil.java    From tassal with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static double log2SumOfExponentials(final double... values) {
	if (values.length == 1) {
		return values[0];
	}
	final double max = max(values);
	double sum = 0.0;
	for (final double value : values) {
		if (value != Double.NEGATIVE_INFINITY) {
			sum += Math.pow(2, value - max);
		}
	}
	return max + DoubleMath.log2(sum);
}
 
Example 10
Source File: EvaluatorFactory.java    From OpenModsLib with MIT License 4 votes vote down vote up
@Override
protected float evaluate(float arg) {
	return (float)DoubleMath.log2(arg);
}
 
Example 11
Source File: GeometricDistribution.java    From api-mining with GNU General Public License v3.0 4 votes vote down vote up
public static double getLog2Prob(int x, double p) {
	final double logP = DoubleMath.log2(p);
	final double log1_p = DoubleMath.log2(1 - p);
	return (x - 1) * log1_p + logP;
}
 
Example 12
Source File: GuavaMathUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void should_calculate_log_2() {
    int result6 = DoubleMath.log2(10, RoundingMode.UP);
    assertThat(result6, equalTo(4));
}
 
Example 13
Source File: GuavaDoubleMathUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test(expected = ArithmeticException.class)
public void whenLog2DoubleValues_shouldThrowArithmeticExceptionIfRoundingNotDefinedButNecessary() {
    DoubleMath.log2(30, RoundingMode.UNNECESSARY);
}
 
Example 14
Source File: GuavaDoubleMathUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void whenLog2DoubleValues_shouldog2ThemAndReturnTheResultForFloorRounding() {
    int result = DoubleMath.log2(30, RoundingMode.FLOOR);
    assertEquals(4, result);
}
 
Example 15
Source File: GuavaDoubleMathUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void whenLog2DoubleValues_shouldLog2ThemAndReturnTheResultForCeilingRounding() {
    int result = DoubleMath.log2(30, RoundingMode.CEILING);
    assertEquals(5, result);
}
 
Example 16
Source File: GuavaDoubleMathUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void whenLog2Double_shouldReturnResult() {
    double result = DoubleMath.log2(4);
    assertEquals(2, result, 0);
}
 
Example 17
Source File: GeometricDistribution.java    From tassal with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static double getLog2Prob(int x, double p) {
	final double logP = DoubleMath.log2(p);
	final double log1_p = DoubleMath.log2(1 - p);
	return (x - 1) * log1_p + logP;
}
 
Example 18
Source File: StatsUtil.java    From tassal with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Code ported from LingPipe This method returns the log of the sum of the
 * natural exponentiated values in the specified array. Mathematically, the
 * result is
 *
 * <blockquote>
 *
 * <pre>
 * logSumOfExponentials(xs) = log <big><big>( &Sigma;</big></big><sub>i</sub> exp(xs[i]) <big><big>)</big></big>
 * </pre>
 *
 * </blockquote>
 *
 * But the result is not calculated directly. Instead, the calculation
 * performed is:
 *
 * <blockquote>
 *
 * <pre>
 * logSumOfExponentials(xs) = max(xs) + log <big><big>( &Sigma;</big></big><sub>i</sub> exp(xs[i] - max(xs)) <big><big>)</big></big>
 * </pre>
 *
 * </blockquote>
 *
 * which produces the same result, but is much more arithmetically stable,
 * because the largest value for which <code>exp()</code> is calculated is
 * 0.0.
 *
 * <p>
 * Values of {@code Double.NEGATIVE_INFINITY} are treated as having
 * exponentials of 0 and logs of negative infinity. That is, they are
 * ignored for the purposes of this computation.
 *
 * @param values
 *            Array of values.
 * @return The log of the sum of the exponentiated values in the array.
 */
public static double log2SumOfExponentials(final Collection<Double> values) {
	if (values.size() == 1) {
		return values.iterator().next();
	}
	final double max = max(values);
	double sum = 0.0;
	for (final double value : values) {
		if (value != Double.NEGATIVE_INFINITY) {
			sum += Math.pow(2, value - max);
		}
	}
	return max + DoubleMath.log2(sum);
}
 
Example 19
Source File: StatsUtil.java    From api-mining with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Code ported from LingPipe This method returns the log of the sum of the
 * natural exponentiated values in the specified array. Mathematically, the
 * result is
 *
 * <blockquote>
 *
 * <pre>
 * logSumOfExponentials(xs) = log <big><big>( &Sigma;</big></big><sub>i</sub> exp(xs[i]) <big><big>)</big></big>
 * </pre>
 *
 * </blockquote>
 *
 * But the result is not calculated directly. Instead, the calculation
 * performed is:
 *
 * <blockquote>
 *
 * <pre>
 * logSumOfExponentials(xs) = max(xs) + log <big><big>( &Sigma;</big></big><sub>i</sub> exp(xs[i] - max(xs)) <big><big>)</big></big>
 * </pre>
 *
 * </blockquote>
 *
 * which produces the same result, but is much more arithmetically stable,
 * because the largest value for which <code>exp()</code> is calculated is
 * 0.0.
 *
 * <p>
 * Values of {@code Double.NEGATIVE_INFINITY} are treated as having
 * exponentials of 0 and logs of negative infinity. That is, they are
 * ignored for the purposes of this computation.
 *
 * @param values
 *            Array of values.
 * @return The log of the sum of the exponentiated values in the array.
 */
public static double log2SumOfExponentials(final Collection<Double> values) {
	if (values.size() == 1) {
		return values.iterator().next();
	}
	final double max = max(values);
	double sum = 0.0;
	for (final double value : values) {
		if (value != Double.NEGATIVE_INFINITY) {
			sum += Math.pow(2, value - max);
		}
	}
	return max + DoubleMath.log2(sum);
}