Algorithm Search⌘K

Algorithmcore

Generalized second-price auction

Also called GSP auction.

Ranks ad bids, gives the s-th highest bidder slot s and charges it the next bid per click. Not truthful, but it has an envy-free equilibrium with the VCG outcome.

Time
O(n log n) worst
Space
O(n) extra
Where
k = the number of slots; n = the number of bidders
Properties
comparison baseddeterministiciterative
Origin
Benjamin Edelman, Michael Ostrovsky, Michael Schwarz, Hal Varian, 2002

The generalized second-price auction (GSP) sells the ad slots next to the results of a web search. The slots are ranked, and a higher slot gets more clicks. Each advertiser bids one price per click. GSP sorts the bids, gives the highest bidder the top slot, the second highest the second slot, and so on, and each winner pays, for each click, the bid of the bidder just below it. With one slot this is the Vickrey auction. With two or more slots it is not truthful: a bidder can gain by bidding less and taking a lower slot at a lower price. It is still the standard format of search advertising, because it has a locally envy-free equilibrium with the same slots and payments as the VCG mechanism.

In plain words

Advertisers bid for places on a page of search results, and a place near the top gets more clicks. The page sorts the advertisers by bid. The best bid gets the top place, the next bid the next place, and so on. Each advertiser pays for each click the bid of the advertiser right below it, not its own bid. This looks like the sealed second-price auction, where the honest bid is always best, but it is different. Here an advertiser can bid less, drop one place, lose a few clicks, and pay much less for every click it keeps. So advertisers do not simply bid their values.

The problem

There are kk slots and nn bidders. Slot ss gets αs\alpha_s clicks, with α1≥α2≥⋯≥αk≥0\alpha_1 \ge \alpha_2 \ge \cdots \ge \alpha_k \ge 0, the same for every bidder. Bidder ii has a private value viv_i per click and quasilinear utility: in slot ss at the price π\pi per click it gets αs(vi−π)\alpha_s (v_i - \pi). This is the position auction model. Each bidder reports a bid bib_i per click. The mechanism gives each slot to at most one bidder and sets a price per click for each winner. A reserve price r≥0r \ge 0 is optional: a bid below rr takes no part.

The algorithm

  1. GSP(b_1, ..., b_n, α_1, ..., α_k, r)
  2. 1R ← the bidders with b_i ≥ r, sorted by bid, highest first; ties go to the smaller index
  3. 2q ← min(k, |R|)
  4. 3for s ← 1 to q
  5. 4give slot s to R_s
  6. 5if R_{s+1} exists: π_s ← b of R_{s+1}
  7. 6else: π_s ← r
  8. 7return the slots and the prices per click π_1, ..., π_q

Bidder RsR_s pays αsπs\alpha_s \pi_s in total. Slots after qq stay empty.

Example

This is the example of Edelman, Ostrovsky and Schwarz (2007). Three bidders have values 10,4,210, 4, 2 per click, and two slots get α1=200\alpha_1 = 200 and α2=199\alpha_2 = 199 clicks. There is no reserve.

bids slot of bidder 1 its price per click its utility
10,4,210, 4, 2 (truthful) 1 4 200⋅(10−4)=1200200 \cdot (10 - 4) = 1200
3,4,23, 4, 2 2 2 199⋅(10−2)=1592199 \cdot (10 - 2) = 1592

Bidder 1 gains by bidding 3: it loses one click and saves 2 on each of 199 clicks. So truthful bidding is not a dominant strategy.

The VCG payments are 398398 for slot 2 (the value 2 of the third bidder times the 199 clicks it would gain) and 402402 for slot 1, in total 800. With the bids 10,2.01,210, 2.01, 2 in GSP, bidder 1 pays 200⋅2.01=402200 \cdot 2.01 = 402 and bidder 2 pays 199⋅2=398199 \cdot 2 = 398. These bids form an equilibrium with the VCG outcome, and the page on the locally envy-free equilibrium explains how to find them.

Correctness

