mathematics//optimization//cost function
A cost function is the rule that assigns a number to each outcome of a decision (each action in each possible state of the world, or each candidate solution), so that an optimizer or a decision rule can rank the alternatives; it is the input that determines what an optimizer, a controller or an automatic decision will do, and the one engineers write least explicitly. In decision theory it is a table \(C(a,s)\): what doing \(a\) costs if the world is in state \(s\). In control it is a formula over errors and inputs, in estimation a sum of squared residuals, in machine learning a loss function averaged over the training data.
A cost function is the rule that assigns a number to each outcome of a decision (each action in each possible state of the world, or each candidate solution), so that an optimizer or a decision rule can rank the alternatives; it is the input that determines what an optimizer, a controller or an automatic decision will do, and the one engineers write least explicitly. In decision theory it is a table C(a,s)C(a,s)C(a,s): what doing aaa costs if the world is in state sss. In control it is a formula over errors and inputs, in estimation a sum of squared residuals, in machine learning a loss function averaged over the training data.
Take a drone on an inspection run whose battery estimate reads 25 %. Turning back now costs a repeated sortie, perhaps 40 euros of crew time; pressing on and running dry over a field costs a lost airframe and a report to the authority, thousands. The estimator gives the probability of each battery state; the cost table says what each mistake is worth. Neither alone decides, and most bad automatic decisions come from mixing the two: a probability treated as if it were the decision, or a cost nobody wrote down.
The cost dominates the result. An optimizer exploits the cost exactly as written, including every place where it differs from what was meant, and optimizing precisely the wrong cost is being wrong precisely; the book counts the costs among the five places where a decision's error lives.
The forms differ by field. An LQR weighs squared state errors against squared inputs through two matrices; a fit of a model to data uses squared residuals, which amounts to assuming Gaussian noise, so choosing the cost is choosing a noise model; a reinforcement-learning reward is a cost with its sign flipped, and an agent that finds a loophole in it is doing exactly what it was told (reward hacking).
The numbers come from operations. Scrap, downtime, fines, energy and crew hours are known to someone in the plant; asking for them, and checking whether the decision flips when each moves by half, is cheap and catches most errors. Catastrophic outcomes do not belong in the average at all: a drone falling on people is imposed as a probability bound and the cost is minimized inside it.
A cost table turns a calibrated probability into an action through the decision threshold, and it is the first thing to write when a problem is cast as optimization.