Free cookie consent management tool by TermsFeed Policy Generator

Changeset 8355


Ignore:
Timestamp:
07/30/12 11:08:14 (12 years ago)
Author:
gkronber
Message:

#1292: fixed bugs in HoeffdingsDependenceCalculator, added test cases for HoeffdingsDependenceCalculator

Location:
trunk/sources
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/HoeffdingsDependenceCalculator.cs

    r7969 r8355  
    103103
    104104    /// <summary>
    105     /// Calculates midranks. Source: Numerical Recipes in C.
     105    /// Calculates midranks. Source: Numerical Recipes in C. p 642
    106106    /// </summary>
    107107    /// <param name="w">Sorted array of elements, replaces the elements by their rank, including midranking of ties</param>
     
    117117        } else {
    118118          int j;
    119           for (j = i + 1; j < n && w[j] <= w[i]; j++) ; // how far does it go (<= effectively means == as w must be sorted)
    120           double rank = 0.5 * (i + j - 1);
     119          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)
     120          double rank = 1 + 0.5 * (i + j - 1);
    121121          int k;
    122           for (k = i; k < j - 1; k++) w[k] = rank; // set the rank for all tied entries
     122          for (k = i; k < j; k++) w[k] = rank; // set the rank for all tied entries
    123123          i = j;
    124124        }
    125125      }
    126126
    127       if (i == n - 1) w[n - 1] = n - 1;   // if the last element was not tied, this is its rank
     127      if (i == n - 1) w[n - 1] = n;   // if the last element was not tied, this is its rank
    128128    }
    129129  }
  • trunk/sources/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis-3.4/StatisticCalculatorsTest.cs

    r7915 r8355  
    139139      }
    140140    }
     141
     142    [TestMethod]
     143    public void CalculateHoeffdingsDTest() {
     144      OnlineCalculatorError error;
     145      // direct perfect dependency
     146      var xs = new double[] { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 };
     147      var ys = new double[] { 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 };
     148      var d = HoeffdingsDependenceCalculator.Calculate(xs, ys, out error);
     149      Assert.AreEqual(error, OnlineCalculatorError.None);
     150      Assert.AreEqual(d, 1.0, 1E-5);
     151
     152      // perfect negative dependency
     153      ys = xs.Select(x => -x).ToArray();
     154      d = HoeffdingsDependenceCalculator.Calculate(xs, ys, out error);
     155      Assert.AreEqual(error, OnlineCalculatorError.None);
     156      Assert.AreEqual(d, 1.0, 1E-5);
     157
     158      // ties
     159      xs = new double[] { 1.0, 1.0, 2.0, 2.0, 3.0, 3.0, 4.0, 4.0, 5.0, 5.0, 5.0 };
     160      ys = new double[] { 2.0, 2.0, 3.0, 3.0, 4.0, 4.0, 5.0, 5.0, 6.0, 6.0, 6.0 };
     161      d = HoeffdingsDependenceCalculator.Calculate(xs, ys, out error);
     162      Assert.AreEqual(error, OnlineCalculatorError.None);
     163      Assert.AreEqual(d, 0.6783, 1E-5);
     164
     165      // ties
     166      xs = new double[] { 1.0, 1.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 6.0, 6.0 };
     167      ys = xs.Select(x => x * x).ToArray();
     168      d = HoeffdingsDependenceCalculator.Calculate(xs, ys, out error);
     169      Assert.AreEqual(error, OnlineCalculatorError.None);
     170      Assert.AreEqual(d, 0.75, 1E-5);
     171
     172      // degenerate
     173      xs = new double[] { 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 };
     174      ys = new double[] { 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0 };
     175      d = HoeffdingsDependenceCalculator.Calculate(xs, ys, out error);
     176      Assert.AreEqual(error, OnlineCalculatorError.None);
     177      Assert.AreEqual(d, -0.3516, 1E-4);
     178
     179
     180      var normal = new HeuristicLab.Random.NormalDistributedRandom(new HeuristicLab.Random.MersenneTwister(31415), 0, 1);
     181
     182      xs = Enumerable.Range(0, 1000).Select(i => normal.NextDouble()).ToArray();
     183      ys = Enumerable.Range(0, 1000).Select(i => normal.NextDouble()).ToArray();
     184
     185      // independent
     186      d = HoeffdingsDependenceCalculator.Calculate(xs, ys, out error);
     187      Assert.AreEqual(error, OnlineCalculatorError.None);
     188      Assert.AreEqual(d, -0.00023, 1E-5);
     189
     190
     191      xs = Enumerable.Range(0, 1000).Select(i => normal.NextDouble()).ToArray();
     192      ys = xs.Select(x => x * x).ToArray();
     193
     194      d = HoeffdingsDependenceCalculator.Calculate(xs, ys, out error);
     195      Assert.AreEqual(error, OnlineCalculatorError.None);
     196      Assert.AreEqual(d, 0.25071, 1E-5);
     197
     198      // symmetric?
     199      d = HoeffdingsDependenceCalculator.Calculate(ys, xs, out error);
     200      Assert.AreEqual(error, OnlineCalculatorError.None);
     201      Assert.AreEqual(d, 0.25071, 1E-5);
     202
     203    }
    141204  }
    142205}
Note: See TracChangeset for help on using the changeset viewer.