# Define the characteristic function (measure)
nu <- function(S) {
if ("A" %in% S && "B" %in% S && "C" %in% S) {
return(4)
} else {
return(0)
}
}
# Calculate the Shapley value for each player (set)
shapley_value <- function(player, n = 3) {
permutations <- permutations(n, n)
num_permutations <- length(permutations)
marginal_contributions <- numeric(num_permutations)
for (i in 1:num_permutations) {
coalition <- permutations[i, ]
if (player %in% coalition) {
previous_coalition <- coalition[coalition != player]
marginal_contributions[i] <- nu(c(coalition)) - nu(c(previous_coalition))
}
}
shapley_value <- sum(marginal_contributions) / num_permutations
return(shapley_value)
}
# Calculate Shapley value for each player
shapley_A <- shapley_value("A")
shapley_B <- shapley_value("B")
shapley_C <- shapley_value("C")
# Output the results
print(paste("Shapley value for player A:", shapley_A))
print(paste("Shapley value for player B:", shapley_B))
print(paste("Shapley value for player C:", shapley_C))