The proof is in proofs/correctness.md. It shows these facts.

  • The output. Slot ss goes to the ss-th eligible bidder in sorted order and costs the next eligible bid per click, or rr if there is none. No bidder gets two slots.
  • Individual rationality. The price per click is at most the bidder's own bid and at least rr, so a bidder that bids its value never gets a negative utility.
  • One slot is Vickrey. With k=1k = 1 the winner pays the larger of rr and the highest other bid.
  • Not truthful for two or more slots, by the example above.
  • GSP payments are at least VCG payments for the same bids. The VCG payment of slot ss is ∑t≥s(αt−αt+1) b(t+1)\sum_{t \ge s} (\alpha_t - \alpha_{t+1})\, b_{(t+1)}, and each bid in the sum is at most b(s+1)b_{(s+1)}, so the sum is at most αsb(s+1)\alpha_s b_{(s+1)}.
Proof: The generalized second-price auction ranks by bid, charges the next bid, is individually rational, and is not truthfulfull

We use the pseudocode of the page: nn bidders, kk slots with click-through rates α1≥α2≥⋯≥αk≥0\alpha_1 \ge \alpha_2 \ge \cdots \ge \alpha_k \ge 0, and a reserve r≥0r \ge 0. A bidder in slot ss with value vv per click and price πs\pi_s per click gets the utility αs(v−πs)\alpha_s (v - \pi_s), and a bidder with no slot gets 0. Let R1,R2,…R_1, R_2, \ldots be the eligible bidders (bid at least rr) in the order of line L1, and write b(s)b_{(s)} for the bid of RsR_s.

Claim 1 (the output). For s=1,…,q=min⁡(k,∣R∣)s = 1, \ldots, q = \min(k, |R|), slot ss goes to RsR_s and its price per click is πs=b(s+1)\pi_s = b_{(s+1)} if s<∣R∣s < |R|, and πs=r\pi_s = r otherwise. No bidder gets two slots, and every slot after qq stays empty.

Proof. Line L1 lists each eligible bidder once, in a fixed order. Lines L3 and L4 give slot ss to the ss-th bidder of the list, so the slots go to distinct bidders. Lines L5 and L6 set the price as stated. □\square

Claim 2 (no bidder pays more than its bid). For s≤qs \le q, r≤πs≤b(s)r \le \pi_s \le b_{(s)}. So a bidder that bids its value never gets a negative utility, and a bidder that bids at least rr but gets no slot pays nothing.

Proof. The list is sorted, so b(s+1)≤b(s)b_{(s+1)} \le b_{(s)}, and b(s+1)≥rb_{(s+1)} \ge r because Rs+1R_{s+1} is eligible. If s=∣R∣s = |R|, the price is r≤b(s)r \le b_{(s)}. A bidder that bids vv and gets slot ss gets αs(v−πs)≥0\alpha_s (v - \pi_s) \ge 0. □\square

Claim 3 (one slot is the Vickrey auction). If k=1k = 1, the highest eligible bid wins and pays the larger of rr and the second-highest eligible bid. A bid below rr cannot win, so this is the larger of rr and the highest other bid, which is the price of the Vickrey auction with reserve rr, with the same tie rule. □\square

Claim 4 (truthful bidding is not dominant when k≥2k \ge 2). Take the example of Edelman, Ostrovsky and Schwarz (2007, Section II): values 10,4,210, 4, 2, two slots with α1=200\alpha_1 = 200 and α2=199\alpha_2 = 199 clicks, and r=0r = 0. With truthful bids, bidder 1 gets slot 1 at the price 4, so its utility is 200⋅(10−4)=1200200 \cdot (10 - 4) = 1200. If it bids 3, it is ranked second and pays the next bid 2, so its utility is 199⋅(10−2)=1592>1200199 \cdot (10 - 2) = 1592 > 1200. □\square

The general reason: the price of slot ss is the bid of the next bidder, and the price of slot s+1s + 1 is the bid after that. A bidder trades αs−αs+1\alpha_s - \alpha_{s+1} clicks against a lower price per click on all its clicks, and when the click-through rates are close, the lower price wins.

