Search in sources :

Example 26 with FeatureDomain

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

the class CounterfactualScoreCalculatorTest method testPrimarySoftScore.

/**
 * Test precision errors for primary soft score.
 * When the primary soft score is calculated between features with the same numerical
 * value a similarity of 1 is expected. For a large number of features, due to floating point errors this distance may be
 * in some cases slightly larger than 1, which will cause the distance (Math.sqrt(1.0-similarity)) to cause an exception.
 * The score calculation method should not let this should not occur.
 */
@ParameterizedTest
@ValueSource(ints = { 0, 1, 2, 3, 4 })
void testPrimarySoftScore(int seed) {
    final Random random = new Random(seed);
    final List<Feature> features = new ArrayList<>();
    final List<FeatureDomain> featureDomains = new ArrayList<>();
    final List<Boolean> constraints = new ArrayList<>();
    final int nFeatures = 1000;
    // Create a large number of identical features
    for (int n = 0; n < nFeatures; n++) {
        features.add(FeatureFactory.newNumericalFeature("f-" + n, random.nextDouble() * 1e-100));
        featureDomains.add(NumericalFeatureDomain.create(0.0, 10.0));
        constraints.add(false);
    }
    final PredictionInput input = new PredictionInput(features);
    final PredictionFeatureDomain domain = new PredictionFeatureDomain(featureDomains);
    final List<CounterfactualEntity> entities = CounterfactualEntityFactory.createEntities(input);
    // Create score calculator and model
    final CounterFactualScoreCalculator scoreCalculator = new CounterFactualScoreCalculator();
    PredictionProvider model = TestUtils.getFeatureSkipModel(0);
    // Create goal
    final List<Output> goal = new ArrayList<>();
    for (int n = 1; n < nFeatures; n++) {
        goal.add(new Output("f-" + n, Type.NUMBER, features.get(n).getValue(), 1.0));
    }
    final CounterfactualSolution solution = new CounterfactualSolution(entities, features, model, goal, UUID.randomUUID(), UUID.randomUUID(), 0.0);
    final BendableBigDecimalScore score = scoreCalculator.calculateScore(solution);
    assertEquals(0.0, score.getSoftScore(0).doubleValue(), 1e-5);
}
Also used : PredictionInput(org.kie.kogito.explainability.model.PredictionInput) ArrayList(java.util.ArrayList) BendableBigDecimalScore(org.optaplanner.core.api.score.buildin.bendablebigdecimal.BendableBigDecimalScore) 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) PredictionProvider(org.kie.kogito.explainability.model.PredictionProvider) Feature(org.kie.kogito.explainability.model.Feature) CounterfactualEntity(org.kie.kogito.explainability.local.counterfactual.entities.CounterfactualEntity) PredictionFeatureDomain(org.kie.kogito.explainability.model.PredictionFeatureDomain) Random(java.util.Random) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) Output(org.kie.kogito.explainability.model.Output) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 27 with FeatureDomain

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

the class CounterfactualScoreCalculatorTest method testNullDoubleInput.

/**
 * Null values for input Double features should not be accepted as valid
 */
@Test
void testNullDoubleInput() {
    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.0, 10.0));
    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)

Example 28 with FeatureDomain

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

the class CompositeEntityTest method distanceUnscaled.

@Test
void distanceUnscaled() {
    final FeatureDomain featureDomain = NumericalFeatureDomain.create(0.0, 40.0);
    final Feature doubleFeature = FeatureFactory.newNumericalFeature("feature-double", 20.0, featureDomain);
    DoubleEntity entity = (DoubleEntity) CounterfactualEntityFactory.from(doubleFeature);
    entity.proposedValue = 30.0;
    assertEquals(10.0, entity.distance());
}
Also used : 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)

Example 29 with FeatureDomain

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

the class DoubleEntityTest method distanceScaled.

