Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GBT-trunkintegration/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/LossFunctions/RelativeErrorLoss.cs @ 12607

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

#2261: also use line search function for the initial estimation f0, changed logistic regression loss function to match description in GBM paper, comments and code improvements

File size: 5.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  // relative error loss is a special case of weighted absolute error loss with weights = (1/target)
31  public class RelativeErrorLoss : ILossFunction {
32    public double GetLoss(IEnumerable<double> target, IEnumerable<double> pred, IEnumerable<double> weight) {
33      var targetEnum = target.GetEnumerator();
34      var predEnum = pred.GetEnumerator();
35      var weightEnum = weight.GetEnumerator();
36
37      double s = 0;
38      while (targetEnum.MoveNext() & predEnum.MoveNext() & weightEnum.MoveNext()) {
39        double res = targetEnum.Current - predEnum.Current;
40        s += weightEnum.Current * Math.Abs(res) * Math.Abs(1.0 / targetEnum.Current);
41      }
42      if (targetEnum.MoveNext() | predEnum.MoveNext() | weightEnum.MoveNext())
43        throw new ArgumentException("target, pred and weight have different lengths");
44
45      return s;
46    }
47
48    public IEnumerable<double> GetLossGradient(IEnumerable<double> target, IEnumerable<double> pred, IEnumerable<double> weight) {
49      var targetEnum = target.GetEnumerator();
50      var predEnum = pred.GetEnumerator();
51      var weightEnum = weight.GetEnumerator();
52
53      while (targetEnum.MoveNext() & predEnum.MoveNext() & weightEnum.MoveNext()) {
54        // weight * sign(res) * abs(1 / target)
55        var res = targetEnum.Current - predEnum.Current;
56        if (res > 0) yield return weightEnum.Current * 1.0 / Math.Abs(targetEnum.Current);
57        else if (res < 0) yield return -weightEnum.Current * 1.0 / Math.Abs(targetEnum.Current);
58        else yield return 0.0;
59      }
60      if (targetEnum.MoveNext() | predEnum.MoveNext() | weightEnum.MoveNext())
61        throw new ArgumentException("target, pred and weight have different lengths");
62    }
63
64    public LineSearchFunc GetLineSearchFunc(IEnumerable<double> target, IEnumerable<double> pred, IEnumerable<double> weight) {
65      var targetArr = target.ToArray();
66      var predArr = pred.ToArray();
67      var weightArr = weight.ToArray();
68      Debug.Assert(weightArr.All(w => w.IsAlmost(1.0)));
69
70      if (targetArr.Length != predArr.Length || predArr.Length != weightArr.Length)
71        throw new ArgumentException("target, pred and weight have different lengths");
72
73      // line search for relative error
74      // weighted median (weight = 1/target)
75      LineSearchFunc lineSearch = (idx, startIdx, endIdx) => {
76        // weighted median calculation
77        int nRows = endIdx - startIdx + 1; // startIdx and endIdx are inclusive
78        if (nRows == 1) return targetArr[idx[startIdx]] - predArr[idx[startIdx]]; // res
79        else if (nRows == 2) {
80          // weighted average of two residuals
81          var w0 = weightArr[idx[startIdx]] * Math.Abs(1.0 / targetArr[idx[startIdx]]);
82          var w1 = weightArr[idx[endIdx]] * Math.Abs(1.0 / targetArr[idx[endIdx]]);
83          if (w0 > w1) {
84            return targetArr[idx[startIdx]] - predArr[idx[startIdx]];
85          } else if (w0 < w1) {
86            return targetArr[idx[endIdx]] - predArr[idx[endIdx]];
87          } else {
88            // same weight -> return average of both residuals
89            return ((targetArr[idx[startIdx]] - predArr[idx[startIdx]]) + (targetArr[idx[endIdx]] - predArr[idx[endIdx]])) / 2;
90          }
91        } else {
92          // create an array of key-value pairs to be sorted (instead of using Array.Sort(res, weights))
93          var res_w = new KeyValuePair<double, double>[nRows];
94          var totalWeight = 0.0;
95          for (int i = startIdx; i <= endIdx; i++) {
96            int row = idx[i];
97            var res = targetArr[row] - predArr[row];
98            var w = weightArr[row] * Math.Abs(1.0 / targetArr[row]);
99            res_w[i - startIdx] = new KeyValuePair<double, double>(res, w);
100            totalWeight += w;
101          }
102          res_w.StableSort((a, b) => Math.Sign(a.Key - b.Key));
103
104          int k = 0;
105          double sum = totalWeight - res_w[k].Value; // total - first weight
106          while (sum > totalWeight / 2) {
107            k++;
108            sum -= res_w[k].Value;
109          }
110          return res_w[k].Key;
111        }
112      };
113      return lineSearch;
114    }
115
116    public override string ToString() {
117      return "Relative error loss";
118    }
119  }
120}
Note: See TracBrowser for help on using the repository browser.