Claim 5 (for the same bids, GSP charges at least the VCG payments). Let r=0r = 0, let all bids be at least 0, and put αt=0\alpha_{t} = 0 for t>kt > k and b(t)=0b_{(t)} = 0 when RtR_t does not exist. The VCG payment of the bidder in slot ss, computed from the bids, is Ps=∑t=sk(αt−αt+1) b(t+1)P_s = \sum_{t=s}^{k} (\alpha_t - \alpha_{t+1})\, b_{(t+1)}. The GSP payment of the same bidder is αsb(s+1)≥Ps\alpha_s b_{(s+1)} \ge P_s.

Proof. Each factor αt−αt+1\alpha_t - \alpha_{t+1} is at least 0, and b(t+1)≤b(s+1)b_{(t+1)} \le b_{(s+1)} for t≥st \ge s. So Ps≤b(s+1)∑t=sk(αt−αt+1)=b(s+1)αsP_s \le b_{(s+1)} \sum_{t=s}^{k} (\alpha_t - \alpha_{t+1}) = b_{(s+1)} \alpha_s, because the sum telescopes and αk+1=0\alpha_{k+1} = 0. □\square

This is Remark 1 of Edelman, Ostrovsky and Schwarz (2007). It does not say that GSP earns more than VCG in equilibrium, because the bids in GSP are not truthful. The equilibrium with the VCG outcome is proved on the page Locally envy-free equilibrium of GSP.

Complexity

Sorting the bids takes O(nlog⁡n)O(n \log n) time and O(n)O(n) space, and lines L2 to L7 take O(k)O(k) time. Only the k+1k + 1 highest bids matter, so a selection of them and a sort of those k+1k + 1 bids give O(n+klog⁡k)O(n + k \log k) time.

Properties

  • Not DSIC when k≥2k \ge 2 and α1>α2>0\alpha_1 > \alpha_2 > 0, whatever the rates; the gain from bidding for a lower slot is largest when the click-through rates differ by little.
  • Ex post individually rational for truthful bids, and every winner pays at most its bid.
  • Efficient equilibrium. Every envy-free equilibrium in the sense of Varian gives the slots in the order of the values and pays at least the VCG payments; the bidder-optimal one has exactly the VCG outcome (Edelman, Ostrovsky and Schwarz 2007; Varian 2007).
  • Same bids, more money. For the same bids GSP charges each bidder at least the VCG payment. In the equilibrium with the VCG outcome the revenue is the same as VCG, because the bids are lower.
  • Simple prices. The price of a slot depends only on the next bid, which is easy to explain to advertisers.

Variants and improvements

  • Weighted GSP. Real systems rank by bid times a quality score, an estimate of the click-through rate of the ad, and charge the smallest bid that keeps the slot: πs=bs+1ws+1/ws\pi_s = b_{s+1} w_{s+1} / w_s for the weights ww. The model and the equilibrium results carry over with values scaled by the weights.
  • Reserve prices. A reserve price, the same for all bidders or one per bidder, raises revenue in the same way as in Myerson's optimal auction.
  • VCG for slots. The VCG mechanism charges slot ss the sum ∑t≥s(αt−αt+1)b(t+1)\sum_{t \ge s} (\alpha_t - \alpha_{t+1}) b_{(t+1)}. It is truthful and gives the same outcome as the bidder-optimal envy-free equilibrium of GSP. Some ad platforms use it.
  • Generalized first-price auction. The early format of Overture charged each winner its own bid. Bids then changed all the time in cycles, which led to GSP.
  • Budgets. With daily budgets the bidders, or the platform for them, scale the bids down; see pacing equilibrium.

When to use it

Use GSP to sell ranked slots with click-through rates that fall from the top, when the bidders are sophisticated and bid repeatedly, so that they can find an equilibrium. Use VCG when you want truthful bidding to be a dominant strategy, and use a Vickrey auction when there is one slot, where the two agree.

Pitfalls

  • "Second price" does not mean truthful. With two or more slots, the truth is not a dominant strategy, and an analysis that assumes it is wrong.
  • Many equilibria. GSP has many Nash equilibria, some inefficient. The envy-free refinement picks a set with the VCG outcome or more revenue, but the refinement is an assumption about how bidders behave.
  • Ties. Line L1 breaks ties by index. Bidders with equal bids pay the same price per click but get different numbers of clicks.
  • Separable click-through rates. The model assumes that the clicks of a slot do not depend on the ad. With quality scores, rank by weighted bid, not by bid.

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.

