use of org.kie.kogito.explainability.model.Feature in project kogito-apps by kiegroup.
the class CounterfactualScoreCalculatorTest method differentFeatureTypes.
@Test
void differentFeatureTypes() {
Feature x = FeatureFactory.newCategoricalFeature("x", UUID.randomUUID().toString());
Feature y = FeatureFactory.newNumericalFeature("y", 0.0);
Output ox = outputFromFeature(x);
Output oy = outputFromFeature(y);
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
CounterFactualScoreCalculator.outputDistance(ox, oy);
});
assertEquals("Features must have the same type. Feature 'x', has type 'categorical' and 'number'", exception.getMessage());
}
use of org.kie.kogito.explainability.model.Feature in project kogito-apps by kiegroup.
the class CounterfactualScoreCalculatorTest method currencyDistanceDifferentValue.
@ParameterizedTest
@ValueSource(ints = { 0, 1, 2, 3, 4 })
void currencyDistanceDifferentValue(int seed) {
final Random random = new Random(seed);
Feature x = FeatureFactory.newCurrencyFeature("x", Currency.getInstance("GBP"));
Feature y = FeatureFactory.newCurrencyFeature("y", Currency.getInstance("EUR"));
Output ox = outputFromFeature(x);
Output oy = outputFromFeature(y);
double distance = CounterFactualScoreCalculator.outputDistance(ox, oy);
assertEquals(Type.CURRENCY, ox.getType());
assertEquals(Type.CURRENCY, oy.getType());
assertEquals(1.0, distance);
// Use a random threshold, mustn't make a difference
distance = CounterFactualScoreCalculator.outputDistance(ox, oy, random.nextDouble());
assertEquals(1.0, distance);
}
use of org.kie.kogito.explainability.model.Feature in project kogito-apps by kiegroup.
the class CounterfactualScoreCalculatorTest method DoubleDistanceDifferentValueThreshold.
@ParameterizedTest
@ValueSource(ints = { 0, 1, 2, 3, 4 })
void DoubleDistanceDifferentValueThreshold(int seed) {
final Random random = new Random(seed);
double value = random.nextDouble() * 100.0;
Feature x = FeatureFactory.newNumericalFeature("x", value);
Feature y = FeatureFactory.newNumericalFeature("y", value + 100.0);
Output ox = outputFromFeature(x);
Output oy = outputFromFeature(y);
double distance = CounterFactualScoreCalculator.outputDistance(ox, oy, 0.25);
assertEquals(Type.NUMBER, ox.getType());
assertEquals(Type.NUMBER, oy.getType());
assertTrue(distance * distance > 0);
y = FeatureFactory.newNumericalFeature("y", value - 100);
oy = outputFromFeature(y);
distance = CounterFactualScoreCalculator.outputDistance(ox, oy, 0.25);
assertTrue(distance * distance > 0);
}
use of org.kie.kogito.explainability.model.Feature in project kogito-apps by kiegroup.
the class CounterfactualScoreCalculatorTest method binaryDistanceSameValue.
@ParameterizedTest
@ValueSource(ints = { 0, 1, 2, 3, 4 })
void binaryDistanceSameValue(int seed) {
final Random random = new Random(seed);
final ByteBuffer value = ByteBuffer.wrap("foo".getBytes());
Feature x = FeatureFactory.newBinaryFeature("x", value);
Feature y = FeatureFactory.newBinaryFeature("y", value);
Output ox = outputFromFeature(x);
Output oy = outputFromFeature(y);
double distance = CounterFactualScoreCalculator.outputDistance(ox, oy);
assertEquals(Type.BINARY, ox.getType());
assertEquals(0.0, distance);
// Use a random threshold, mustn't make a difference
distance = CounterFactualScoreCalculator.outputDistance(ox, oy, random.nextDouble());
assertEquals(0.0, distance);
}
use of org.kie.kogito.explainability.model.Feature in project kogito-apps by kiegroup.
the class DataUtils method dropOnLinearizedFeatures.
/**
* Drop a target feature from a "linearized" version of a source feature.
* Any of such linearized features are eventually dropped if they match on associated name, type and value.
*
* @param target the target feature
* @param sourceFeature the source feature
* @return the source feature having one of its underlying "linearized" values eventually dropped
*/
protected static Feature dropOnLinearizedFeatures(Feature target, Feature sourceFeature) {
Feature f = null;
List<Feature> linearizedFeatures = DataUtils.getLinearizedFeatures(List.of(sourceFeature));
int i = 0;
for (Feature linearizedFeature : linearizedFeatures) {
if (target.getValue().equals(linearizedFeature.getValue())) {
linearizedFeatures.set(i, FeatureFactory.copyOf(linearizedFeature, linearizedFeature.getType().drop(target.getValue())));
f = FeatureFactory.newCompositeFeature(target.getName(), linearizedFeatures);
break;
} else {
i++;
}
}
// not found
if (f == null) {
f = FeatureFactory.copyOf(sourceFeature, sourceFeature.getValue());
}
return f;
}
Aggregations