1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2018 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 HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
32 | // loss function for quantile regression
|
---|
33 | // Generalized Boosted Models - A Guide To The gbm Package, Greg Ridgeway, August 2007, page 11
|
---|
34 | [StorableClass]
|
---|
35 | [Item("QuantileRegressionloss", "Loss function for quantile regression")]
|
---|
36 | public sealed class QuantileRegressionLoss : ParameterizedNamedItem, ILossFunction {
|
---|
37 | public IFixedValueParameter<PercentValue> AlphaParameter {
|
---|
38 | get { return (IFixedValueParameter<PercentValue>)Parameters["Alpha"]; }
|
---|
39 | }
|
---|
40 |
|
---|
41 | public double Alpha {
|
---|
42 | get { return AlphaParameter.Value.Value; }
|
---|
43 | set {
|
---|
44 | if (value <= 0.0 || value >= 1.0) throw new ArgumentException("Valid values for alpha: 0 < alpha < 1");
|
---|
45 | AlphaParameter.Value.Value = value;
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | public QuantileRegressionLoss()
|
---|
50 | : base("QuantileRegressionLoss", "Loss function for quantile regression") {
|
---|
51 | Parameters.Add(new FixedValueParameter<PercentValue>("Alpha", new PercentValue(0.9)));
|
---|
52 | }
|
---|
53 |
|
---|
54 | public double GetLoss(IEnumerable<double> target, IEnumerable<double> pred) {
|
---|
55 | var targetEnum = target.GetEnumerator();
|
---|
56 | var predEnum = pred.GetEnumerator();
|
---|
57 | var alpha = Alpha;
|
---|
58 | double leftSum = 0;
|
---|
59 | double rightsum = 0;
|
---|
60 | while (targetEnum.MoveNext() & predEnum.MoveNext()) {
|
---|
61 | double res = targetEnum.Current - predEnum.Current;
|
---|
62 | if (res > 0) leftSum += res;
|
---|
63 | else rightsum += -res;
|
---|
64 | }
|
---|
65 | if (targetEnum.MoveNext() | predEnum.MoveNext())
|
---|
66 | throw new ArgumentException("target and pred have differing lengths");
|
---|
67 |
|
---|
68 | return alpha * leftSum + (1 - alpha) * rightsum;
|
---|
69 | }
|
---|
70 |
|
---|
71 | public IEnumerable<double> GetLossGradient(IEnumerable<double> target, IEnumerable<double> pred) {
|
---|
72 | var targetEnum = target.GetEnumerator();
|
---|
73 | var predEnum = pred.GetEnumerator();
|
---|
74 | var alpha = AlphaParameter.Value.Value;
|
---|
75 |
|
---|
76 | while (targetEnum.MoveNext() & predEnum.MoveNext()) {
|
---|
77 | var res = targetEnum.Current - predEnum.Current;
|
---|
78 | if (res > 0) yield return alpha;
|
---|
79 | else if (res < 0) yield return -(1.0 - alpha);
|
---|
80 | else yield return 0.0;
|
---|
81 | }
|
---|
82 | if (targetEnum.MoveNext() | predEnum.MoveNext())
|
---|
83 | throw new ArgumentException("target and pred have differing lengths");
|
---|
84 | }
|
---|
85 |
|
---|
86 | // targetArr and predArr are not changed by LineSearch
|
---|
87 | public double LineSearch(double[] targetArr, double[] predArr, int[] idx, int startIdx, int endIdx) {
|
---|
88 | if (targetArr.Length != predArr.Length)
|
---|
89 | throw new ArgumentException("target and pred have differing lengths");
|
---|
90 |
|
---|
91 | // Quantile() is allocating an array anyway
|
---|
92 | // It would be possible to pre-allocated an array for the residuals if Quantile() would allow specification of a sub-range
|
---|
93 | int nRows = endIdx - startIdx + 1;
|
---|
94 | var res = new double[nRows];
|
---|
95 | for (int i = startIdx; i <= endIdx; i++) {
|
---|
96 | var row = idx[i];
|
---|
97 | res[i - startIdx] = targetArr[row] - predArr[row];
|
---|
98 | }
|
---|
99 | return res.Quantile(Alpha);
|
---|
100 | }
|
---|
101 |
|
---|
102 | #region item implementation
|
---|
103 | [StorableConstructor]
|
---|
104 | private QuantileRegressionLoss(bool deserializing) : base(deserializing) { }
|
---|
105 |
|
---|
106 | private QuantileRegressionLoss(QuantileRegressionLoss original, Cloner cloner)
|
---|
107 | : base(original, cloner) {
|
---|
108 | }
|
---|
109 |
|
---|
110 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
111 | return new QuantileRegressionLoss(this, cloner);
|
---|
112 | }
|
---|
113 | #endregion
|
---|
114 |
|
---|
115 | public override bool CanChangeName { get { return false; } }
|
---|
116 | public override bool CanChangeDescription { get { return false; } }
|
---|
117 | }
|
---|
118 | }
|
---|