Algorithmadvanced
McAfee's trade reduction mechanism
Also called McAfee double auction.
A double auction for one good that drops the least valuable efficient trade when needed, so that it is truthful, individually rational and never runs a deficit.
- Time
- O(n log n) worst
- Space
- O(n) extra
- Where
- n = the number of buyers and sellers
- Model
- Word RAM
- Properties
- comparison baseddeterministiciterative
- Evidence
- Proof sketchCode in 1 languages
- Origin
- R. Preston McAfee, 1992
McAfee's trade reduction mechanism is a double auction for one kind of good. Each buyer wants one unit and each seller has one unit. The mechanism finds the trades that an efficient market would make, and then either lets all pairs trade at one price taken from the first pair that does not trade, or, when that price does not fit, drops the -th pair and charges the others prices set by that dropped pair. Truthful reporting is a dominant strategy for every buyer and every seller, nobody trades at a loss, and the auctioneer never adds money. The cost is at most one trade, the least valuable one, so the mechanism gets at least a fraction of the possible gains from trade. The Myerson-Satterthwaite theorem says that no mechanism can have all of these properties and also be fully efficient.
In plain words
Buyers say the most they would pay, and sellers say the least they would accept. Line the buyers up from the highest offer down and the sellers from the lowest ask up, and pair them until a buyer offers less than its seller asks. Those pairs could all trade. Now look at the first pair that cannot trade, and take the midpoint of their two numbers as a price. If every paired trader is happy at that price, they all trade at it. If not, the last pair is sent home, and the other buyers pay the offer of the sent-home buyer while the other sellers get the ask of the sent-home seller. Nobody can change their own price by lying, so the truth is the best report.
The problem
There are buyers with values for one unit, and sellers with costs for their unit, traders in all. Every trader has quasilinear utility: a buyer that trades at price gets , a seller that trades at price gets , and a trader that does not trade gets 0. The traders report bids and asks, and the mechanism chooses which buyers and sellers trade and their prices. The aims are: truthful reporting is a dominant strategy (DSIC), no truthful trader loses (individual rationality), the buyers pay at least what the sellers receive (weak budget balance), and the gains from trade, the sum of over the trading pairs, are close to the maximum.
The algorithm
MCAFEE(b_1, ..., b_B, s_1, ..., s_S)- 1
rank buyers b_(1) ≥ b_(2) ≥ ... and sellers s_(1) ≤ s_(2) ≤ ...; ties go to the smaller index - 2
k ← the largest t with b_(t) ≥ s_(t), or 0 if there is none - 3
if k = 0: return no trade - 4
if buyer k + 1 and seller k + 1 exist: p0 ← (b_(k+1) + s_(k+1)) / 2 - 5
if s_(k) ≤ p0 ≤ b_(k): - 6
return buyers and sellers of ranks 1..k, all trading at the price p0 - 7
return buyers and sellers of ranks 1..k-1; each buyer pays b_(k), each seller receives s_(k)
The bids do not increase and the asks do not decrease along the ranks, so the pairs with are exactly the first . In line L7 the auctioneer keeps .
Example
All efficient trades happen. Buyers bid and sellers ask . The pairs are , and , so . The price is , which lies in . Both pairs trade at 5.5. The buyers get and , and the sellers get and . The gains from trade are , the maximum.
One trade is dropped. Buyers bid and sellers ask . Again , but is above . Line L7 runs: the buyer with 10 pays 5 and the seller with 1 receives 3, and the auctioneer keeps 2. The gains from trade are 9, against the maximum . The lost trade is worth 2, the smaller of the two.
Correctness
The proof is in proofs/correctness.md. It shows these facts.
- Efficient trades. With truthful reports, the largest possible gains from trade are , made by the first pairs.
- Budget balance and the loss. The buyer price is at least the seller price. Line L6 makes all trades, and line L7 drops only the pair with the smallest gain, so the gains are at least of the maximum.
- A threshold for each trader. Fix the reports of everyone except one buyer. There is a number , which depends only on the others, such that the buyer trades when its bid is above , does not trade when its bid is below , and pays exactly when it trades. The proof finds by a case analysis on the number of efficient trades without this buyer. The value is the midpoint price of the first pair that does not trade, or the bid of the last trading buyer, depending on the case. Sellers are symmetric.
- Truthful and individually rational. A threshold with a payment equal to it is the condition of Myerson's lemma for this single-parameter setting, so the truth is a dominant strategy and a truthful trader never loses.
Proof: McAfee's trade reduction mechanism is truthful, individually rational, weakly budget balanced and loses at most one tradefull
We use the pseudocode of the page. Buyers report bids and sellers report asks. Line L1 ranks the buyers as and the sellers as , with ties broken by index. Line L2 sets to the largest with . Because the do not increase and the do not decrease, the set is a prefix of the ranks. We write when both traders of rank exist.
Claim 1 (the efficient trades). With truthful reports, the largest gain from trade over all sets of trading pairs is .
Proof. A set of trading pairs uses buyers and sellers. Its gain is at most the sum of the largest bids minus the sum of the smallest asks, which is . The terms do not increase in , they are at least 0 for and below 0 for . So is largest at .
Claim 2 (feasibility, budget balance and the trade loss). The output has as many buyers as sellers. Every trading buyer pays at most its bid and every trading seller receives at least its ask. The buyer price is at least the seller price, so the auctioneer never adds money. The gain from trade of the output is at least when .
Proof. If , line L3 returns no trade. Line L6 returns the top of each side at the price , and it runs only if . So each trading buyer has and each trading seller has . Line L7 returns the top of each side. Each buyer pays and each seller receives , and by the definition of , so the auctioneer keeps . Line L6 trades all pairs of Claim 1. Line L7 trades all of them but the pair of rank , whose gain is the smallest of the nonnegative terms. So it is at most .
Claim 3 (a threshold for each buyer). Fix a buyer and the reports of all other traders. There is a number that depends only on these reports, such that buyer trades if its bid , does not trade if , and pays exactly whenever it trades.
Proof. Let be the bids of the other buyers, ranked as in line L1, and put . Let be the asks. Let be the largest with , the number of efficient trades without . If there is no seller, nobody trades, and works. So assume exists.
Let be the ranked bids with added. The value is the -th largest of a list that contains the list and is one longer, so . Then and , hence . If buyer has a rank , the values below it shift down: for . If buyer trades, it has rank in line L6 and in line L7.
Case I: exists and . Here , so when . If , then and , so and buyer has a rank above ; it does not trade. Now let , so . Since , buyer has a rank , and . So does not depend on , and .
- (a) If exists and , set . For , line L5 holds and buyer trades at in line L6. For , we get , so line L7 runs, and buyer has rank and does not trade.
- (b) Otherwise set . Line L5 fails for every : either does not exist, or , or . So line L7 runs with . Buyer trades if and only if its rank is at most , which holds for and fails for . Then , so it pays .
Case II: does not exist, or . Then , because . We have for every : if does not exist there are only sellers, and otherwise . If buyer has rank at most , then , so , which does not depend on . Note .
- (a) If exists and , set . For , buyer ranks above , so its rank is at most , and . Line L5 holds and trades at . For , either buyer has a rank above , or it has rank with , so line L7 runs and the trader of rank does not trade. In both cases does not trade.
- (b) Otherwise set . If does not exist, line L7 runs. If it exists, when has rank at most , so again line L7 runs. Buyer trades if and only if its rank is at most , which holds for and fails for , and then it pays .
In every case the price paid by buyer is , and the outcome at only depends on the tie rule.
Claim 4 (truthful reporting is a dominant strategy, and it is individually rational). For every buyer with value , every bid and all reports of the others, the utility of bidding is at least the utility of bidding and at least 0. The same holds for every seller.
Proof. Take from Claim 3. If , the bid trades and gives , and every other bid gives or 0. If , the bid gives 0, and any bid that trades gives . If , every bid gives 0. For sellers, replace each bid by and each ask by and exchange the two sides. This maps the ranking, the index , the price , the test of line L5 and the prices of line L7 to the same objects of the other side, so the mechanism is symmetric, and Claim 3 holds for sellers with the inequalities reversed.
Claims 2 and 4 together are the theorem of McAfee (1992): the mechanism is dominant-strategy incentive compatible, ex post individually rational and weakly budget balanced, and with truthful reports it loses at most the least valuable of the efficient trades.
Complexity
Ranking the two sides takes time and space, and lines L2 to L7 take time. With bids that arrive already sorted, the whole mechanism takes time.
Properties
- DSIC for buyers and sellers.
- Ex post individually rational. Every trading buyer pays at most its bid and every trading seller receives at least its ask.
- Weakly budget balanced. In line L6 the budget balances exactly; in line L7 the auctioneer keeps a surplus.
- Loses at most one trade. The gains are at least the maximum minus , which is at least of the maximum. As markets grow, the share of lost gains tends to 0.
- Deterministic and prior-free. It needs no distribution of values.
Variants and improvements
- Plain trade reduction. Always use line L7. It keeps the same properties and is simpler, but it drops a trade even when the midpoint price would work.
- Vickrey double auction. All efficient pairs trade; each buyer pays and each seller receives . This is the VCG mechanism for the double auction. It is efficient and DSIC, but the sellers receive more than the buyers pay, so the auctioneer must cover a deficit.
- Strong budget balance. Later mechanisms keep truthfulness and lose few trades while sending all money from buyers to sellers, with no surplus for the auctioneer, for example by letting a randomly chosen trader set the price.
- Bayesian mechanisms. If the auctioneer knows the distributions, the mechanism can be tuned for expected gains, but by the Myerson-Satterthwaite theorem some loss remains.
When to use it
Use it for a market of one good with many buyers and sellers who each trade one unit, when the market maker must not lose money and when the traders should not need to guess the others' reports. It suits markets with many traders, where losing one trade costs little. In a market with one buyer and one seller it never trades, so use a posted price or a bargaining rule there.
Pitfalls
- Small markets. With and no pair at rank 2, line L7 drops the only trade. The guarantee is 0 for .
- Missing traders at rank . If there is no buyer or no seller of rank , the price does not exist and line L7 runs. Some write-ups add a dummy trader with a bound on the values instead; this changes the outcome, so state the rule.
- Unit demand and unit supply. The proof uses one unit per trader. With multi-unit traders, a trader's own bids can set its price, as in the uniform price auction, and the mechanism is no longer truthful.
- Exact arithmetic. The price is a midpoint, and the test of line L5 compares it with the bids. Use exact fractions so that the boundary cases do not change the outcome.
Implementations
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/mcafee-trade-reduction/. The authors are listed in prov/AMA27A.jsonl.
from fractions import Fraction
from typing import Sequence, Union
Number = Union[int, Fraction]
def mcafee_trade_reduction(
buyer_bids: Sequence[Number], seller_asks: Sequence[Number]
) -> tuple[list[int], list[int], Number, Number]:
"""Run McAfee's double auction with trade reduction.
Each buyer wants one unit and bids buyer_bids[i]. Each seller has one unit
and asks seller_asks[j]. Buyers are ranked by bid, highest first, and
sellers by ask, lowest first; a tie goes to the smaller index. Let k be
the number of efficient trades, the largest k with b(k) >= s(k).
If both a (k + 1)-th buyer and a (k + 1)-th seller exist and the price
p0 = (b(k + 1) + s(k + 1)) / 2 lies in [s(k), b(k)], the top k buyers and
sellers trade at p0. Otherwise only the top k - 1 pairs trade: each of
those buyers pays b(k) and each of those sellers receives s(k), and the
auctioneer keeps the difference.
Return (buyers, sellers, buyer_price, seller_price), with the 0-based
indices of the buyers and sellers who trade, in rank order.
"""
buyers = sorted(range(len(buyer_bids)), key=lambda i: (-buyer_bids[i], i)) # @line L1
sellers = sorted(range(len(seller_asks)), key=lambda j: (seller_asks[j], j)) # @line L1
b = [buyer_bids[i] for i in buyers]
s = [seller_asks[j] for j in sellers]
k = 0 # @line L2
while k < min(len(b), len(s)) and b[k] >= s[k]: # @line L2
k += 1 # @line L2
if k == 0: # @line L3
return [], [], 0, 0 # @line L3
if k < len(b) and k < len(s): # @line L4
p0 = Fraction(b[k] + s[k], 2) # @line L4
if s[k - 1] <= p0 <= b[k - 1]: # @line L5
return buyers[:k], sellers[:k], p0, p0 # @line L6
return buyers[:k - 1], sellers[:k - 1], b[k - 1], s[k - 1] # @line L7
def buyer_utility(value: Number, bids: Sequence[Number], asks: Sequence[Number], i: int) -> Number:
"""Utility of buyer i with the given value under the reports bids and asks."""
buyers, _, price, _ = mcafee_trade_reduction(bids, asks)
return value - price if i in buyers else 0
def seller_utility(cost: Number, bids: Sequence[Number], asks: Sequence[Number], j: int) -> Number:
"""Utility of seller j with the given cost under the reports bids and asks."""
_, sellers, _, price = mcafee_trade_reduction(bids, asks)
return price - cost if j in sellers else 0
test_mcafee_trade_reduction.py 152 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/mcafee-trade-reduction/. The authors are listed in prov/AMA27A.jsonl.
import itertools
import random
from fractions import Fraction
from mcafee_trade_reduction import buyer_utility, mcafee_trade_reduction, seller_utility
def optimal_gain(bids, asks):
"""Largest gain from trade, by brute force over equal-size sets of traders."""
best = 0
for t in range(min(len(bids), len(asks)) + 1):
for bs in itertools.combinations(range(len(bids)), t):
for ss in itertools.combinations(range(len(asks)), t):
best = max(best, sum(bids[i] for i in bs) - sum(asks[j] for j in ss))
return best
def gain(bids, asks):
buyers, sellers, _, _ = mcafee_trade_reduction(bids, asks)
return sum(bids[i] for i in buyers) - sum(asks[j] for j in sellers)
def test_example() -> None:
# k = 2 and p0 = (2 + 9) / 2 = 11/2 lies in [3, 8]: both pairs trade.
assert mcafee_trade_reduction([10, 8, 2], [1, 3, 9]) == ([0, 1], [0, 1], Fraction(11, 2), Fraction(11, 2))
# k = 2 and p0 = (4 + 9) / 2 = 13/2 is not in [3, 5]: one pair trades.
assert mcafee_trade_reduction([10, 4, 5], [9, 1, 3]) == ([0], [1], 5, 3)
# Without a third trader on a side the reduction branch is used.
assert mcafee_trade_reduction([10, 8], [1, 3, 9]) == ([0], [0], 8, 3)
def test_edges() -> None:
assert mcafee_trade_reduction([], []) == ([], [], 0, 0)
assert mcafee_trade_reduction([5], []) == ([], [], 0, 0)
assert mcafee_trade_reduction([2], [3]) == ([], [], 0, 0)
assert mcafee_trade_reduction([5], [1]) == ([], [], 5, 1)
assert mcafee_trade_reduction([4, 4], [4, 4]) == ([0], [0], 4, 4)
assert mcafee_trade_reduction([4, 4, 4], [4, 4, 4]) == ([0, 1], [0, 1], 4, 4)
assert mcafee_trade_reduction([10, 5, 6], [7, 1, 3]) == ([0, 2], [1, 2], 6, 6)
assert mcafee_trade_reduction([6, 6, 1], [1, 2, 5]) == ([0, 1], [0, 1], 3, 3)
def test_budget_balance_and_trade_loss() -> None:
rng = random.Random(1)
for _ in range(3000):
bids = [rng.randrange(0, 10) for _ in range(rng.randrange(0, 5))]
asks = [rng.randrange(0, 10) for _ in range(rng.randrange(0, 5))]
buyers, sellers, pb, ps = mcafee_trade_reduction(bids, asks)
assert len(buyers) == len(sellers)
assert pb >= ps
assert all(bids[i] >= pb for i in buyers)
assert all(asks[j] <= ps for j in sellers)
b = sorted(bids, reverse=True)
s = sorted(asks)
k = sum(1 for t in range(min(len(b), len(s))) if b[t] >= s[t])
opt = optimal_gain(bids, asks)
assert opt == sum(b[t] - s[t] for t in range(k))
assert len(buyers) in (k, max(k - 1, 0))
g = gain(bids, asks)
assert g >= opt - (b[k - 1] - s[k - 1] if k else 0)
assert k * g >= (k - 1) * opt
def test_truthful_is_dominant() -> None:
for nb, ns, top in ((1, 2, 5), (2, 1, 5), (2, 2, 5), (3, 2, 4), (2, 3, 4), (3, 3, 3)):
grid = range(0, top)
for bids in itertools.product(grid, repeat=nb):
for asks in itertools.product(grid, repeat=ns):
bids, asks = list(bids), list(asks)
for i in range(nb):
u_true = buyer_utility(bids[i], bids, asks, i)
assert u_true >= 0
for lie in grid:
other = bids[:i] + [lie] + bids[i + 1:]
assert u_true >= buyer_utility(bids[i], other, asks, i)
for j in range(ns):
u_true = seller_utility(asks[j], bids, asks, j)
assert u_true >= 0
for lie in grid:
other = asks[:j] + [lie] + asks[j + 1:]
assert u_true >= seller_utility(asks[j], bids, other, j)
INF = float("inf")
def buyer_threshold(others, asks):
"""The critical bid of a new buyer, by the case analysis of the proof."""
c = [INF] + sorted(others, reverse=True)
s = [None] + sorted(asks)
def cc(t):
return c[t] if t < len(c) else None
def ss(t):
return s[t] if t < len(s) else None
h = 0
while cc(h + 1) is not None and ss(h + 1) is not None and c[h + 1] >= s[h + 1]:
h += 1
if ss(1) is None:
return INF
if ss(h + 1) is not None and c[h] >= s[h + 1]:
if cc(h + 1) is not None and ss(h + 2) is not None:
p = Fraction(c[h + 1] + s[h + 2], 2)
if s[h + 1] <= p <= c[h]:
return p
return c[h]
if ss(h + 1) is not None:
p = Fraction(c[h] + s[h + 1], 2)
if c[h - 1] >= p:
return p
return c[h - 1]
def test_buyer_threshold() -> None:
rng = random.Random(3)
grid = [Fraction(t, 2) for t in range(0, 17)]
for _ in range(400):
others = [rng.randrange(0, 8) for _ in range(rng.randrange(0, 4))]
asks = [rng.randrange(0, 8) for _ in range(rng.randrange(0, 4))]
theta = buyer_threshold(others, asks)
for b in grid:
bids = others + [b]
buyers, _, price, _ = mcafee_trade_reduction(bids, asks)
i = len(others)
if b > theta:
assert i in buyers and price == theta
elif b < theta:
assert i not in buyers
elif i in buyers:
assert price == theta
def test_truthful_with_half_steps() -> None:
# The price p0 can be a half integer, so test lies on a finer grid too.
rng = random.Random(2)
grid = [Fraction(t, 2) for t in range(0, 13)]
for _ in range(300):
bids = [rng.choice(grid) for _ in range(3)]
asks = [rng.choice(grid) for _ in range(3)]
for i in range(3):
u_true = buyer_utility(bids[i], bids, asks, i)
for lie in grid:
assert u_true >= buyer_utility(bids[i], bids[:i] + [lie] + bids[i + 1:], asks, i)
for j in range(3):
u_true = seller_utility(asks[j], bids, asks, j)
for lie in grid:
assert u_true >= seller_utility(asks[j], bids, asks[:j] + [lie] + asks[j + 1:], j)
History
Myerson and Satterthwaite (1983) proved that even with one buyer and one seller, no mechanism is efficient, Bayesian incentive compatible, individually rational and budget balanced when the supports of the values overlap. McAfee (1992) showed that in a large market this obstacle costs little. He gave a double auction that is dominant-strategy truthful for both sides, individually rational and weakly budget balanced, and that loses at most the least valuable efficient trade.
References
- R. Preston McAfee, A dominant strategy double auction, Journal of Economic Theory 56(2) (1992), 434-450, the mechanism with the price (b(k+1) + s(k+1))/2 and the reduction to k - 1 trades, the proof that truthful reporting is dominant, and the bound on the lost gains from trade
- Roger B. Myerson and Mark A. Satterthwaite, Efficient mechanisms for bilateral trading, Journal of Economic Theory 29(2) (1983), 265-281, the impossibility of an efficient, incentive compatible, individually rational and budget balanced mechanism for bilateral trade
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 | W2 basic |
|
Tag AMA27A Edit this page History Report an error Markdown