Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Breadcrumbs/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis-3.4/StatisticCalculatorsTest.cs @ 12015

Last change on this file since 12015 was 11594, checked in by jkarder, 10 years ago

#2116: merged r10041-r11593 from trunk into branch

File size: 8.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using Microsoft.VisualStudio.TestTools.UnitTesting;
26namespace HeuristicLab.Problems.DataAnalysis.Tests {
27
28  [TestClass()]
29  public class StatisticCalculatorsTest {
30    private double[,] testData = new double[,] {
31     {5,1,1,1,2,1,3,1,1,2},
32     {5,4,4,5,7,10,3,2,1,2},
33     {3,1,1,1,2,2,3,1,1,2},
34     {6,8,8,1,3,4,3,7,1,2},
35     {4,1,1,3,2,1,3,1,1,2},
36     {8,10,10,8,7,10,9,7,1,4},           
37     {1,1,1,1,2,10,3,1,1,2},             
38     {2,1,2,1,2,1,3,1,1,2},                 
39     {2,1,1,1,2,1,1,1,5,2},                 
40     {4,2,1,1,2,1,2,1,1,2},                   
41     {1,1,1,1,1,1,3,1,1,2},   
42     {2,1,1,1,2,1,2,1,1,2},                   
43     {5,3,3,3,2,3,4,4,1,4},                         
44     {8,7,5,10,7,9,5,5,4,4},         
45     {7,4,6,4,6,1,4,3,1,4},                         
46     {4,1,1,1,2,1,2,1,1,2},     
47     {4,1,1,1,2,1,3,1,1,2},     
48     {10,7,7,6,4,10,4,1,2,4}, 
49     {6,1,1,1,2,1,3,1,1,2},     
50     {7,3,2,10,5,10,5,4,4,4},   
51     {10,5,5,3,6,7,7,10,1,4}
52      };
53
54    [TestMethod]
55    [TestCategory("Problems.DataAnalysis")]
56    [TestProperty("Time", "short")]
57    public void CalculateMeanAndVarianceTest() {
58      System.Random random = new System.Random(31415);
59
60      int n = testData.GetLength(0);
61      int cols = testData.GetLength(1);
62      {
63        for (int col = 0; col < cols; col++) {
64          double scale = random.NextDouble();
65          IEnumerable<double> x = from rows in Enumerable.Range(0, n)
66                                  select testData[rows, col] * scale;
67          double[] xs = x.ToArray();
68          double mean_alglib, variance_alglib;
69          mean_alglib = variance_alglib = 0.0;
70          double tmp = 0;
71
72          alglib.samplemoments(xs, n, out  mean_alglib, out variance_alglib, out tmp, out tmp);
73
74          var calculator = new OnlineMeanAndVarianceCalculator();
75          for (int i = 0; i < n; i++) {
76            calculator.Add(xs[i]);
77          }
78          double mean = calculator.Mean;
79          double variance = calculator.Variance;
80
81          Assert.IsTrue(mean_alglib.IsAlmost(mean));
82          Assert.IsTrue(variance_alglib.IsAlmost(variance));
83        }
84      }
85    }
86
87    [TestMethod]
88    [TestCategory("Problems.DataAnalysis")]
89    [TestProperty("Time", "short")]
90    public void CalculatePearsonsRSquaredTest() {
91      System.Random random = new System.Random(31415);
92      int n = testData.GetLength(0);
93      int cols = testData.GetLength(1);
94      for (int c1 = 0; c1 < cols; c1++) {
95        for (int c2 = c1 + 1; c2 < cols; c2++) {
96          {
97            double c1Scale = random.NextDouble() * 1E7;
98            double c2Scale = random.NextDouble() * 1E7;
99            IEnumerable<double> x = from rows in Enumerable.Range(0, n)
100                                    select testData[rows, c1] * c1Scale;
101            IEnumerable<double> y = from rows in Enumerable.Range(0, n)
102                                    select testData[rows, c2] * c2Scale;
103            double[] xs = x.ToArray();
104            double[] ys = y.ToArray();
105            double r2_alglib = alglib.pearsoncorrelation(xs, ys, n);
106            r2_alglib *= r2_alglib;
107
108            var r2Calculator = new OnlinePearsonsRSquaredCalculator();
109            for (int i = 0; i < n; i++) {
110              r2Calculator.Add(xs[i], ys[i]);
111            }
112            double r2 = r2Calculator.RSquared;
113
114            Assert.IsTrue(r2_alglib.IsAlmost(r2));
115          }
116        }
117      }
118    }
119
120    [TestMethod]
121    [TestCategory("Problems.DataAnalysis")]
122    [TestProperty("Time", "short")]
123    public void CalculatePearsonsRSquaredOfConstantTest() {
124      System.Random random = new System.Random(31415);
125      int n = 12;
126      int cols = testData.GetLength(1);
127      for (int c1 = 0; c1 < cols; c1++) {
128        double c1Scale = random.NextDouble() * 1E7;
129        IEnumerable<double> x = from rows in Enumerable.Range(0, n)
130                                select testData[rows, c1] * c1Scale;
131        IEnumerable<double> y = (new List<double>() { 150494407424305.47 })
132          .Concat(Enumerable.Repeat(150494407424305.47, n - 1));
133        double[] xs = x.ToArray();
134        double[] ys = y.ToArray();
135        double r2_alglib = alglib.pearsoncorrelation(xs, ys, n);
136        r2_alglib *= r2_alglib;
137
138        var r2Calculator = new OnlinePearsonsRSquaredCalculator();
139        for (int i = 0; i < n; i++) {
140          r2Calculator.Add(xs[i], ys[i]);
141        }
142        double r2 = r2Calculator.RSquared;
143
144        Assert.AreEqual(r2_alglib.ToString(), r2.ToString());
145      }
146    }
147
148    [TestMethod]
149    [TestCategory("Problems.DataAnalysis")]
150    [TestProperty("Time", "short")]
151    public void CalculateHoeffdingsDTest() {
152      OnlineCalculatorError error;
153      // direct perfect dependency
154      var xs = new double[] { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 };
155      var ys = new double[] { 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 };
156      var d = HoeffdingsDependenceCalculator.CalculateHoeffdings(xs, ys, out error);
157      Assert.AreEqual(error, OnlineCalculatorError.None);
158      Assert.AreEqual(d, 1.0, 1E-5);
159
160      // perfect negative dependency
161      ys = xs.Select(x => -x).ToArray();
162      d = HoeffdingsDependenceCalculator.CalculateHoeffdings(xs, ys, out error);
163      Assert.AreEqual(error, OnlineCalculatorError.None);
164      Assert.AreEqual(d, 1.0, 1E-5);
165
166      // ties
167      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 };
168      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 };
169      d = HoeffdingsDependenceCalculator.CalculateHoeffdings(xs, ys, out error);
170      Assert.AreEqual(error, OnlineCalculatorError.None);
171      Assert.AreEqual(d, 0.6783, 1E-5);
172
173      // ties
174      xs = new double[] { 1.0, 1.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 6.0, 6.0 };
175      ys = xs.Select(x => x * x).ToArray();
176      d = HoeffdingsDependenceCalculator.CalculateHoeffdings(xs, ys, out error);
177      Assert.AreEqual(error, OnlineCalculatorError.None);
178      Assert.AreEqual(d, 0.75, 1E-5);
179
180      // degenerate
181      xs = new double[] { 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 };
182      ys = new double[] { 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0 };
183      d = HoeffdingsDependenceCalculator.CalculateHoeffdings(xs, ys, out error);
184      Assert.AreEqual(error, OnlineCalculatorError.None);
185      Assert.AreEqual(d, -0.3516, 1E-4);
186
187
188      var normal = new HeuristicLab.Random.NormalDistributedRandom(new HeuristicLab.Random.MersenneTwister(31415), 0, 1);
189
190      xs = Enumerable.Range(0, 1000).Select(i => normal.NextDouble()).ToArray();
191      ys = Enumerable.Range(0, 1000).Select(i => normal.NextDouble()).ToArray();
192
193      // independent
194      d = HoeffdingsDependenceCalculator.CalculateHoeffdings(xs, ys, out error);
195      Assert.AreEqual(error, OnlineCalculatorError.None);
196      Assert.AreEqual(d, -0.00023, 1E-5);
197
198
199      xs = Enumerable.Range(0, 1000).Select(i => normal.NextDouble()).ToArray();
200      ys = xs.Select(x => x * x).ToArray();
201
202      d = HoeffdingsDependenceCalculator.CalculateHoeffdings(xs, ys, out error);
203      Assert.AreEqual(error, OnlineCalculatorError.None);
204      Assert.AreEqual(d, 0.25071, 1E-5);
205
206      // symmetric?
207      d = HoeffdingsDependenceCalculator.CalculateHoeffdings(ys, xs, out error);
208      Assert.AreEqual(error, OnlineCalculatorError.None);
209      Assert.AreEqual(d, 0.25071, 1E-5);
210
211    }
212  }
213}
Note: See TracBrowser for help on using the repository browser.