Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/LossFunctions/AbsoluteErrorLoss.cs @ 12803

Last change on this file since 12803 was 12700, checked in by gkronber, 9 years ago

#2261: copied GBT implementation from branch to trunk

File size: 3.2 KB
Line 
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
23using System;
24using System.Collections.Generic;
25using System.Diagnostics;
26using System.Linq;
27using HeuristicLab.Common;
28
29namespace HeuristicLab.Algorithms.DataAnalysis {
30  // loss function for the weighted absolute error
31  public class AbsoluteErrorLoss : ILossFunction {
32    public double GetLoss(IEnumerable<double> target, IEnumerable<double> pred) {
33      var targetEnum = target.GetEnumerator();
34      var predEnum = pred.GetEnumerator();
35
36      double s = 0;
37      while (targetEnum.MoveNext() & predEnum.MoveNext()) {
38        double res = targetEnum.Current - predEnum.Current;
39        s += Math.Abs(res);  // |res|
40      }
41      if (targetEnum.MoveNext() | predEnum.MoveNext())
42        throw new ArgumentException("target and pred have differing lengths");
43
44      return s;
45    }
46
47    public IEnumerable<double> GetLossGradient(IEnumerable<double> target, IEnumerable<double> pred) {
48      var targetEnum = target.GetEnumerator();
49      var predEnum = pred.GetEnumerator();
50
51      while (targetEnum.MoveNext() & predEnum.MoveNext()) {
52        // dL(y, f(x)) / df(x) = sign(res)
53        var res = targetEnum.Current - predEnum.Current;
54        if (res > 0) yield return 1.0;
55        else if (res < 0) yield return -1.0;
56        else yield return 0.0;
57      }
58      if (targetEnum.MoveNext() | predEnum.MoveNext())
59        throw new ArgumentException("target and pred have differing lengths");
60    }
61
62    // return median of residuals
63    // targetArr and predArr are not changed by LineSearch
64    public double LineSearch(double[] targetArr, double[] predArr, int[] idx, int startIdx, int endIdx) {
65      if (targetArr.Length != predArr.Length)
66        throw new ArgumentException("target and pred have differing lengths");
67
68      // Median() is allocating an array anyway
69      // It would be possible to pre-allocated an array for the residuals if Median() would allow specification of a sub-range
70      int nRows = endIdx - startIdx + 1;
71      var res = new double[nRows];
72      for (int i = startIdx; i <= endIdx; i++) {
73        var row = idx[i];
74        res[i - startIdx] = targetArr[row] - predArr[row];
75      }
76      return res.Median(); // TODO: improve efficiency
77    }
78
79    public override string ToString() {
80      return "Absolute error loss";
81    }
82  }
83}
Note: See TracBrowser for help on using the repository browser.