Search in sources :

Example 26 with Feature

use of org.kie.kogito.explainability.model.Feature in project kogito-apps by kiegroup.

the class CounterfactualScoreCalculatorTest method BooleanDistanceDifferentValue.

@ParameterizedTest
@ValueSource(ints = { 0, 1, 2, 3, 4 })
void BooleanDistanceDifferentValue(int seed) {
    final Random random = new Random(seed);
    boolean value = random.nextBoolean();
    Feature x = FeatureFactory.newBooleanFeature("x", value);
    Feature y = FeatureFactory.newBooleanFeature("y", !value);
    Output ox = outputFromFeature(x);
    Output oy = outputFromFeature(y);
    double distance = CounterFactualScoreCalculator.outputDistance(ox, oy);
    assertEquals(Type.BOOLEAN, ox.getType());
    assertEquals(Type.BOOLEAN, oy.getType());
    assertEquals(1.0, distance);
}
Also used : Random(java.util.Random) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) Output(org.kie.kogito.explainability.model.Output) Feature(org.kie.kogito.explainability.model.Feature) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 27 with Feature

use of org.kie.kogito.explainability.model.Feature in project kogito-apps by kiegroup.

the class CounterfactualScoreCalculatorTest method IntegerDistanceDifferentValue.

@ParameterizedTest
@ValueSource(ints = { 0, 1, 2, 3, 4 })
void IntegerDistanceDifferentValue(int seed) {
    final Random random = new Random(seed);
    int value = random.nextInt(1000);
    Feature x = FeatureFactory.newNumericalFeature("x", value);
    Feature y = FeatureFactory.newNumericalFeature("y", value + 100);
    Output ox = outputFromFeature(x);
    Output oy = outputFromFeature(y);
    double distance = CounterFactualScoreCalculator.outputDistance(ox, oy);
    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);
    assertTrue(distance * distance > 0);
}
Also used : Random(java.util.Random) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) Output(org.kie.kogito.explainability.model.Output) Feature(org.kie.kogito.explainability.model.Feature) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 28 with Feature

use of org.kie.kogito.explainability.model.Feature in project kogito-apps by kiegroup.

the class CounterfactualScoreCalculatorTest method IntegerDistanceNull.

@ParameterizedTest
@ValueSource(ints = { 0, 1, 2, 3, 4 })
void IntegerDistanceNull(int seed) {
    final Random random = new Random(seed);
    final int value = random.nextInt(1000);
    // Null as a goal
    IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
        Feature predictionFeature = FeatureFactory.newNumericalFeature("x", value);
        Feature goalFeature = FeatureFactory.newNumericalFeature("x", null);
        Output predictionOutput = outputFromFeature(predictionFeature);
        Output goalOutput = outputFromFeature(goalFeature);
        CounterFactualScoreCalculator.outputDistance(predictionOutput, goalOutput);
    });
    assertEquals("Unsupported NaN or NULL for numeric feature 'x'", exception.getMessage());
    // Null as a prediction
    exception = assertThrows(IllegalArgumentException.class, () -> {
        Feature predictionFeature = FeatureFactory.newNumericalFeature("x", null);
        Feature goalFeature = FeatureFactory.newNumericalFeature("x", value);
        Output predictionOutput = outputFromFeature(predictionFeature);
        Output goalOutput = outputFromFeature(goalFeature);
        CounterFactualScoreCalculator.outputDistance(predictionOutput, goalOutput);
    });
    assertEquals("Unsupported NaN or NULL for numeric feature 'x'", exception.getMessage());
    // Null as both prediction and goal
    exception = assertThrows(IllegalArgumentException.class, () -> {
        Feature predictionFeature = FeatureFactory.newNumericalFeature("x", null);
        Feature goalFeature = FeatureFactory.newNumericalFeature("x", null);
        Output predictionOutput = outputFromFeature(predictionFeature);
        Output goalOutput = outputFromFeature(goalFeature);
        CounterFactualScoreCalculator.outputDistance(predictionOutput, goalOutput);
    });
    assertEquals("Unsupported NaN or NULL for numeric feature 'x'", exception.getMessage());
}
Also used : Random(java.util.Random) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) Output(org.kie.kogito.explainability.model.Output) Feature(org.kie.kogito.explainability.model.Feature) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 29 with Feature