generalized_second_price_auction.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/generalized-second-price-auction/. The authors are listed in prov/ATPJNQ.jsonl.

from fractions import Fraction
from typing import Sequence, Union

Number = Union[int, Fraction]


def gsp(
    bids: Sequence[Number], ctrs: Sequence[Number], reserve: Number = 0
) -> tuple[list[int], list[Number]]:
    """Run the generalized second-price auction for ranked ad slots.

    ctrs[s] is the click-through rate of slot s, with ctrs nonincreasing.
    Only bids of at least the reserve take part. They are ranked by bid,
    highest first, with ties to the smaller index, and the s-th ranked
    bidder gets slot s. It pays per click the next bid in the ranking, or the
    reserve if there is no next bid. Return (winners, prices): winners[s] is
    the 0-based bidder index in slot s, and prices[s] is its price per click.
    """
    ranked = sorted((i for i in range(len(bids)) if bids[i] >= reserve), key=lambda i: (-bids[i], i))  # @line L1
    filled = min(len(ctrs), len(ranked))  # @line L2
    winners: list[int] = []
    prices: list[Number] = []
    for s in range(filled):  # @line L3
        winners.append(ranked[s])  # @line L4
        if s + 1 < len(ranked):  # @line L5
            prices.append(bids[ranked[s + 1]])  # @line L5
        else:  # @line L6
            prices.append(reserve)  # @line L6
    return winners, prices  # @line L7


def utility(value: Number, bids: Sequence[Number], ctrs: Sequence[Number], i: int, reserve: Number = 0) -> Number:
    """Utility of bidder i per unit of time: ctr of its slot times value minus price."""
    winners, prices = gsp(bids, ctrs, reserve)
    for s, w in enumerate(winners):
        if w == i:
            return ctrs[s] * (value - prices[s])
    return 0


def revenue(bids: Sequence[Number], ctrs: Sequence[Number], reserve: Number = 0) -> Number:
    """Total payment of all bidders per unit of time."""
    winners, prices = gsp(bids, ctrs, reserve)
    return sum(ctrs[s] * prices[s] for s in range(len(winners)))


def vcg_payments(values: Sequence[Number], ctrs: Sequence[Number]) -> tuple[list[int], list[Number]]:
    """VCG outcome of the position auction with reported values.

    Rank by value as in gsp. The bidder in slot s pays the externality
    sum over t >= s of (ctrs[t] - ctrs[t + 1]) * v(t + 1), where v(t + 1)
    is the value ranked t + 1 (0 if it does not exist) and ctrs past the
    last slot are 0. Return (winners, total payments per slot).
    """
    ranked = sorted(range(len(values)), key=lambda i: (-values[i], i))
    k = min(len(ctrs), len(ranked))
    alpha = list(ctrs[:k]) + [0]
    v = [values[i] for i in ranked] + [0]
    pay = [sum((alpha[t] - alpha[t + 1]) * v[t + 1] for t in range(s, k)) for s in range(k)]
    return ranked[:k], pay


def eos_bids(values: Sequence[Number], ctrs: Sequence[Number]) -> list[Number]:
    """The bidder-optimal locally envy-free equilibrium of Edelman, Ostrovsky and Schwarz.

    The bidder ranked j >= 2 bids the VCG payment of slot j - 1 divided by
    ctrs[j - 1], the bidder ranked first bids its value, and bidders ranked
    below the first loser bid their values. Click-through rates must be
    positive.
    """
    ranked = sorted(range(len(values)), key=lambda i: (-values[i], i))
    _, pay = vcg_payments(values, ctrs)
    bids: list[Number] = [0] * len(values)
    for j, i in enumerate(ranked):
        if j == 0 or j > len(pay):
            bids[i] = values[i]
        else:
            bids[i] = Fraction(pay[j - 1]) / ctrs[j - 1]
    return bids
