Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/LossFunctions/RelativeErrorLoss.cs @ 15762

Last change on this file since 15762 was 15584, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers on stable

File size: 5.0 KB
RevLine 
[12590]1#region License Information
2/* HeuristicLab
[15584]3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[12590]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;
[12374]24using System.Collections.Generic;
25using HeuristicLab.Common;
[13184]26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[12374]28
[12590]29namespace HeuristicLab.Algorithms.DataAnalysis {
[12607]30  // relative error loss is a special case of weighted absolute error loss with weights = (1/target)
[13184]31  [StorableClass]
32  [Item("Relative error loss", "")]
33  public sealed class RelativeErrorLoss : Item, ILossFunction {
34    public RelativeErrorLoss() { }
35
[12696]36    public double GetLoss(IEnumerable<double> target, IEnumerable<double> pred) {
[12374]37      var targetEnum = target.GetEnumerator();
38      var predEnum = pred.GetEnumerator();
39
40      double s = 0;
[12696]41      while (targetEnum.MoveNext() & predEnum.MoveNext()) {
[12374]42        double res = targetEnum.Current - predEnum.Current;
[12696]43        s += Math.Abs(res) * Math.Abs(1.0 / targetEnum.Current);
[12374]44      }
[12696]45      if (targetEnum.MoveNext() | predEnum.MoveNext())
46        throw new ArgumentException("target and pred have different lengths");
[12374]47
48      return s;
49    }
50
[12696]51    public IEnumerable<double> GetLossGradient(IEnumerable<double> target, IEnumerable<double> pred) {
[12374]52      var targetEnum = target.GetEnumerator();
53      var predEnum = pred.GetEnumerator();
54
[12696]55      while (targetEnum.MoveNext() & predEnum.MoveNext()) {
56        // sign(res) * abs(1 / target)
[12374]57        var res = targetEnum.Current - predEnum.Current;
[12696]58        if (res > 0) yield return 1.0 / Math.Abs(targetEnum.Current);
59        else if (res < 0) yield return -1.0 / Math.Abs(targetEnum.Current);
[12374]60        else yield return 0.0;
61      }
[12696]62      if (targetEnum.MoveNext() | predEnum.MoveNext())
63        throw new ArgumentException("target and pred have different lengths");
[12374]64    }
65
[12697]66    // targetArr and predArr are not changed by LineSearch
67    public double LineSearch(double[] targetArr, double[] predArr, int[] idx, int startIdx, int endIdx) {
[12696]68      if (targetArr.Length != predArr.Length)
69        throw new ArgumentException("target and pred have different lengths");
[12374]70
[12590]71      // line search for relative error
[12597]72      // weighted median (weight = 1/target)
[12697]73      int nRows = endIdx - startIdx + 1; // startIdx and endIdx are inclusive
74      if (nRows == 1) return targetArr[idx[startIdx]] - predArr[idx[startIdx]]; // res
75      else if (nRows == 2) {
76        // weighted average of two residuals
77        var w0 = Math.Abs(1.0 / targetArr[idx[startIdx]]);
78        var w1 = Math.Abs(1.0 / targetArr[idx[endIdx]]);
79        if (w0 > w1) {
80          return targetArr[idx[startIdx]] - predArr[idx[startIdx]];
81        } else if (w0 < w1) {
82          return targetArr[idx[endIdx]] - predArr[idx[endIdx]];
[12374]83        } else {
[12697]84          // same weight -> return average of both residuals
85          return ((targetArr[idx[startIdx]] - predArr[idx[startIdx]]) + (targetArr[idx[endIdx]] - predArr[idx[endIdx]])) / 2;
86        }
87      } else {
88        // create an array of key-value pairs to be sorted (instead of using Array.Sort(res, weights))
89        var res_w = new KeyValuePair<double, double>[nRows];
90        var totalWeight = 0.0;
91        for (int i = startIdx; i <= endIdx; i++) {
92          int row = idx[i];
93          var res = targetArr[row] - predArr[row];
94          var w = Math.Abs(1.0 / targetArr[row]);
95          res_w[i - startIdx] = new KeyValuePair<double, double>(res, w);
96          totalWeight += w;
97        }
98        // TODO: improve efficiency (find median without sort)
99        res_w.StableSort((a, b) => Math.Sign(a.Key - b.Key));
[12374]100
[12697]101        int k = 0;
102        double sum = totalWeight - res_w[k].Value; // total - first weight
103        while (sum > totalWeight / 2) {
104          k++;
105          sum -= res_w[k].Value;
[12374]106        }
[12697]107        return res_w[k].Key;
108      }
[12374]109    }
110
[13184]111    #region item implementation
112    [StorableConstructor]
113    private RelativeErrorLoss(bool deserializing) : base(deserializing) { }
114
115    private RelativeErrorLoss(RelativeErrorLoss original, Cloner cloner) : base(original, cloner) { }
116
117    public override IDeepCloneable Clone(Cloner cloner) {
118      return new RelativeErrorLoss(this, cloner);
[12374]119    }
[13184]120    #endregion
[12374]121  }
122}
Note: See TracBrowser for help on using the repository browser.