@Test public void evaluateSimilarity(){ BitSet flags = createFlags(Arrays.asList(0, 0, 1, 1)); BitSet referenceFlags = createFlags(Arrays.asList(0, 1, 0, 1)); ValueFactory<?> valueFactory = MeasureUtilTest.valueFactoryFactory.newValueFactory(MathContext.DOUBLE); ComparisonMeasure comparisonMeasure = new ComparisonMeasure(ComparisonMeasure.Kind.SIMILARITY, new SimpleMatching()); List<ClusteringField> clusteringFields = createClusteringFields("one", "two", "three", "four"); assertEquals(valueFactory.newValue(2d / 4d), MeasureUtil.evaluateSimilarity(valueFactory, comparisonMeasure, clusteringFields, flags, referenceFlags)); comparisonMeasure.setMeasure(new Jaccard()); assertEquals(valueFactory.newValue(1d / 3d), MeasureUtil.evaluateSimilarity(valueFactory, comparisonMeasure, clusteringFields, flags, referenceFlags)); comparisonMeasure.setMeasure(new Tanimoto()); assertEquals(valueFactory.newValue(2d / (1d + 2 * 2d + 1d)), MeasureUtil.evaluateSimilarity(valueFactory, comparisonMeasure, clusteringFields, flags, referenceFlags)); comparisonMeasure.setMeasure(new BinarySimilarity(0.5d, 0.5d, 0.5d, 0.5d, 1d, 1d, 1d, 1d)); assertEquals(valueFactory.newValue(2d / 4d), MeasureUtil.evaluateSimilarity(valueFactory, comparisonMeasure, clusteringFields, flags, referenceFlags)); }
static public <V extends Number> Value<V> evaluateSimilarity(ValueFactory<V> valueFactory, ComparisonMeasure comparisonMeasure, List<? extends ComparisonField<?>> comparisonFields, BitSet flags, BitSet referenceFlags){ Similarity measure = TypeUtil.cast(Similarity.class, comparisonMeasure.getMeasure()); int a11 = 0; int a10 = 0; int a01 = 0; int a00 = 0; for(int i = 0; i < comparisonFields.size(); i++){ if(flags.get(i)){ if(referenceFlags.get(i)){ a11 += 1; } else { a10 += 1; } } else { if(referenceFlags.get(i)){ a01 += 1; } else { a00 += 1; } } } Value<V> numerator = valueFactory.newValue(); Value<V> denominator = valueFactory.newValue(); if(measure instanceof SimpleMatching){ numerator.add(a11 + a00); denominator.add(a11 + a10 + a01 + a00); } else if(measure instanceof Jaccard){ numerator.add(a11); denominator.add(a11 + a10 + a01); } else if(measure instanceof Tanimoto){ numerator.add(a11 + a00); denominator .add(a11) .add(Numbers.DOUBLE_TWO, (a10 + a01)) .add(a00); } else if(measure instanceof BinarySimilarity){ BinarySimilarity binarySimilarity = (BinarySimilarity)measure; Number c00 = binarySimilarity.getC00Parameter(); if(c00 == null){ throw new MissingAttributeException(binarySimilarity, PMMLAttributes.BINARYSIMILARITY_C00PARAMETER); } Number c01 = binarySimilarity.getC01Parameter(); if(c01 == null){ throw new MissingAttributeException(binarySimilarity, PMMLAttributes.BINARYSIMILARITY_C01PARAMETER); } Number c10 = binarySimilarity.getC10Parameter(); if(c10 == null){ throw new MissingAttributeException(binarySimilarity, PMMLAttributes.BINARYSIMILARITY_C10PARAMETER); } Number c11 = binarySimilarity.getC11Parameter(); if(c11 == null){ throw new MissingAttributeException(binarySimilarity, PMMLAttributes.BINARYSIMILARITY_C11PARAMETER); } numerator .add(c11, a11) .add(c10, a10) .add(c01, a01) .add(c00, a00); Number d00 = binarySimilarity.getD00Parameter(); if(d00 == null){ throw new MissingAttributeException(binarySimilarity, PMMLAttributes.BINARYSIMILARITY_D00PARAMETER); } Number d01 = binarySimilarity.getD01Parameter(); if(d01 == null){ throw new MissingAttributeException(binarySimilarity, PMMLAttributes.BINARYSIMILARITY_D01PARAMETER); } Number d10 = binarySimilarity.getD10Parameter(); if(d10 == null){ throw new MissingAttributeException(binarySimilarity, PMMLAttributes.BINARYSIMILARITY_D10PARAMETER); } Number d11 = binarySimilarity.getD11Parameter(); if(d11 == null){ throw new MissingAttributeException(binarySimilarity, PMMLAttributes.BINARYSIMILARITY_D11PARAMETER); } denominator .add(d11, a11) .add(d10, a10) .add(d01, a01) .add(d00, a00); } else { throw new UnsupportedElementException(measure); } if(denominator.isZero()){ throw new UndefinedResultException(); } return numerator.divide(denominator); }
MeasureUtil { static public <V extends Number> Value<V> evaluateSimilarity(ValueFactory<V> valueFactory, ComparisonMeasure comparisonMeasure, List<? extends ComparisonField<?>> comparisonFields, BitSet flags, BitSet referenceFlags){ Similarity measure = TypeUtil.cast(Similarity.class, comparisonMeasure.getMeasure()); int a11 = 0; int a10 = 0; int a01 = 0; int a00 = 0; for(int i = 0; i < comparisonFields.size(); i++){ if(flags.get(i)){ if(referenceFlags.get(i)){ a11 += 1; } else { a10 += 1; } } else { if(referenceFlags.get(i)){ a01 += 1; } else { a00 += 1; } } } Value<V> numerator = valueFactory.newValue(); Value<V> denominator = valueFactory.newValue(); if(measure instanceof SimpleMatching){ numerator.add(a11 + a00); denominator.add(a11 + a10 + a01 + a00); } else if(measure instanceof Jaccard){ numerator.add(a11); denominator.add(a11 + a10 + a01); } else if(measure instanceof Tanimoto){ numerator.add(a11 + a00); denominator .add(a11) .add(Numbers.DOUBLE_TWO, (a10 + a01)) .add(a00); } else if(measure instanceof BinarySimilarity){ BinarySimilarity binarySimilarity = (BinarySimilarity)measure; Number c00 = binarySimilarity.getC00Parameter(); if(c00 == null){ throw new MissingAttributeException(binarySimilarity, PMMLAttributes.BINARYSIMILARITY_C00PARAMETER); } Number c01 = binarySimilarity.getC01Parameter(); if(c01 == null){ throw new MissingAttributeException(binarySimilarity, PMMLAttributes.BINARYSIMILARITY_C01PARAMETER); } Number c10 = binarySimilarity.getC10Parameter(); if(c10 == null){ throw new MissingAttributeException(binarySimilarity, PMMLAttributes.BINARYSIMILARITY_C10PARAMETER); } Number c11 = binarySimilarity.getC11Parameter(); if(c11 == null){ throw new MissingAttributeException(binarySimilarity, PMMLAttributes.BINARYSIMILARITY_C11PARAMETER); } numerator .add(c11, a11) .add(c10, a10) .add(c01, a01) .add(c00, a00); Number d00 = binarySimilarity.getD00Parameter(); if(d00 == null){ throw new MissingAttributeException(binarySimilarity, PMMLAttributes.BINARYSIMILARITY_D00PARAMETER); } Number d01 = binarySimilarity.getD01Parameter(); if(d01 == null){ throw new MissingAttributeException(binarySimilarity, PMMLAttributes.BINARYSIMILARITY_D01PARAMETER); } Number d10 = binarySimilarity.getD10Parameter(); if(d10 == null){ throw new MissingAttributeException(binarySimilarity, PMMLAttributes.BINARYSIMILARITY_D10PARAMETER); } Number d11 = binarySimilarity.getD11Parameter(); if(d11 == null){ throw new MissingAttributeException(binarySimilarity, PMMLAttributes.BINARYSIMILARITY_D11PARAMETER); } denominator .add(d11, a11) .add(d10, a10) .add(d01, a01) .add(d00, a00); } else { throw new UnsupportedElementException(measure); } if(denominator.isZero()){ throw new UndefinedResultException(); } return numerator.divide(denominator); } }
MeasureUtil { static public <V extends Number> Value<V> evaluateSimilarity(ValueFactory<V> valueFactory, ComparisonMeasure comparisonMeasure, List<? extends ComparisonField<?>> comparisonFields, BitSet flags, BitSet referenceFlags){ Similarity measure = TypeUtil.cast(Similarity.class, comparisonMeasure.getMeasure()); int a11 = 0; int a10 = 0; int a01 = 0; int a00 = 0; for(int i = 0; i < comparisonFields.size(); i++){ if(flags.get(i)){ if(referenceFlags.get(i)){ a11 += 1; } else { a10 += 1; } } else { if(referenceFlags.get(i)){ a01 += 1; } else { a00 += 1; } } } Value<V> numerator = valueFactory.newValue(); Value<V> denominator = valueFactory.newValue(); if(measure instanceof SimpleMatching){ numerator.add(a11 + a00); denominator.add(a11 + a10 + a01 + a00); } else if(measure instanceof Jaccard){ numerator.add(a11); denominator.add(a11 + a10 + a01); } else if(measure instanceof Tanimoto){ numerator.add(a11 + a00); denominator .add(a11) .add(Numbers.DOUBLE_TWO, (a10 + a01)) .add(a00); } else if(measure instanceof BinarySimilarity){ BinarySimilarity binarySimilarity = (BinarySimilarity)measure; Number c00 = binarySimilarity.getC00Parameter(); if(c00 == null){ throw new MissingAttributeException(binarySimilarity, PMMLAttributes.BINARYSIMILARITY_C00PARAMETER); } Number c01 = binarySimilarity.getC01Parameter(); if(c01 == null){ throw new MissingAttributeException(binarySimilarity, PMMLAttributes.BINARYSIMILARITY_C01PARAMETER); } Number c10 = binarySimilarity.getC10Parameter(); if(c10 == null){ throw new MissingAttributeException(binarySimilarity, PMMLAttributes.BINARYSIMILARITY_C10PARAMETER); } Number c11 = binarySimilarity.getC11Parameter(); if(c11 == null){ throw new MissingAttributeException(binarySimilarity, PMMLAttributes.BINARYSIMILARITY_C11PARAMETER); } numerator .add(c11, a11) .add(c10, a10) .add(c01, a01) .add(c00, a00); Number d00 = binarySimilarity.getD00Parameter(); if(d00 == null){ throw new MissingAttributeException(binarySimilarity, PMMLAttributes.BINARYSIMILARITY_D00PARAMETER); } Number d01 = binarySimilarity.getD01Parameter(); if(d01 == null){ throw new MissingAttributeException(binarySimilarity, PMMLAttributes.BINARYSIMILARITY_D01PARAMETER); } Number d10 = binarySimilarity.getD10Parameter(); if(d10 == null){ throw new MissingAttributeException(binarySimilarity, PMMLAttributes.BINARYSIMILARITY_D10PARAMETER); } Number d11 = binarySimilarity.getD11Parameter(); if(d11 == null){ throw new MissingAttributeException(binarySimilarity, PMMLAttributes.BINARYSIMILARITY_D11PARAMETER); } denominator .add(d11, a11) .add(d10, a10) .add(d01, a01) .add(d00, a00); } else { throw new UnsupportedElementException(measure); } if(denominator.isZero()){ throw new UndefinedResultException(); } return numerator.divide(denominator); } private  MeasureUtil(); }
MeasureUtil { static public <V extends Number> Value<V> evaluateSimilarity(ValueFactory<V> valueFactory, ComparisonMeasure comparisonMeasure, List<? extends ComparisonField<?>> comparisonFields, BitSet flags, BitSet referenceFlags){ Similarity measure = TypeUtil.cast(Similarity.class, comparisonMeasure.getMeasure()); int a11 = 0; int a10 = 0; int a01 = 0; int a00 = 0; for(int i = 0; i < comparisonFields.size(); i++){ if(flags.get(i)){ if(referenceFlags.get(i)){ a11 += 1; } else { a10 += 1; } } else { if(referenceFlags.get(i)){ a01 += 1; } else { a00 += 1; } } } Value<V> numerator = valueFactory.newValue(); Value<V> denominator = valueFactory.newValue(); if(measure instanceof SimpleMatching){ numerator.add(a11 + a00); denominator.add(a11 + a10 + a01 + a00); } else if(measure instanceof Jaccard){ numerator.add(a11); denominator.add(a11 + a10 + a01); } else if(measure instanceof Tanimoto){ numerator.add(a11 + a00); denominator .add(a11) .add(Numbers.DOUBLE_TWO, (a10 + a01)) .add(a00); } else if(measure instanceof BinarySimilarity){ BinarySimilarity binarySimilarity = (BinarySimilarity)measure; Number c00 = binarySimilarity.getC00Parameter(); if(c00 == null){ throw new MissingAttributeException(binarySimilarity, PMMLAttributes.BINARYSIMILARITY_C00PARAMETER); } Number c01 = binarySimilarity.getC01Parameter(); if(c01 == null){ throw new MissingAttributeException(binarySimilarity, PMMLAttributes.BINARYSIMILARITY_C01PARAMETER); } Number c10 = binarySimilarity.getC10Parameter(); if(c10 == null){ throw new MissingAttributeException(binarySimilarity, PMMLAttributes.BINARYSIMILARITY_C10PARAMETER); } Number c11 = binarySimilarity.getC11Parameter(); if(c11 == null){ throw new MissingAttributeException(binarySimilarity, PMMLAttributes.BINARYSIMILARITY_C11PARAMETER); } numerator .add(c11, a11) .add(c10, a10) .add(c01, a01) .add(c00, a00); Number d00 = binarySimilarity.getD00Parameter(); if(d00 == null){ throw new MissingAttributeException(binarySimilarity, PMMLAttributes.BINARYSIMILARITY_D00PARAMETER); } Number d01 = binarySimilarity.getD01Parameter(); if(d01 == null){ throw new MissingAttributeException(binarySimilarity, PMMLAttributes.BINARYSIMILARITY_D01PARAMETER); } Number d10 = binarySimilarity.getD10Parameter(); if(d10 == null){ throw new MissingAttributeException(binarySimilarity, PMMLAttributes.BINARYSIMILARITY_D10PARAMETER); } Number d11 = binarySimilarity.getD11Parameter(); if(d11 == null){ throw new MissingAttributeException(binarySimilarity, PMMLAttributes.BINARYSIMILARITY_D11PARAMETER); } denominator .add(d11, a11) .add(d10, a10) .add(d01, a01) .add(d00, a00); } else { throw new UnsupportedElementException(measure); } if(denominator.isZero()){ throw new UndefinedResultException(); } return numerator.divide(denominator); } private  MeasureUtil(); static Measure ensureMeasure(ComparisonMeasure comparisonMeasure); static Value<V> evaluateSimilarity(ValueFactory<V> valueFactory, ComparisonMeasure comparisonMeasure, List<? extends ComparisonField<?>> comparisonFields, BitSet flags, BitSet referenceFlags); static BitSet toBitSet(List<FieldValue> values); static Value<V> evaluateDistance(ValueFactory<V> valueFactory, ComparisonMeasure comparisonMeasure, List<? extends ComparisonField<?>> comparisonFields, List<FieldValue> values, List<FieldValue> referenceValues, Value<V> adjustment); static Value<V> calculateAdjustment(ValueFactory<V> valueFactory, List<FieldValue> values); static Value<V> calculateAdjustment(ValueFactory<V> valueFactory, List<FieldValue> values, List<? extends Number> adjustmentValues); }
MeasureUtil { static public <V extends Number> Value<V> evaluateSimilarity(ValueFactory<V> valueFactory, ComparisonMeasure comparisonMeasure, List<? extends ComparisonField<?>> comparisonFields, BitSet flags, BitSet referenceFlags){ Similarity measure = TypeUtil.cast(Similarity.class, comparisonMeasure.getMeasure()); int a11 = 0; int a10 = 0; int a01 = 0; int a00 = 0; for(int i = 0; i < comparisonFields.size(); i++){ if(flags.get(i)){ if(referenceFlags.get(i)){ a11 += 1; } else { a10 += 1; } } else { if(referenceFlags.get(i)){ a01 += 1; } else { a00 += 1; } } } Value<V> numerator = valueFactory.newValue(); Value<V> denominator = valueFactory.newValue(); if(measure instanceof SimpleMatching){ numerator.add(a11 + a00); denominator.add(a11 + a10 + a01 + a00); } else if(measure instanceof Jaccard){ numerator.add(a11); denominator.add(a11 + a10 + a01); } else if(measure instanceof Tanimoto){ numerator.add(a11 + a00); denominator .add(a11) .add(Numbers.DOUBLE_TWO, (a10 + a01)) .add(a00); } else if(measure instanceof BinarySimilarity){ BinarySimilarity binarySimilarity = (BinarySimilarity)measure; Number c00 = binarySimilarity.getC00Parameter(); if(c00 == null){ throw new MissingAttributeException(binarySimilarity, PMMLAttributes.BINARYSIMILARITY_C00PARAMETER); } Number c01 = binarySimilarity.getC01Parameter(); if(c01 == null){ throw new MissingAttributeException(binarySimilarity, PMMLAttributes.BINARYSIMILARITY_C01PARAMETER); } Number c10 = binarySimilarity.getC10Parameter(); if(c10 == null){ throw new MissingAttributeException(binarySimilarity, PMMLAttributes.BINARYSIMILARITY_C10PARAMETER); } Number c11 = binarySimilarity.getC11Parameter(); if(c11 == null){ throw new MissingAttributeException(binarySimilarity, PMMLAttributes.BINARYSIMILARITY_C11PARAMETER); } numerator .add(c11, a11) .add(c10, a10) .add(c01, a01) .add(c00, a00); Number d00 = binarySimilarity.getD00Parameter(); if(d00 == null){ throw new MissingAttributeException(binarySimilarity, PMMLAttributes.BINARYSIMILARITY_D00PARAMETER); } Number d01 = binarySimilarity.getD01Parameter(); if(d01 == null){ throw new MissingAttributeException(binarySimilarity, PMMLAttributes.BINARYSIMILARITY_D01PARAMETER); } Number d10 = binarySimilarity.getD10Parameter(); if(d10 == null){ throw new MissingAttributeException(binarySimilarity, PMMLAttributes.BINARYSIMILARITY_D10PARAMETER); } Number d11 = binarySimilarity.getD11Parameter(); if(d11 == null){ throw new MissingAttributeException(binarySimilarity, PMMLAttributes.BINARYSIMILARITY_D11PARAMETER); } denominator .add(d11, a11) .add(d10, a10) .add(d01, a01) .add(d00, a00); } else { throw new UnsupportedElementException(measure); } if(denominator.isZero()){ throw new UndefinedResultException(); } return numerator.divide(denominator); } private  MeasureUtil(); static Measure ensureMeasure(ComparisonMeasure comparisonMeasure); static Value<V> evaluateSimilarity(ValueFactory<V> valueFactory, ComparisonMeasure comparisonMeasure, List<? extends ComparisonField<?>> comparisonFields, BitSet flags, BitSet referenceFlags); static BitSet toBitSet(List<FieldValue> values); static Value<V> evaluateDistance(ValueFactory<V> valueFactory, ComparisonMeasure comparisonMeasure, List<? extends ComparisonField<?>> comparisonFields, List<FieldValue> values, List<FieldValue> referenceValues, Value<V> adjustment); static Value<V> calculateAdjustment(ValueFactory<V> valueFactory, List<FieldValue> values); static Value<V> calculateAdjustment(ValueFactory<V> valueFactory, List<FieldValue> values, List<? extends Number> adjustmentValues);  }