Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Problems.DataAnalysis/3.4/Tests/StatisticCalculatorsTest.cs @ 11316

Last change on this file since 11316 was 6760, checked in by epitzer, 13 years ago

#1530 integrate changes from trunk

File size: 5.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Problems.DataAnalysis;
25using Microsoft.VisualStudio.TestTools.UnitTesting;
26namespace HeuristicLab.Problems.DataAnalysis_3_4.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    public void CalculateMeanAndVarianceTest() {
56      System.Random random = new System.Random(31415);
57
58      int n = testData.GetLength(0);
59      int cols = testData.GetLength(1);
60      {
61        for (int col = 0; col < cols; col++) {
62          double scale = random.NextDouble() * 1E7;
63          IEnumerable<double> x = from rows in Enumerable.Range(0, n)
64                                  select testData[rows, col] * scale;
65          double[] xs = x.ToArray();
66          double mean_alglib, variance_alglib;
67          mean_alglib = variance_alglib = 0.0;
68          double tmp = 0;
69
70          alglib.samplemoments(xs, n, out  mean_alglib, out variance_alglib, out tmp, out tmp);
71
72          var calculator = new OnlineMeanAndVarianceCalculator();
73          for (int i = 0; i < n; i++) {
74            calculator.Add(xs[i]);
75          }
76          double mean = calculator.Mean;
77          double variance = calculator.Variance;
78
79          Assert.AreEqual(mean_alglib.ToString(), mean.ToString());
80          Assert.AreEqual(variance_alglib.ToString(), variance.ToString());
81        }
82      }
83    }
84
85    [TestMethod]
86    public void CalculatePearsonsRSquaredTest() {
87      System.Random random = new System.Random(31415);
88      int n = testData.GetLength(0);
89      int cols = testData.GetLength(1);
90      for (int c1 = 0; c1 < cols; c1++) {
91        for (int c2 = c1 + 1; c2 < cols; c2++) {
92          {
93            double c1Scale = random.NextDouble() * 1E7;
94            double c2Scale = random.NextDouble() * 1E7;
95            IEnumerable<double> x = from rows in Enumerable.Range(0, n)
96                                    select testData[rows, c1] * c1Scale;
97            IEnumerable<double> y = from rows in Enumerable.Range(0, n)
98                                    select testData[rows, c2] * c2Scale;
99            double[] xs = x.ToArray();
100            double[] ys = y.ToArray();
101            double r2_alglib = alglib.pearsoncorrelation(xs, ys, n);
102            r2_alglib *= r2_alglib;
103
104            var r2Calculator = new OnlinePearsonsRSquaredCalculator();
105            for (int i = 0; i < n; i++) {
106              r2Calculator.Add(xs[i], ys[i]);
107            }
108            double r2 = r2Calculator.RSquared;
109
110            Assert.AreEqual(r2_alglib.ToString(), r2.ToString());
111          }
112        }
113      }
114    }
115    [TestMethod]
116    public void CalculatePearsonsRSquaredOfConstantTest() {
117      System.Random random = new System.Random(31415);
118      int n = 12;
119      int cols = testData.GetLength(1);
120      for (int c1 = 0; c1 < cols; c1++) {
121        double c1Scale = random.NextDouble() * 1E7;
122        double c2Scale = 1.0;
123        IEnumerable<double> x = from rows in Enumerable.Range(0, n)
124                                select testData[rows, c1] * c1Scale;
125        IEnumerable<double> y = (new List<double>() { 150494407424305.47 })
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    }
141  }
142}
Note: See TracBrowser for help on using the repository browser.