@Override public Double get(Object key) { return delegate.get(key); }
/** * Compute the DCG of a list of items with respect to a value vector. */ double computeDCG(long[] items, Long2DoubleFunction values) { double gain = 0; int rank = 0; for (long item: items) { final double v = values.get(item); rank++; gain += v * discount.discount(rank); } return gain; }
@Override public double get(long l) { if (delegate.containsKey(l)) { return delegate.get(l); } else { return dft; } }
/** * Compute the DCG of a list of items with respect to a value vector. */ double computeDCG(List<Long> items, Long2DoubleFunction values) { double gain = 0; int rank = 0; for (long item: items) { final double v = values.get(item); rank++; gain += v * discount.discount(rank); } return gain; }
/** * Create a new map with existing data. * @param data Use {@link #create(Map)} instead, as it can avoid copying maps that are already packed. */ @Deprecated public Long2DoubleSortedArrayMap(Map<Long,Double> data) { Long2DoubleFunction vf = LongUtils.asLong2DoubleFunction(data); keys = SortedKeyIndex.fromCollection(data.keySet()); int size = keys.size(); values = new double[size]; for (int i = 0; i < size; i++) { values[i] = vf.get(keys.getKey(i)); } }
public Long2DoubleSortedMap clampVector(Map<Long,Double> scores) { SortedKeyIndex keys = SortedKeyIndex.fromCollection(scores.keySet()); Long2DoubleFunction baseVals = LongUtils.asLong2DoubleMap(scores); double[] values = new double[keys.size()]; for (int i = 0; i < values.length; i++) { long item = keys.getKey(i); values[i] = clampValue(baseVals.get(item)); } return Long2DoubleSortedArrayMap.wrap(keys, values); }
/** * Compute the dot product of two maps. This method assumes any value missing in one map is 0, so it is the dot * product of the values of common keys. * @param v1 The first vector. * @param v2 The second vector. * @return The sum of the products of corresponding values in the two vectors. */ public static double dotProduct(Long2DoubleMap v1, Long2DoubleMap v2) { if (v1.size() > v2.size()) { // compute dot product the other way around for speed return dotProduct(v2, v1); } if (v1 instanceof Long2DoubleSortedArrayMap && v2 instanceof Long2DoubleSortedArrayMap) { return dotProduct((Long2DoubleSortedArrayMap) v1, (Long2DoubleSortedArrayMap) v2); } else { double result = 0; Long2DoubleFunction v2d = adaptDefaultValue(v2, 0.0); Iterator<Long2DoubleMap.Entry> iter = fastEntryIterator(v1); while (iter.hasNext()) { Long2DoubleMap.Entry e = iter.next(); long k = e.getLongKey(); result += e.getDoubleValue() * v2d.get(k); // since default is 0 } return result; } }
@Nonnull @Override public ResultMap scoreWithDetails(long user, @Nonnull Collection<Long> items) { Long2DoubleMap userRatings = rvDAO.userRatingVector(user); if (userRatings.isEmpty()) { Map<Long, Double> scores = baseline.score(user, items); return Results.newResultMap(Iterables.transform(scores.entrySet(), Results.fromEntryFunction())); } else { // score everything, both rated and not, for offsets LongSet allItems = new LongOpenHashSet(userRatings.keySet()); allItems.addAll(items); Map<Long, Double> baseScores = baseline.score(user, allItems); Long2DoubleMap offsets = new Long2DoubleOpenHashMap(); // subtract scores from ratings, yielding offsets Long2DoubleFunction bsf = LongUtils.asLong2DoubleMap(baseScores); for (Long2DoubleMap.Entry e: userRatings.long2DoubleEntrySet()) { double base = bsf.get(e.getLongKey()); offsets.put(e.getLongKey(), e.getDoubleValue() - base); } double meanOffset = Vectors.sum(offsets) / (offsets.size() + damping); // to score: fill with baselines, add user mean offset List<Result> results = new ArrayList<>(items.size()); LongIterator iter = LongIterators.asLongIterator(items.iterator()); while (iter.hasNext()) { long item = iter.nextLong(); results.add(Results.create(item, bsf.get(item) + meanOffset)); } return Results.newResultMap(results); } }
/** * Add a vector to another (scaled) vector and a scalar. The result is \\(x_i + s_y y_i + o\\). * * @param x The source vector. * @param y The addition vector. {@link Long2DoubleFunction#defaultReturnValue()} is assumed for missing values. * @param sy The scale by which elements of {@code y} are multipled. * @param o The offset to add. * @return A vector with the same keys as {@code x}, transformed by the specified linear formula. */ public static Long2DoubleMap combine(Long2DoubleMap x, Long2DoubleFunction y, double sy, double o) { SortedKeyIndex idx = SortedKeyIndex.fromCollection(x.keySet()); final int n = idx.size(); double[] values = new double[n]; if (x instanceof Long2DoubleSortedArrayMap) { // TODO make this fast for two sorted maps Long2DoubleSortedArrayMap sx = (Long2DoubleSortedArrayMap) x; assert idx == sx.keySet().getIndex(); for (int i = 0; i < n; i++) { values[i] = sx.getValueByIndex(i) + y.get(idx.getKey(i)) * sy + o; } } else { for (int i = 0; i < n; i++) { long k = idx.getKey(i); values[i] = x.get(k) + y.get(k) * sy + o; } } return Long2DoubleSortedArrayMap.wrap(idx, values); }
@Override public double get(final long k) { return function.get(k); } @Override
/** * {@inheritDoc} * * @since 8.0.0 */ @Override default double applyAsDouble(long operand) { return get(operand); } /**
@Override public double get(final long k) { synchronized (sync) { return function.get(k); } } @Override
@Override public Double get(Object key) { return delegate.get(key); }
/** * {@inheritDoc} * * @deprecated Please use the corresponding type-specific method instead. */ @Deprecated @Override public Double get(final Object k) { return function.get(k); } /**
/** * {@inheritDoc} * * @deprecated Please use the corresponding type-specific method instead. */ @Deprecated @Override public Double get(final Object k) { synchronized (sync) { return function.get(k); } } /**
@Override public double get(long l) { if (delegate.containsKey(l)) { return delegate.get(l); } else { return dft; } }
/** * Compute the DCG of a list of items with respect to a value vector. */ double computeDCG(long[] items, Long2DoubleFunction values) { double gain = 0; int rank = 0; for (long item: items) { final double v = values.get(item); rank++; gain += v * discount.discount(rank); } return gain; }
public Long2DoubleSortedMap clampVector(Map<Long,Double> scores) { SortedKeyIndex keys = SortedKeyIndex.fromCollection(scores.keySet()); Long2DoubleFunction baseVals = LongUtils.asLong2DoubleFunction(scores); double[] values = new double[keys.size()]; for (int i = 0; i < values.length; i++) { long item = keys.getKey(i); values[i] = clampValue(baseVals.get(item)); } return Long2DoubleSortedArrayMap.wrap(keys, values); }