Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3043-Regression-Instances-For-Scaling/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/NormalizedGiniCalculator.cs

Last change on this file was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 2.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25
26namespace HeuristicLab.Problems.DataAnalysis {
27  public class NormalizedGiniCalculator {
28
29    public static double Calculate(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {
30      var originalValuesArr = originalValues.ToArray();
31      var estimatedValuesArr = estimatedValues.ToArray();
32      if (originalValuesArr.Count() != estimatedValuesArr.Count()) {
33        throw new ArgumentException("Number of elements in originalValues and estimatedValues enumerations doesn't match.");
34      }
35      double oe = Gini(originalValuesArr, estimatedValuesArr, out errorState);
36      if (errorState != OnlineCalculatorError.None) return double.NaN;
37
38      return oe / (Gini(originalValuesArr, originalValuesArr, out errorState));
39    }
40
41    private static double Gini(IEnumerable<double> original, IEnumerable<double> estimated, out OnlineCalculatorError errorState) {
42      var pairs =
43        estimated.Zip(original, (e, o) => new { e, o })
44          .OrderByDescending(p => p.e);
45      if (pairs.Any()) errorState = OnlineCalculatorError.None;
46      else errorState = OnlineCalculatorError.InsufficientElementsAdded;
47      double giniSum = 0.0;
48      double sumOriginal = 0.0;
49      int n = 0;
50      foreach (var p in pairs) {
51        if (double.IsNaN(p.o) || double.IsNaN(p.e)) {
52          errorState = OnlineCalculatorError.InvalidValueAdded;
53          return double.NaN;
54        }
55        sumOriginal += p.o;
56        giniSum += sumOriginal;
57        n++;
58      }
59      giniSum /= sumOriginal;
60
61      return (giniSum - ((n + 1) / 2.0)) / n;
62    }
63  }
64}
Note: See TracBrowser for help on using the repository browser.