1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | * and the BEACON Center for the Study of Evolution in Action.
|
---|
5 | *
|
---|
6 | * This file is part of HeuristicLab.
|
---|
7 | *
|
---|
8 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
9 | * it under the terms of the GNU General Public License as published by
|
---|
10 | * the Free Software Foundation, either version 3 of the License, or
|
---|
11 | * (at your option) any later version.
|
---|
12 | *
|
---|
13 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
16 | * GNU General Public License for more details.
|
---|
17 | *
|
---|
18 | * You should have received a copy of the GNU General Public License
|
---|
19 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
20 | */
|
---|
21 | #endregion
|
---|
22 |
|
---|
23 | using System;
|
---|
24 | using System.Collections.Generic;
|
---|
25 | using System.Linq;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
28 | public class SquaredErrorLoss : ILossFunction {
|
---|
29 | public double GetLoss(IEnumerable<double> target, IEnumerable<double> pred) {
|
---|
30 | var targetEnum = target.GetEnumerator();
|
---|
31 | var predEnum = pred.GetEnumerator();
|
---|
32 |
|
---|
33 | double s = 0;
|
---|
34 | while (targetEnum.MoveNext() & predEnum.MoveNext()) {
|
---|
35 | double res = targetEnum.Current - predEnum.Current;
|
---|
36 | s += res * res; // (res)^2
|
---|
37 | }
|
---|
38 | if (targetEnum.MoveNext() | predEnum.MoveNext())
|
---|
39 | throw new ArgumentException("target and pred have different lengths");
|
---|
40 |
|
---|
41 | return s;
|
---|
42 | }
|
---|
43 |
|
---|
44 | public IEnumerable<double> GetLossGradient(IEnumerable<double> target, IEnumerable<double> pred) {
|
---|
45 | var targetEnum = target.GetEnumerator();
|
---|
46 | var predEnum = pred.GetEnumerator();
|
---|
47 |
|
---|
48 | while (targetEnum.MoveNext() & predEnum.MoveNext()) {
|
---|
49 | yield return 2.0 * (targetEnum.Current - predEnum.Current); // dL(y, f(x)) / df(x) = 2 * res
|
---|
50 | }
|
---|
51 | if (targetEnum.MoveNext() | predEnum.MoveNext())
|
---|
52 | throw new ArgumentException("target and pred have different lengths");
|
---|
53 | }
|
---|
54 |
|
---|
55 | // targetArr and predArr are not changed by LineSearch
|
---|
56 | public double LineSearch(double[] targetArr, double[] predArr, int[] idx, int startIdx, int endIdx) {
|
---|
57 | if (targetArr.Length != predArr.Length)
|
---|
58 | throw new ArgumentException("target and pred have different lengths");
|
---|
59 |
|
---|
60 | // line search for squared error loss
|
---|
61 | // for a given partition of rows the optimal constant that should be added to the current prediction values is the average of the residuals
|
---|
62 | double s = 0.0;
|
---|
63 | int n = 0;
|
---|
64 | for (int i = startIdx; i <= endIdx; i++) {
|
---|
65 | int row = idx[i];
|
---|
66 | s += (targetArr[row] - predArr[row]);
|
---|
67 | n++;
|
---|
68 | }
|
---|
69 | return s / n;
|
---|
70 | }
|
---|
71 |
|
---|
72 | public override string ToString() {
|
---|
73 | return "Squared error loss";
|
---|
74 | }
|
---|
75 | }
|
---|
76 | }
|
---|