AI Wiki中文/EN

Bayesian decision theory: probability needs a loss table

贝叶斯决策理论

Compute posterior risks, action thresholds, exact ties and the value of perfect information in a reproducible decision problem.

On this pageIntuitionMathematics & methodImplementationLimitationsExercisesResearch connections

The most likely state does not choose the action

A parcel is probably fine. Should you ship it, block it, or inspect it? “Probably fine” is a statement about uncertainty. The action also depends on the consequences of a mistake.

Use a synthetic two-state example: normal 0 or defective 1. Prior probabilities are(9/10,1/10). A flag appears with probability 2/5 for a normal parcel and 9/10 for a defective one. After seeing a flag, the posterior is(4/5,1/5). Normal is still the most likely state. That fact alone does not determine what to do. Start with Bayesian inference and expectation and variance.

Choose the smallest posterior expected loss

Let θ be an unknown state, D the information available, a an allowed action and L(a,θ) its loss. For finite states and actions:

R(a∣D)=∑θL(a,θ)p(θ∣D),A∗(D)=arg min⁡aR(a∣D).R(a\mid D)=\sum_\theta L(a,\theta)p(\theta\mid D),\qquad \mathcal A^*(D)=\operatorname*{arg\,min}_a R(a\mid D).

The minimizing set can contain several actions. A tie-breaking rule is an additional policy choice. Integrability replaces finite sums for continuous states, and an optimum requires appropriate existence conditions. Posterior risk here averages over uncertainty in θ; it should not be confused with the frequentist risk of a decision rule at a fixed θ.

Conditional minimization also minimizes the prior-averaged loss over decision rules: apply the law of total expectation to E[L(δ(D),θ)] and choose a minimizing action for each D. Shalizi’s decision-theory lecture explains expected-loss minimization and its conditional decomposition; GPML Section 2.4 connects predictions to losses.

For a real-valued quantity with finite second moment, squared loss yields:

E[(θ−a)2∣D]=Var⁡(θ∣D)+(a−E[θ∣D])2.\mathbb E[(\theta-a)^2\mid D] =\operatorname{Var}(\theta\mid D)+(a-\mathbb E[\theta\mid D])^2.

Therefore the posterior mean minimizes squared loss. Absolute loss, with finite first moment, is minimized by a posterior median. Discrete zero-one classification loss selects a posterior mode. These are different consequences of different losses, not interchangeable definitions of a universally best estimate.

Work through the parcel decision

The flag has probability(9/10)(2/5)+(1/10)(9/10)=9/20. The defective posterior is(9/100)/(9/20)=1/5. Set the losses as follows; numbers are hypothetical loss units.

ActionNormalDefectivePosterior risk
Ship0102
Block104/5
Perfect inspection1/21/21/2

Inspection is an idealized compound action: pay 1/2, observe the true state perfectly, then ship normal parcels or block defective ones at zero additional loss. It is not a claim about a real inspection system. With this option available, inspection wins even though normal is more likely. With only ship and block, blocking wins.

Writing p for the defect probability, the direct risks are 10p and 1−p. They tie at p=1/11. With inspection cost 1/2, shipping is uniquely optimal below 1/20, inspection between 1/20 and 1/2, and blocking above 1/2. Both adjacent actions are optimal at each boundary. A largest-posterior-label rule would instead use a 1/2 threshold under zero-one loss.

Keep the full risk table and all ties

The complete Python 3.12 program computes event posteriors, action risks and perfect-information value with exact fractions. It supports up to eight states and eight actions, nonnegative rational losses at most one million, and zero state probabilities. An impossible conditioning event raises an error.

from fractions import Fraction as F


def probabilities(values):
    values=list(values)
    if not 1<=len(values)<=8 or any(type(x) not in (int,F) or not 0<=x<=1 for x in values) or sum(values,F(0))!=1:
        raise ValueError('one to eight rational probabilities summing to one required')
    return list(map(F,values))


def posterior(prior, likelihood):
    prior=probabilities(prior);likelihood=list(likelihood)
    if len(likelihood)!=len(prior) or any(type(x) not in (int,F) or not 0<=x<=1 for x in likelihood):
        raise ValueError('matching rational event likelihoods between zero and one required')
    weights=[p*F(v) for p,v in zip(prior,likelihood)];evidence=sum(weights,F(0))
    if not evidence:raise ValueError('conditioning event has zero probability')
    return [v/evidence for v in weights],evidence


def decision(probability, losses):
    p=probabilities(probability);losses=[list(row) for row in losses]
    if not 1<=len(losses)<=8 or any(len(row)!=len(p) for row in losses):
        raise ValueError('one to eight action rows with one loss per state required')
    if any(type(v) not in (int,F) or not 0<=v<=1000000 for row in losses for v in row):
        raise ValueError('bounded nonnegative rational losses required')
    risks=[sum((p[j]*F(v) for j,v in enumerate(row)),F(0)) for row in losses]
    best=min(risks)
    return risks,[i for i,v in enumerate(risks) if v==best]


def perfect_information_value(probability, direct_losses, cost=0):
    p=probabilities(probability);losses=[list(row) for row in direct_losses]
    risks,_=decision(p,losses)
    if type(cost) not in (int,F) or not 0<=cost<=1000000:
        raise ValueError('bounded nonnegative rational information cost required')
    after=sum((p[j]*min(F(row[j]) for row in losses) for j in range(len(p))),F(0))
    gross=min(risks)-after
    return gross,gross-F(cost)


