Intro to Loss Functions

Which one is appropriate for the task at hand?

There are many forms that a loss function can take. Deep learning introduces a much more general approach to constructing loss functions but this intro to loss functions should get you thinking about it the right way.

Models Minimize a Loss Function

A loss function is something that ML algorithms seek to minimize.

Every model comes with its own loss function that it minimizes either implicitly or explicitly Orderinary least squares (OLS) minimizes the sum of squared residuals. Lasso, ridge, and elastic net minimize an additional penalty term to OLS. Regression trees greedily solve ∑j=1J∑i∈RJ(yi−y^Rj)2\sum_{j=1}^J \sum_{i \in R_J} (y_i - \hat y_{R_j})^2 where jj indicates predictor spaces (pg. 306 from this book for me info), and neural networks minimize whatever loss function is given them.

Though models minimize loss functions, what are some general types of loss functions and what are each of them good for?

Examples of Regression Loss Functions

Let d=y−y^d = y - \hat y.

  • Squared Loss: loss=∑i=1ndi2=d′dloss = \sum _{i=1}^n d_i^2 = \pmb d'\pmb d
  • Absolute Deviation: loss=∑i=1n∣di∣=1′abs(d)loss = \sum _{i=1}^n |d_i| = \pmb 1'\text{abs}(\pmb d)
  • Asymetric Loss: loss=∑i=1ndi⋅(τ−1(di<0))loss = \sum_{i=1}^n d_i\cdot(\tau - 1(d_i<0)) where τ∈(0,1)\tau \in (0,1) and 1(⋅)1(\cdot) is an indicator function that evaluates to 11 when the condition is satisfied and 00.

Let's simplify thinking about this function by just considering a single value of dd and plotting the loss as a function of a single loss amount dd.

MSE becomes loss=d2loss = d^2. Which is a parabola.

Absolute deviation becomes loss=∣d∣loss = |d| which is a V shape with the bottom of the V on the origin.

Asymetric loss becomes

loss(d)={τdd≥0(τ−1)dd<0loss(d) = \left\{ \begin{array}{ll} \tau d & d\geq 0 \\ (\tau-1)d & d < 0 \end{array} \right.

Which is V shaped loss function with the V on the origin, the difference this time is that over estimates (positive values of dd) have slope τ\tau while underestimates increase in loss at a rate of 1−τ1-\tau. Note that absolute loss is a special case of asymmetric loss with τ=0.5\tau = 0.5

Comparing Regression Loss Functions

What are some differences between squared loss and absolute loss? First note that when ∣d∣<1|d| < 1 absolute loss is greater than squared loss. But note that when ∣d∣>1|d| > 1 MSE is greater and grows exponentially larger than absolute deviation. Knowing this, which model would be more resistant to high noise data when sample size is small? What would be real world example of when MSE would be more appropriate than mean absolute deviation?

Now consider asymmetric loss. When τ\tau is close to 11 over predictions receive large loss with almost no loss for under estimates. On the other hand when τ\tau is close to 00 the relationship is reversed.

Classification Loss

The loss functions discussed so far only make sense in a regression context. What would the general form for the loss of a binary classifier look like? If y^∈{0,1}\hat y \in \{0,1\} and y∈{0,1}y \in \{0, 1\} then what values could we have for dd with d=y−y^d = y - \hat y? d∈{−1,0,1}d \in \{-1,0,1\}. −1-1 would correspond to false positives, 11 would be a false negative, and 00 would be if the classification were correct. Therefore our loss function would look like this:

L(d)={αd=−1βd=10d=0\begin{align} L(d) = \left\{ \begin{array}{ll} \alpha & d = -1 \\ \beta & d = 1 \\ 0 & d= 0 \end{array} \right. \end{align}

Best solutions for Loss Functions

It can be shown that the best loss prediction given a squared loss function is E[y∣x]E[y|x].

The best prediction for absolute loss is Med(y∣x)Med(y|x).

For asymmetric loss, the best prediction is QY(1−τ∣x)Q_Y(1-\tau | x). Again, this is the median when τ=0.5\tau = 0.5.

For classification loss, the best prediction is

f∗(x)={1Pr(y∣x)>βα+β0otherwise\begin{align} f^*(x) = \left\{ \begin{array}{ll} 1 & Pr(y|x) > \frac{\beta}{\alpha + \beta} \\ 0 & \text{otherwise} \end{array} \right. \end{align}