File tree 1 file changed +27
-0
lines changed
1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change
1
+ import numpy as np
2
+
3
+ def shapley_value (payoff_matrix ):
4
+ n = payoff_matrix .shape [0 ] # Number of players
5
+ shapley_values = np .zeros (n ) # Initialize Shapley values
6
+
7
+ # Iterate over each player
8
+ for i in range (n ):
9
+ # Iterate over all subsets of players (from 0 to 2^n - 1)
10
+ for s in range (1 << n ): # All subsets of players
11
+ if (s & (1 << i )) == 0 : # If player i is not in subset S
12
+ continue
13
+
14
+ # Calculate the value of the subset S without player i
15
+ s_without_i = s & ~ (1 << i ) # Remove player i from the subset
16
+ marginal_contribution = payoff_matrix [s ][i ] - (payoff_matrix [s_without_i ][i ] if s_without_i else 0 )
17
+
18
+ # Count the size of the subset S
19
+ size_of_s = bin (s ).count ('1' ) # Number of players in subset S
20
+ shapley_values [i ] += marginal_contribution / (size_of_s * (n - size_of_s )) # Normalize by size of S
21
+
22
+ return shapley_values
23
+
24
+ # Example usage
25
+ payoff_matrix = np .array ([[1 , 2 ], [3 , 4 ]])
26
+ shapley_vals = shapley_value (payoff_matrix )
27
+ print ("Shapley Values:" , shapley_vals )
You can’t perform that action at this time.
0 commit comments