Skip to content

Commit d5a54f4

Browse files
committed
chore:shapley_value.py added to game theory
1 parent 61b33f6 commit d5a54f4

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

game_theory/shapley_value.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
def shapley_value(payoff_matrix):
2+
n = payoff_matrix.shape[0]
3+
shapley_values = np.zeros(n)
4+
5+
for i in range(n):
6+
for S in range(1 << n): # All subsets of players
7+
if (S & (1 << i)) == 0: # i not in S
8+
continue
9+
10+
S_without_i = S & ~(1 << i)
11+
marginal_contribution = payoff_matrix[S][i] - (payoff_matrix[S_without_i][i] if S_without_i else 0)
12+
shapley_values[i] += marginal_contribution / (len(bin(S)) - 2) # Normalize by size of S
13+
14+
return shapley_values
15+
16+
# Example usage
17+
payoff_matrix = np.array([[1, 2], [3, 4]])
18+
shapley_vals = shapley_value(payoff_matrix)
19+
print("Shapley Values:", shapley_vals)

0 commit comments

Comments
 (0)