This directory contains comprehensive examples demonstrating various applications and use cases of the Shapley Value Calculator package. Each example is self-contained and includes detailed explanations and practical scenarios.
example_basic_coalition.py)Difficulty: Beginner
Use Case: When you have predefined values for each coalition
Learn the fundamentals of Shapley value calculation using the ShapleyCombinations class. This example covers:
python examples/example_basic_coalition.py
example_function_evaluation.py)Difficulty: Intermediate
Use Case: When coalition values are computed dynamically
Explore dynamic coalition evaluation using the ShapleyValueCalculator class. This example demonstrates:
python examples/example_function_evaluation.py
example_business_case.py)Difficulty: Intermediate to Advanced
Use Case: Practical business applications
Discover how Shapley values solve real business problems:
python examples/example_business_case.py
example_ml_features.py)Difficulty: Advanced
Use Case: ML model interpretation and feature analysis
Apply Shapley values to understand machine learning models:
python examples/example_ml_features.py
example_parallel_processing.py)Difficulty: Advanced
Use Case: Large-scale exact computations and optimization
Optimise performance for large exact games using ShapleyValueCalculator:
n_jobs)python examples/example_parallel_processing.py
example_montecarlo.py)Difficulty: Advanced
Use Case: Games with many players (20+) where exact computation is infeasible
Approximate Shapley values for large games using MonteCarloShapleyValue:
n_jobs timing table with speedup ratiosget_convergence_data() running estimatesget_raw_data() per-permutation marginal contributionspython examples/example_montecarlo.py
pip install shapley-value
# Individual example
python examples/example_basic_coalition.py
# All examples
for example in examples/example_*.py; do
echo "Running $example..."
python "$example"
echo "---"
done
| Example | Class used | Players | Complexity | Runtime |
|---|---|---|---|---|
| Basic Coalition | ShapleyCombinations |
3 | Beginner | < 1 s |
| Function Evaluation | ShapleyValueCalculator |
4 | Intermediate | < 1 s |
| Business Cases | ShapleyCombinations |
3โ4 | Intermediate | < 1 s |
| ML Features | ShapleyValueCalculator |
5 | Advanced | < 1 s |
| Parallel Processing | ShapleyValueCalculator |
8โ16 | Advanced | 1โ30 s |
| Monte Carlo | MonteCarloShapleyValue |
4โ50 | Advanced | 1โ30 s |
| Players | Recommended approach | Example |
|---|---|---|
| โค 10 | ShapleyCombinations (exact, pre-defined values) |
Basic Coalition |
| โค 20 | ShapleyValueCalculator (exact, evaluation function) |
Function Evaluation / Parallel |
| 20+ | MonteCarloShapleyValue (approximate, scalable) |
Monte Carlo |
# Exact โ when every coalition value is known in advance
from shapley_value import ShapleyCombinations
# Exact โ when values are computed by a function (โค ~20 players)
from shapley_value import ShapleyValueCalculator
# Approximate โ for 20+ players or expensive evaluation functions
from shapley_value import MonteCarloShapleyValue
Both ShapleyValueCalculator and MonteCarloShapleyValue follow the
scikit-learn n_jobs convention:
# Sequential (no overhead)
ShapleyValueCalculator(f, players, n_jobs=1)
MonteCarloShapleyValue(f, players, n_jobs=1)
# All available CPU cores
ShapleyValueCalculator(f, players, n_jobs=-1)
MonteCarloShapleyValue(f, players, n_jobs=-1)
# Exactly 4 cores
ShapleyValueCalculator(f, players, n_jobs=4)
MonteCarloShapleyValue(f, players, n_jobs=4)
Tip: For cheap evaluation functions (< 10 ยตs),
n_jobs=1avoids process-spawn overhead and is faster. For expensive functions (ML models, simulations),n_jobs=-1gives the best throughput.
mc = MonteCarloShapleyValue(f, players, num_samples=5000, random_seed=0)
convergence_df = mc.get_convergence_data() # shape: (5000, n_players)
# Plot with your favourite library to see when estimates stabilise:
# convergence_df.plot(title="Running Shapley estimates")
These examples demonstrate the versatility and power of Shapley values across various domains. Each example is designed to be educational, practical, and adaptable to your specific needs.