Tutorials › Statistical Learning (Graduate) › Multiclass Decisions With General Cost Matrices

Bayes Rules and Conditional Risk · Tutorial 57 of 989

Multiclass Decisions With General Cost Matrices

Use posterior probabilities and a general cost matrix to calculate the minimum-cost action for a multiclass problem.

Beginner 10 min read

What You'll Learn

  • Read a multiclass cost matrix using the action-by-true-class convention.
  • Calculate each action’s conditional expected cost from the posterior probabilities.
  • Select a Bayes action by minimizing conditional risk, including when there are ties.
  • Identify why the most probable class can have greater expected cost than another action.
  • Recognize cost-matrix changes that leave the Bayes action unchanged.

Why the Most Probable Class May Not Be the Best Decision

In multiclass classification, it is tempting to choose the class with the largest posterior probability. That is optimal under zero-one loss, where every incorrect classification has the same cost. But many decisions do not treat all errors equally: confusing two similar categories may be less costly than confusing two very different ones. In those settings, the most probable class need not be the action with the smallest expected cost.

The relevant quantity is the conditional risk of an action: its expected loss given the observed features. A general cost matrix lets us calculate this risk for every possible action and choose one with minimum risk. The probabilities still matter, but the costs determine how they should be combined.

Cost Matrices and Conditional Risk

Let the true class \(Y\) take values in \(\{1,\ldots,K\}\), and suppose the available actions are also labels \(1,\ldots,K\). Write \(C_{jk}\) for the cost of taking action \(j\) when the true class is \(k\). Thus, rows index actions and columns index true classes. The cost of a correct decision need not be zero, and costs for different errors need not be equal.

Definition: At a feature value \(x\), let \(\eta_k(x)=P(Y=k\mid X=x)\) be the posterior probability of class \(k\). The conditional risk of action \(j\) is $$ R(j\mid x)=\sum_{k=1}^{K} C_{jk}\eta_k(x). $$ A Bayes action at \(x\) is any action in \(\operatorname*{arg\,min}_{1\le j\le K}R(j\mid x)\). A Bayes rule chooses a Bayes action at each feature value, subject to measurability.

Each row of the cost matrix is combined with the posterior probability vector. In vector notation, if \(C_j=(C_{j1},\ldots,C_{jK})\) is row \(j\), then \(R(j\mid x)=C_j\cdot\eta(x)\). The choice is therefore not necessarily the row corresponding to the largest component of \(\eta(x)\).

We assume throughout that the number of classes and actions is finite and that all entries of \(C\) are finite. These conditions ensure that every displayed conditional risk is finite. When choosing among tied minimizers, any measurable tie-breaking rule gives the same conditional risk.

Worked Example: The Most Probable Class Has Higher Cost

Suppose there are three classes, the posterior probabilities at an observed feature value are \((0.45,0.35,0.20)\), and the cost matrix is $$ C= \begin{pmatrix} 0&5&5\\ 2&0&1\\ 3&2&0 \end{pmatrix}. $$ Rows are actions and columns are true classes. Class 1 is most probable. Calculate the conditional risk of each action.

Action 1: its costs across true classes are \((0,5,5)\), so $$ R(1\mid x)=0(0.45)+5(0.35)+5(0.20)=0+1.75+1.00=2.75. $$

Action 2: its costs are \((2,0,1)\), giving $$ R(2\mid x)=2(0.45)+0(0.35)+1(0.20)=0.90+0+0.20=1.10. $$

Action 3: its costs are \((3,2,0)\), giving $$ R(3\mid x)=3(0.45)+2(0.35)+0(0.20)=1.35+0.70+0=2.05. $$

Since \(1.10<2.05<2.75\), action 2 is the unique Bayes action, even though class 1 has the largest posterior probability. The relatively large cost of choosing action 1 when the truth is class 2 or 3 outweighs its advantage in posterior probability.

Why Minimizing Conditional Risk Gives a Bayes Rule

The calculation above is pointwise: it is made separately at each \(x\). The following result explains why these pointwise choices also minimize the overall expected loss.

