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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Empirical CDF for Win Probability / Clearing Price Estimation
|
| 3 |
+
Based on: Wang et al. "Learning to Bid in Repeated First-Price Auctions with Budgets" (2023)
|
| 4 |
+
arXiv: 2304.13477, Algorithm 1, Section 3.1
|
| 5 |
+
|
| 6 |
+
Non-parametric online estimation of:
|
| 7 |
+
- Win probability: G̃_t(b) = (1/(t-1)) Σ_{s=1}^{t-1} 𝟙{b ≥ d_s}
|
| 8 |
+
- Expected cost given win: E[cost|win,b] = mean of {d_s : d_s ≤ b}
|
| 9 |
+
- Expected reward: r̃_t(v,b) = (v-b) · G̃_t(b)
|
| 10 |
+
- Expected cost (for dual): c̃_t(b) = b · G̃_t(b)
|
| 11 |
+
|
| 12 |
+
This is the simplest approach — no model training, updates online, theoretically sound.
|
| 13 |
+
Used by Wang et al. (2023) as the core estimation in their DualOGD algorithm.
|
| 14 |
+
"""
|
| 15 |
+
import numpy as np
|
| 16 |
+
from collections import deque
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
class EmpiricalCDF:
|
| 20 |
+
"""
|
| 21 |
+
Online empirical CDF of competing bids for first-price auctions.
|
| 22 |
+
|
| 23 |
+
Under full information feedback: the bidder observes ALL maximum competing bids d_t,
|
| 24 |
+
regardless of whether they won or lost. This enables the empirical CDF approach.
|
| 25 |
+
|
| 26 |
+
Under one-sided feedback: only observe d_t when you win. See the value-shading
|
| 27 |
+
extension in Wang et al. for how DualOGD handles this case.
|
| 28 |
+
"""
|
| 29 |
+
|
| 30 |
+
def __init__(self, max_history=100000):
|
| 31 |
+
"""
|
| 32 |
+
Args:
|
| 33 |
+
max_history: Maximum number of historical bids to keep (FIFO buffer)
|
| 34 |
+
"""
|
| 35 |
+
self.max_history = max_history
|
| 36 |
+
self.competing_bids = deque(maxlen=max_history)
|
| 37 |
+
self.bid_counter = 0
|
| 38 |
+
|
| 39 |
+
def update(self, d_t):
|
| 40 |
+
"""
|
| 41 |
+
Record a new competing bid observation.
|
| 42 |
+
|
| 43 |
+
Args:
|
| 44 |
+
d_t: Maximum competing bid in auction t (observed under full feedback)
|
| 45 |
+
"""
|
| 46 |
+
self.competing_bids.append(d_t)
|
| 47 |
+
self.bid_counter += 1
|
| 48 |
+
|
| 49 |
+
def win_probability(self, b):
|
| 50 |
+
"""
|
| 51 |
+
Estimate P(win | bid=b) = fraction of historical competing bids ≤ b.
|
| 52 |
+
|
| 53 |
+
Args:
|
| 54 |
+
b: Bid price (scalar or array)
|
| 55 |
+
Returns:
|
| 56 |
+
win_prob: Estimated win probability [0, 1]
|
| 57 |
+
"""
|
| 58 |
+
if len(self.competing_bids) == 0:
|
| 59 |
+
return 0.5 # Uniform prior when no history
|
| 60 |
+
|
| 61 |
+
bids_arr = np.array(self.competing_bids)
|
| 62 |
+
b = np.asarray(b)
|
| 63 |
+
# Handle both scalar and array inputs
|
| 64 |
+
if b.ndim == 0:
|
| 65 |
+
return np.mean(bids_arr <= b)
|
| 66 |
+
else:
|
| 67 |
+
return np.array([np.mean(bids_arr <= bi) for bi in b])
|
| 68 |
+
|
| 69 |
+
def expected_cost_given_win(self, b):
|
| 70 |
+
"""
|
| 71 |
+
Estimate E[cost | win, bid=b].
|
| 72 |
+
In first-price, cost = bid when you win. But we estimate the
|
| 73 |
+
mean competing bid among those we beat.
|
| 74 |
+
|
| 75 |
+
Returns:
|
| 76 |
+
expected_cost: Mean of competing bids ≤ b
|
| 77 |
+
"""
|
| 78 |
+
if len(self.competing_bids) == 0:
|
| 79 |
+
return b
|
| 80 |
+
|
| 81 |
+
bids_arr = np.array(self.competing_bids)
|
| 82 |
+
wins = bids_arr[bids_arr <= b]
|
| 83 |
+
if len(wins) == 0:
|
| 84 |
+
return b # Fallback: bid your own price
|
| 85 |
+
return np.mean(wins)
|
| 86 |
+
|
| 87 |
+
def expected_reward(self, v, b):
|
| 88 |
+
"""
|
| 89 |
+
Estimate r̃_t(v, b) = (v - b) · G̃_t(b)
|
| 90 |
+
Used by DualOGD to evaluate bid candidates.
|
| 91 |
+
|
| 92 |
+
Args:
|
| 93 |
+
v: Value of winning (pCTR × value_per_click)
|
| 94 |
+
b: Bid price
|
| 95 |
+
Returns:
|
| 96 |
+
expected_reward
|
| 97 |
+
"""
|
| 98 |
+
win_prob = self.win_probability(b)
|
| 99 |
+
return (v - b) * win_prob
|
| 100 |
+
|
| 101 |
+
def expected_cost_dual(self, b):
|
| 102 |
+
"""
|
| 103 |
+
Estimate c̃_t(b) = b · G̃_t(b)
|
| 104 |
+
Used by DualOGD for the Lagrangian cost term.
|
| 105 |
+
|
| 106 |
+
Args:
|
| 107 |
+
b: Bid price
|
| 108 |
+
Returns:
|
| 109 |
+
expected_cost for dual update
|
| 110 |
+
"""
|
| 111 |
+
win_prob = self.win_probability(b)
|
| 112 |
+
return b * win_prob
|
| 113 |
+
|
| 114 |
+
def find_optimal_bid(self, v, lambd, bid_range=None, n_candidates=50):
|
| 115 |
+
"""
|
| 116 |
+
Find b_t = argmax_b (r̃_t(v, b) - λ · c̃_t(b))
|
| 117 |
+
This is the core bid optimization in Wang et al. (2023), Algorithm 1, line 6.
|
| 118 |
+
|
| 119 |
+
Args:
|
| 120 |
+
v: Value of winning
|
| 121 |
+
lambd: Current dual multiplier λ (budget pace multiplier)
|
| 122 |
+
bid_range: (min_bid, max_bid) or None for auto-range
|
| 123 |
+
n_candidates: Number of bid candidates to evaluate
|
| 124 |
+
Returns:
|
| 125 |
+
optimal_bid
|
| 126 |
+
"""
|
| 127 |
+
if bid_range is None:
|
| 128 |
+
# Auto-range: bid between 0 and v (since bidding above v loses money)
|
| 129 |
+
bid_range = (0.1, v * 2.0)
|
| 130 |
+
|
| 131 |
+
candidates = np.linspace(bid_range[0], bid_range[1], n_candidates)
|
| 132 |
+
|
| 133 |
+
best_bid = candidates[0]
|
| 134 |
+
best_score = -float('inf')
|
| 135 |
+
|
| 136 |
+
for b in candidates:
|
| 137 |
+
reward = self.expected_reward(v, b)
|
| 138 |
+
cost = self.expected_cost_dual(b)
|
| 139 |
+
score = reward - lambd * cost
|
| 140 |
+
|
| 141 |
+
if score > best_score:
|
| 142 |
+
best_score = score
|
| 143 |
+
best_bid = b
|
| 144 |
+
|
| 145 |
+
return best_bid
|
| 146 |
+
|
| 147 |
+
def estimate_full_curve(self, v, lambd, n_points=100):
|
| 148 |
+
"""
|
| 149 |
+
Get the full bid optimization landscape for analysis/plotting.
|
| 150 |
+
|
| 151 |
+
Returns:
|
| 152 |
+
dict with bids, rewards, costs, scores
|
| 153 |
+
"""
|
| 154 |
+
max_b = v * 2.0
|
| 155 |
+
bids = np.linspace(0.1, max_b, n_points)
|
| 156 |
+
rewards = np.array([self.expected_reward(v, b) for b in bids])
|
| 157 |
+
costs = np.array([self.expected_cost_dual(b) for b in bids])
|
| 158 |
+
scores = rewards - lambd * costs
|
| 159 |
+
|
| 160 |
+
return {
|
| 161 |
+
'bids': bids,
|
| 162 |
+
'rewards': rewards,
|
| 163 |
+
'costs': costs,
|
| 164 |
+
'scores': scores,
|
| 165 |
+
'optimal_bid': bids[np.argmax(scores)]
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
@property
|
| 169 |
+
def n_observations(self):
|
| 170 |
+
return len(self.competing_bids)
|
| 171 |
+
|
| 172 |
+
def get_statistics(self):
|
| 173 |
+
"""Get summary statistics of competing bid distribution."""
|
| 174 |
+
if len(self.competing_bids) == 0:
|
| 175 |
+
return {'mean': 0, 'std': 0, 'min': 0, 'max': 0, 'n': 0}
|
| 176 |
+
|
| 177 |
+
bids_arr = np.array(self.competing_bids)
|
| 178 |
+
return {
|
| 179 |
+
'mean': float(np.mean(bids_arr)),
|
| 180 |
+
'std': float(np.std(bids_arr)),
|
| 181 |
+
'median': float(np.median(bids_arr)),
|
| 182 |
+
'min': float(np.min(bids_arr)),
|
| 183 |
+
'max': float(np.max(bids_arr)),
|
| 184 |
+
'p10': float(np.percentile(bids_arr, 10)),
|
| 185 |
+
'p90': float(np.percentile(bids_arr, 90)),
|
| 186 |
+
'n': len(bids_arr)
|
| 187 |
+
}
|