Algorithm Search⌘K

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
Properties
comparison baseddeterministiciterative
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 kk trades that an efficient market would make, and then either lets all kk pairs trade at one price taken from the first pair that does not trade, or, when that price does not fit, drops the kk-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 1−1/k1 - 1/k 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 v1,v2,…v_1, v_2, \ldots for one unit, and sellers with costs c1,c2,…c_1, c_2, \ldots for their unit, nn traders in all. Every trader has quasilinear utility: a buyer that trades at price pp gets v−pv - p, a seller that trades at price pp gets p−cp - c, 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 v−cv - c over the trading pairs, are close to the maximum.

The algorithm

  1. MCAFEE(b_1, ..., b_B, s_1, ..., s_S)
  2. 1rank buyers b_(1) ≥ b_(2) ≥ ... and sellers s_(1) ≤ s_(2) ≤ ...; ties go to the smaller index
  3. 2k ← the largest t with b_(t) ≥ s_(t), or 0 if there is none
  4. 3if k = 0: return no trade
  5. 4if buyer k + 1 and seller k + 1 exist: p0 ← (b_(k+1) + s_(k+1)) / 2
  6. 5if s_(k) ≤ p0 ≤ b_(k):
  7. 6return buyers and sellers of ranks 1..k, all trading at the price p0
  8. 7return 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 b(t)≥s(t)b_{(t)} \ge s_{(t)} are exactly the first kk. In line L7 the auctioneer keeps (k−1)(b(k)−s(k))≥0(k - 1)(b_{(k)} - s_{(k)}) \ge 0.

Example

All efficient trades happen. Buyers bid 10,8,210, 8, 2 and sellers ask 1,3,91, 3, 9. The pairs are (10,1)(10, 1), (8,3)(8, 3) and (2,9)(2, 9), so k=2k = 2. The price is p0=(2+9)/2=5.5p_0 = (2 + 9)/2 = 5.5, which lies in [s(2),b(2)]=[3,8][s_{(2)}, b_{(2)}] = [3, 8]. Both pairs trade at 5.5. The buyers get 4.54.5 and 2.52.5, and the sellers get 4.54.5 and 2.52.5. The gains from trade are 9+5=149 + 5 = 14, the maximum.

One trade is dropped. Buyers bid 10,5,410, 5, 4 and sellers ask 1,3,91, 3, 9. Again k=2k = 2, but p0=(4+9)/2=6.5p_0 = (4 + 9)/2 = 6.5 is above b(2)=5b_{(2)} = 5. 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 9+2=119 + 2 = 11. 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 ∑t≤k(b(t)−s(t))\sum_{t \le k} (b_{(t)} - s_{(t)}), made by the first kk pairs.
  • Budget balance and the loss. The buyer price is at least the seller price. Line L6 makes all kk trades, and line L7 drops only the pair with the smallest gain, so the gains are at least (1−1/k)(1 - 1/k) of the maximum.
  • A threshold for each trader. Fix the reports of everyone except one buyer. There is a number θ\theta, which depends only on the others, such that the buyer trades when its bid is above θ\theta, does not trade when its bid is below θ\theta, and pays exactly θ\theta when it trades. The proof finds θ\theta by a case analysis on the number hh 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 b1≥b2≥⋯b_1 \ge b_2 \ge \cdots and the sellers as s1≤s2≤⋯s_1 \le s_2 \le \cdots, with ties broken by index. Line L2 sets kk to the largest tt with bt≥stb_t \ge s_t. Because the btb_t do not increase and the sts_t do not decrease, the set {t:bt≥st}\{t : b_t \ge s_t\} is a prefix {1,…,k}\{1, \ldots, k\} of the ranks. We write p0=(bk+1+sk+1)/2p_0 = (b_{k+1} + s_{k+1})/2 when both traders of rank k+1k + 1 exist.

Claim 1 (the efficient trades). With truthful reports, the largest gain from trade over all sets of trading pairs is OPT=∑t=1k(bt−st)\mathrm{OPT} = \sum_{t=1}^{k} (b_t - s_t).