test_generalized_second_price_auction.py 156 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/generalized-second-price-auction/. The authors are listed in prov/ATPJNQ.jsonl.

import itertools
import random
from fractions import Fraction

from generalized_second_price_auction import eos_bids, gsp, revenue, utility, vcg_payments

DELTA = Fraction(1, 1000)


def is_nash(values, bids, ctrs):
    """Every deviation of every bidder is tried: each slot can be reached by
    a bid just above, equal to or just below some other bid, or by 0."""
    for i, v in enumerate(values):
        current = utility(v, bids, ctrs, i)
        others = [bids[j] for j in range(len(bids)) if j != i]
        candidates = {0} | {b + d for b in others for d in (-DELTA, 0, DELTA) if b + d >= 0}
        for c in candidates:
            if utility(v, bids[:i] + [c] + bids[i + 1:], ctrs, i) > current:
                return False
    return True


def slot_offers(bids, ctrs):
    """Price per click and click-through rate of every slot, plus no slot."""
    winners, prices = gsp(bids, ctrs)
    return winners, [(ctrs[s], prices[s]) for s in range(len(winners))] + [(0, 0)]


def is_envy_free(values, bids, ctrs):
    """Varian's symmetric Nash equilibrium: nobody prefers another slot at its price."""
    winners, offers = slot_offers(bids, ctrs)
    for i, v in enumerate(values):
        current = utility(v, bids, ctrs, i)
        if any(a * (v - p) > current for a, p in offers):
            return False
    return True


def is_locally_envy_free(values, bids, ctrs):
    """Edelman, Ostrovsky and Schwarz: the bidder at rank r does not envy rank r - 1,
    for 2 <= r <= min(slots + 1, bidders)."""
    ranked = sorted(range(len(bids)), key=lambda i: (-bids[i], i))
    alpha = list(ctrs) + [0]
    for r in range(1, min(len(ctrs) + 1, len(bids))):
        i = ranked[r]
        nxt = bids[ranked[r + 1]] if r + 1 < len(ranked) else 0
        if alpha[r] * (values[i] - nxt) < alpha[r - 1] * (values[i] - bids[i]):
            return False
    return True


def test_example() -> None:
    # Edelman, Ostrovsky and Schwarz, Section II: values 10, 4, 2 and 200, 199 clicks.
    values, ctrs = [10, 4, 2], [200, 199]
    assert gsp(values, ctrs) == ([0, 1], [4, 2])
    assert utility(10, values, ctrs, 0) == 1200
    assert utility(10, [3, 4, 2], ctrs, 0) == 1592
    # The VCG payments and the equilibrium bids b* of Theorem 1.
    assert vcg_payments(values, ctrs) == ([0, 1], [402, 398])
    assert eos_bids(values, ctrs) == [10, Fraction(402, 200), 2]
    b = eos_bids(values, ctrs)
    assert revenue(b, ctrs) == 800 == sum(vcg_payments(values, ctrs)[1])


def test_edges() -> None:
    assert gsp([], [3, 2]) == ([], [])
    assert gsp([5], [3, 2]) == ([0], [0])
    assert gsp([5], [3, 2], reserve=2) == ([0], [2])
    assert gsp([1, 5], [3, 2], reserve=2) == ([1], [2])
    assert gsp([5, 5, 5], [3, 2]) == ([0, 1], [5, 5])
    # One slot: GSP is the Vickrey auction.
    assert gsp([3, 9, 7], [1]) == ([1], [7])


def test_truthful_revenue_at_least_vcg() -> None:
    rng = random.Random(1)
    for _ in range(500):
        n, k = rng.randrange(1, 6), rng.randrange(1, 4)
        values = [rng.randrange(0, 20) for _ in range(n)]
        ctrs = sorted((rng.randrange(1, 10) for _ in range(k)), reverse=True)
        winners, pay = vcg_payments(values, ctrs)
        assert gsp(values, ctrs)[0] == winners
        assert revenue(values, ctrs) >= sum(pay)


def random_instance(rng):
    n, k = rng.randrange(2, 5), rng.randrange(1, 4)
    values = rng.sample(range(1, 30), n)
    ctrs = sorted(rng.sample(range(1, 20), k), reverse=True)
    return values, ctrs


