use of org.kie.kogito.explainability.local.counterfactual.CounterfactualExplainer in project kogito-apps by kiegroup.
the class CounterfactualExplainerProducerTest method produce.
@Test
void produce() {
final ManagedExecutor executor = SmallRyeManagedExecutor.builder().build();
CounterfactualExplainerProducer producer = new CounterfactualExplainerProducer(0.01, executor);
CounterfactualExplainer counterfactualExplainer = producer.produce();
assertNotNull(counterfactualExplainer);
assertEquals(0.01, counterfactualExplainer.getCounterfactualConfig().getGoalThreshold());
assertEquals(executor, counterfactualExplainer.getCounterfactualConfig().getExecutor());
}
use of org.kie.kogito.explainability.local.counterfactual.CounterfactualExplainer in project kogito-apps by kiegroup.
the class LoanEligibilityDmnCounterfactualExplainerTest method testLoanEligibilityDMNExplanation.
@Test
void testLoanEligibilityDMNExplanation() throws ExecutionException, InterruptedException, TimeoutException {
PredictionProvider model = getModel();
final List<Output> goal = List.of(new Output("Is Enought?", Type.NUMBER, new Value(100), 0.0d), new Output("Eligibility", Type.TEXT, new Value("No"), 0.0d), new Output("Decide", Type.BOOLEAN, new Value(true), 0.0d));
final TerminationConfig terminationConfig = new TerminationConfig().withScoreCalculationCountLimit(steps);
final SolverConfig solverConfig = SolverConfigBuilder.builder().withTerminationConfig(terminationConfig).build();
solverConfig.setRandomSeed(randomSeed);
solverConfig.setEnvironmentMode(EnvironmentMode.REPRODUCIBLE);
CounterfactualConfig config = new CounterfactualConfig();
config.withSolverConfig(solverConfig);
final CounterfactualExplainer explainer = new CounterfactualExplainer(config);
PredictionInput input = getTestInput();
PredictionOutput output = new PredictionOutput(goal);
// test model
List<PredictionOutput> predictionOutputs = model.predictAsync(List.of(input)).get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit());
Prediction prediction = new CounterfactualPrediction(input, output, null, UUID.randomUUID(), null);
CounterfactualResult counterfactualResult = explainer.explainAsync(prediction, model).get();
List<Feature> cfFeatures = counterfactualResult.getEntities().stream().map(CounterfactualEntity::asFeature).collect(Collectors.toList());
List<Feature> unflattened = CompositeFeatureUtils.unflattenFeatures(cfFeatures, input.getFeatures());
List<PredictionOutput> outputs = model.predictAsync(List.of(new PredictionInput(unflattened))).get();
assertTrue(counterfactualResult.isValid());
final Output decideOutput = outputs.get(0).getOutputs().get(2);
assertEquals("Decide", decideOutput.getName());
assertTrue((Boolean) decideOutput.getValue().getUnderlyingObject());
}
use of org.kie.kogito.explainability.local.counterfactual.CounterfactualExplainer in project kogito-apps by kiegroup.
the class ComplexEligibilityDmnCounterfactualExplainerTest method testDMNScoringFunction.
@Test
void testDMNScoringFunction() throws ExecutionException, InterruptedException, TimeoutException {
PredictionProvider model = getModel();
final List<Output> goal = generateGoal(true, true, 1.0);
List<Feature> features = new LinkedList<>();
features.add(FeatureFactory.newNumericalFeature("age", 40, NumericalFeatureDomain.create(18, 60)));
features.add(FeatureFactory.newBooleanFeature("hasReferral", true));
features.add(FeatureFactory.newNumericalFeature("monthlySalary", 500, NumericalFeatureDomain.create(10, 100_000)));
final TerminationConfig terminationConfig = new TerminationConfig().withScoreCalculationCountLimit(10_000L);
// for the purpose of this test, only a few steps are necessary
final SolverConfig solverConfig = SolverConfigBuilder.builder().withTerminationConfig(terminationConfig).build();
solverConfig.setRandomSeed((long) 23);
solverConfig.setEnvironmentMode(EnvironmentMode.REPRODUCIBLE);
final CounterfactualConfig counterfactualConfig = new CounterfactualConfig().withSolverConfig(solverConfig).withGoalThreshold(0.01);
final CounterfactualExplainer counterfactualExplainer = new CounterfactualExplainer(counterfactualConfig);
PredictionInput input = new PredictionInput(features);
PredictionOutput output = new PredictionOutput(goal);
Prediction prediction = new CounterfactualPrediction(input, output, null, UUID.randomUUID(), 60L);
final CounterfactualResult counterfactualResult = counterfactualExplainer.explainAsync(prediction, model).get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit());
List<Output> cfOutputs = counterfactualResult.getOutput().get(0).getOutputs();
assertTrue(counterfactualResult.isValid());
assertEquals("inputsAreValid", cfOutputs.get(0).getName());
assertTrue((Boolean) cfOutputs.get(0).getValue().getUnderlyingObject());
assertEquals("canRequestLoan", cfOutputs.get(1).getName());
assertTrue((Boolean) cfOutputs.get(1).getValue().getUnderlyingObject());
assertEquals("my-scoring-function", cfOutputs.get(2).getName());
assertEquals(1.0, ((BigDecimal) cfOutputs.get(2).getValue().getUnderlyingObject()).doubleValue(), 0.01);
List<CounterfactualEntity> entities = counterfactualResult.getEntities();
assertEquals("age", entities.get(0).asFeature().getName());
assertEquals(18, entities.get(0).asFeature().getValue().asNumber());
assertEquals("hasReferral", entities.get(1).asFeature().getName());
assertTrue((Boolean) entities.get(1).asFeature().getValue().getUnderlyingObject());
assertEquals("monthlySalary", entities.get(2).asFeature().getName());
final double monthlySalary = entities.get(2).asFeature().getValue().asNumber();
assertEquals(7900, monthlySalary, 10);
// since the scoring function is ((0.6 * ((42 - age + 18)/42)) + (0.4 * (monthlySalary/8000)))
// for a result of 1.0 the relation must be age = (7*monthlySalary)/2000 - 10
assertEquals(18, (7 * monthlySalary) / 2000.0 - 10.0, 0.5);
}
use of org.kie.kogito.explainability.local.counterfactual.CounterfactualExplainer in project kogito-apps by kiegroup.
the class ComplexEligibilityDmnCounterfactualExplainerTest method testDMNInvalidCounterfactualExplanation.
@Test
void testDMNInvalidCounterfactualExplanation() throws ExecutionException, InterruptedException, TimeoutException {
PredictionProvider model = getModel();
final List<Output> goal = generateGoal(true, true, 0.6);
List<Feature> features = new LinkedList<>();
// DMN model does not allow loans for age >= 60, so no CF will be possible
features.add(FeatureFactory.newNumericalFeature("age", 61));
features.add(FeatureFactory.newBooleanFeature("hasReferral", true));
features.add(FeatureFactory.newNumericalFeature("monthlySalary", 500, NumericalFeatureDomain.create(10, 10_000)));
final TerminationConfig terminationConfig = new TerminationConfig().withScoreCalculationCountLimit(10_000L);
// for the purpose of this test, only a few steps are necessary
final SolverConfig solverConfig = SolverConfigBuilder.builder().withTerminationConfig(terminationConfig).build();
solverConfig.setRandomSeed((long) 23);
solverConfig.setEnvironmentMode(EnvironmentMode.REPRODUCIBLE);
final CounterfactualConfig counterfactualConfig = new CounterfactualConfig().withSolverConfig(solverConfig).withGoalThreshold(0.01);
final CounterfactualExplainer counterfactualExplainer = new CounterfactualExplainer(counterfactualConfig);
PredictionInput input = new PredictionInput(features);
PredictionOutput output = new PredictionOutput(goal);
Prediction prediction = new CounterfactualPrediction(input, output, null, UUID.randomUUID(), 60L);
final CounterfactualResult counterfactualResult = counterfactualExplainer.explainAsync(prediction, model).get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit());
assertFalse(counterfactualResult.isValid());
}
use of org.kie.kogito.explainability.local.counterfactual.CounterfactualExplainer in project kogito-apps by kiegroup.
the class ComplexEligibilityDmnCounterfactualExplainerTest method testDMNValidCounterfactualExplanation.
@Test
void testDMNValidCounterfactualExplanation() throws ExecutionException, InterruptedException, TimeoutException {
PredictionProvider model = getModel();
final List<Output> goal = generateGoal(true, true, 0.6);
List<Feature> features = new LinkedList<>();
features.add(FeatureFactory.newNumericalFeature("age", 40));
features.add(FeatureFactory.newBooleanFeature("hasReferral", true));
features.add(FeatureFactory.newNumericalFeature("monthlySalary", 500, NumericalFeatureDomain.create(10, 10_000)));
final TerminationConfig terminationConfig = new TerminationConfig().withScoreCalculationCountLimit(10_000L);
// for the purpose of this test, only a few steps are necessary
final SolverConfig solverConfig = SolverConfigBuilder.builder().withTerminationConfig(terminationConfig).build();
solverConfig.setRandomSeed((long) 23);
solverConfig.setEnvironmentMode(EnvironmentMode.REPRODUCIBLE);
final CounterfactualConfig counterfactualConfig = new CounterfactualConfig().withSolverConfig(solverConfig).withGoalThreshold(0.01);
final CounterfactualExplainer counterfactualExplainer = new CounterfactualExplainer(counterfactualConfig);
PredictionInput input = new PredictionInput(features);
PredictionOutput output = new PredictionOutput(goal);
Prediction prediction = new CounterfactualPrediction(input, output, null, UUID.randomUUID(), 60L);
final CounterfactualResult counterfactualResult = counterfactualExplainer.explainAsync(prediction, model).get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit());
List<Output> cfOutputs = counterfactualResult.getOutput().get(0).getOutputs();
assertTrue(counterfactualResult.isValid());
assertEquals("inputsAreValid", cfOutputs.get(0).getName());
assertTrue((Boolean) cfOutputs.get(0).getValue().getUnderlyingObject());
assertEquals("canRequestLoan", cfOutputs.get(1).getName());
assertTrue((Boolean) cfOutputs.get(1).getValue().getUnderlyingObject());
assertEquals("my-scoring-function", cfOutputs.get(2).getName());
assertEquals(0.6, ((BigDecimal) cfOutputs.get(2).getValue().getUnderlyingObject()).doubleValue(), 0.05);
List<CounterfactualEntity> entities = counterfactualResult.getEntities();
assertEquals("age", entities.get(0).asFeature().getName());
assertEquals(40, entities.get(0).asFeature().getValue().asNumber());
assertEquals("hasReferral", entities.get(1).asFeature().getName());
assertTrue((Boolean) entities.get(1).asFeature().getValue().getUnderlyingObject());
assertEquals("monthlySalary", entities.get(2).asFeature().getName());
assertTrue(entities.get(2).asFeature().getValue().asNumber() > 6000);
}
Aggregations