Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/NormalizedGiniCalculator.cs @ 15055

Last change on this file since 15055 was 14186, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 2.5 KB
RevLine 
[6913]1#region License Information
2/* HeuristicLab
[14186]3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[6913]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;
25using HeuristicLab.Common;
26
27namespace HeuristicLab.Problems.DataAnalysis {
28  public class NormalizedGiniCalculator {
29
[6964]30    public static double Calculate(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {
31      var originalValuesArr = originalValues.ToArray();
32      var estimatedValuesArr = estimatedValues.ToArray();
33      if (originalValuesArr.Count() != estimatedValuesArr.Count()) {
34        throw new ArgumentException("Number of elements in originalValues and estimatedValues enumerations doesn't match.");
[6913]35      }
[6964]36      double oe = Gini(originalValuesArr, estimatedValuesArr, out errorState);
[6913]37      if (errorState != OnlineCalculatorError.None) return double.NaN;
38
[7023]39      return oe / (Gini(originalValuesArr, originalValuesArr, out errorState));
[6913]40    }
41
42    private static double Gini(IEnumerable<double> original, IEnumerable<double> estimated, out OnlineCalculatorError errorState) {
43      var pairs =
44        estimated.Zip(original, (e, o) => new { e, o })
45          .OrderByDescending(p => p.e);
46      if (pairs.Any()) errorState = OnlineCalculatorError.None;
47      else errorState = OnlineCalculatorError.InsufficientElementsAdded;
48      double giniSum = 0.0;
49      double sumOriginal = 0.0;
50      int n = 0;
51      foreach (var p in pairs) {
52        if (double.IsNaN(p.o) || double.IsNaN(p.e)) {
53          errorState = OnlineCalculatorError.InvalidValueAdded;
54          return double.NaN;
55        }
56        sumOriginal += p.o;
57        giniSum += sumOriginal;
58        n++;
59      }
60      giniSum /= sumOriginal;
61
62      return (giniSum - ((n + 1) / 2.0)) / n;
63    }
64  }
65}
Note: See TracBrowser for help on using the repository browser.