@ParameterizedTest
@ValueSource(ints = { 0, 1, 2, 3, 4 })
void distanceScaled(int seed) {
    Random random = new Random();
    random.setSeed(seed);
    final FeatureDomain featureDomain = NumericalFeatureDomain.create(0.0, 40.0);
    final Feature doubleFeature = FeatureFactory.newNumericalFeature("feature-double", 20.0, featureDomain);
    final FeatureDistribution featureDistribution = new NumericFeatureDistribution(doubleFeature, random.doubles(5000, 10.0, 40.0).toArray());
    DoubleEntity entity = (DoubleEntity) CounterfactualEntityFactory.from(doubleFeature, featureDistribution);
    entity.proposedValue = 30.0;
    final double distance = entity.distance();
    assertTrue(distance > 0.1 && distance < 0.2);
}
Also used : NumericFeatureDistribution(org.kie.kogito.explainability.model.NumericFeatureDistribution) FeatureDistribution(org.kie.kogito.explainability.model.FeatureDistribution) Random(java.util.Random) NumericalFeatureDomain(org.kie.kogito.explainability.model.domain.NumericalFeatureDomain) FeatureDomain(org.kie.kogito.explainability.model.domain.FeatureDomain) Feature(org.kie.kogito.explainability.model.Feature) NumericFeatureDistribution(org.kie.kogito.explainability.model.NumericFeatureDistribution) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 30 with FeatureDomain

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

the class IntegerEntityTest method distanceScaled.

@ParameterizedTest
@ValueSource(ints = { 0, 1, 2, 3, 4 })
void distanceScaled(int seed) {
    Random random = new Random();
    random.setSeed(seed);
    final FeatureDomain featureDomain = NumericalFeatureDomain.create(0, 100);
    final Feature integerFeature = FeatureFactory.newNumericalFeature("feature-integer", 20, featureDomain);
    final FeatureDistribution featureDistribution = new NumericFeatureDistribution(integerFeature, random.ints(5000, 10, 40).mapToDouble(x -> x).toArray());
    IntegerEntity entity = (IntegerEntity) CounterfactualEntityFactory.from(integerFeature, featureDistribution);
    entity.proposedValue = 40;
    final double distance = entity.distance();
    assertTrue(distance > 0.2 && distance < 0.3);
}
Also used : NumericFeatureDistribution(org.kie.kogito.explainability.model.NumericFeatureDistribution) FeatureDistribution(org.kie.kogito.explainability.model.FeatureDistribution) Random(java.util.Random) NumericalFeatureDomain(org.kie.kogito.explainability.model.domain.NumericalFeatureDomain) FeatureDomain(org.kie.kogito.explainability.model.domain.FeatureDomain) Feature(org.kie.kogito.explainability.model.Feature) NumericFeatureDistribution(org.kie.kogito.explainability.model.NumericFeatureDistribution) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

FeatureDomain (org.kie.kogito.explainability.model.domain.FeatureDomain)39 NumericalFeatureDomain (org.kie.kogito.explainability.model.domain.NumericalFeatureDomain)38 Test (org.junit.jupiter.api.Test)32 EmptyFeatureDomain (org.kie.kogito.explainability.model.domain.EmptyFeatureDomain)31 Feature (org.kie.kogito.explainability.model.Feature)29 CategoricalFeatureDomain (org.kie.kogito.explainability.model.domain.CategoricalFeatureDomain)25 CounterfactualEntity (org.kie.kogito.explainability.local.counterfactual.entities.CounterfactualEntity)17 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)15 BinaryFeatureDomain (org.kie.kogito.explainability.model.domain.BinaryFeatureDomain)13 CurrencyFeatureDomain (org.kie.kogito.explainability.model.domain.CurrencyFeatureDomain)13 DurationFeatureDomain (org.kie.kogito.explainability.model.domain.DurationFeatureDomain)13 ObjectFeatureDomain (org.kie.kogito.explainability.model.domain.ObjectFeatureDomain)13 URIFeatureDomain (org.kie.kogito.explainability.model.domain.URIFeatureDomain)13 TimeFeatureDomain (org.kie.kogito.explainability.model.domain.TimeFeatureDomain)12 ArrayList (java.util.ArrayList)10 Output (org.kie.kogito.explainability.model.Output)8 Random (java.util.Random)7 PredictionFeatureDomain (org.kie.kogito.explainability.model.PredictionFeatureDomain)7 PredictionInput (org.kie.kogito.explainability.model.PredictionInput)7 Value (org.kie.kogito.explainability.model.Value)7