Skip to content

Commit

Permalink
Merge pull request #2 from benedekrozemberczki/abc-test-suite
Browse files Browse the repository at this point in the history
ABC Test suite
  • Loading branch information
benedekrozemberczki committed Apr 28, 2022
2 parents 95449f5 + 41d9550 commit 7d089cf
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 15 deletions.
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@
setup(
name="shapley",
packages=find_packages(),
version="1.0.2",
version="1.0.3",
license="MIT",
description="A general purpose library to quantify the value of classifiers in an ensemble.",
author="Benedek Rozemberczki",
author_email="[email protected]",
url="https://github.com/benedekrozemberczki/shapley",
download_url="https://github.com/benedekrozemberczki/shapley/archive/v_10002.tar.gz",
download_url="https://github.com/benedekrozemberczki/shapley/archive/v_10003.tar.gz",
keywords=keywords,
install_requires=install_requires,
setup_requires=setup_requires,
Expand Down
1 change: 1 addition & 0 deletions shapley/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from shapley.solvers import *
from shapley.solution_concept import SolutionConcept
from shapley.version import __version__ # noqa:F401,F403

__all__ = [
"shapley",
Expand Down
28 changes: 25 additions & 3 deletions shapley/solution_concept.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
"""Solution Concept base class."""


import numpy as np
from abc import ABCMeta, abstractmethod


class SolutionConcept(object):
class SolutionConcept(metaclass=ABCMeta):
"""Solution Concept base class with constructor and public methods."""

def __init__(self):
"""Creating a Solution Concept."""
@abstractmethod
def get_solution(self) -> np.ndarray:
r"""Returning the solution.
Return Types:
Phi (Numpy array): Approximate Shapley matrix of players in the game(s) with size :math:`n \times m`.
"""
pass

@abstractmethod
def solve_game(self, W: np.ndarray, q: float):
r"""Solving the weigted voting game(s).
Args:
W (Numpy array): An :math:`n \times m` matrix of voting weights for the :math:`n` games with :math:`m` players.
q (float): Quota in the games.
"""
pass

@abstractmethod
def setup(self, W: np.ndarray):
"""Creating an empty Shapley value matrix and a player pool."""
pass

def _check_quota(self, q: float):
Expand Down
4 changes: 2 additions & 2 deletions shapley/solvers/exact_enumeration.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ExactEnumeration(SolutionConcept):
`"A Value for N-Person Games." <https://www.rand.org/pubs/papers/P0295.html>`_
"""

def _setup(self, W: np.ndarray):
def setup(self, W: np.ndarray):
"""Creating an empty Shapley value matrix and a player pool."""
self._Phi = np.zeros(W.shape)
self._indices = [i for i in range(W.shape[1])]
Expand All @@ -36,7 +36,7 @@ def solve_game(self, W: np.ndarray, q: float):
q (float): Quota in the games.
"""
self._check_quota(q)
self._setup(W)
self.setup(W)
self._run_permutations(W, q)
self._run_sanity_check(W, self._Phi)
self._set_average_shapley()
Expand Down
8 changes: 4 additions & 4 deletions shapley/solvers/expected_marginal_contributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def create_integrand_vectors(player_count, q, w):
:return b_s: Vector of upper integrand limits.
"""
a_s = (q - w) / np.linspace(1, player_count - 1, player_count - 1)
b_s = (q - 10 ** -20) / np.linspace(1, player_count - 1, player_count - 1)
b_s = (q - 10**-20) / np.linspace(1, player_count - 1, player_count - 1)
return a_s, b_s


Expand All @@ -42,10 +42,10 @@ class ExpectedMarginalContributions(SolutionConcept):
<https://www.sciencedirect.com/science/article/pii/S0004370208000696>`_
"""

def __init__(self, epsilon: float = 10 ** -8):
def __init__(self, epsilon: float = 10**-8):
self.epsilon = epsilon

def _setup(self, W: np.ndarray):
def setup(self, W: np.ndarray):
"""Creating an empty Shapley value matrix."""
self._Phi = np.zeros(W.shape)

Expand Down Expand Up @@ -73,7 +73,7 @@ def solve_game(self, W: np.ndarray, q: float):
q (float): Quota in the games.
"""
self._check_quota(q)
self._setup(W)
self.setup(W)
self._approximate(W, q)
self._run_sanity_check(W, self._Phi)
self._set_average_shapley()
Expand Down
4 changes: 2 additions & 2 deletions shapley/solvers/multilinear_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class MultilinearExtension(SolutionConcept):
`"Multilinear Extensions of Games." <https://www.jstor.org/stable/2661445#metadata_info_tab_contents>`_
"""

def _setup(self, W: np.ndarray):
def setup(self, W: np.ndarray):
"""Creating an empty Shapley value matrix."""
self._Phi = np.zeros(W.shape)

Expand All @@ -35,7 +35,7 @@ def solve_game(self, W: np.ndarray, q: float):
q (float): Quota in the games.
"""
self._check_quota(q)
self._setup(W)
self.setup(W)
self._approximate(W, q)
self._run_sanity_check(W, self._Phi)
self._set_average_shapley()
Expand Down
4 changes: 2 additions & 2 deletions shapley/solvers/permutation_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class PermutationSampler(SolutionConcept):
def __init__(self, permutations: int = 1000):
self.permutations = permutations

def _setup(self, W: np.ndarray):
def setup(self, W: np.ndarray):
"""Creating an empty Shapley value matrix and a player pool."""
self._Phi = np.zeros(W.shape)
self._indices = [i for i in range(W.shape[1])]
Expand All @@ -43,7 +43,7 @@ def solve_game(self, W: np.ndarray, q: float):
q (float): Quota in the games.
"""
self._check_quota(q)
self._setup(W)
self.setup(W)
self._run_permutations(W, q)
self._run_sanity_check(W, self._Phi)
self._set_average_shapley()
Expand Down
3 changes: 3 additions & 0 deletions shapley/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""Contains the version of ChemicalX."""

__version__ = "1.0.3"

0 comments on commit 7d089cf

Please sign in to comment.