Theorem: Suppose \(Y\) has \(K\) classes, the action set is finite, all costs are finite, and the posterior probabilities \(\eta_k(X)=P(Y=k\mid X)\) are defined as measurable versions of the conditional probabilities. For any measurable decision rule \(g\), its population risk is $$ \mathcal R(g)=E[C_{g(X),Y}]=E[R(g(X)\mid X)]. $$ A measurable rule \(g^*\) that minimizes \(R(j\mid X)\) over all actions almost surely minimizes \(\mathcal R(g)\) over all measurable rules.

Proof. For each action \(j\), let \(A_j=\{X:g(X)=j\}\). Because the action set is finite and \(g\) is measurable, each \(A_j\) is measurable. Conditional on \(X\), the cost of the chosen action is \(C_{g(X),Y}\). On the event \(A_j\), this is \(C_{jY}\). Using the definition of conditional probability for the finite-valued variable \(Y\), $$ E[C_{jY}\mid X]=\sum_{k=1}^{K}C_{jk}P(Y=k\mid X) =\sum_{k=1}^{K}C_{jk}\eta_k(X)=R(j\mid X). $$ Thus, on \(A_j\), \(E[C_{g(X),Y}\mid X]=R(j\mid X)\), and consequently $$ E[C_{g(X),Y}\mid X]=R(g(X)\mid X). $$ The costs are bounded because there are finitely many finite matrix entries, so these conditional expectations and their expectations exist. Taking expectations gives \(\mathcal R(g)=E[R(g(X)\mid X)]\).

Now suppose \(g^*\) minimizes conditional risk almost surely. For any other measurable rule \(g\), the pointwise minimizing property gives \(R(g^*(X)\mid X)\le R(g(X)\mid X)\) almost surely. Taking expectations and applying the identity just proved yields \(\mathcal R(g^*)\le\mathcal R(g)\). Therefore \(g^*\) is a population-risk minimizer. \(\square\)

With finitely many actions, one can choose the smallest-index action among the minimizers. Its selection is measurable because each conditional risk is measurable and comparisons among finitely many measurable real-valued functions define measurable sets. This provides a definite Bayes rule even when ties occur.

Comparing Two Actions Directly

Sometimes it is useful to compare two rows without calculating every risk in full. The difference is a weighted sum of the differences between their costs.

Result: For any actions \(a\) and \(b\), $$ R(a\mid x)-R(b\mid x)=\sum_{k=1}^{K}(C_{ak}-C_{bk})\eta_k(x). $$ Action \(a\) has no greater conditional risk than action \(b\) exactly when this sum is at most zero.

Derivation. By the definition of conditional risk, $$ R(a\mid x)-R(b\mid x) =\sum_{k=1}^{K}C_{ak}\eta_k(x)-\sum_{k=1}^{K}C_{bk}\eta_k(x) =\sum_{k=1}^{K}(C_{ak}-C_{bk})\eta_k(x). $$ Since \(a\) has no greater risk precisely when \(R(a\mid x)-R(b\mid x)\le 0\), the stated comparison follows. \(\square\)

This expression makes the trade-off explicit. If action \(a\) is cheaper than \(b\) for some true classes but more expensive for others, the posterior probabilities determine which differences matter most.

Worked Example: A Decision Can Favor a Less Probable Severe Class

Consider three ordered categories: mild, moderate, and severe. The actions are to report one of these three categories. Use the cost matrix $$ C= \begin{pmatrix} 0&1&8\\ 2&0&5\\ 4&1&0 \end{pmatrix} $$ and posterior probabilities \((0.20,0.45,0.35)\). The most probable class is moderate. Find the Bayes action.

For action 1, the conditional risk is $$ R(1\mid x)=0(0.20)+1(0.45)+8(0.35)=0+0.45+2.80=3.25. $$ For action 2, it is $$ R(2\mid x)=2(0.20)+0(0.45)+5(0.35)=0.40+0+1.75=2.15. $$ For action 3, it is $$ R(3\mid x)=4(0.20)+1(0.45)+0(0.35)=0.80+0.45+0=1.25. $$

