use of org.kie.kogito.explainability.model.Output in project kogito-apps by kiegroup.
the class CounterfactualExplainerTest method testCounterfactualMatchThreshold.
@ParameterizedTest
@ValueSource(ints = { 0, 1, 2 })
void testCounterfactualMatchThreshold(int seed) throws ExecutionException, InterruptedException, TimeoutException {
Random random = new Random();
random.setSeed(seed);
final double scoreThreshold = 0.9;
final List<Output> goal = List.of(new Output("inside", Type.BOOLEAN, new Value(true), scoreThreshold));
List<Feature> features = new LinkedList<>();
features.add(FeatureFactory.newNumericalFeature("f-num1", 100.0, NumericalFeatureDomain.create(0.0, 1000.0)));
features.add(FeatureFactory.newNumericalFeature("f-num2", 100.0, NumericalFeatureDomain.create(0.0, 1000.0)));
features.add(FeatureFactory.newNumericalFeature("f-num3", 100.0, NumericalFeatureDomain.create(0.0, 1000.0)));
features.add(FeatureFactory.newNumericalFeature("f-num4", 100.0, NumericalFeatureDomain.create(0.0, 1000.0)));
final double center = 500.0;
final double epsilon = 10.0;
final PredictionProvider model = TestUtils.getSumThresholdModel(center, epsilon);
final CounterfactualResult result = runCounterfactualSearch((long) seed, goal, features, model, DEFAULT_GOAL_THRESHOLD);
final List<CounterfactualEntity> counterfactualEntities = result.getEntities();
double totalSum = 0;
for (CounterfactualEntity entity : counterfactualEntities) {
totalSum += entity.asFeature().getValue().asNumber();
logger.debug("Entity: {}", entity);
}
assertTrue(totalSum <= center + epsilon);
assertTrue(totalSum >= center - epsilon);
final List<Feature> cfFeatures = counterfactualEntities.stream().map(CounterfactualEntity::asFeature).collect(Collectors.toList());
final PredictionInput cfInput = new PredictionInput(cfFeatures);
final PredictionOutput cfOutput = model.predictAsync(List.of(cfInput)).get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit()).get(0);
final double predictionScore = cfOutput.getOutputs().get(0).getScore();
logger.debug("Prediction score: {}", predictionScore);
assertTrue(predictionScore >= scoreThreshold);
assertTrue(result.isValid());
}
use of org.kie.kogito.explainability.model.Output in project kogito-apps by kiegroup.
the class CounterfactualExplainerTest method testCounterfactualMatchNoThreshold.
@ParameterizedTest
@ValueSource(ints = { 0, 1, 2 })
void testCounterfactualMatchNoThreshold(int seed) throws ExecutionException, InterruptedException, TimeoutException {
Random random = new Random();
random.setSeed(seed);
final double scoreThreshold = 0.0;
final List<Output> goal = List.of(new Output("inside", Type.BOOLEAN, new Value(true), scoreThreshold));
List<Feature> features = new LinkedList<>();
features.add(FeatureFactory.newNumericalFeature("f-num1", 100.0, NumericalFeatureDomain.create(0.0, 1000.0)));
features.add(FeatureFactory.newNumericalFeature("f-num2", 100.0, NumericalFeatureDomain.create(0.0, 1000.0)));
features.add(FeatureFactory.newNumericalFeature("f-num3", 100.0, NumericalFeatureDomain.create(0.0, 1000.0)));
features.add(FeatureFactory.newNumericalFeature("f-num4", 100.0, NumericalFeatureDomain.create(0.0, 1000.0)));
final double center = 500.0;
final double epsilon = 10.0;
final PredictionProvider model = TestUtils.getSumThresholdModel(center, epsilon);
final CounterfactualResult result = runCounterfactualSearch((long) seed, goal, features, model, DEFAULT_GOAL_THRESHOLD);
final List<CounterfactualEntity> counterfactualEntities = result.getEntities();
double totalSum = 0;
for (CounterfactualEntity entity : counterfactualEntities) {
totalSum += entity.asFeature().getValue().asNumber();
logger.debug("Entity: {}", entity);
}
assertTrue(totalSum <= center + epsilon);
assertTrue(totalSum >= center - epsilon);
final List<Feature> cfFeatures = counterfactualEntities.stream().map(CounterfactualEntity::asFeature).collect(Collectors.toList());
final PredictionInput cfInput = new PredictionInput(cfFeatures);
final PredictionOutput cfOutput = model.predictAsync(List.of(cfInput)).get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit()).get(0);
final double predictionScore = cfOutput.getOutputs().get(0).getScore();
logger.debug("Prediction score: {}", predictionScore);
assertTrue(predictionScore < 0.5);
assertTrue(result.isValid());
}
use of org.kie.kogito.explainability.model.Output in project kogito-apps by kiegroup.
the class CounterfactualExplainerTest method testNoCounterfactualPossible.
@ParameterizedTest
@ValueSource(ints = { 0, 1, 2 })
void testNoCounterfactualPossible(long seed) throws ExecutionException, InterruptedException, TimeoutException {
Random random = new Random();
final PerturbationContext perturbationContext = new PerturbationContext(seed, random, 4);
final List<Output> goal = List.of(new Output("inside", Type.BOOLEAN, new Value(true), 0.0));
List<Feature> features = new LinkedList<>();
List<FeatureDomain> featureBoundaries = new LinkedList<>();
List<Boolean> constraints = new LinkedList<>();
features.add(FeatureFactory.newNumericalFeature("f-num1", 1.0));
constraints.add(true);
featureBoundaries.add(EmptyFeatureDomain.create());
features.add(FeatureFactory.newNumericalFeature("f-num2", 1.0));
constraints.add(false);
featureBoundaries.add(NumericalFeatureDomain.create(0.0, 2.0));
features.add(FeatureFactory.newNumericalFeature("f-num3", 1.0));
constraints.add(false);
featureBoundaries.add(NumericalFeatureDomain.create(0.0, 2.0));
features.add(FeatureFactory.newNumericalFeature("f-num4", 1.0));
constraints.add(true);
featureBoundaries.add(EmptyFeatureDomain.create());
final DataDomain dataDomain = new DataDomain(featureBoundaries);
final double center = 500.0;
final double epsilon = 1.0;
List<Feature> perturbedFeatures = DataUtils.perturbFeatures(features, perturbationContext);
final CounterfactualResult result = runCounterfactualSearch((long) seed, goal, perturbedFeatures, TestUtils.getSumThresholdModel(center, epsilon), DEFAULT_GOAL_THRESHOLD);
assertFalse(result.isValid());
}
use of org.kie.kogito.explainability.model.Output in project kogito-apps by kiegroup.
the class CounterfactualScoreCalculatorTest method binaryDistanceDifferentValue.
@ParameterizedTest
@ValueSource(ints = { 0, 1, 2, 3, 4 })
void binaryDistanceDifferentValue(int seed) {
final Random random = new Random(seed);
Feature x = FeatureFactory.newBinaryFeature("x", ByteBuffer.wrap("foo".getBytes()));
Feature y = FeatureFactory.newBinaryFeature("y", ByteBuffer.wrap("bar".getBytes()));
Output ox = outputFromFeature(x);
Output oy = outputFromFeature(y);
double distance = CounterFactualScoreCalculator.outputDistance(ox, oy);
assertEquals(Type.BINARY, ox.getType());
assertEquals(Type.BINARY, 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.Output in project kogito-apps by kiegroup.
the class CounterfactualScoreCalculatorTest method testNullBooleanInput.
/**
* Null values for input Boolean features should be accepted as valid
*/
@Test
void testNullBooleanInput() throws ExecutionException, InterruptedException {
final CounterFactualScoreCalculator scoreCalculator = new CounterFactualScoreCalculator();
PredictionProvider model = TestUtils.getFeatureSkipModel(0);
List<Feature> features = new ArrayList<>();
List<FeatureDomain> featureDomains = new ArrayList<>();
List<Boolean> constraints = new ArrayList<>();
// f-1
features.add(FeatureFactory.newNumericalFeature("f-1", 1.0));
featureDomains.add(NumericalFeatureDomain.create(0.0, 10.0));
constraints.add(false);
// f-2
features.add(FeatureFactory.newBooleanFeature("f-2", null));
featureDomains.add(EmptyFeatureDomain.create());
constraints.add(false);
// f-3
features.add(FeatureFactory.newBooleanFeature("f-3", true));
featureDomains.add(EmptyFeatureDomain.create());
constraints.add(false);
PredictionInput input = new PredictionInput(features);
PredictionFeatureDomain domains = new PredictionFeatureDomain(featureDomains);
List<CounterfactualEntity> entities = CounterfactualEntityFactory.createEntities(input);
List<Output> goal = new ArrayList<>();
goal.add(new Output("f-2", Type.BOOLEAN, new Value(null), 0.0));
goal.add(new Output("f-3", Type.BOOLEAN, new Value(true), 0.0));
final CounterfactualSolution solution = new CounterfactualSolution(entities, features, model, goal, UUID.randomUUID(), UUID.randomUUID(), 0.0);
BendableBigDecimalScore score = scoreCalculator.calculateScore(solution);
List<PredictionOutput> predictionOutputs = model.predictAsync(List.of(input)).get();
assertTrue(score.isFeasible());
assertEquals(2, goal.size());
// A single prediction is expected
assertEquals(1, predictionOutputs.size());
// Single prediction with two features
assertEquals(2, predictionOutputs.get(0).getOutputs().size());
assertEquals(0, score.getHardScore(0).compareTo(BigDecimal.ZERO));
assertEquals(0, score.getHardScore(1).compareTo(BigDecimal.ZERO));
assertEquals(0, score.getHardScore(2).compareTo(BigDecimal.ZERO));
assertEquals(0, score.getSoftScore(0).compareTo(BigDecimal.ZERO));
assertEquals(0, score.getSoftScore(1).compareTo(BigDecimal.ZERO));
assertEquals(3, score.getHardLevelsSize());
assertEquals(2, score.getSoftLevelsSize());
}
Aggregations