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;
}
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;
}
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;
}
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);
}
}
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;
}
Aggregations