Proof. A set of qq trading pairs uses qq buyers and qq sellers. Its gain is at most the sum of the qq largest bids minus the sum of the qq smallest asks, which is g(q)=∑t=1q(bt−st)g(q) = \sum_{t=1}^{q} (b_t - s_t). The terms bt−stb_t - s_t do not increase in tt, they are at least 0 for t≤kt \le k and below 0 for t>kt > k. So gg is largest at q=kq = k. □\square

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 OPT−(bk−sk)≥(1−1/k) OPT\mathrm{OPT} - (b_k - s_k) \ge (1 - 1/k)\,\mathrm{OPT} when k≥1k \ge 1.

Proof. If k=0k = 0, line L3 returns no trade. Line L6 returns the top kk of each side at the price p0p_0, and it runs only if sk≤p0≤bks_k \le p_0 \le b_k. So each trading buyer has bt≥bk≥p0b_t \ge b_k \ge p_0 and each trading seller has st≤sk≤p0s_t \le s_k \le p_0. Line L7 returns the top k−1k - 1 of each side. Each buyer pays bk≤btb_k \le b_t and each seller receives sk≥sts_k \ge s_t, and bk≥skb_k \ge s_k by the definition of kk, so the auctioneer keeps (k−1)(bk−sk)≥0(k - 1)(b_k - s_k) \ge 0. Line L6 trades all kk pairs of Claim 1. Line L7 trades all of them but the pair of rank kk, whose gain bk−skb_k - s_k is the smallest of the kk nonnegative terms. So it is at most OPT/k\mathrm{OPT}/k. □\square

Claim 3 (a threshold for each buyer). Fix a buyer ii and the reports of all other traders. There is a number θ\theta that depends only on these reports, such that buyer ii trades if its bid b>θb > \theta, does not trade if b<θb < \theta, and pays exactly θ\theta whenever it trades.

Proof. Let c1≥c2≥⋯c_1 \ge c_2 \ge \cdots be the bids of the other buyers, ranked as in line L1, and put c0=+∞c_0 = +\infty. Let s1≤s2≤⋯s_1 \le s_2 \le \cdots be the asks. Let hh be the largest tt with ct≥stc_t \ge s_t, the number of efficient trades without ii. If there is no seller, nobody trades, and θ=+∞\theta = +\infty works. So assume s1s_1 exists.

Let b1′≥b2′≥⋯b'_1 \ge b'_2 \ge \cdots be the ranked bids with bb added. The value bt′b'_t is the tt-th largest of a list that contains the list cc and is one longer, so ct≤bt′≤ct−1c_t \le b'_t \le c_{t-1}. Then bh′≥ch≥shb'_h \ge c_h \ge s_h and bh+2′≤ch+1<sh+2b'_{h+2} \le c_{h+1} < s_{h+2}, hence k∈{h,h+1}k \in \{h, h + 1\}. If buyer ii has a rank rr, the values below it shift down: bt′=ct−1b'_t = c_{t-1} for t>rt > r. If buyer ii trades, it has rank r≤kr \le k in line L6 and r≤k−1r \le k - 1 in line L7.

Case I: sh+1s_{h+1} exists and ch≥sh+1c_h \ge s_{h+1}. Here bh+1′≥min⁡(b,ch)b'_{h+1} \ge \min(b, c_h), so k=h+1k = h + 1 when b≥sh+1b \ge s_{h+1}. If b<sh+1b < s_{h+1}, then b<chb < c_h and bh+1′≤max⁡(ch+1,b)<sh+1b'_{h+1} \le \max(c_{h+1}, b) < s_{h+1}, so k=hk = h and buyer ii has a rank above hh; it does not trade. Now let b≥sh+1b \ge s_{h+1}, so k=h+1k = h + 1. Since b≥sh+1>ch+1b \ge s_{h+1} > c_{h+1}, buyer ii has a rank r≤h+1r \le h + 1, and bh+2′=ch+1b'_{h+2} = c_{h+1}. So p0=q:=(ch+1+sh+2)/2p_0 = q := (c_{h+1} + s_{h+2})/2 does not depend on bb, and bh+1′=min⁡(b,ch)b'_{h+1} = \min(b, c_h).

  • (a) If qq exists and sh+1≤q≤chs_{h+1} \le q \le c_h, set θ=q\theta = q. For b≥qb \ge q, line L5 holds and buyer ii trades at qq in line L6. For sh+1≤b<qs_{h+1} \le b < q, we get bh+1′=b<qb'_{h+1} = b < q, so line L7 runs, and buyer ii has rank h+1=kh + 1 = k and does not trade.
  • (b) Otherwise set θ=ch\theta = c_h. Line L5 fails for every b≥sh+1b \ge s_{h+1}: either qq does not exist, or q<sh+1q < s_{h+1}, or q>ch≥bh+1′q > c_h \ge b'_{h+1}. So line L7 runs with k=h+1k = h + 1. Buyer ii trades if and only if its rank is at most hh, which holds for b>chb > c_h and fails for b<chb < c_h. Then bh+1′=chb'_{h+1} = c_h, so it pays chc_h.

