static private Integer toInteger(long value){ if(value < Integer.MIN_VALUE || value > Integer.MAX_VALUE){ throw new UndefinedResultException(); } return Integer.valueOf((int)value); }
@Override public int intValue(){ long value = longValue(); if(value < Integer.MIN_VALUE || value > Integer.MAX_VALUE){ throw new UndefinedResultException(); } return (int)value; }
public Value<V> median(){ if(this.weights != null){ throw new IllegalStateException(); } int size = this.values.size(); if(size == 0){ throw new UndefinedResultException(); } return this.values.median(); }
@Override public Value<V> apply(Vector<V> values){ if(this.size == 0){ throw new UndefinedResultException(); } return (values.sum()).divide(this.size); } };
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> weightedAverage(){ if(this.weights == null || this.weightedValues == null){ throw new IllegalStateException(); } Value<V> weightSum = this.weights.sum(); if(weightSum.equals(0d)){ throw new UndefinedResultException(); } return (this.weightedValues.sum()).divide(weightSum); }
static public <V extends Number> Value<V> calculateAdjustment(ValueFactory<V> valueFactory, List<FieldValue> values, List<? extends Number> adjustmentValues){ Value<V> sum = valueFactory.newValue(); Value<V> nonmissingSum = valueFactory.newValue(); for(int i = 0; i < values.size(); i++){ FieldValue value = values.get(i); double adjustmentValue = (adjustmentValues != null ? (adjustmentValues.get(i)).doubleValue() : 1d); if(adjustmentValue != 0d){ sum.add(adjustmentValue); if(value != null){ nonmissingSum.add(adjustmentValue); } } } if(nonmissingSum.equals(0d)){ throw new UndefinedResultException(); } return sum.divide(nonmissingSum); } }
static public <V extends Number> void normalizeSimpleMax(Iterable<Value<V>> values){ Value<V> sum = sum(values); if(sum == null || sum.equals(1d)){ return; } else if(sum.equals(0d)){ throw new UndefinedResultException(); } for(Value<V> value : values){ value.divide(sum); } }
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(); }
private FieldValue evaluate(FieldValue left, FieldValue right){ // "If one of the input fields of a simple arithmetic function is a missing value, then the result evaluates to missing value" if(Objects.equals(FieldValues.MISSING_VALUE, left) || Objects.equals(FieldValues.MISSING_VALUE, right)){ return FieldValues.MISSING_VALUE; } DataType dataType = TypeUtil.getCommonDataType(left.getDataType(), right.getDataType()); Number result; try { result = evaluate(left.asNumber(), right.asNumber()); } catch(ArithmeticException ae){ throw new UndefinedResultException() .initCause(ae); } return FieldValueUtil.create(dataType, OpType.CONTINUOUS, result); } }
private Value<V> computeProbability(String category){ ValueMap<String, V> values = getValues(); if(this.sum == null){ throw new EvaluationException("Vote distribution result has not been computed"); } Value<V> probability = values.get(category); if(probability != null){ probability = probability.copy(); if(this.sum.equals(0d)){ throw new UndefinedResultException(); } probability.divide(this.sum); } return probability; } }
throw new UndefinedResultException() .ensureContext(characteristic);