Explainable AI and Finance: Delve into the future of AI with Python

LIME output shows a local interpretable model-agnostic explanation for a single prediction. In this case, the model predicted a closing price of 123.59. The explanation highlights the features that contributed most to this prediction, both positively and negatively. Here’s a breakdown of the key elements:

1. Capture:

This is the base prediction of the model if none of the elements were present. In this case it is 242.49.

2. Prediction_local:

This is the local interpretable model prediction which is 117.32. This is closer to the actual prediction of 123.59 than the intercept, indicating that the properties have some influence on the prediction.

3. Features:

The table shows the features that were used by the local interpretable model, along with their values ​​and their contribution to the prediction.

  • Positive Contributions: These properties increased the expected closing price. For example, the fact that the low price was below 125.98, as well as the fact that the open price was below 127.00 and volume was high, contributed positively to the prediction.
  • Negative Contributions: These properties lowered the projected closing price. For example, the fact that the high price was lower than 128.29 contributed negatively to the prediction.

It is important to note that LIME only provides a local explanation for a single prediction. It is not guaranteed to be accurate for other forecasts and may not capture all the important factors that influence the model’s predictions. However, it can be a useful tool for understanding how an ML model makes decisions.

Here are a few other things to keep in mind:

  • The importance of each feature is relative to the other features in the explanation. For example, a feature with a small contribution may still be important if other features have even smaller contributions.
  • A LIME explanation does not necessarily imply causality. For example, just because an explanation says that a low low price contributed positively to the prediction does not mean that low prices cause stocks to rise.

More details about LIME

  • LIME (Local Interpretable Model Agnostic Explanation):
  • LIME is a model-agnostic technique used to explain the individual predictions of any machine learning model.
  • Locally approximates the predictions of a black-box model by training an interpretable model (eg, linear regression) on perturbed samples around the instance to be explained.
  • LIME generates local explanations that are human-interpretable and provide insight into why the model made a particular prediction for a given instance.

How Lime works

1. LIME selects a specific instance to explain.

2. It generates perturbations around this instance by randomly perturbing the function values ​​while the other functions are constant.

3. For each perturbed instance, it predicts the output using a black box model.

4. LIME fits an interpretable model (eg, linear regression) to the disturbed instances and their corresponding predicted outputs.

5. Assigns weights to features based on their importance in an interpretable model and provides local explanations.

LIME (Local Interpretable Model Agnostic Explanation):


1. Local linear approximation:

LIME approximates a complex black box model locally around a particular instance using a simple, interpretable model, typically a linear model.

For a given instance of x, LIME generates noise around x by randomly sampling nearby data points, possibly with minor changes.

It then calculates the distances between these perturbed points ax to consider their effect on the local approximation.

Using these perturbed data points and their corresponding model predictions, LIME fits a simple linear model that explains the behavior of the black box model around x.

2. Importance of function:

Once the local linear model is trained, LIME assigns weights to each feature.

These weights represent the importance of an element for local approximation.

Higher weights indicate features that have a stronger influence on the model’s prediction for a particular instance of x.

Additional features of LIME (Local Interpretable Model-Agnostic Explans)

Explanatory initialization:

LimeTabularExplainer: Initializes the explanation for the table data.

LimeTextExplainer: Initializes the explainer for text data.

LimeImageExplainer: Initializes the explanation for the image data.

LimeTimeSeriesExplainer: Initializes the explainer for time series data.

Visualization Explanation:

explain_instance: Explains individual predictions.

show_in_notebook: Shows explanations in Jupyter notebooks.

show_in_notebook_text: Displays text explanations in Jupyter notebooks.

show_in_notebook_html: Displays HTML explanations in Jupyter notebooks.

as_pyplot_figure: Generates explainer plots using Matplotlib.

save_to_file: Saves the explanation to a file.

Advanced options:

discretize_continuous: Discretizes continuous elements for easier interpretation.

kernel_width: Sets the kernel width for weighted samples.

feature_selection: Specifies the element selection method.

num_features: Specifies the number of features to include in the explanation.

sample_around_instance: Sets the number of samples to generate within an instance.

perturbations_per_eval: Sets the number of faults to be evaluated.

In this section, we dive into a powerful tool known as SHAP (Shapley Additive Explanations) to gain insight into our model’s predictions. Using game theoretic principles, SHAP provides us with a comprehensive understanding of the importance of features and their contributions to the model’s output. Through the SHAP values, we examine how each feature affects our predictions, both individually and collectively. This section will show how SHAP improves our understanding of financial algorithms by offering a differentiated view of the importance of features and model behavior.

# Step 6: Explain Model Predictions using SHAP
explainer_shap = shap.TreeExplainer(model)
shap_values = explainer_shap.shap_values(X_test)

# Plot SHAP values
shap.summary_plot(shap_values, X_test)


Summary plot of SHAP (image by author)

