How an algorithm decides which ad to show you in less than 100 milliseconds.
When you open a webpage, an invisible auction happens in the background. Advertisers want to show you their ad, but they don't bid a flat amount. Instead, the ad network (like Google or Facebook) uses Machine Learning to predict how likely you are to click the ad. It multiplies the advertiser's bid by your predicted Click-Through Rate (pCTR) to calculate an Expected CPM (eCPM). The ad with the highest Expected Value wins the auction. This maximizes revenue for the platform while showing you ads you actually care about.
If a platform only looked at the raw bid, it would always show the Rolex ad ($50). But if nobody ever clicks the Rolex ad, the platform makes $0. Machine Learning models (like Logistic Regression or Deep Neural Networks) analyze thousands of features (time of day, user history, device type) to predict the probability of a click in real-time.
// The core logic of an Ad Auction
function runAuction(user, ads) {
let winningAd = null;
let highestExpectedValue = 0;
for (let ad of ads) {
// ML Model predicts probability of a click (0.0 to 1.0)
let pCTR = mlModel.predict(user.features, ad.features);
// Expected Value = Bid * Probability
let eCPM = ad.bidAmount * pCTR;
if (eCPM > highestExpectedValue) {
highestExpectedValue = eCPM;
winningAd = ad;
}
}
return winningAd;
}
This ML prediction must happen during the HTTP request for the webpage, meaning it has a strict latency budget of roughly 100ms. To evaluate thousands of candidate ads in milliseconds, systems use a two-pass architecture: a fast, "dumb" filter to narrow it down to the top 100 ads, followed by a heavy, accurate Deep Learning model to score the final 100.