Search in sources :

Example 16 with PredictionInput

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

the class FairnessMetrics method individualConsistency.

/**
 * Calculate individual fairness in terms of consistency of predictions across similar inputs.
 *
 * @param proximityFunction a function that finds the top k similar inputs, given a reference input and a list of inputs
 * @param samples a list of inputs to be tested for consistency
 * @param predictionProvider the model under inspection
 * @return the consistency measure
 * @throws ExecutionException if any error occurs during model prediction
 * @throws InterruptedException if timeout or other interruption issues occur during model prediction
 */
public static double individualConsistency(BiFunction<PredictionInput, List<PredictionInput>, List<PredictionInput>> proximityFunction, List<PredictionInput> samples, PredictionProvider predictionProvider) throws ExecutionException, InterruptedException {
    double consistency = 1;
    for (PredictionInput input : samples) {
        List<PredictionOutput> predictionOutputs = predictionProvider.predictAsync(List.of(input)).get();
        PredictionOutput predictionOutput = predictionOutputs.get(0);
        List<PredictionInput> neighbors = proximityFunction.apply(input, samples);
        List<PredictionOutput> neighborsOutputs = predictionProvider.predictAsync(neighbors).get();
        for (Output output : predictionOutput.getOutputs()) {
            Value originalValue = output.getValue();
            for (PredictionOutput neighborOutput : neighborsOutputs) {
                Output currentOutput = neighborOutput.getByName(output.getName()).orElse(null);
                if (currentOutput != null && !originalValue.equals(currentOutput.getValue())) {
                    consistency -= 1f / (neighbors.size() * predictionOutput.getOutputs().size() * samples.size());
                }
            }
        }
    }
    return consistency;
}
Also used : PredictionInput(org.kie.kogito.explainability.model.PredictionInput) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) Output(org.kie.kogito.explainability.model.Output) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) Value(org.kie.kogito.explainability.model.Value)

Example 17 with PredictionInput

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

the class PartialDependencePlotExplainer method prepareInputs.

/**
 * Generate inputs for a particular feature, using 1) a specific discrete value from the data distribution of the
 * feature under analysis for that particular feature and 2) values from a training data distribution (which we sample)
 * for all the other feature values.
 * The resulting list of prediction inputs will have the very same value for the feature under analysis, and values
 * from the training data for all other features.
 *
 * @param featureXs specific value of the feature under analysis
 * @param trainingData training data
 * @return a list of prediction inputs
 */
private List<PredictionInput> prepareInputs(Feature featureXs, List<PredictionInput> trainingData) {
    List<PredictionInput> predictionInputs = new ArrayList<>(config.getSeriesLength());
    for (PredictionInput trainingSample : trainingData) {
        List<Feature> features = trainingSample.getFeatures();
        List<Feature> newFeatures = replaceFeatures(featureXs, features);
        predictionInputs.add(new PredictionInput(newFeatures));
    }
    return predictionInputs;
}
Also used : PredictionInput(org.kie.kogito.explainability.model.PredictionInput) ArrayList(java.util.ArrayList) Feature(org.kie.kogito.explainability.model.Feature)

Example 18 with PredictionInput

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

the class CounterFactualScoreCalculator method calculateScore.

/**
 * Calculates the counterfactual score for each proposed solution.
 * This method assumes that each model used as {@link org.kie.kogito.explainability.model.PredictionProvider} is
 * consistent, in the sense that for repeated operations, the size of the returned collection of
 * {@link PredictionOutput} is the same, if the size of {@link PredictionInput} doesn't change.
 *
 * @param solution Proposed solution
 * @return A {@link BendableBigDecimalScore} with three "hard" levels and one "soft" level
 */