use of org.kie.kogito.explainability.model.Feature in project kogito-apps by kiegroup.

the class CounterfactualScoreCalculatorTest method DoubleDistanceDifferentValueThresholdMet.

@ParameterizedTest
@ValueSource(ints = { 0, 1, 2, 3, 4 })
void DoubleDistanceDifferentValueThresholdMet(int seed) {
    final double value = 100.0;
    Feature x = FeatureFactory.newNumericalFeature("x", value);
    Feature y = FeatureFactory.newNumericalFeature("y", value - 20.0);
    Output ox = outputFromFeature(x);
    Output oy = outputFromFeature(y);
    double distance = CounterFactualScoreCalculator.outputDistance(ox, oy);
    assertEquals(Type.NUMBER, ox.getType());
    assertEquals(Type.NUMBER, oy.getType());
    assertTrue(distance * distance > 0);
    distance = CounterFactualScoreCalculator.outputDistance(ox, oy, 0.1);
    assertTrue(distance * distance > 0);
    distance = CounterFactualScoreCalculator.outputDistance(ox, oy, 0.2);
    assertTrue(distance * distance > 0);
    distance = CounterFactualScoreCalculator.outputDistance(ox, oy, 0.3);
    assertFalse(distance * distance > 0);
}
Also used : PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) Output(org.kie.kogito.explainability.model.Output) Feature(org.kie.kogito.explainability.model.Feature) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 30 with Feature

use of org.kie.kogito.explainability.model.Feature in project kogito-apps by kiegroup.

the class CounterfactualScoreCalculatorTest method testNullIntegerInput.

/**
 * Null values for input Integer features should not be accepted as valid
 */
@Test
void testNullIntegerInput() throws ExecutionException, InterruptedException {
    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.newNumericalFeature("f-2", null));
    featureDomains.add(NumericalFeatureDomain.create(0, 10));
    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);
    IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
        CounterfactualEntityFactory.createEntities(input);
    });
    assertEquals("Null numeric features are not supported in counterfactuals", exception.getMessage());
}
Also used : PredictionFeatureDomain(org.kie.kogito.explainability.model.PredictionFeatureDomain) PredictionInput(org.kie.kogito.explainability.model.PredictionInput) ArrayList(java.util.ArrayList) EmptyFeatureDomain(org.kie.kogito.explainability.model.domain.EmptyFeatureDomain) PredictionFeatureDomain(org.kie.kogito.explainability.model.PredictionFeatureDomain) NumericalFeatureDomain(org.kie.kogito.explainability.model.domain.NumericalFeatureDomain) FeatureDomain(org.kie.kogito.explainability.model.domain.FeatureDomain) Feature(org.kie.kogito.explainability.model.Feature) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Feature (org.kie.kogito.explainability.model.Feature)233 PredictionOutput (org.kie.kogito.explainability.model.PredictionOutput)118 Test (org.junit.jupiter.api.Test)107 PredictionInput (org.kie.kogito.explainability.model.PredictionInput)107 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)104 Output (org.kie.kogito.explainability.model.Output)102 ArrayList (java.util.ArrayList)97 Random (java.util.Random)92 PredictionProvider (org.kie.kogito.explainability.model.PredictionProvider)78 Value (org.kie.kogito.explainability.model.Value)74 LinkedList (java.util.LinkedList)72 ValueSource (org.junit.jupiter.params.provider.ValueSource)71 Prediction (org.kie.kogito.explainability.model.Prediction)67 List (java.util.List)51 CounterfactualEntity (org.kie.kogito.explainability.local.counterfactual.entities.CounterfactualEntity)46 PerturbationContext (org.kie.kogito.explainability.model.PerturbationContext)42 Type (org.kie.kogito.explainability.model.Type)39 NumericalFeatureDomain (org.kie.kogito.explainability.model.domain.NumericalFeatureDomain)37 SimplePrediction (org.kie.kogito.explainability.model.SimplePrediction)35 FeatureDomain (org.kie.kogito.explainability.model.domain.FeatureDomain)33