The SquaredLoss loss function for regression L(x, y) = (x-y) 2 . This function is twice differentiable. @author Edward Raff
| 8 | * @author Edward Raff |
| 9 | */ |
| 10 | public class SquaredLoss implements LossR |
| 11 | { |
| 12 | |
| 13 | private static final long serialVersionUID = 130786305325167077L; |
| 14 | |
| 15 | /** |
| 16 | * Computes the SquaredLoss loss |
| 17 | * |
| 18 | * @param pred the predicted value |
| 19 | * @param y the true value |
| 20 | * @return the squared loss |
| 21 | */ |
| 22 | public static double loss(double pred, double y) |
| 23 | { |
| 24 | final double x = y - pred; |
| 25 | return x * x * 0.5; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Computes the first derivative of the squared loss |
| 30 | * |
| 31 | * @param pred the predicted value |
| 32 | * @param y the true value |
| 33 | * @return the first derivative of the squared loss |
| 34 | */ |
| 35 | public static double deriv(double pred, double y) |
| 36 | { |
| 37 | return (pred - y); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Computes the second derivative of the squared loss, which is always |
| 42 | * {@code 1} |
| 43 | * |
| 44 | * @param pred the predicted value |
| 45 | * @param y the true value |
| 46 | * @return the second derivative of the squared loss |
| 47 | */ |
| 48 | public static double deriv2(double pred, double y) |
| 49 | { |
| 50 | return 1; |
| 51 | } |
| 52 | |
| 53 | public static double regress(double score) |
| 54 | { |
| 55 | return score; |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public double getLoss(double pred, double y) |
| 60 | { |
| 61 | return loss(pred, y); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public double getDeriv(double pred, double y) |
| 66 | { |
| 67 | return deriv(pred, y); |
nothing calls this directly
no outgoing calls
no test coverage detected