Case II: sh+1s_{h+1} does not exist, or ch<sh+1c_h < s_{h+1}. Then h≥1h \ge 1, because c0=+∞≥s1c_0 = +\infty \ge s_1. We have k=hk = h for every bb: if sh+1s_{h+1} does not exist there are only hh sellers, and otherwise bh+1′=max⁡(ch+1,min⁡(b,ch))<sh+1b'_{h+1} = \max(c_{h+1}, \min(b, c_h)) < s_{h+1}. If buyer ii has rank at most hh, then bh+1′=chb'_{h+1} = c_h, so p0=q:=(ch+sh+1)/2p_0 = q := (c_h + s_{h+1})/2, which does not depend on bb. Note q>ch≥shq > c_h \ge s_h.

  • (a) If sh+1s_{h+1} exists and ch−1≥qc_{h-1} \ge q, set θ=q\theta = q. For b≥qb \ge q, buyer ii ranks above ch<qc_h < q, so its rank is at most hh, and bh′=min⁡(b,ch−1)≥q≥shb'_h = \min(b, c_{h-1}) \ge q \ge s_h. Line L5 holds and ii trades at qq. For b<qb < q, either buyer ii has a rank above hh, or it has rank hh with bh′=b<qb'_h = b < q, so line L7 runs and the trader of rank hh does not trade. In both cases ii does not trade.
  • (b) Otherwise set θ=ch−1\theta = c_{h-1}. If sh+1s_{h+1} does not exist, line L7 runs. If it exists, bh′≤ch−1<qb'_h \le c_{h-1} < q when ii has rank at most hh, so again line L7 runs. Buyer ii trades if and only if its rank is at most h−1h - 1, which holds for b>ch−1b > c_{h-1} and fails for b<ch−1b < c_{h-1}, and then it pays bh′=ch−1b'_h = c_{h-1}.

In every case the price paid by buyer ii is θ\theta, and the outcome at b=θb = \theta only depends on the tie rule. □\square

Claim 4 (truthful reporting is a dominant strategy, and it is individually rational). For every buyer ii with value vv, every bid bb and all reports of the others, the utility of bidding vv is at least the utility of bidding bb and at least 0. The same holds for every seller.

Proof. Take θ\theta from Claim 3. If v>θv > \theta, the bid vv trades and gives v−θ>0v - \theta > 0, and every other bid gives v−θv - \theta or 0. If v<θv < \theta, the bid vv gives 0, and any bid that trades gives v−θ<0v - \theta < 0. If v=θv = \theta, every bid gives 0. For sellers, replace each bid bb by −b-b and each ask ss by −s-s and exchange the two sides. This maps the ranking, the index kk, the price p0p_0, 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. □\square

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 kk efficient trades.

Complexity

Ranking the two sides takes O(nlog⁡n)O(n \log n) time and O(n)O(n) space, and lines L2 to L7 take O(n)O(n) time. With bids that arrive already sorted, the whole mechanism takes O(n)O(n) 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 b(k)−s(k)b_{(k)} - s_{(k)}, which is at least (1−1/k)(1 - 1/k) 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 kk efficient pairs trade; each buyer pays max⁡(s(k),b(k+1))\max(s_{(k)}, b_{(k+1)}) and each seller receives min⁡(b(k),s(k+1))\min(b_{(k)}, s_{(k+1)}). 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 k=1k = 1 and no pair at rank 2, line L7 drops the only trade. The guarantee 1−1/k1 - 1/k is 0 for k=1k = 1.
  • Missing traders at rank k+1k + 1. If there is no buyer or no seller of rank k+1k + 1, the price p0p_0 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 p0p_0 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.

mcafee_trade_reduction.pyI0RawGitHub
# 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

  1. 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
  2. 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.

LadderLevelWhat the next level needs
Human proofH1 sketch
  • a full proof of time.worst
  • a full proof of space.extra
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • at least three sources
  • a scene preset for the example