Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis/3.3/Tests/StatisticCalculatorsTest.cs @ 5275

Last change on this file since 5275 was 5275, checked in by gkronber, 13 years ago

Merged changes from trunk to data analysis exploration branch and added fractional distance metric evaluator. #1142

File size: 4.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.IO;
23using System;
24using HeuristicLab.Random;
25using HeuristicLab.Common;
26using System.Collections.Generic;
27using System.Diagnostics;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.Problems.DataAnalysis.Symbolic;
30using Microsoft.VisualStudio.TestTools.UnitTesting;
31using System.Linq;
32using System.Globalization;
33using HeuristicLab.Problems.DataAnalysis.Regression.Symbolic;
34using HeuristicLab.Problems.DataAnalysis.Evaluators;
35namespace HeuristicLab.Problems.DataAnalysis.Tests {
36
37  [TestClass()]
38  public class StatisticCalculatorsTest {
39    private double[,] testData = new double[,] {
40     {5,1,1,1,2,1,3,1,1,2},
41     {5,4,4,5,7,10,3,2,1,2},
42     {3,1,1,1,2,2,3,1,1,2},
43     {6,8,8,1,3,4,3,7,1,2},
44     {4,1,1,3,2,1,3,1,1,2},
45     {8,10,10,8,7,10,9,7,1,4},           
46     {1,1,1,1,2,10,3,1,1,2},             
47     {2,1,2,1,2,1,3,1,1,2},                 
48     {2,1,1,1,2,1,1,1,5,2},                 
49     {4,2,1,1,2,1,2,1,1,2},                   
50     {1,1,1,1,1,1,3,1,1,2},   
51     {2,1,1,1,2,1,2,1,1,2},                   
52     {5,3,3,3,2,3,4,4,1,4},                         
53     {8,7,5,10,7,9,5,5,4,4},         
54     {7,4,6,4,6,1,4,3,1,4},                         
55     {4,1,1,1,2,1,2,1,1,2},     
56     {4,1,1,1,2,1,3,1,1,2},     
57     {10,7,7,6,4,10,4,1,2,4}, 
58     {6,1,1,1,2,1,3,1,1,2},     
59     {7,3,2,10,5,10,5,4,4,4},   
60     {10,5,5,3,6,7,7,10,1,4}
61      };
62
63    [TestMethod]
64    public void CalculateMeanAndVarianceTest() {
65      System.Random random = new System.Random(31415);
66
67      int n = testData.GetLength(0);
68      int cols = testData.GetLength(1);
69      {
70        for (int col = 0; col < cols; col++) {
71          double scale = random.NextDouble() * 1E7;
72          IEnumerable<double> x = from rows in Enumerable.Range(0, n)
73                                  select testData[rows, col] * scale;
74          double[] xs = x.ToArray();
75          double mean_alglib, variance_alglib;
76          mean_alglib = variance_alglib = 0.0;
77          double tmp = 0;
78
79          alglib.basestat.samplemoments(xs, n, ref mean_alglib, ref variance_alglib, ref tmp, ref tmp);
80
81          var calculator = new OnlineMeanAndVarianceCalculator();
82          for (int i = 0; i < n; i++) {
83            calculator.Add(xs[i]);
84          }
85          double mean = calculator.Mean;
86          double variance = calculator.Variance;
87
88          Assert.AreEqual(mean_alglib, mean, 1E-6 * scale);
89          Assert.AreEqual(variance_alglib, variance, 1E-6 * scale);
90        }
91      }
92    }
93
94    [TestMethod]
95    public void CalculatePearsonsRSquaredTest() {
96      System.Random random = new System.Random(31415);
97      int n = testData.GetLength(0);
98      int cols = testData.GetLength(1);
99      for (int c1 = 0; c1 < cols; c1++) {
100        for (int c2 = c1 + 1; c2 < cols; c2++) {
101          {
102            double c1Scale = random.NextDouble() * 1E7;
103            double c2Scale = random.NextDouble() * 1E7;
104            IEnumerable<double> x = from rows in Enumerable.Range(0, n)
105                                    select testData[rows, c1] * c1Scale;
106            IEnumerable<double> y = from rows in Enumerable.Range(0, n)
107                                    select testData[rows, c2] * c2Scale;
108            double[] xs = x.ToArray();
109            double[] ys = y.ToArray();
110            double r2_alglib = alglib.pearsoncorr2(xs, ys, n);
111            r2_alglib *= r2_alglib;
112
113            var r2Calculator = new OnlinePearsonsRSquaredEvaluator();
114            for (int i = 0; i < n; i++) {
115              r2Calculator.Add(xs[i], ys[i]);
116            }
117            double r2 = r2Calculator.RSquared;
118
119            Assert.AreEqual(r2_alglib, r2, 1E-6 * Math.Max(c1Scale, c2Scale));
120          }
121        }
122      }
123    }
124  }
125}
Note: See TracBrowser for help on using the repository browser.