# XAI Experiment 3: Approximate Shapley Value (permutation sampling)  [exam version]
# Requires: exam: none (random) | lab: numpy, pandas, matplotlib
# ===========================================================================

import random

def model(x):
    return 2*x[0] + 3*x[1] + 5*x[2]

x = [4, 2, 3]

background = [
    [5, 1, 4],
    [2, 5, 1],
    [3, 2, 6]
]

M = int(input("Enter iterations: "))
p = len(x)
shapley = [0] * p

for _ in range(M):
    z = random.choice(background)
    order = list(range(p))
    random.shuffle(order)

    current = z[:]

    for j in order:
        before = model(current)

        current[j] = x[j]

        after = model(current)

        shapley[j] += after - before

for j in range(p):
    shapley[j] /= M
    print("Feature", j+1, "Shapley Value:", round(shapley[j], 3))
