Example usage for org.apache.commons.math3.linear RealVector getMaxIndex

List of usage examples for org.apache.commons.math3.linear RealVector getMaxIndex

Introduction

In this page you can find the example usage for org.apache.commons.math3.linear RealVector getMaxIndex.

Prototype

public int getMaxIndex() 

Source Link

Document

Get the index of the maximum entry.

Usage

From source file:edu.byu.nlp.math.RealVectors.java

/**
 * Computes the log of the sum of the exponentiated elements of a vector. For any index j:
 * /*w  ww .  j a  v a  2s  .c o m*/
 * <pre>
 * log(e^{x_1} + e^{x_2} + ...) = log(e^{x_j} * sum\_{i \neq j} e^{x_i - x_j} + 1)
  *                              = x_j + log(sum\_{i \neq j} e^{x_i - x_j} + 1)
  * </pre>
  * 
  * This method ignores elements that are twenty orders of magnitude different than x_j.
  * 
  * @throws NullPointerException if vector is null
  */
public static double logSumSloppy(RealVector x) {
    Preconditions.checkNotNull(x);

    // TODO(rah67): consider just using the first element
    // use max as j
    int argMax = x.getMaxIndex();
    double max = x.getEntry(argMax);

    if (max == Double.NEGATIVE_INFINITY)
        return Double.NEGATIVE_INFINITY;

    return DoubleArrays.logSum(x.toArray());
}

From source file:com.yahoo.semsearch.fastlinking.DPSearch.java

/**
 *
 * @param forwardPath lattice of forwardPath
 * @param backwardPath lattice of backwardPath
 * @param nBestList array of arraylists (of string type) with n-best list for each entity span
 * @return best forward + best backward path
 *///from   w  w w .j a v  a 2  s . com
public String bestMergedPath(double[][] forwardPath, double[][] backwardPath, List<String>[] nBestList) {
    int length = forwardPath.length;
    for (int i = 0; i < forwardPath.length; i++) {
        for (int j = 0; j < nBestList[i].size(); j++) {
            //System.out.println(forwardPath[i][j] + ", " + backwardPath[length - 1 - i][j] + ": " + nBestList[i].get(j));

            forwardPath[i][j] += backwardPath[length - 1 - i][j];
        }
    }
    StringBuilder bestPath = new StringBuilder();
    for (int i = 0; i < forwardPath.length; i++) {
        RealVector realVector = new ArrayRealVector(forwardPath[i]);
        int bestPathIndex = realVector.getMaxIndex();
        bestPath.append(nBestList[i].get(bestPathIndex));
        bestPath.append(CANDIDATE_DELIMITER);
    }
    return bestPath.toString();
}

From source file:com.yahoo.semsearch.fastlinking.DPSearch.java

/**
 *
 * @param nBestList an array of arraylists of wiki entities; for each entity span we have a list of candidate
 *                       wikilinks (nbest list)
 * @param surfaceStrings  an array of entity spans
 * @return String of coherent entities//from www.j  ava  2 s .  c  o m
 */
public String bestForwardPath(List<String>[] nBestList, String[] surfaceStrings) {
    DPSearch forwardPath = dynamicProgrammingSearch(nBestList, surfaceStrings);
    RealVector realVector = new ArrayRealVector(forwardPath.logSimilarity[surfaceStrings.length - 1]);
    int bestPathIndex = realVector.getMaxIndex();
    return forwardPath.path[surfaceStrings.length - 1][bestPathIndex];
}

From source file:com.yahoo.semsearch.fastlinking.DPSearch.java

/**
 * Run forward-backward algorithm on multiple entity candidates and returns a filtered list of coherent entities
 * Takes nbest list and//from w w w. ja  v a  2  s. com
 * @param nBestList an array of arraylists of wiki entities; for each entity span we have a list of candidate
 *                       wikilinks (nbest list)
 * @param entitySurfaceForms an array of entity spans
 * @return DPSearch object that contains lattice of paths and likelihood scores
 */
