Cooperative games and voting
When players can make binding agreements, the question is how to share what a coalition earns. When voters rank candidates, the question is how to choose. Both have several reasonable answers that can disagree, and here every answer is an exact fraction.
The Shapley value
import { cooperativeGame, shapley, core, inCore } from "@ratmath/discrete";
const glove = cooperativeGame(["L1", "L2", "R"],
(S) => (S.includes("R") && (S.includes("L1") || S.includes("L2")) ? 1 : 0));
shapley(glove); // { L1: 1/6, L2: 1/6, R: 2/3 }
A coalitional game assigns a worth to each coalition . Player 's Shapley value is their average marginal contribution over all orders in which the players could arrive:
It is the only sharing rule that is efficient, symmetric, additive and gives nothing to a dummy.
The core
core(glove); // { empty: false, allocation: { L1: 0, L2: 0, R: 1 } }
const majority = cooperativeGame(["a", "b", "c"], (S) => (S.length >= 2 ? 1 : 0));
core(majority);
// { empty: true, balanced: [{ab, 1/2}, {ac, 1/2}, {bc, 1/2}],
// reason: "these coalitions, with these weights, cover every player exactly once and are worth 3/2 together, more than v(N) = 1" }
The core is the set of allocations no coalition can improve on by leaving: for all , with . It is found by exact linear programming. When it is empty, the dual gives a balanced collection (Bondareva–Shapley): coalitions with weights covering each player exactly once whose weighted worth exceeds , a short proof that no allocation can satisfy them all.
core gives one allocation; coreVertices(game) gives them all. Every vertex of the core polytope makes and independent coalition constraints tight, so each such system is solved exactly and kept when it satisfies every constraint. kind is "empty", "point" or "polytope", with the dimension. In the glove game with one left glove and two right ones, the right gloves compete and the core is the single point .
const gloves = cooperativeGame(["L", "R1", "R2"], (S) => (S.includes("L") && S.length >= 2 ? 1 : 0));
coreVertices(gloves); // { kind: "point", vertices: [{ L: 1, R1: 0, R2: 0 }], dimension: 0 }
Voting power
import { weightedVoting } from "@ratmath/discrete";
weightedVoting({ quota: 51, weights: { A: 49, B: 48, C: 3 } }).shapleyShubik; // 1/3 each
With 49%, 48% and 3% of the votes and a simple majority, any two parties win, so all three have equal power. Banzhaf counts swings instead of orders; both are given.
Choosing a winner
import { plurality, borda, instantRunoff, condorcet } from "@ratmath/discrete";
const ballots = [{ ranking: ["A", "B", "C"], count: 8 }, { ranking: ["B", "C", "A"], count: 5 }, { ranking: ["C", "B", "A"], count: 4 }];
plurality(ballots).winners; // ["A"]
borda(ballots).winners; // ["B"]
instantRunoff(ballots).winners; // ["B"]
condorcet(ballots).winner; // "B": beats each other candidate head to head
condorcet([["a", "b", "c"], ["b", "c", "a"], ["c", "a", "b"]]).cycle; // ["a", "b", "c", "a"]
Arrow's theorem says no ranked method with three or more candidates has every fairness property one would like, and small profiles like this one show methods disagreeing. A Condorcet cycle is reported as the cycle.
Apportionment and fair division
import { apportion, adjustedWinner } from "@ratmath/discrete";
apportion({ A: 6, B: 6, C: 2 }, 10).seats; // { A: 4, B: 4, C: 2 }
apportion({ A: 6, B: 6, C: 2 }, 11).seats; // { A: 5, B: 5, C: 1 }: the Alabama paradox
adjustedWinner({ house: 50, car: 20, boat: 30 }, { house: 40, car: 40, boat: 20 }).points; // 520/9 each
Hamilton's method rounds quotas down and hands out the remaining seats by largest remainder; adding a seat can cost a state one. The divisor methods (Jefferson, Webster, Adams, Huntington–Hill) avoid that paradox but can violate quota. Adjusted winner splits at most one item and leaves both parties with equal points and no envy.
@ratmath/discrete and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.