Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis-3.4/StatisticCalculatorsTest.cs @ 11813

Last change on this file since 11813 was 9363, checked in by spimming, 12 years ago

#1888:

  • Merged revisions from trunk
File size: 8.0 KB
RevLine 
[5574]1#region License Information
2/* HeuristicLab
[7259]3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[5574]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.Collections.Generic;
23using System.Linq;
[6880]24using HeuristicLab.Common;
[5944]25using HeuristicLab.Problems.DataAnalysis;
[5574]26using Microsoft.VisualStudio.TestTools.UnitTesting;
[7915]27namespace HeuristicLab.Problems.DataAnalysis_34.Tests {
[5574]28
29  [TestClass()]
30  public class StatisticCalculatorsTest {
31    private double[,] testData = new double[,] {
32     {5,1,1,1,2,1,3,1,1,2},
33     {5,4,4,5,7,10,3,2,1,2},
34     {3,1,1,1,2,2,3,1,1,2},
35     {6,8,8,1,3,4,3,7,1,2},
36     {4,1,1,3,2,1,3,1,1,2},
37     {8,10,10,8,7,10,9,7,1,4},           
38     {1,1,1,1,2,10,3,1,1,2},             
39     {2,1,2,1,2,1,3,1,1,2},                 
40     {2,1,1,1,2,1,1,1,5,2},                 
41     {4,2,1,1,2,1,2,1,1,2},                   
42     {1,1,1,1,1,1,3,1,1,2},   
43     {2,1,1,1,2,1,2,1,1,2},                   
44     {5,3,3,3,2,3,4,4,1,4},                         
45     {8,7,5,10,7,9,5,5,4,4},         
46     {7,4,6,4,6,1,4,3,1,4},                         
47     {4,1,1,1,2,1,2,1,1,2},     
48     {4,1,1,1,2,1,3,1,1,2},     
49     {10,7,7,6,4,10,4,1,2,4}, 
50     {6,1,1,1,2,1,3,1,1,2},     
51     {7,3,2,10,5,10,5,4,4,4},   
52     {10,5,5,3,6,7,7,10,1,4}
53      };
54
55    [TestMethod]
56    public void CalculateMeanAndVarianceTest() {
57      System.Random random = new System.Random(31415);
58
59      int n = testData.GetLength(0);
60      int cols = testData.GetLength(1);
61      {
62        for (int col = 0; col < cols; col++) {
[6880]63          double scale = random.NextDouble();
[5574]64          IEnumerable<double> x = from rows in Enumerable.Range(0, n)
65                                  select testData[rows, col] * scale;
66          double[] xs = x.ToArray();
67          double mean_alglib, variance_alglib;
68          mean_alglib = variance_alglib = 0.0;
69          double tmp = 0;
70
71          alglib.samplemoments(xs, n, out  mean_alglib, out variance_alglib, out tmp, out tmp);
72
73          var calculator = new OnlineMeanAndVarianceCalculator();
74          for (int i = 0; i < n; i++) {
75            calculator.Add(xs[i]);
76          }
77          double mean = calculator.Mean;
78          double variance = calculator.Variance;
79
[6880]80          Assert.IsTrue(mean_alglib.IsAlmost(mean));
81          Assert.IsTrue(variance_alglib.IsAlmost(variance));
[5574]82        }
83      }
84    }
85
86    [TestMethod]
87    public void CalculatePearsonsRSquaredTest() {
88      System.Random random = new System.Random(31415);
89      int n = testData.GetLength(0);
90      int cols = testData.GetLength(1);
91      for (int c1 = 0; c1 < cols; c1++) {
92        for (int c2 = c1 + 1; c2 < cols; c2++) {
93          {
94            double c1Scale = random.NextDouble() * 1E7;
95            double c2Scale = random.NextDouble() * 1E7;
96            IEnumerable<double> x = from rows in Enumerable.Range(0, n)
97                                    select testData[rows, c1] * c1Scale;
98            IEnumerable<double> y = from rows in Enumerable.Range(0, n)
99                                    select testData[rows, c2] * c2Scale;
100            double[] xs = x.ToArray();
101            double[] ys = y.ToArray();
102            double r2_alglib = alglib.pearsoncorrelation(xs, ys, n);
103            r2_alglib *= r2_alglib;
104
[5944]105            var r2Calculator = new OnlinePearsonsRSquaredCalculator();
[5574]106            for (int i = 0; i < n; i++) {
107              r2Calculator.Add(xs[i], ys[i]);
108            }
109            double r2 = r2Calculator.RSquared;
110
[6880]111            Assert.IsTrue(r2_alglib.IsAlmost(r2));
[5574]112          }
113        }
114      }
115    }
[6184]116    [TestMethod]
117    public void CalculatePearsonsRSquaredOfConstantTest() {
118      System.Random random = new System.Random(31415);
119      int n = 12;
120      int cols = testData.GetLength(1);
121      for (int c1 = 0; c1 < cols; c1++) {
122        double c1Scale = random.NextDouble() * 1E7;
123        IEnumerable<double> x = from rows in Enumerable.Range(0, n)
124                                select testData[rows, c1] * c1Scale;
[6738]125        IEnumerable<double> y = (new List<double>() { 150494407424305.47 })
[6184]126          .Concat(Enumerable.Repeat(150494407424305.47, n - 1));
127        double[] xs = x.ToArray();
128        double[] ys = y.ToArray();
129        double r2_alglib = alglib.pearsoncorrelation(xs, ys, n);
130        r2_alglib *= r2_alglib;
131
132        var r2Calculator = new OnlinePearsonsRSquaredCalculator();
133        for (int i = 0; i < n; i++) {
134          r2Calculator.Add(xs[i], ys[i]);
135        }
136        double r2 = r2Calculator.RSquared;
137
138        Assert.AreEqual(r2_alglib.ToString(), r2.ToString());
139      }
140    }
[9363]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.CalculateHoeffdings(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.CalculateHoeffdings(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.CalculateHoeffdings(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.CalculateHoeffdings(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.CalculateHoeffdings(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.CalculateHoeffdings(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.CalculateHoeffdings(xs, ys, out error);
195      Assert.AreEqual(error, OnlineCalculatorError.None);
196      Assert.AreEqual(d, 0.25071, 1E-5);
197
198      // symmetric?
199      d = HoeffdingsDependenceCalculator.CalculateHoeffdings(ys, xs, out error);
200      Assert.AreEqual(error, OnlineCalculatorError.None);
201      Assert.AreEqual(d, 0.25071, 1E-5);
202
203    }
[5574]204  }
205}
Note: See TracBrowser for help on using the repository browser.