The SHAP summary plot shows the relationship between the feature values ​​in your dataset and the model output. The x-axis represents the SHAP value, which is a measure of how much each element contributes to the model’s output. The y-axis represents the element values. The color of the dots represents the function value, with red indicating high values ​​and blue indicating low values.

Dot size represents the number of data points that have a given value of a particular element. The larger the dot, the more data points there are with that feature value.

A SHAP summary chart can be used to identify which elements are most important for making predictions and how these elements interact to influence the model output.

In the particular SHAP summary chart you sent me, the x-axis represents the value of SHAP, which is measured in dollars. The y-axis represents the element values ​​for the elements adjusted_close, openand volume. The color of the dots represents the function value, with red indicating high values ​​and blue indicating low values.

Dot size represents the number of data points that have a given value of a particular element. The larger the dot, the more data points there are with that feature value.

The SHAP summary chart shows that the function adjusted_close, openand volume all have a positive impact on the model output. This means that when these features have high values, the model is more likely to make a high prediction. Function adjusted_close has the greatest impact on model output, follows open and then volume.

The SHAP summary chart also shows that there is some interaction between the elements. For example impact adjusted_close is higher when open is also high. This means that the model is more likely to make a high prediction when both adjusted_close and open they are high.

SHAP (Shapley Additive Explained):

SHAP is a game-theoretic approach to explaining the output of any machine learning model.

It is based on Shapley values ​​from cooperative game theory.

The SHAP values ​​represent the contribution of each element to the difference between the actual model prediction and the average prediction (expected value). SHAP values ​​provide a global understanding of feature importance and can also explain individual predictions. They offer an overview of how individual features contribute to the output of the model.

From behind the scenes of SHAP:

1. SHAP assigns each feature combination a unique value that represents the model output for that combination.

2. Computes Shapley values ​​that quantify the average contribution of each feature value to the prediction across all possible feature combinations.

3. SHAP values ​​provide insight into the marginal contribution of each feature value to the difference between the actual and average prediction.

4. SHAP values ​​enable a comprehensive understanding of the importance of model features and behavior, both globally and locally.

SHAP (Shapley Additive Explained):


1. Concepts of game theory:

SHAP values ​​are based on cooperative game theory concepts, namely Shapley values.

In cooperative games, players form coalitions to achieve certain outcomes, and Shapley values ​​determine the fair distribution of reward among players.

When applied to machine learning, the features are considered as players and the result is the prediction of the model.

2. Shapley values:

For a given prediction f(x), where x is a set of functions, Shapley values ​​assign each element a value representing its contribution to the difference between the true prediction and the average prediction across all possible combinations of elements.

Mathematically, for fig iThe Shapley value ϕ is defined as the average marginal contribution of an element i through all possible coalitions in which it may participate.

It is calculated as the weighted sum of the differences in the predictions when the feature is included i compared to when it is excluded, with the weights representing the probability of each element being included.

3. Additive property:

The SHAP values ​​satisfy the additive property, which means that the sum of the SHAP values ​​for all features plus the average prediction equals the true model prediction.

This property ensures that the individual feature contributions add up to the model output and provide a coherent explanation of the prediction.

Additional features of SHAP (Shapley Additive Explanations):

Explanatory initialization:

TreeExplainer: Initializes the explainer for tree models.

KernelExplainer: Initializes the explainer for kernel-based models.

DeepExplainer: Initializes the explainer for deep learning models.

LinearExplainer: Initializes the explainer for linear models.

Visualization Explanation:

summary_plot: Generates a summary graph of SHAP values.

force_plot: Generates a power plot for a single prediction.

dependence_plot: Plots the dependence of the output on a single element.

waterfall_plot: Generates a waterfall plot to explain the contribution of each element to the output.

interaction_plot: Renders interactions between pairs of elements.

Advanced options:

approximate: Approximates SHAP values ​​for faster calculation.

shap_values: Computes SHAP values ​​for a given set of instances.

background: Specifies background data for conditional expectations.

link: Specifies the link function to transform model outputs.

feature_perturbation: Specifies the fault type of the function for calculating SHAP values.

The financial world is increasingly connected to complex algorithms and models for making investment decisions. However, these models are often opaque and difficult to understand, making it difficult for users to trust their outputs. This is where Explainable AI (XAI) comes in.

XAI techniques such as LIME and SHAP provide valuable tools for understanding the inner workings of these models and interpreting their predictions. Leverage EODHD for historical financial data, we can train and evaluate models for tasks such as stock price prediction. Subsequently, LIME and SHAP can be used to explain these predictions and highlight the elements that contributed most to the model output.

This combination of EODHD and XAI techniques enables users to gain valuable insights into financial markets and make more informed investment decisions.

Here are some key takeaways:

  • EODHD provides a comprehensive source of financial data that is essential for training and evaluating machine learning models in finance.
  • LIME and SHAP are powerful XAI techniques that can be used to explain the predictions of complex financial models.
  • By understanding how these models work, users can make more informed decisions and build confidence in their outputs.

This has brought you to the end of the article. I hope you learned something new and useful today. Thank you very much for your time.

Leave a Comment