Skip to content

Commit

Permalink
fixed FSII and removed tqdm
Browse files Browse the repository at this point in the history
  • Loading branch information
FFmgll committed Apr 10, 2024
1 parent e8de080 commit b650ec2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 34 deletions.
13 changes: 6 additions & 7 deletions shapiq/exact_computer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from typing import Callable

import numpy as np
import tqdm
from scipy.special import bernoulli, binom

from shapiq import powerset
Expand Down Expand Up @@ -56,9 +55,7 @@ def compute_game_values(self, game_fun: Callable):
coalition_lookup = {}
# compute the game values
coalition_matrix = np.zeros((2**self.n, self.n))
for i, T in tqdm.tqdm(
enumerate(powerset(self.N, min_size=0, max_size=self.n)), total=2**self.n
):
for i, T in enumerate(powerset(self.N, min_size=0, max_size=self.n)):
coalition_lookup[T] = i
coalition_matrix[i, T] = 1
game_values = game_fun(coalition_matrix)
Expand All @@ -83,7 +80,7 @@ def moebius_transform(self):
coalition_lookup = {}
for i, S in enumerate(powerset(self.N)):
coalition_lookup[S] = i
for i, S in tqdm.tqdm(enumerate(powerset(self.N)), total=2**self.n):
for i, S in enumerate(powerset(self.N)):
s = len(S)
S_pos = coalition_lookup[S]
for T in powerset(S):
Expand Down Expand Up @@ -159,7 +156,9 @@ def get_fsii_weights(self):
fsii_weights[0] = self.big_M
fsii_weights[-1] = self.big_M
for coalition_size in range(1, self.n):
fsii_weights[coalition_size] = 1 / (self.n - 1) * binom(self.n - 2, coalition_size - 1)
fsii_weights[coalition_size] = 1 / (
(self.n - 1) * binom(self.n - 2, coalition_size - 1)
)
return fsii_weights

def get_stii_weights(self, order: int):
Expand Down Expand Up @@ -245,7 +244,7 @@ def base_interactions(self, index: str, order: int):

base_interaction_values = np.zeros(self.n_interactions[order])
base_weights = self.get_base_weights(index, order)
for coalition in tqdm.tqdm(powerset(self.N), total=2**self.n):
for coalition in powerset(self.N):
coalition_size = len(coalition)
coalition_pos = self.coalition_lookup[coalition]
for j, interaction in enumerate(powerset(self.N, max_size=order)):
Expand Down
2 changes: 1 addition & 1 deletion shapiq/interaction_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from shapiq.utils import generate_interaction_lookup, powerset

AVAILABLE_INDICES = {"k-SII", "SII", "STI", "FSI", "STII", "FSII", "SV", "BV", "Moebius"}
AVAILABLE_INDICES = {"k-SII", "SII", "STI", "FSI", "STII", "FSII", "SV", "BV", "BZV", "Moebius"}


@dataclass
Expand Down
52 changes: 26 additions & 26 deletions tests/test_exact_computer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,33 @@


def test_exact_computer_on_soum():
n = np.random.randint(low=2, high=12)
order = np.random.randint(low=1, high=min(n, 5))
n_basis_games = np.random.randint(low=1, high=100)
for i in range(100):
n = np.random.randint(low=2, high=12)
N = set(range(n))
order = np.random.randint(low=1, high=min(n, 5))
n_basis_games = np.random.randint(low=1, high=100)
soum = SOUM(n, n_basis_games=1, min_interaction_size=1)

n = 5
order = 2
n_basis_games = 1
soum = SOUM(n, n_basis_games=1, min_interaction_size=1)
predicted_value = soum(np.ones(n))[0]
emptyset_prediction = soum(np.zeros(n))[0]

predicted_value = soum(np.ones(n))[0]
emptyset_prediction = soum(np.zeros(n))[0]
# Compute via exactComputer
exact_computer = ExactComputer(N, soum)

# Compute via exactComputer
exact_computer = ExactComputer(soum.N, soum)
# Compute via sparse Möbius representation
moebius_converter = MoebiusConverter(N, soum.moebius_coefficients)

# Compute via sparse Möbius representation
moebius_converter = MoebiusConverter(soum.N, soum.moebius_coefficients)

# Compare ground truth via MoebiusConvert with exact computation of ExactComputer
shapley_interactions_gt = {}
shapley_interactions_exact = {}
for index in ["STII", "k-SII", "FSII"]:
shapley_interactions_gt[index] = moebius_converter.moebius_to_shapley_interaction(
order, index
)
shapley_interactions_exact[index] = exact_computer.shapley_interaction(order, index)
print(
np.sum((shapley_interactions_exact[index] - shapley_interactions_gt[index]).values ** 2)
)
print("test")
# Compare ground truth via MoebiusConvert with exact computation of ExactComputer
shapley_interactions_gt = {}
shapley_interactions_exact = {}
for index in ["STII", "k-SII", "FSII"]:
shapley_interactions_gt[index] = moebius_converter.moebius_to_shapley_interaction(
order, index
)
shapley_interactions_exact[index] = exact_computer.shapley_interaction(order, index)
assert (
np.sum(
(shapley_interactions_exact[index] - shapley_interactions_gt[index]).values ** 2
)
< 10e-7
)

0 comments on commit b650ec2

Please sign in to comment.