Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysisCSVImport/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/HoeffdingsDependenceCalculator.cs @ 8842

Last change on this file since 8842 was 8842, checked in by sforsten, 12 years ago

#1942: merged r8690:8840 from trunk into branch

File size: 5.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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;
25using HeuristicLab.Data;
26
27namespace HeuristicLab.Problems.DataAnalysis {
28  public class HoeffdingsDependenceCalculator : IDependencyCalculator {
29
30    public DoubleRange Interval { get { return new DoubleRange(1.0, -0.5); } }
31
32    public string Name { get { return "Hoeffdings Dependence"; } }
33
34    public double Calculate(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {
35      return HoeffdingsDependenceCalculator.CalculateHoeffdings(originalValues, estimatedValues, out errorState);
36    }
37
38    public static double CalculateHoeffdings(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {
39      double d = HoeffD(originalValues, estimatedValues, out errorState);
40      if (errorState != OnlineCalculatorError.None) return double.NaN;
41      return d;
42    }
43
44    /// <summary>
45    /// computes Hoeffding's dependence coefficient.
46    /// Source: hoeffd.r from R package hmisc http://cran.r-project.org/web/packages/Hmisc/index.html
47    /// </summary>
48    private static double HoeffD(IEnumerable<double> xs, IEnumerable<double> ys, out OnlineCalculatorError errorState) {
49      double[] rx = TiedRank(xs);
50      double[] ry = TiedRank(ys);
51      if (rx.Length != ry.Length) throw new ArgumentException("The number of elements in xs and ys does not match");
52      double[] rxy = TiedRank(xs, ys);
53
54      int n = rx.Length;
55      double q = 0, r = 0, s = 0;
56      double scaling = 1.0 / (n * (n - 1));
57      for (int i = 0; i < n; i++) {
58        q += (rx[i] - 1) * (rx[i] - 2) * (ry[i] - 1) * (ry[i] - 2) * scaling;
59        r += (rx[i] - 2) * (ry[i] - 2) * rxy[i] * scaling;
60        s += rxy[i] * (rxy[i] - 1) * scaling;
61      }
62      errorState = OnlineCalculatorError.None;
63      // return 30.0 * (q - 2 * (n - 2) * r + (n - 2) * (n - 3) * s) / n / (n - 1) / (n - 2) / (n - 3) / (n - 4);
64      double t0 = q / (n - 2) / (n - 3) / (n - 4);
65      double t1 = 2 * r / (n - 3) / (n - 4);
66      double t2 = s / (n - 4);
67      return 30.0 * (t0 - t1 + t2);
68    }
69
70    private static double[] TiedRank(IEnumerable<double> xs) {
71      var xsArr = xs.ToArray();
72      var idx = Enumerable.Range(1, xsArr.Length).ToArray();
73      Array.Sort(xsArr, idx);
74      CRank(xsArr);
75      Array.Sort(idx, xsArr);
76      return xsArr;
77    }
78
79    /// <summary>
80    /// Calculates the joint rank with midranks for ties. Source: hoeffd.r from R package hmisc http://cran.r-project.org/web/packages/Hmisc/index.html
81    /// </summary>
82    /// <param name="xs"></param>
83    /// <param name="ys"></param>
84    /// <returns></returns>
85    private static double[] TiedRank(IEnumerable<double> xs, IEnumerable<double> ys) {
86      var xsArr = xs.ToArray();
87      var ysArr = ys.ToArray();
88      var r = new double[xsArr.Length];
89      int n = r.Length;
90      for (int i = 0; i < n; i++) {
91        var xi = xsArr[i];
92        var yi = ysArr[i];
93        double ri = 0.0;
94        for (int j = 0; j < n; j++) {
95          if (i != j) {
96            double cx;
97            if (xsArr[j] < xi) cx = 1.0;
98            else if (xsArr[j] > xi) cx = 0.0;
99            else cx = 0.5;  // eq
100            double cy;
101            if (ysArr[j] < yi) cy = 1.0;
102            else if (ysArr[j] > yi) cy = 0.0;
103            else cy = 0.5; // eq
104            ri = ri + cx * cy;
105          }
106        }
107        r[i] = ri;
108      }
109      return r;
110    }
111
112    /// <summary>
113    /// Calculates midranks. Source: Numerical Recipes in C. p 642
114    /// </summary>
115    /// <param name="w">Sorted array of elements, replaces the elements by their rank, including midranking of ties</param>
116    /// <returns></returns>
117    private static void CRank(double[] w) {
118      int i = 0;
119      int n = w.Length;
120      while (i < n - 1) {
121        if (w[i + 1] > w[i]) {    // w[i+1] must be larger or equal w[i] as w must be sorted
122          // not a tie
123          w[i] = i + 1;
124          i++;
125        } else {
126          int j;
127          for (j = i + 1; j < n && w[j] <= w[i]; j++) ; // how far does it go (<= effectively means == as w must be sorted, side-step equality for double values)
128          double rank = 1 + 0.5 * (i + j - 1);
129          int k;
130          for (k = i; k < j; k++) w[k] = rank; // set the rank for all tied entries
131          i = j;
132        }
133      }
134
135      if (i == n - 1) w[n - 1] = n;   // if the last element was not tied, this is its rank
136    }
137  }
138}
Note: See TracBrowser for help on using the repository browser.