def test_eos_bids_are_envy_free_equilibrium_with_vcg_outcome() -> None:
    rng = random.Random(2)
    for _ in range(300):
        values, ctrs = random_instance(rng)
        b = eos_bids(values, ctrs)
        assert is_nash(values, b, ctrs)
        assert is_envy_free(values, b, ctrs)
        assert is_locally_envy_free(values, b, ctrs)
        winners, prices = gsp(b, ctrs)
        vcg_winners, vcg_pay = vcg_payments(values, ctrs)
        assert winners == vcg_winners
        assert [ctrs[s] * prices[s] for s in range(len(winners))] == vcg_pay


def test_envy_free_equilibria_on_a_grid() -> None:
    # Every envy-free bid profile (Varian's symmetric Nash equilibrium) is
    # efficient and pays for each slot at least its VCG payment.
    rng = random.Random(3)
    grid = range(0, 8)
    count = 0
    for slots in (2, 2, 2, 1, 1, 1):
        values = rng.sample(range(1, 8), 3)
        ctrs = sorted(rng.sample(range(1, 6), slots), reverse=True)
        vcg_winners, vcg_pay = vcg_payments(values, ctrs)
        for bids in itertools.product(grid, repeat=3):
            bids = list(bids)
            if not is_envy_free(values, bids, ctrs):
                continue
            count += 1
            winners, prices = gsp(bids, ctrs)
            assert winners == vcg_winners
            assert all(ctrs[s] * prices[s] >= vcg_pay[s] for s in range(len(winners)))
    assert count > 0


def test_locally_envy_free_can_be_below_vcg() -> None:
    # One slot, values 5, 1, 8 and bids 10, 2, 0: a locally envy-free Nash
    # equilibrium in which the bidder with value 8 loses and revenue is 2 < 5.
    values, bids, ctrs = [5, 1, 8], [10, 2, 0], [1]
    assert is_nash(values, bids, ctrs)
    assert is_locally_envy_free(values, bids, ctrs)
    assert not is_envy_free(values, bids, ctrs)
    assert gsp(bids, ctrs) == ([0], [2])
    assert vcg_payments(values, ctrs) == ([2], [5])


def test_truthful_bidding_is_not_dominant() -> None:
    rng = random.Random(4)
    found = 0
    for _ in range(200):
        values, ctrs = random_instance(rng)
        if len(ctrs) < 2:
            continue
        for i, v in enumerate(values):
            truthful = utility(v, values, ctrs, i)
            others = [values[j] for j in range(len(values)) if j != i]
            for c in {o + d for o in others for d in (-DELTA, DELTA) if o + d >= 0}:
                if utility(v, values[:i] + [c] + values[i + 1:], ctrs, i) > truthful:
                    found += 1
                    break
    assert found > 0

History

Overture (formerly GoTo.com) sold search ads from 1997 in a pay-per-click auction where each winner paid its own bid. Bids changed quickly in cycles, and in 2002 Google introduced the rule that each winner pays the next bid, and Overture then adopted it. Edelman, Ostrovsky and Schwarz (2007) named the format the generalized second-price auction, showed that it is not truthful, and found its locally envy-free equilibria; Varian (2007) studied the same equilibria as symmetric Nash equilibria and tested them on data.

References

  1. Benjamin Edelman, Michael Ostrovsky and Michael Schwarz, Internet advertising and the generalized second-price auction: selling billions of dollars worth of keywords, American Economic Review 97(1) (2007), 242-259, Section II (the rules of GSP and the example in which truthful bidding is not dominant) and Remark 1 (for the same bids, GSP payments are at least the VCG payments)
  2. Hal R. Varian, Position auctions, International Journal of Industrial Organization 25(6) (2007), 1163-1178, the model of position auctions and the symmetric Nash equilibria of the next-bid price rule
  3. Tim Roughgarden, Twenty Lectures on Algorithmic Game Theory, Cambridge University Press, 2016, Lectures 2 and 3 (sponsored search auctions and the GSP auction)

Where this leads

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
  • a scene preset for the example