[5574] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9456] | 3 | * Copyright (C) 2002-2013 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 |
|
---|
| 22 | using System.Collections.Generic;
|
---|
| 23 | using System.Linq;
|
---|
[6880] | 24 | using HeuristicLab.Common;
|
---|
[5574] | 25 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
[9764] | 26 | namespace HeuristicLab.Problems.DataAnalysis.Tests {
|
---|
[5574] | 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]
|
---|
[9785] | 55 | [TestCategory("Problems.DataAnalysis")]
|
---|
| 56 | [TestProperty("Time", "short")]
|
---|
[5574] | 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++) {
|
---|
[6880] | 64 | double scale = random.NextDouble();
|
---|
[5574] | 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 |
|
---|
[6880] | 81 | Assert.IsTrue(mean_alglib.IsAlmost(mean));
|
---|
| 82 | Assert.IsTrue(variance_alglib.IsAlmost(variance));
|
---|
[5574] | 83 | }
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | [TestMethod]
|
---|
[9785] | 88 | [TestCategory("Problems.DataAnalysis")]
|
---|
| 89 | [TestProperty("Time", "short")]
|
---|
[5574] | 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 |
|
---|
[5944] | 108 | var r2Calculator = new OnlinePearsonsRSquaredCalculator();
|
---|
[5574] | 109 | for (int i = 0; i < n; i++) {
|
---|
| 110 | r2Calculator.Add(xs[i], ys[i]);
|
---|
| 111 | }
|
---|
| 112 | double r2 = r2Calculator.RSquared;
|
---|
| 113 |
|
---|
[6880] | 114 | Assert.IsTrue(r2_alglib.IsAlmost(r2));
|
---|
[5574] | 115 | }
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
[9785] | 119 |
|
---|
[6184] | 120 | [TestMethod]
|
---|
[9785] | 121 | [TestCategory("Problems.DataAnalysis")]
|
---|
| 122 | [TestProperty("Time", "short")]
|
---|
[6184] | 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;
|
---|
[6738] | 131 | IEnumerable<double> y = (new List<double>() { 150494407424305.47 })
|
---|
[6184] | 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 | }
|
---|
[8355] | 147 |
|
---|
| 148 | [TestMethod]
|
---|
[9785] | 149 | [TestCategory("Problems.DataAnalysis")]
|
---|
| 150 | [TestProperty("Time", "short")]
|
---|
[8355] | 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 };
|
---|
[8834] | 156 | var d = HoeffdingsDependenceCalculator.CalculateHoeffdings(xs, ys, out error);
|
---|
[8355] | 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();
|
---|
[8834] | 162 | d = HoeffdingsDependenceCalculator.CalculateHoeffdings(xs, ys, out error);
|
---|
[8355] | 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 };
|
---|
[8834] | 169 | d = HoeffdingsDependenceCalculator.CalculateHoeffdings(xs, ys, out error);
|
---|
[8355] | 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();
|
---|
[8834] | 176 | d = HoeffdingsDependenceCalculator.CalculateHoeffdings(xs, ys, out error);
|
---|
[8355] | 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 };
|
---|
[8834] | 183 | d = HoeffdingsDependenceCalculator.CalculateHoeffdings(xs, ys, out error);
|
---|
[8355] | 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
|
---|
[8834] | 194 | d = HoeffdingsDependenceCalculator.CalculateHoeffdings(xs, ys, out error);
|
---|
[8355] | 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 |
|
---|
[8834] | 202 | d = HoeffdingsDependenceCalculator.CalculateHoeffdings(xs, ys, out error);
|
---|
[8355] | 203 | Assert.AreEqual(error, OnlineCalculatorError.None);
|
---|
| 204 | Assert.AreEqual(d, 0.25071, 1E-5);
|
---|
| 205 |
|
---|
| 206 | // symmetric?
|
---|
[8834] | 207 | d = HoeffdingsDependenceCalculator.CalculateHoeffdings(ys, xs, out error);
|
---|
[8355] | 208 | Assert.AreEqual(error, OnlineCalculatorError.None);
|
---|
| 209 | Assert.AreEqual(d, 0.25071, 1E-5);
|
---|
| 210 |
|
---|
| 211 | }
|
---|
[5574] | 212 | }
|
---|
| 213 | }
|
---|