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.Diagnostics;
|
---|
26 | using System.Linq;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 |
|
---|
29 | namespace 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) {
|
---|
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) * Math.Abs(1.0 / targetEnum.Current);
|
---|
40 | }
|
---|
41 | if (targetEnum.MoveNext() | predEnum.MoveNext())
|
---|
42 | throw new ArgumentException("target and pred have different 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 | // sign(res) * abs(1 / target)
|
---|
53 | var res = targetEnum.Current - predEnum.Current;
|
---|
54 | if (res > 0) yield return 1.0 / Math.Abs(targetEnum.Current);
|
---|
55 | else if (res < 0) yield return -1.0 / Math.Abs(targetEnum.Current);
|
---|
56 | else yield return 0.0;
|
---|
57 | }
|
---|
58 | if (targetEnum.MoveNext() | predEnum.MoveNext())
|
---|
59 | throw new ArgumentException("target and pred have different lengths");
|
---|
60 | }
|
---|
61 |
|
---|
62 | // targetArr and predArr are not changed by LineSearch
|
---|
63 | public double LineSearch(double[] targetArr, double[] predArr, int[] idx, int startIdx, int endIdx) {
|
---|
64 | if (targetArr.Length != predArr.Length)
|
---|
65 | throw new ArgumentException("target and pred have different lengths");
|
---|
66 |
|
---|
67 | // line search for relative error
|
---|
68 | // weighted median (weight = 1/target)
|
---|
69 | int nRows = endIdx - startIdx + 1; // startIdx and endIdx are inclusive
|
---|
70 | if (nRows == 1) return targetArr[idx[startIdx]] - predArr[idx[startIdx]]; // res
|
---|
71 | else if (nRows == 2) {
|
---|
72 | // weighted average of two residuals
|
---|
73 | var w0 = Math.Abs(1.0 / targetArr[idx[startIdx]]);
|
---|
74 | var w1 = Math.Abs(1.0 / targetArr[idx[endIdx]]);
|
---|
75 | if (w0 > w1) {
|
---|
76 | return targetArr[idx[startIdx]] - predArr[idx[startIdx]];
|
---|
77 | } else if (w0 < w1) {
|
---|
78 | return targetArr[idx[endIdx]] - predArr[idx[endIdx]];
|
---|
79 | } else {
|
---|
80 | // same weight -> return average of both residuals
|
---|
81 | return ((targetArr[idx[startIdx]] - predArr[idx[startIdx]]) + (targetArr[idx[endIdx]] - predArr[idx[endIdx]])) / 2;
|
---|
82 | }
|
---|
83 | } else {
|
---|
84 | // create an array of key-value pairs to be sorted (instead of using Array.Sort(res, weights))
|
---|
85 | var res_w = new KeyValuePair<double, double>[nRows];
|
---|
86 | var totalWeight = 0.0;
|
---|
87 | for (int i = startIdx; i <= endIdx; i++) {
|
---|
88 | int row = idx[i];
|
---|
89 | var res = targetArr[row] - predArr[row];
|
---|
90 | var w = Math.Abs(1.0 / targetArr[row]);
|
---|
91 | res_w[i - startIdx] = new KeyValuePair<double, double>(res, w);
|
---|
92 | totalWeight += w;
|
---|
93 | }
|
---|
94 | // TODO: improve efficiency (find median without sort)
|
---|
95 | res_w.StableSort((a, b) => Math.Sign(a.Key - b.Key));
|
---|
96 |
|
---|
97 | int k = 0;
|
---|
98 | double sum = totalWeight - res_w[k].Value; // total - first weight
|
---|
99 | while (sum > totalWeight / 2) {
|
---|
100 | k++;
|
---|
101 | sum -= res_w[k].Value;
|
---|
102 | }
|
---|
103 | return res_w[k].Key;
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | public override string ToString() {
|
---|
108 | return "Relative error loss";
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|