[4122] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[5445] | 3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[4122] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Problems.DataAnalysis.Evaluators;
|
---|
[4722] | 26 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
[4122] | 27 | namespace HeuristicLab.Problems.DataAnalysis.Tests {
|
---|
| 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 |
|
---|
[4477] | 55 | [TestMethod]
|
---|
[4122] | 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++) {
|
---|
| 63 | double scale = random.NextDouble() * 1E7;
|
---|
| 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 |
|
---|
[4980] | 71 | alglib.samplemoments(xs, n, out mean_alglib, out variance_alglib, out tmp, out tmp);
|
---|
[4122] | 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 |
|
---|
| 80 | Assert.AreEqual(mean_alglib, mean, 1E-6 * scale);
|
---|
| 81 | Assert.AreEqual(variance_alglib, variance, 1E-6 * scale);
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[4477] | 86 | [TestMethod]
|
---|
[4122] | 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();
|
---|
[4980] | 102 | double r2_alglib = alglib.pearsoncorrelation(xs, ys, n);
|
---|
[4122] | 103 | r2_alglib *= r2_alglib;
|
---|
| 104 |
|
---|
| 105 | var r2Calculator = new OnlinePearsonsRSquaredEvaluator();
|
---|
| 106 | for (int i = 0; i < n; i++) {
|
---|
| 107 | r2Calculator.Add(xs[i], ys[i]);
|
---|
| 108 | }
|
---|
| 109 | double r2 = r2Calculator.RSquared;
|
---|
| 110 |
|
---|
| 111 | Assert.AreEqual(r2_alglib, r2, 1E-6 * Math.Max(c1Scale, c2Scale));
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 | }
|
---|