public Value<V> sum(){ if(this.weights != null){ throw new IllegalStateException(); } return this.values.sum(); }
public Value<V> weightedSum(){ if(this.weights == null || this.weightedValues == null){ throw new IllegalArgumentException(); } return this.weightedValues.sum(); }
@Override public Value<V> apply(Vector<V> values){ if(this.size == 0){ throw new UndefinedResultException(); } return (values.sum()).divide(this.size); } };
public ValueMap<String, V> weightedAverageMap(){ if(this.weights == null){ throw new IllegalStateException(); } Function<Vector<V>, Value<V>> function = new Function<Vector<V>, Value<V>>(){ private Value<V> weightSum = ProbabilityAggregator.this.weights.sum(); @Override public Value<V> apply(Vector<V> values){ if(this.weightSum.equals(0d)){ throw new UndefinedResultException(); } return (values.sum()).divide(this.weightSum); } }; return new ValueMap<>(asTransformedMap(function)); }
public Value<V> average(){ if(this.weights != null){ throw new IllegalStateException(); } int size = this.values.size(); if(size == 0){ throw new UndefinedResultException(); } return (this.values.sum()).divide(size); }
@Override public Value<V> apply(Vector<V> values){ if(this.weightSum.equals(0d)){ throw new UndefinedResultException(); } return (values.sum()).divide(this.weightSum); } };
public Value<V> weightedMedian(){ if(this.weights == null){ throw new IllegalStateException(); } int size = this.values.size(); if(size == 0){ throw new UndefinedResultException(); } List<Entry> entries = new ArrayList<>(size); for(int i = 0; i < size; i++){ Entry entry = new Entry(this.values.get(i), this.weights.get(i)); entries.add(entry); } Collections.sort(entries); double weightSumThreshold = 0.5d * (this.weights.sum()).doubleValue(); double weightSum = 0d; // Naive, brute-force search. // It is assumed that entries have unique ordering (at least in the area of the weighted median) // and that the calculation may be performed using the "whole median" approach // (as opposed to the "split median" approach). for(Entry entry : entries){ weightSum += (entry.weight).doubleValue(); if(weightSum >= weightSumThreshold){ return entry.value; } } throw new NotImplementedException(); }
Value<V> result = distances.sum();