Contact us
Back to knowledgebase

Cross-Entropy

Cross-entropy is the expected logarithmic loss of a probabilistic forecast. For demand counted in units, the score on each order depends entirely on the probability the forecaster gave to the quantity requested. Halving that probability costs one extra bit when the order comes in. Assigning zero probability incurs infinite loss.

Two forecasts can agree on the mean and assign very different probabilities to the quantities that matter. A backtest can distinguish them only to the extent that its records reveal which quantities customers actually requested.

The order just beyond the forecast

A depot expects an order of 1,000 units on average. In the forecast below, every quantity from 990 through 1,010 has some chance of occurring. That range accounts for the entire probability mass. The customer orders 1,011.

A forecast centered at 1,000 stops at 1,010. At an observed order of 1,011 its logarithmic loss becomes infinite, while its CRPS continues to 8.32 units.

The continuous ranked probability score (CRPS) comes to 8.32 units. Most of the forecast mass lies quite close to the order. Had the customer asked for another ten units, CRPS would have risen by exactly ten. Log loss is already infinite at 1,011. As far as this forecast was concerned, an order of 1,011 was as impossible as one of 1,500.

The order book and the dispatch file

The depot has 1,010 units on hand. The first forecast gives an order of exactly 1,010 a 2% chance. A second forecaster splits that probability equally between 1,010 and 1,011. He agrees with his colleague about every smaller order. Their means are 1,000 and 1,000.01.

The original forecast puts 2% on a request for exactly 1,010. The revised forecast gives 1% each to requests for 1,010 and 1,011. Both paths lead to the same dispatch entry, 1,010 units shipped with 2% probability.

The order book records 1,011 requested. The first forecast incurs infinite loss. The second gave the order a 1% chance and incurs −log₂(0.01), or 6.64 bits. On an order of exactly 1,010, the first forecaster would win by one bit.

The dispatch file records 1,010 shipped. Suppose the depot ships as much of the order as it can from stock. As long as some stock remains, the shipment reveals exactly what the customer requested. When the last unit leaves the shelf, all the dispatch clerk knows is that demand reached at least the stock on hand. Statisticians call this a right-censored observation. With s units on hand, selling out includes both the customer who wanted exactly s and the one who would have taken more.1

$$ P(\text{sell out at }s)=\sum_{d\ge s}q(d). $$

Both forecasters gave the dispatch clerk’s figure a 2% chance. Both score 5.64 bits. Reading the invoice as an order for exactly 1,010 would award the first forecaster a bit he never earned.

Another year of shipments cannot settle the matter if the depot always opens with 1,010 units. The two forecasts agree on the probability of every entry the clerk could make. One backorder for the missing unit would tell them apart. So could a day when the depot had 1,011 units to sell. An online store that disables its buy button at stockout has even less to go on. A customer who would have placed an order leaves no trace in the sales history.

From one order to cross-entropy

Write q(d) for the model’s probability of an order of d units. Its loss on that order is −log₂ q(d). To obtain cross-entropy, average over the quantities customers might request, weighting each by its true probability p(d) given the information available when the forecast was made.2

$$ H(p,q)=-\sum_d p(d)\log_2 q(d). $$

Even the forecaster who knows p incurs some loss whenever several orders are possible. His expected loss is the entropy H(p,p). For a distribution of finite entropy, the price of issuing some other forecast q is the Kullback–Leibler divergence. That price is infinite if q rules out any order that can actually occur. An order genuinely impossible under p contributes nothing.

$$ H(p,q)-H(p,p)=D_{\mathrm{KL}}(p\Vert q). $$

The divergence is nonnegative and vanishes only when the distributions agree. This makes the logarithmic score strictly proper. The word bit has its usual meaning from coding theory. With an ideal code based on q, an order assigned probability ⅛ takes three bits to encode. Cross-entropy gives the average code length. Using natural logarithms changes the unit to nats and preserves the ranking.

Counting parts gives each possible order a probability mass. Lead time modeled on a continuous scale requires a density. Its value changes with the unit of time, so both forecasters must use the same unit. If the ERP records only the arrival date, the observable event is a whole day. Integrating the density over that day gives the probability to score.

How much probability belongs in the tail?

The depot takes one order a day for 100 days. Just one exceeds 1,010 units. A forecaster who gave that event a 1% chance each day incurs 6.64 bits on the exceptional day. Each of the 99 ordinary days costs very little, but together they add 1.44 bits. Here the score is for crossing a threshold. An order for 1,011 and one for 1,500 each cross it once.

For one exceptional order in 100 days, quoting a daily probability of 0.1%, 1% or 10% gives total losses of 10.11, 8.08 or 18.37 bits. A larger tail probability saves bits on the exceptional day and costs bits on the other 99.