@Override
public BendableBigDecimalScore calculateScore(CounterfactualSolution solution) {
    BendableBigDecimalScore currentScore = calculateInputScore(solution);
    final List<Feature> flattenedFeatures = solution.getEntities().stream().map(CounterfactualEntity::asFeature).collect(Collectors.toList());
    final List<Feature> input = CompositeFeatureUtils.unflattenFeatures(flattenedFeatures, solution.getOriginalFeatures());
    final List<PredictionInput> inputs = List.of(new PredictionInput(input));
    final CompletableFuture<List<PredictionOutput>> predictionAsync = solution.getModel().predictAsync(inputs);
    try {
        List<PredictionOutput> predictions = predictionAsync.get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit());
        solution.setPredictionOutputs(predictions);
        final BendableBigDecimalScore outputScore = calculateOutputScore(solution);
        currentScore = currentScore.add(outputScore);
    } catch (ExecutionException e) {
        logger.error("Prediction returned an error {}", e.getMessage());
    } catch (InterruptedException e) {
        logger.error("Interrupted while waiting for prediction {}", e.getMessage());
        Thread.currentThread().interrupt();
    } catch (TimeoutException e) {
        logger.error("Timed out while waiting for prediction");
    }
    return currentScore;
}
Also used : PredictionInput(org.kie.kogito.explainability.model.PredictionInput) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) BendableBigDecimalScore(org.optaplanner.core.api.score.buildin.bendablebigdecimal.BendableBigDecimalScore) List(java.util.List) ExecutionException(java.util.concurrent.ExecutionException) Feature(org.kie.kogito.explainability.model.Feature) TimeoutException(java.util.concurrent.TimeoutException)

Example 19 with PredictionInput

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

the class ShapKernelExplainer method explain.

/**
 * Compute the shap values for a specific prediction
 *
 * @param prediction: The ShapPrediction to be explained.
 * @param model: The PredictionProvider we are explaining.
 *
 * @return the shap values for this prediction, of shape [n_model_outputs x n_features]
 */
private CompletableFuture<ShapResults> explain(Prediction prediction, PredictionProvider model) {
    ShapDataCarrier sdc = this.initialize(model);
    sdc.setSamplesAdded(new ArrayList<>());
    PredictionInput pi = prediction.getInput();
    PredictionOutput po = prediction.getOutput();
    if (pi.getFeatures().size() != sdc.getCols()) {
        throw new IllegalArgumentException(String.format("Prediction input feature count (%d) does not match background data feature count (%d)", pi.getFeatures().size(), sdc.getCols()));
    }
    int cols = sdc.getCols();
    CompletableFuture<RealMatrix> output = sdc.getOutputSize().thenApply(os -> {
        if (po.getOutputs().size() != os) {
            throw new IllegalArgumentException(String.format("Prediction output size (%d) does not match background data output size (%d)", po.getOutputs().size(), os));
        }
        return MatrixUtils.createRealMatrix(new double[os][cols]);
    });
    RealVector poVector = MatrixUtilsExtensions.vectorFromPredictionOutput(po);
    // first find varying features
    this.setVaryingFeatureGroups(pi, sdc);
    // if no features vary, then the features do not effect output, and all shap values are zero.
    if (sdc.getNumVarying() == 0) {
        return output.thenApply(o -> saliencyFromMatrix(o, pi, po)).thenCombine(sdc.getFnull(), ShapResults::new);
    } else if (sdc.getNumVarying() == 1) // if 1 feature varies, this feature has all the effect
    {
        CompletableFuture<RealVector> diff = sdc.getLinkNull().thenApply(poVector::subtract);
        return output.thenCompose(o -> diff.thenCombine(sdc.getOutputSize(), (df, os) -> {
            RealMatrix out = MatrixUtils.createRealMatrix(new double[os][cols]);
            for (int i = 0; i < os; i++) {
                out.setEntry(i, sdc.getVaryingFeatureGroups(0), df.getEntry(i));
            }
            return saliencyFromMatrix(out, pi, po);
        })).thenCombine(sdc.getFnull(), ShapResults::new);
    } else // if more than 1 feature varies, we need to perform WLR
    {
        // establish sizes of feature permutations (called subsets)
        ShapStatistics shapStats = this.computeSubsetStatistics(sdc);
        // weight each subset by number of features
        this.initializeWeights(shapStats, sdc);
        // add all fully enumerated subsets
        this.addCompleteSubsets(shapStats, pi, sdc);
        // renormalize weights after full subsets have been added
        this.renormalizeWeights(shapStats);
        // sample non-fully enumerated subsets
        this.addNonCompleteSubsets(shapStats, pi, sdc);
        // run the synthetic data generated through the model
        CompletableFuture<RealMatrix> expectations = this.runSyntheticData(sdc);
        // run the wlr model over the synthetic data results
        return output.thenCompose(o -> this.solveSystem(expectations, poVector, sdc).thenApply(wo -> saliencyFromMatrix(wo[0], wo[1], pi, po))).thenCombine(sdc.getFnull(), ShapResults::new);
    }
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) LarsPath(org.kie.kogito.explainability.utils.LarsPath) Prediction(org.kie.kogito.explainability.model.Prediction) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) RealVector(org.apache.commons.math3.linear.RealVector) WeightedLinearRegression(org.kie.kogito.explainability.utils.WeightedLinearRegression) Saliency(org.kie.kogito.explainability.model.Saliency) ArrayList(java.util.ArrayList) MathArithmeticException(org.apache.commons.math3.exception.MathArithmeticException) MatrixUtils(org.apache.commons.math3.linear.MatrixUtils) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) LassoLarsIC(org.kie.kogito.explainability.utils.LassoLarsIC) CombinatoricsUtils(org.apache.commons.math3.util.CombinatoricsUtils) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) LocalExplainer(org.kie.kogito.explainability.local.LocalExplainer) AnyMatrix(org.apache.commons.math3.linear.AnyMatrix) WeightedLinearRegressionResults(org.kie.kogito.explainability.utils.WeightedLinearRegressionResults) FeatureImportance(org.kie.kogito.explainability.model.FeatureImportance) Collectors(java.util.stream.Collectors) PredictionProvider(org.kie.kogito.explainability.model.PredictionProvider) Consumer(java.util.function.Consumer) PredictionInput(org.kie.kogito.explainability.model.PredictionInput) List(java.util.List) MatrixUtilsExtensions(org.kie.kogito.explainability.utils.MatrixUtilsExtensions) RandomChoice(org.kie.kogito.explainability.utils.RandomChoice) RealMatrix(org.apache.commons.math3.linear.RealMatrix) Collections(java.util.Collections) PredictionInput(org.kie.kogito.explainability.model.PredictionInput) CompletableFuture(java.util.concurrent.CompletableFuture) RealMatrix(org.apache.commons.math3.linear.RealMatrix) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) RealVector(org.apache.commons.math3.linear.RealVector)