if __name__=='__main__':
    p,evidence=posterior([F(9,10),F(1,10)],[F(2,5),F(9,10)])
    direct=[[0,10],[1,0]];losses=direct+[[F(1,2),F(1,2)]]
    risks,best=decision(p,losses);names=['ship','block','perfect inspection']
    print('event probability:',evidence,'posterior:',*[str(v) for v in p])
    print('action risks:',*[str(v) for v in risks])
    print('minimum-risk actions:',*[names[i] for i in best])
    print('perfect information gross value and net benefit:',*[str(v) for v in perfect_information_value(p,direct,F(1,2))])
    for defective in [F(1,20),F(1,2)]:
        _,ties=decision([1-defective,defective],losses)
        print('defect probability:',defective,'tied best actions:',*[names[i] for i in ties])

Output:

event probability: 9/20 posterior: 4/5 1/5
action risks: 2 4/5 1/2
minimum-risk actions: perfect inspection
perfect information gross value and net benefit: 4/5 3/10
defect probability: 1/20 tied best actions: ship perfect inspection
defect probability: 1/2 tied best actions: block perfect inspection

Risk evaluation takes O(AS) rational operations for A actions and S states, plus rational bit costs. The code returns every minimizing index, rather than breaking exact ties by array order. It does not learn or validate the supplied probabilities or loss values. Its likelihood input describes a single event, so those entries need not sum to one across states.

Information has value under a specified decision problem

For a possible signal S, the gross expected value of sample information is:

EVSI⁡=min⁡aE[L(a,θ)∣D]−ES∣D ⁣[min⁡aE[L(a,θ)∣D,S]]≥0.\operatorname{EVSI}= \min_a\mathbb E[L(a,\theta)\mid D] -\mathbb E_{S\mid D}\!\left[\min_a\mathbb E[L(a,\theta)\mid D,S]\right]\geq0.

The inequality assumes coherent probabilities, the same losses and actions, and the ability to ignore information. For each signal, the old optimal action remains available; minimizing cannot do worse than taking it. Averaging then recovers the old risk by total expectation. This is gross value before acquisition cost.

Perfect information is the special case that reveals θ. In our direct-action table its future risk is zero, so gross value is 4/5. Paying 1/2 leaves net benefit 3/10. The helper computes that special case; it does not simulate an imperfect inspection or calculate general EVSI. A sufficiently expensive test has negative net benefit even though information’s gross value is nonnegative.

A loss table can omit delay, unequal impacts, or constraints on allowed actions. Those omissions are modeling choices, not facts resolved by Bayes’ rule. Probability miscalibration can also change the recommended action near a threshold. Sensitivity analysis should vary both probabilities and losses. Dynamic decisions may need future consequences and information gathering to be modeled together.

Change the loss before changing the probability

  1. What is the posterior defect probability after the flag?

Answer 1: 1/5; the flag’s total probability is 9/20.

  1. Without inspection, at what defect probability do shipping and blocking tie?

Answer 2: 10p=1−p, so p=1/11.

  1. What is the net benefit of perfect information at cost 1/2 in this example?

Answer 3: 4/5−1/2=3/10 relative to the best direct action.

  1. Does the most likely state always specify the minimum-risk action?

Answer 4: No. It does so for the corresponding zero-one classification problem, not for arbitrary losses and actions.

Make the decision assumptions inspectable

Project: replace perfect inspection with a binary test of specified sensitivity and false-positive rate. Compute the two signal probabilities and their posteriors, choose a direct action after each signal, and average the resulting loss plus test cost. Compare this imperfect test with the perfect-information upper bound under the same action set.

Bayesian decision theory supplies a common language for classification thresholds, prediction, experiment design and sequential decisions. It does not eliminate disagreements about objectives. Keep a record of the posterior, the allowed actions, the loss table and the sensitivity range so that another reader can reproduce why an action was chosen. Gaussian processes provide one predictive distribution; this chapter supplies the next step from a distribution to an action.

Make a prediction, then check

Which action minimizes risk after the flag in this example?

Choose one answer

Learning objectives

  • Calculate posterior expected losses and all minimizing actions.
  • Derive loss-dependent classification and inspection thresholds.
  • Distinguish gross information value from net benefit after cost.

Prerequisites & connections

Explore related knowledge →

References

  1. Gaussian Processes for Machine Learning — Chapter 2: Regression
    Carl Edward Rasmussen; Christopher K. I. Williams · 2006
  2. Predictions and Decision Theory — Lecture 2, February 4, 2021
    Cosma Rohilla Shalizi · 2021

Version1 · Editorial status: Recorded technical checks completed

View technical check record (AI)

Codex · 2026-09-06 · Codex single-agent authored technical review: cited primary-source scope, explicit assumptions, original calculations and counterexamples, runnable tests and bilingual review; not independent human or second-agent review.

  • Single-agent authored technical review: bounded primary-source locations, assumptions, original derivations, exact/literal references and matching bilingual equations/examples/exercises. Not independent human or second-agent review.
  • Four focused CPython3.12.4 tests passed per chapter. IS and decisions use exact fractions; VI and GP use floating-point calculations checked against analytic constants, invariance and meaningful boundaries. NumPy1.26.4 used for GP.
  • Promoted the full prose, equations, code and exercises. Tests read formal MDX only; removed temporary fallbacks/draft skips and updated review assertion to checked. Algorithm and independent references are unchanged; final record binds both locale hashes.
  • Publication navigation points published topics to complete articles and removes corresponding pending wording, retaining directory entries for unwritten targets. Principles, equations, programs and exercises are unchanged; final article SHA values rebound.

Source locations checked:

Related topics

No matches. Try a synonym or acronym.

0 results shown

Wrap the whole query in double quotes for a phrase match.