The minimum is \(1.25\), so action 3 is the unique Bayes action. Reporting “severe” is optimal under this matrix because the cost of reporting a less severe category when the truth is severe is high. The posterior probability of severe need not be the largest for that cost to affect the decision.

Worked Example: When the Most Probable Class Is Also Optimal

A different cost matrix is $$ C= \begin{pmatrix} 0&1&3\\ 2&0&2\\ 4&1&0 \end{pmatrix}, $$ and the posterior probabilities are \((0.60,0.25,0.15)\). Class 1 is most probable. Check whether it is also the Bayes action.

The three risks are $$ R(1\mid x)=0(0.60)+1(0.25)+3(0.15)=0.25+0.45=0.70, $$ $$ R(2\mid x)=2(0.60)+0(0.25)+2(0.15)=1.20+0.30=1.50, $$ and $$ R(3\mid x)=4(0.60)+1(0.25)+0(0.15)=2.40+0.25=2.65. $$

Because \(0.70<1.50<2.65\), action 1 is uniquely optimal. This example does not make the most-probable-class rule universally valid; it shows that posterior mode and minimum-cost action can agree for a particular combination of probabilities and costs.

Cost Changes That Do Not Affect the Decision

Some changes to the cost matrix alter the numerical risks without changing which action is best. For example, adding the same constant to every entry in a row adds that constant to the risk of that action. Adding the same constant to every row in one column adds a common amount to every action’s risk, weighted by the posterior probability of that true class. The latter cannot change the minimizing action.

Result: Let \(\widetilde C_{jk}=C_{jk}+d_k\), where \(d_k\) depends on the true class but not on the action. Then for every action \(j\), $$ \widetilde R(j\mid x)=R(j\mid x)+\sum_{k=1}^{K}d_k\eta_k(x). $$ The added term is identical for every action, so the set of Bayes actions is unchanged.

Proof. Substitute \(\widetilde C_{jk}=C_{jk}+d_k\) into the definition of conditional risk: $$ \widetilde R(j\mid x) =\sum_{k=1}^{K}(C_{jk}+d_k)\eta_k(x) =R(j\mid x)+\sum_{k=1}^{K}d_k\eta_k(x). $$ The second term does not depend on \(j\). For any two actions \(a,b\), it therefore cancels in \(\widetilde R(a\mid x)-\widetilde R(b\mid x)\). All pairwise comparisons, and hence the minimizers, are unchanged. \(\square\)

Common Mistakes

  • Choosing the largest posterior automatically. That rule minimizes expected cost under zero-one loss, not under every cost matrix. Calculate the row-wise conditional risks for the loss actually specified.
  • Reversing rows and columns. Here, row \(j\) describes the costs of taking action \(j\), while column \(k\) describes the true class \(k\). Check this convention before multiplying by the posterior probabilities.
  • Using only the cost of the most likely class. Conditional risk averages costs over all possible true classes, weighted by their posterior probabilities. Even a less probable class can matter greatly if its associated cost is large.
  • Treating a tie as an error. If several actions attain the same minimum conditional risk, each is a Bayes action. A tie-breaking convention chooses among them without changing the minimum risk.
Key takeaway: For a multiclass cost matrix, compute \(R(j\mid x)=\sum_k C_{jk}\eta_k(x)\) for every action and choose a minimizer. The most probable class is optimal only when the posterior probabilities and costs together make its action’s conditional risk smallest.

Check Your Understanding

Use the action-by-true-class convention for each question.

  1. For a three-class problem, posterior probabilities are \((0.5,0.3,0.2)\), and the cost of every incorrect action is 1 while correct actions cost 0. Calculate the three conditional risks and identify the Bayes action.
  2. Explain why a class with the largest posterior probability need not correspond to a minimum-risk action under a general cost matrix.
  3. For two actions \(a\) and \(b\), write the expression for \(R(a\mid x)-R(b\mid x)\) in terms of the cost entries and posterior probabilities.
  4. If the same number is added to every entry of one true-class column, why does this leave the Bayes actions unchanged?
  5. What does a tie between two minimum conditional risks imply about the set of Bayes actions?
Clinical Biostats