public DPSearch dynamicProgrammingSearch(List<String>[] nBestList, String[] entitySurfaceForms) {

    int sequenceLength = entitySurfaceForms.length;
    int nBestLength = MAXNBEST;
    double[][] logSimilarityOverall = new double[sequenceLength][nBestLength];
    for (int i = 0; i < logSimilarityOverall.length; i++) {
        Arrays.fill(logSimilarityOverall[i], DEFAULT_LOG_LIKELIHOOD);
    }
    String[][] path = new String[sequenceLength][nBestLength];
    for (int i = 0; i < path.length; i++) {
        Arrays.fill(path[i], "");
    }
    ListIterator<String> it = nBestList[0].listIterator();
    while (it.hasNext()) {
        //MAKE SURE to call NextIndex before Next

        int index = it.nextIndex();
        String currentCandidate = it.next();

        double entity2WordSim = gensimEntityEmbeddings.entity2WordSimilarity(prependWiki(currentCandidate),
                entitySurfaceForms[0].replace(" ", "_"));
        double lexicalSim = gensimEntityEmbeddings.lexicalSimilarity(currentCandidate.replace("_", " "),
                entitySurfaceForms[0]);

        logSimilarityOverall[0][index] = Math.max(
                Math.log((1 - LEXSIM_LAMBDA) * entity2WordSim + LEXSIM_LAMBDA * lexicalSim),
                DEFAULT_LOG_LIKELIHOOD);

        path[0][index] = currentCandidate;
    }

    ListIterator<String> currentCandidateIterator, previousCandidateIterator;
    for (int i = 1; i < sequenceLength; i++) {
        currentCandidateIterator = nBestList[i].listIterator();

        while (currentCandidateIterator.hasNext()) {

            //MAKE SURE to call NextIndex before Next
            int currentCandidateIndex = currentCandidateIterator.nextIndex();
            String currentCandidate = currentCandidateIterator.next();

            double entity2WordSim = gensimEntityEmbeddings.entity2WordSimilarity(prependWiki(currentCandidate),
                    entitySurfaceForms[i].replace(" ", "_"));
            double lexicalSim = gensimEntityEmbeddings.lexicalSimilarity(currentCandidate.replace("_", " "),
                    entitySurfaceForms[i]);

            double candidateNBestSimilarity = Math
                    .log((1 - LEXSIM_LAMBDA) * entity2WordSim + LEXSIM_LAMBDA * lexicalSim);

            double bestSimilarity = 0.0;
            double interCandidateSimilarity = 0.0;
            int previousBestCandidateIndex = -1;
            previousCandidateIterator = nBestList[i - 1].listIterator();
            while (previousCandidateIterator.hasNext()) {

                //MAKE SURE to call NextIndex before Next

                int index = previousCandidateIterator.nextIndex();
                String previousCandidate = previousCandidateIterator.next();

                double entity2EntitySimilarity = gensimEntityEmbeddings
                        .entity2EntitySimilarity(prependWiki(previousCandidate), prependWiki(currentCandidate));

                double entity2EntityLexicalSimilarity = gensimEntityEmbeddings.lexicalSimilarity(
                        previousCandidate.replace("_", " "), currentCandidate.replace("_", " "));

                double jointSimilarity = (1 - LEXSIM_LAMBDA) * entity2EntitySimilarity
                        + LEXSIM_LAMBDA * entity2EntityLexicalSimilarity;
                interCandidateSimilarity = Math.log(jointSimilarity);

                if (bestSimilarity == 0.0) {
                    bestSimilarity = interCandidateSimilarity + logSimilarityOverall[i - 1][index];
                    previousBestCandidateIndex = index;

                } else if (interCandidateSimilarity + logSimilarityOverall[i - 1][index] > bestSimilarity) {
                    bestSimilarity = interCandidateSimilarity + logSimilarityOverall[i - 1][index];
                    previousBestCandidateIndex = index;

                }
            }
            try {
                logSimilarityOverall[i][currentCandidateIndex] = Math
                        .max(bestSimilarity + candidateNBestSimilarity, DEFAULT_LOG_LIKELIHOOD);

                path[i][currentCandidateIndex] = path[i - 1][previousBestCandidateIndex] + CANDIDATE_DELIMITER
                        + currentCandidate;

            } catch (ArrayIndexOutOfBoundsException e) {
                e.getMessage();
            }
        }

    }
    RealVector realVector = new ArrayRealVector(logSimilarityOverall[sequenceLength - 1]);
    int bestPathIndex = realVector.getMaxIndex();

    DPSearch dpSearch = new DPSearch(logSimilarityOverall, path);
    return dpSearch;
}

From source file:org.lenskit.predict.ordrec.OrdRecRatingPredictor.java

@Nonnull
private ResultMap computePredictions(long user, @Nonnull Collection<Long> items, boolean includeDetails) {
    logger.debug("predicting {} items for {}", items.size(), user);
    SparseVector ratings = makeUserVector(user, userEventDao);
    LongSet allItems = new LongOpenHashSet(ratings.keySet());
    allItems.addAll(items);//from  w  w  w .  ja  v  a2  s .  c o m

    ResultMap baseResults = null;
    Map<Long, Double> scores;
    if (includeDetails) {
        baseResults = itemScorer.scoreWithDetails(user, allItems);
        scores = baseResults.scoreMap();
    } else {
        scores = itemScorer.score(user, allItems);
    }
    MutableSparseVector scoreVector = MutableSparseVector.create(scores);

    OrdRecModel params = new OrdRecModel(quantizer);
    trainModel(params, ratings, scoreVector);
    logger.debug("trained parameters for {}: {}", user, params);

    RealVector probabilities = new ArrayRealVector(params.getLevelCount());

    List<Result> results = new ArrayList<>();

    LongIterator iter = LongIterators.asLongIterator(items.iterator());
    while (iter.hasNext()) {
        final long item = iter.nextLong();
        double score = scoreVector.get(item, Double.NaN);
        if (Double.isNaN(score)) {
            continue;
        }
        params.getProbDistribution(score, probabilities);
        int mlIdx = probabilities.getMaxIndex();
        double pred = quantizer.getIndexValue(mlIdx);
        if (includeDetails) {
            results.add(new FullResult(baseResults.get(item), pred, new ArrayRealVector(probabilities)));
        } else {
            results.add(Results.create(item, pred));
        }
    }

    return Results.newResultMap(results);
}