Data structureadvanced
Marginal contribution nets
Also called MC-nets.
Represents a TU game by rules of the form pattern of players gives value, with Shapley values computable in time linear in the rules.
- Time
- O(z + n) banzhaf · shapleyO(z) value
- Space
- O(z) rules
- Where
- n = the number of players; z = the total number of literals in all rules
- Model
- Word RAM
- Properties
- not concurrentnot externalnot persistent
- Evidence
- Proof sketchCode in 1 languages
- Origin
- Samuel Ieong, Yoav Shoham, 2005
A marginal contribution net (MC-net) stores a TU game as a list of rules. Each rule names some players that must be in a coalition, some players that must be out of it, and a value. The worth of a coalition is the sum of the values of the rules that it meets. A game in which the players interact in small groups gets a short net, where the table of has entries. The main point of the structure is that the Shapley value of a net takes time linear in its size, because each rule is a small game with a closed-form value and the value is additive (Shapley's uniqueness theorem). An MC-net is a representation of one game; the solution concepts on top of it, such as the core, have their own pages.
In plain words
Think of a list of bonus rules for a team. "If Ann and Bob both join, add 4." "If Ann and Cid both join, add 2." "If Dan joins and Eve does not, add 1." To find what a team is worth, check every rule and add up the bonuses that apply. To split the worth fairly with the Shapley value, you do not need to look at all the teams. Each rule hands out its own bonus: a rule with only people who must be present splits its bonus equally among them, and a rule that also names people who must be absent gives a share to the first kind and takes a share from the second kind. Add up the shares from all the rules, and you have each person's value.
The abstract type
The structure holds a TU game with and , and supports these operations:
- VALUE: return for a coalition .
- SHAPLEY: return the Shapley value .
- BANZHAF: return the Banzhaf value , where .
- Build the net from a table of (FROM-GAME), and add or remove a rule.
Representation
A rule is a triple : a nonempty set of positive literals, a set of negative literals with , and a number . Ieong and Shoham write it as a pattern of literals, for example . The rule applies to if and . The net is a list of rules, and its game is
Since , no rule applies to , so . The size of the net is , the number of literals. In memory, each rule is two short lists or two bit masks of players and one number. With bit masks and at most the word size, a test " and " is two word operations.
Every game has a net with positive literals only. Write , where is the unanimity game of and is the Harsanyi dividend (Möbius inversion). The rules for the with form a net for . With negative literals the net can be much shorter, as the example below shows.
Operations
Write and for a rule.
VALUE(R, S)- 1
total ← 0 - 2
for each rule (P, Q, x) in R do - 3
if P ⊆ S and Q ∩ S = ∅ then total ← total + x - 4
return total SHAPLEY(R, n)- 5
phi[i] ← 0 for each player i - 6
for each rule (P, Q, x) in R do ▷ a = |P|, b = |Q| - 7
for each i in P do - 8
phi[i] ← phi[i] + x (a-1)! b! / (a+b)! - 9
for each j in Q do - 10
phi[j] ← phi[j] - x a! (b-1)! / (a+b)! - 11
return phi BANZHAF(R, n)- 12
beta[i] ← 0 for each player i - 13
for each rule (P, Q, x) in R do - 14
share ← x / 2^(a+b-1) - 15
for each i in P do beta[i] ← beta[i] + share - 16
for each j in Q do beta[j] ← beta[j] - share - 17
return beta
Players that occur in no rule are null players and keep the value 0. A rule with splits equally among the players of , which is the value of the game . To add a rule, append it; to remove one, delete it. Both change by the game of that one rule.
FROM-GAME computes the dividends of all nonempty coalitions from a table of and keeps the nonzero ones. The direct sum over subsets takes time. The fast Möbius transform over the subset lattice takes .
Example
Positive rules only. The game with , , and otherwise is the net
VALUE, and VALUE since no rule applies. SHAPLEY gives player 1 the share from the first rule and from the second, player 2 gets and player 3 gets : . BANZHAF gives the shares and in the same way, so as well.
A rule with a negative literal. The weighted voting game wins exactly on , and . Two rules describe it:
The first rule applies to and , and the second only to , so each winning coalition gets exactly 1. For SHAPLEY, the first rule (, ) gives to players 1 and 2. The second rule (, ) gives to players 1 and 3 and to player 2. The sums are
the Shapley-Shubik index of the game. For BANZHAF the shares are from the first rule and from the second, so , the raw Banzhaf index. A single rule can give a negative share, but the sum is right. The net from the dividends has three rules instead: , and .
Negative literals save space. Let if meets a set and if not. Its dividends are for every nonempty , so the net with positive literals only has rules. With negative literals, rules are enough: , , and in general . For each that meets , exactly the rule of the smallest with applies. SHAPLEY gives each player of the value .
Graph games. For a weighted graph on the players, let be the total weight of the edges with both ends in (Deng and Papadimitriou 1994). The net has one rule per edge, and SHAPLEY gives each player half the weight of its edges.
Correctness
VALUE adds exactly the values of the rules that apply, which is the definition of . For SHAPLEY, take one rule and a uniform random order. A player adds exactly when all of comes before it and all of comes after it. Among the relative orders of , there are such orders. A player adds exactly when all of comes before it and all of after it, in orders. The other players never change whether the rule applies. The Shapley value is additive, so the values of the rules add up. For BANZHAF, the marginal contribution of to is for the sets that contain and avoid , which gives , and the same count holds for . The full proofs are in the proof file.
Proof: The value of a coalition and the Shapley and Banzhaf values of an MC-netfull
An MC-net on the players is a list of rules , where is nonempty, is disjoint from and is a number. The rule applies to if and . The game of the net is , where if applies to and if not. Since , no rule applies to , so and is a TU game.
Lemma (VALUE). VALUE returns .
Proof. Line L1 sets . The loop of line L2 visits each rule once, and line L3 adds exactly when the rule applies to . So after the loop, , which line L4 returns.
Lemma (one rule). Let with and . The Shapley value of the game is
and for . The raw Banzhaf value is for , for , and 0 for the other players.
Proof. We use the order form of the Shapley value: is the mean over the orders of , where is the set of the players before in .
A player does not change whether the rule applies, so for every . Every marginal contribution of is 0.
Let and let . Since and , the rule does not apply to , so . The rule applies to if and only if and . So the marginal contribution is if, in , all the other players of come before and all the players of come after , and 0 if not. This event depends only on the relative order of the players of , which is uniform over the orders of these players when is uniform. The orders in the event put in the first places in any order, in place , and in the last places in any order. There are of them, which gives the first formula.
Let and . The rule does not apply to , because . It applies to if and only if and . So the marginal contribution is if all of comes before and all of comes after , and 0 if not. The same count gives orders out of .
For the Banzhaf value of , the marginal contribution to is if and , and 0 if not. Such a set contains the fixed players, avoids the fixed players and is free on the other players, so there are of them. Divided by , this gives . For , the sets with marginal contain and avoid , which again fixes players and leaves sets.
Theorem (SHAPLEY and BANZHAF). SHAPLEY returns , and BANZHAF returns the raw Banzhaf values .
Proof. Both values are linear in the game: the order form and the subset form are sums of terms with coefficients that do not depend on . So and . Line L5 sets each to 0. The loop of line L6 visits each rule once, and lines L7 to L10 add the terms of the one-rule lemma for the players in and in . Players outside get 0 from the rule, so they need no update. After the loop, is the sum of the contributions of all rules, which line L11 returns. Lines L12 to L17 do the same for the Banzhaf value with the term of line L14.
Check of efficiency. For one rule with , the Shapley values sum to , since . For , they sum to .
Lemma (from a table). For every TU game on , the rules with , over the nonempty with , form an MC-net with .
Proof. The rule applies to if and only if , so . By Möbius inversion on the lattice of subsets, for every , and . So .
Complexity
Let be the number of literals in the net and the number of players.
- VALUE takes time with in a hash set or a Boolean array. With bit masks and at most the word size, it takes time for rules.
- SHAPLEY and BANZHAF take arithmetic operations: to clear the output, and per literal with a table of the factorials or of the powers of 2. With integer rule values the results are rationals whose denominators divide (for SHAPLEY) or (for BANZHAF), so exact arithmetic adds bits to the numbers.
- Space is words for the rules.
- FROM-GAME takes time from a table of , and can return rules.
The size of the net is the right measure: the Shapley value is linear in it, but a net can itself be large. The OR game above needs positive rules, and other games, such as a majority game on many players, have dense dividends and need long nets.
Other questions are harder on nets. Ieong and Shoham (2005) show that questions about the core are intractable on general nets. They give algorithms for core membership and core emptiness whose time is exponential only in the treewidth of the graph that links two players when they share a rule, using a tree decomposition.
Variants
- Positive literals only. The net is the list of the nonzero Harsanyi dividends. It is unique for each game and has the dividend formula directly.
- Graph games (induced subgraph games): rules with two positive literals. The Shapley value is half the weighted degree. Deng and Papadimitriou (1994) study their core.
- Patterns other than conjunctions. A rule can use a Boolean formula as the pattern. A conjunction of literals keeps the closed form of SHAPLEY; for other formulas the value of a rule is no longer a single formula in and .
- Banzhaf and other semivalues. Any value that is linear in can be computed rule by rule in the same way, with the value of one rule in closed form, as BANZHAF does.
When to use it
Use an MC-net when the worth of a coalition comes from many small interactions: pairs or small groups of players that create a bonus together, with some exceptions when a certain player is present. It is the natural format when you need Shapley or Banzhaf values exactly for many players and the game is sparse in this sense. If the game is a weighted voting game, use its weights and dynamic programming instead, since threshold games rarely have short nets. If the game is only an oracle, use sampling.
Pitfalls
- An empty positive part. A rule with applies to the empty coalition and breaks . A rule with never applies. Reject both when the net is built.
- Signs of the negative literals. The share of is , with and , not the mirror of the share of . With , and , the players of get each and the player of gets .
- Negative shares are normal. A single rule can give a negative share even when the game is monotone. Check only the total.
- The net is not unique. With negative literals, many nets describe the same game. Two nets are equal as games only if their dividends are equal, and computing the dividends can take exponential time.
- Converting into a net can be expensive. The Shapley value is linear in the size of the net, not in the size of some other description of the game.
Implementations
The Python version stores a rule as two frozensets and a value, and returns exact Fractions for the Shapley and Banzhaf values. FROM-GAME uses the direct dividend sum, which is fine for the small where a full table of exists.
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/marginal-contribution-nets/. The authors are listed in prov/J0TAYV.jsonl.
"""Marginal contribution nets (MC-nets): a TU game written as a list of rules.
A rule is a triple (P, Q, x). P is the set of positive literals, Q the set of negative
literals and x the value. The rule applies to a coalition S if P is a subset of S and Q does
not meet S. The value v(S) is the sum of x over the rules that apply to S. P must be nonempty
and disjoint from Q, so that no rule applies to the empty coalition and v(empty set) = 0.
Players are 0 to n - 1. Values may be ints or Fractions, and the indices come out exact.
"""
from fractions import Fraction
from itertools import combinations
from math import factorial
Rule = tuple[frozenset, frozenset, object]
def make_rule(pos, neg, x) -> Rule:
"""Build a rule and check that it is valid."""
P, Q = frozenset(pos), frozenset(neg)
if not P:
raise ValueError("a rule needs at least one positive literal")
if P & Q:
raise ValueError("a player cannot be both a positive and a negative literal")
return (P, Q, x)
def value(rules: list[Rule], S) -> object:
"""v(S): the sum of the values of the rules that apply to S."""
S = frozenset(S)
total = 0 # @line L1
for P, Q, x in rules: # @line L2
if P <= S and not (Q & S): # @line L3
total += x
return total # @line L4
def shapley(rules: list[Rule], n: int) -> list[Fraction]:
"""The Shapley value of every player, in time linear in the size of the rules."""
phi = [Fraction(0)] * n # @line L5
for P, Q, x in rules: # @line L6
a, b = len(P), len(Q)
for i in P: # @line L7
phi[i] += Fraction(x) * factorial(a - 1) * factorial(b) / factorial(a + b) # @line L8
for j in Q: # @line L9
phi[j] -= Fraction(x) * factorial(a) * factorial(b - 1) / factorial(a + b) # @line L10
return phi # @line L11
def banzhaf(rules: list[Rule], n: int) -> list[Fraction]:
"""The raw Banzhaf value of every player: the mean of v(S + i) - v(S) over the 2^(n-1) sets S."""
beta = [Fraction(0)] * n # @line L12
for P, Q, x in rules: # @line L13
share = Fraction(x) / 2 ** (len(P) + len(Q) - 1) # @line L14
for i in P: # @line L15
beta[i] += share
for j in Q: # @line L16
beta[j] -= share
return beta # @line L17
def from_game(n: int, v) -> list[Rule]:
"""Rules with no negative literals for any game v, from its Harsanyi dividends.
The dividend of T is the sum over S in T of (-1)^(|T| - |S|) v(S). The game is the sum of
dividend(T) times the unanimity game of T, so the rule (T, empty set, dividend(T)) for each
nonempty T with a nonzero dividend gives back v. The output can have 2^n - 1 rules.
"""
rules = []
for r in range(1, n + 1):
for T in combinations(range(n), r):
d = 0
for k in range(r + 1):
for S in combinations(T, k):
d += (-1) ** (r - k) * v(frozenset(S))
if d != 0:
rules.append(make_rule(T, (), d))
return rules
test_marginal_contribution_nets.py 118 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/marginal-contribution-nets/. The authors are listed in prov/J0TAYV.jsonl.
import random
from fractions import Fraction
from itertools import combinations
from math import factorial
import pytest
from marginal_contribution_nets import banzhaf, from_game, make_rule, shapley, value
def all_sets(n):
for r in range(n + 1):
for S in combinations(range(n), r):
yield frozenset(S)
def brute_shapley(n, v):
phi = []
for i in range(n):
t = Fraction(0)
for S in all_sets(n):
if i in S:
continue
s = len(S)
t += Fraction(factorial(s) * factorial(n - 1 - s), factorial(n)) * (v(S | {i}) - v(S))
phi.append(t)
return phi
def brute_banzhaf(n, v):
out = []
for i in range(n):
t = sum(v(S | {i}) - v(S) for S in all_sets(n) if i not in S)
out.append(Fraction(t, 2 ** (n - 1)))
return out
def random_rules(rng, n, count):
rules = []
for _ in range(count):
players = list(range(n))
rng.shuffle(players)
a = rng.randint(1, n)
b = rng.randint(0, n - a)
rules.append(make_rule(players[:a], players[a:a + b], rng.randint(-6, 9)))
return rules
def test_example_game():
# v(12) = 4, v(13) = 2, v(123) = 6 (players 0, 1, 2 here): two rules.
rules = [make_rule({0, 1}, (), 4), make_rule({0, 2}, (), 2)]
assert [value(rules, S) for S in ({0, 1}, {0, 2}, {1, 2}, {0, 1, 2})] == [4, 2, 0, 6]
assert shapley(rules, 3) == [3, 2, 1]
assert banzhaf(rules, 3) == [3, 2, 1]
def test_random_nets_against_enumeration():
rng = random.Random(2005)
for _ in range(40):
n = rng.randint(1, 7)
rules = random_rules(rng, n, rng.randint(0, 6))
v = lambda S, rules=rules: value(rules, S)
assert v(frozenset()) == 0
assert shapley(rules, n) == brute_shapley(n, v)
assert banzhaf(rules, n) == brute_banzhaf(n, v)
assert sum(shapley(rules, n)) == v(frozenset(range(n)))
def test_round_trip_from_game():
rng = random.Random(4)
for _ in range(15):
n = rng.randint(1, 6)
table = {S: (rng.randint(-5, 10) if S else 0) for S in all_sets(n)}
rules = from_game(n, table.__getitem__)
assert all(not Q for _, Q, _ in rules)
for S in all_sets(n):
assert value(rules, S) == table[S]
assert shapley(rules, n) == brute_shapley(n, table.__getitem__)
def test_or_game_is_small_with_negations():
k = 6
v = lambda S: 1 if S else 0
# Without negative literals the dividends give 2^k - 1 rules.
assert len(from_game(k, v)) == 2 ** k - 1
# With negative literals, k rules: player i counts if no lower player is present.
rules = [make_rule({i}, range(i), 1) for i in range(k)]
for S in all_sets(k):
assert value(rules, S) == v(S)
assert shapley(rules, k) == [Fraction(1, k)] * k
def test_graph_game():
# Edge weights; v(S) is the weight of the edges with both ends in S.
edges = {(0, 1): 3, (1, 2): 5, (0, 3): -2, (2, 3): 4}
rules = [make_rule(e, (), w) for e, w in edges.items()]
expect = [Fraction(0)] * 4
for (a, b), w in edges.items():
expect[a] += Fraction(w, 2)
expect[b] += Fraction(w, 2)
assert shapley(rules, 4) == expect
def test_fraction_values():
rules = [make_rule({0}, {1}, Fraction(1, 3)), make_rule({1, 2}, (), Fraction(5, 2))]
v = lambda S: value(rules, S)
assert shapley(rules, 3) == brute_shapley(3, v)
assert banzhaf(rules, 3) == brute_banzhaf(3, v)
def test_invalid_rules():
with pytest.raises(ValueError):
make_rule((), {0}, 1)
with pytest.raises(ValueError):
make_rule({0, 1}, {1}, 1)
History
Ieong and Shoham (2005) introduced marginal contribution nets at the ACM Conference on Electronic Commerce, as a representation that is both expressive and fast for the Shapley value. They proved that the Shapley value takes linear time in the size of the net and studied the core through the treewidth of the interaction graph. Their work built on the graph games of Deng and Papadimitriou (1994), which are nets whose rules have two players. The textbooks of Shoham and Leyton-Brown (2009) and of Chalkiadakis, Elkind and Wooldridge (2011) cover MC-nets among the compact representations of coalitional games.
References
- Samuel Ieong and Yoav Shoham, Marginal contribution nets: a compact representation scheme for coalitional games, Proceedings of the 6th ACM Conference on Electronic Commerce (EC 2005), 193-202, the rules with conjunctions of literals, the Shapley value in time linear in the size of the net, and the core algorithms by tree decomposition
- Xiaotie Deng and Christos H. Papadimitriou, On the complexity of cooperative solution concepts, Mathematics of Operations Research 19(2) (1994), 257-266, the games on weighted graphs, whose Shapley value is half the weight of the edges at each node
- Georgios Chalkiadakis, Edith Elkind and Michael Wooldridge, Computational Aspects of Cooperative Game Theory, Morgan and Claypool, 2011, Chapter 3, Representations and algorithms (induced subgraph games and marginal contribution nets)
- Yoav Shoham and Kevin Leyton-Brown, Multiagent Systems, Algorithmic, Game-Theoretic, and Logical Foundations, Cambridge University Press, 2009, Section 12.3.5, Marginal contribution nets
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H1 sketch |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag J0TAYV Edit this page History Report an error Markdown