Example 20 with PredictionInput

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

the class ShapSyntheticDataSample method createSyntheticData.

/**
 * Create synthetic data for this particular sample,
 * according to the conditions set up in the ShapSyntheticDataSample initialization.
 *
 * @return Synthetic data for this particular sample
 */
private List<PredictionInput> createSyntheticData() {
    List<Feature> piFeatures = this.x.getFeatures();
    List<PredictionInput> synthData = new ArrayList<>();
    for (int i = 0; i < this.background.getRowDimension(); i++) {
        List<Feature> maskedFeatures = new ArrayList<>();
        for (int j = 0; j < this.mask.length; j++) {
            Feature oldFeature = piFeatures.get(j);
            if (this.mask[j]) {
                maskedFeatures.add(oldFeature);
            } else {
                maskedFeatures.add(FeatureFactory.newNumericalFeature(oldFeature.getName(), this.background.getEntry(i, j)));
            }
        }
        synthData.add(new PredictionInput(maskedFeatures));
    }
    return synthData;
}
Also used : PredictionInput(org.kie.kogito.explainability.model.PredictionInput) ArrayList(java.util.ArrayList) Feature(org.kie.kogito.explainability.model.Feature)

Aggregations

PredictionInput (org.kie.kogito.explainability.model.PredictionInput)187 PredictionOutput (org.kie.kogito.explainability.model.PredictionOutput)143 PredictionProvider (org.kie.kogito.explainability.model.PredictionProvider)135 Prediction (org.kie.kogito.explainability.model.Prediction)126 Feature (org.kie.kogito.explainability.model.Feature)109 Test (org.junit.jupiter.api.Test)107 ArrayList (java.util.ArrayList)97 SimplePrediction (org.kie.kogito.explainability.model.SimplePrediction)95 Random (java.util.Random)86 PerturbationContext (org.kie.kogito.explainability.model.PerturbationContext)67 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)60 Output (org.kie.kogito.explainability.model.Output)55 LimeConfig (org.kie.kogito.explainability.local.lime.LimeConfig)54 LinkedList (java.util.LinkedList)53 LimeExplainer (org.kie.kogito.explainability.local.lime.LimeExplainer)52 Value (org.kie.kogito.explainability.model.Value)52 Saliency (org.kie.kogito.explainability.model.Saliency)50 List (java.util.List)39 LimeConfigOptimizer (org.kie.kogito.explainability.local.lime.optim.LimeConfigOptimizer)33 Type (org.kie.kogito.explainability.model.Type)31