 # Loss functions for classification

In machine learning and mathematical optimization, loss functions for classification are computationally feasible loss functions representing the price paid for inaccuracy of predictions in classification problems (problems of identifying which category a particular observation belongs to). Given ${\mathcal {X}}$ as the space of all possible inputs (usually ${\mathcal {X}}\subset \mathbb {R} ^{d}$ ), and $\displaystyle \mathcal{Y} = \{ -1,1 \}$ as the set of labels (possible outputs), a typical goal of classification algorithms is to find a function $f:{\mathcal {X}}\to {\mathcal {Y}}$ which best predicts a label Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "http://localhost:6011/en.wikipedia.org/v1/":): y for a given input $\vec {x}$ . However, because of incomplete information, noise in the measurement, or probabilistic components in the underlying process, it is possible for the same ${\vec {x}}$ to generate different $y$ . As a result, the goal of the learning problem is to minimize expected loss (also known as the risk), defined as

$I[f]=\displaystyle \int _{{\mathcal {X}}\times {\mathcal {Y}}}V(f({\vec {x}}),y)\,p({\vec {x}},y)\,d{\vec {x}}\,dy$  Bayes consistent loss functions: Zero-one loss (gray), Savage loss (green), Logistic loss (orange), Exponential loss (purple), Tangent loss (brown), Square loss (blue)

where $\vec {x}$ is a given loss function, and $p({\vec {x}},y)$ is the probability density function of the process that generated the data, which can equivalently be written as

$p({\vec {x}},y)=p(y\mid {\vec {x}})p({\vec {x}}).$ Within classification, several commonly used loss functions are written solely in terms of the product of the true label $y$ and the predicted label $\vec {x}$ . Therefore, they can be defined as functions of only one variable $\upsilon =yf({\vec {x}})$ , so that $V(f({\vec {x}}),y)=\phi (yf({\vec {x}}))=\phi (\upsilon )$ with a suitably chosen function $\ph$ :\mathbb {R} \to \mathbb {R} } . These are called margin-based loss functions. Choosing a margin-based loss function amounts to choosing $\phi$ . Selection of a loss function within this framework impacts the optimal $f_{\phi }^{*}$ which minimizes the expected risk.

In the case of binary classification, it is possible to simplify the calculation of expected risk from the integral specified above. Specifically,

{\begin{aligned}I[f]&=\int _{{\mathcal {X}}\times {\mathcal {Y}}}V(f({\vec {x}}),y)\,p({\vec {x}},y)\,d{\vec {x}}\,dy\\[6pt]&=\int _{\mathcal {X}}\int _{\mathcal {Y}}\phi (yf({\vec {x}}))\,p(y\mid {\vec {x}})\,p({\vec {x}})\,dy\,d{\vec {x}}\\[6pt]&=\int _{\mathcal {X}}[\phi (f({\vec {x}}))\,p(1\mid {\vec {x}})+\phi (-f({\vec {x}}))\,p(-1\mid {\vec {x}})]\,p({\vec {x}})\,d{\vec {x}}\\[6pt]&=\int _{\mathcal {X}}[\phi (f({\vec {x}}))\,p(1\mid {\vec {x}})+\phi (-f({\vec {x}}))\,(1-p(1\mid {\vec {x}}))]\,p({\vec {x}})\,d{\vec {x}}\end{aligned}} The second equality follows from the properties described above. The third equality follows from the fact that 1 and −1 are the only possible values for Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "http://localhost:6011/en.wikipedia.org/v1/":): y , and the fourth because $p(-1\mid x)=1-p(1\mid x)$ . The term within brackets $[\phi (f({\vec {x}}))p(1\mid {\vec {x}})+\phi (-f({\vec {x}}))(1-p(1\mid {\vec {x}}))]$ is known as the conditional risk.

One can solve for the minimizer of $I[f]$ by taking the functional derivative of the last equality with respect to $f$ and setting the derivative equal to 0. This will result in the following equation

${\frac {\partial \phi (f)}{\partial f}}\eta +{\frac {\partial \phi (-f)}{\partial f}}(1-\eta )=0\;\;\;\;\;(1)$ [citation needed][clarification needed]

which is also equivalent to setting the derivative of the conditional risk equal to zero.

Given the binary nature of classification, a natural selection for a loss function (assuming equal cost for false positives and false negatives) would be the 0-1 loss function (0–1 indicator function), which takes the value of 0 if the predicted classification equals that of the true class or a 1 if the predicted classification does not match the true class. This selection is modeled by

$V(f({\vec {x}}),y)=H(-yf({\vec {x}}))$ where $H$ indicates the Heaviside step function. However, this loss function is non-convex and non-smooth, and solving for the optimal solution is an NP-hard combinatorial optimization problem. As a result, it is better to substitute loss function surrogates which are tractable for commonly used learning algorithms, as they have convenient properties such as being convex and smooth. In addition to their computational tractability, one can show that the solutions to the learning problem using these loss surrogates allow for the recovery of the actual solution to the original classification problem. Some of these surrogates are described below.

In practice, the probability distribution $p({\vec {x}},y)$ is unknown. Consequently, utilizing a training set of $n$ independently and identically distributed sample points

$S=\{({\vec {x}}_{1},y_{1}),\dots ,({\vec {x}}_{n},y_{n})\}$ drawn from the data sample space, one seeks to minimize empirical risk

$I_{S}[f]={\frac {1}{n}}\sum _{i=1}^{n}V(f({\vec {x}}_{i}),y_{i})$ as a proxy for expected risk. (See statistical learning theory for a more detailed description.)