Search in sources :

Example 16 with Output

use of org.kie.kogito.explainability.model.Output 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 17 with Output

use of org.kie.kogito.explainability.model.Output 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 18 with Output

use of org.kie.kogito.explainability.model.Output 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 19 with Output

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

the class CounterfactualScoreCalculatorTest method DoubleDistanceSameValueZero.

@ParameterizedTest
@ValueSource(ints = { 0, 1, 2, 3, 4 })
void DoubleDistanceSameValueZero(int seed) {
    final Random random = new Random(seed);
    final double value = 0.0;
    Feature x = FeatureFactory.newNumericalFeature("x", value);
    Feature y = FeatureFactory.newNumericalFeature("y", value);
    Output ox = outputFromFeature(x);
    Output oy = outputFromFeature(y);
    // Use a random threshold, mustn't make a difference
    final double distance = CounterFactualScoreCalculator.outputDistance(ox, oy, random.nextDouble());
    assertEquals(Type.NUMBER, ox.getType());
    assertEquals(0.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 20 with Output

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

the class CounterfactualScoreCalculatorTest method currencyDistanceSameValue.

@ParameterizedTest
@ValueSource(ints = { 0, 1, 2, 3, 4 })
void currencyDistanceSameValue(int seed) {
    final Random random = new Random(seed);
    final Currency value = Currency.getInstance(Locale.US);
    Feature x = FeatureFactory.newCurrencyFeature("x", value);
    Feature y = FeatureFactory.newCurrencyFeature("y", value);
    Output ox = outputFromFeature(x);
    Output oy = outputFromFeature(y);
    double distance = CounterFactualScoreCalculator.outputDistance(ox, oy);
    assertEquals(Type.CURRENCY, 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);
}
Also used : Random(java.util.Random) Currency(java.util.Currency) 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)

Aggregations

Output (org.kie.kogito.explainability.model.Output)120 PredictionOutput (org.kie.kogito.explainability.model.PredictionOutput)109 Feature (org.kie.kogito.explainability.model.Feature)102 Value (org.kie.kogito.explainability.model.Value)63 Random (java.util.Random)61 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)59 PredictionInput (org.kie.kogito.explainability.model.PredictionInput)57 PredictionProvider (org.kie.kogito.explainability.model.PredictionProvider)52 ArrayList (java.util.ArrayList)47 ValueSource (org.junit.jupiter.params.provider.ValueSource)47 Prediction (org.kie.kogito.explainability.model.Prediction)46 Test (org.junit.jupiter.api.Test)42 List (java.util.List)39 Type (org.kie.kogito.explainability.model.Type)36 LinkedList (java.util.LinkedList)35 CounterfactualEntity (org.kie.kogito.explainability.local.counterfactual.entities.CounterfactualEntity)23 Mockito.mock (org.mockito.Mockito.mock)20 Optional (java.util.Optional)19 ExecutionException (java.util.concurrent.ExecutionException)19 Collectors (java.util.stream.Collectors)18