Raising the forecast to 10% saves 3.32 bits on the exceptional order and costs 13.61 on the other 99. Suppose the depot reviews its forecasts only when demand exceeds 1,010. A forecaster who predicted an exceptional order every single day would then have a perfect record.

For r exceptions in n days, a fixed probability q incurs −r log₂ q on the exceptions and −(n − r) log₂(1 − q) on the other days. Their sum is smallest at the observed frequency, r / n. A buyer who stands to lose a contract over a single shortage might quite reasonably stock the extra unit at a 1% chance of needing it. Inflating the forecast to justify the purchase would spoil a sound probability estimate.

Estimating the odds from sparse orders

For a spare part introduced ten days ago, the observed frequency of exceptional orders is likely to be zero. Even at a genuine 1% risk per day, ten independent days pass without one about 90% of the time. An engineer may know the part fits the same assemblies as its predecessor. A Bayesian model can use the predecessor’s orders to set a prior on the rate, then revise it with each new observation. A frequentist model can estimate a common rate from both order books by maximum likelihood. Both forecasts can be scored on the next order.

In practice the forecaster seldom knows p. If qₜ was the forecast on record before order dₜ arrived, the mean log loss on n orders estimates average predictive cross-entropy. An order size seen forty times enters the sum forty times.

$$ \overline L=-\frac1n\sum_{t=1}^n \log_2 q_t(d_t). $$

To fit a model by maximum likelihood, multiply the probabilities it assigned to the successive observations, each conditional on the history so far. Taking the logarithm turns that product into a sum. Maximizing the likelihood and minimizing the mean log loss give the same fit. A sufficiently flexible model could memorize the order book and achieve zero training loss. The test of that fit begins with orders the model has yet to see.

The conditioning matters even on a single job. Suppose a particular repair consumes one seal and one bearing, and neither part has any other use that day. A 10% chance of the repair gives a 10% chance of needing the pair. Once the seal is needed, the conditional probability of needing the bearing is one. Multiplying the two marginal probabilities as though the parts were ordered independently gives just 1% for the pair, an extra 3.32 bits on that job.3

A year’s likelihood can round to zero on a computer even for an excellent forecast. A thousand observations each assigned probability 0.1 give a product of 10⁻¹⁰⁰⁰. Summing their log probabilities avoids the underflow. In a simulated histogram, a zero may simply mean there were too few draws to see a rare order. No number of draws from the depot’s original forecast will ever yield 1,011. A forecaster with reason to allow larger orders can reserve some mass for a broader distribution before the backtest. Adding it after the order arrives amounts to scoring a different forecast.

Why train on log loss?

Lokad used CRPS in its fourth-generation forecasting engine for aerospace and fashion. During experiments in 2017, training on cross-entropy yielded forecasts that also did better on CRPS. The account of the switch singles out the ease with which the earlier engine could dismiss a possible order. A distribution fitted by likelihood still supplies a mean for a squared-error test, a median for an absolute-error test, and the full distribution for CRPS. All three can be checked on subsequent orders.

Consider a model that adjusts the log-odds z of an exceptional order and converts them to a probability q with a sigmoid. The model gave the order a 1% chance yesterday, and today it came in. With natural logs, the derivative of log loss with respect to z is q − 1, or −0.99. For squared probability error the derivative is 2(q − 1)q(1 − q), about −0.020. Gradient descent would raise the log-odds in either case. Squared error gives a particularly weak signal here because the sigmoid is nearly flat at this end of its range.4

When an exceptional order occurs, the log-loss gradient with respect to its log-odds has magnitude 0.99 at a forecast probability of 1%. The squared-error gradient has magnitude about 0.020. Both fall to zero as the probability approaches one.

When the forecast was 50%, doubling the learning rate for squared error would match the log-loss update. At 1%, it would take roughly fifty times the rate. At the depot, either calculation presupposes a record of the exceptional order. If only the dispatch file survives, the training example is a shipment of 1,010. The customer’s last unit never made it onto the invoice.

References


  1. Steven Nahmias, “Demand estimation in lost sales inventory systems,” Naval Research Logistics 41, 1994, pp. 739–757. ↩︎

  2. Tilmann Gneiting and Adrian E. Raftery, “Strictly Proper Scoring Rules, Prediction, and Estimation,” Journal of the American Statistical Association 102, 2007, §4.1. The convention here reports loss, with smaller values preferred. ↩︎

  3. Probabilistic Forecasting for Supply Chain develops the likelihood calculation for forecasting. Introduction to Supply Chain, §7.4.7 discusses dependence among forecast quantities. ↩︎

  4. Ian Goodfellow, Yoshua Bengio and Aaron Courville, Deep Learning, 2016, §6.2.2.2, “Sigmoid Units for Bernoulli Output Distributions.” ↩︎