1  #region License Information


2  /* HeuristicLab


3  * Copyright (C) 20022011 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 


22  using System;


23  using System.Collections.Generic;


24  using System.Linq;


25  using HeuristicLab.Common;


26  using HeuristicLab.Problems.DataAnalysis;


27  using Microsoft.VisualStudio.TestTools.UnitTesting;


28  namespace HeuristicLab.Problems.DataAnalysis_3_4.Tests {


29 


30  [TestClass()]


31  public class StatisticCalculatorsTest {


32  private double[,] testData = new double[,] {


33  {5,1,1,1,2,1,3,1,1,2},


34  {5,4,4,5,7,10,3,2,1,2},


35  {3,1,1,1,2,2,3,1,1,2},


36  {6,8,8,1,3,4,3,7,1,2},


37  {4,1,1,3,2,1,3,1,1,2},


38  {8,10,10,8,7,10,9,7,1,4},


39  {1,1,1,1,2,10,3,1,1,2},


40  {2,1,2,1,2,1,3,1,1,2},


41  {2,1,1,1,2,1,1,1,5,2},


42  {4,2,1,1,2,1,2,1,1,2},


43  {1,1,1,1,1,1,3,1,1,2},


44  {2,1,1,1,2,1,2,1,1,2},


45  {5,3,3,3,2,3,4,4,1,4},


46  {8,7,5,10,7,9,5,5,4,4},


47  {7,4,6,4,6,1,4,3,1,4},


48  {4,1,1,1,2,1,2,1,1,2},


49  {4,1,1,1,2,1,3,1,1,2},


50  {10,7,7,6,4,10,4,1,2,4},


51  {6,1,1,1,2,1,3,1,1,2},


52  {7,3,2,10,5,10,5,4,4,4},


53  {10,5,5,3,6,7,7,10,1,4}


54  };


55 


56  [TestMethod]


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  public void CalculatePearsonsRSquaredTest() {


89  System.Random random = new System.Random(31415);


90  int n = testData.GetLength(0);


91  int cols = testData.GetLength(1);


92  for (int c1 = 0; c1 < cols; c1++) {


93  for (int c2 = c1 + 1; c2 < cols; c2++) {


94  {


95  double c1Scale = random.NextDouble() * 1E7;


96  double c2Scale = random.NextDouble() * 1E7;


97  IEnumerable<double> x = from rows in Enumerable.Range(0, n)


98  select testData[rows, c1] * c1Scale;


99  IEnumerable<double> y = from rows in Enumerable.Range(0, n)


100  select testData[rows, c2] * c2Scale;


101  double[] xs = x.ToArray();


102  double[] ys = y.ToArray();


103  double r2_alglib = alglib.pearsoncorrelation(xs, ys, n);


104  r2_alglib *= r2_alglib;


105 


106  var r2Calculator = new OnlinePearsonsRSquaredCalculator();


107  for (int i = 0; i < n; i++) {


108  r2Calculator.Add(xs[i], ys[i]);


109  }


110  double r2 = r2Calculator.RSquared;


111 


112  Assert.IsTrue(r2_alglib.IsAlmost(r2));


113  }


114  }


115  }


116  }


117  [TestMethod]


118  public void CalculatePearsonsRSquaredOfConstantTest() {


119  System.Random random = new System.Random(31415);


120  int n = 12;


121  int cols = testData.GetLength(1);


122  for (int c1 = 0; c1 < cols; c1++) {


123  double c1Scale = random.NextDouble() * 1E7;


124  IEnumerable<double> x = from rows in Enumerable.Range(0, n)


125  select testData[rows, c1] * c1Scale;


126  IEnumerable<double> y = (new List<double>() { 150494407424305.47 })


127  .Concat(Enumerable.Repeat(150494407424305.47, n  1));


128  double[] xs = x.ToArray();


129  double[] ys = y.ToArray();


130  double r2_alglib = alglib.pearsoncorrelation(xs, ys, n);


131  r2_alglib *= r2_alglib;


132 


133  var r2Calculator = new OnlinePearsonsRSquaredCalculator();


134  for (int i = 0; i < n; i++) {


135  r2Calculator.Add(xs[i], ys[i]);


136  }


137  double r2 = r2Calculator.RSquared;


138 


139  Assert.AreEqual(r2_alglib.ToString(), r2.ToString());


140  }


141  }


142 


143  [TestMethod]


144  public void CalculateDirectionalSymmetryTest() {


145  // delta: +0.01, +1, 0.01, 2, 0.01, 1, +0.01, +2


146  var original = new double[]


147  {


148  0,


149  0.01,


150  1.01,


151  1,


152  1,


153  1.01,


154  2.01,


155  2,


156  0


157  };


158  // delta to original(t1): +1, +0, 1, 0, 1, +0.01, +0.01, +2


159  var estimated = new double[]


160  {


161  1,


162  1,


163  0.01,


164  0.01,


165  1,


166  1,


167  1.02,


168  2.02,


169  0


170  };


171 


172  // onestep forecast


173  var startValues = original;


174  var actualContinuations = from x in original.Skip(1)


175  select Enumerable.Repeat(x, 1);


176  var predictedContinuations = from x in estimated.Skip(1)


177  select Enumerable.Repeat(x, 1);


178  double expected = 0.5; // half of the predicted deltas are correct


179  OnlineCalculatorError errorState;


180  double actual = OnlineDirectionalSymmetryCalculator.Calculate(startValues, actualContinuations, predictedContinuations, out errorState);


181  Assert.AreEqual(expected, actual, 1E9);


182  }


183  [TestMethod]


184  public void CalculateWeightedDirectionalSymmetryTest() {


185  var original = new double[] { 0, 0.01, 1.01, 1, 1, 1.01, 2.01, 2, 0 }; // +0.01, +1, 0.01, 2, 0.01, 1, +0.01, +2


186  var estimated = new double[] { 1, 2, 2, 1, 1, 0, 0.01, 0.02, 2.02 }; // delta to original: +2, +1.99, 0.01, 0, +1, 1.02, +2.01, +4.02


187  // onestep forecast


188  var startValues = original;


189  var actualContinuations = from x in original.Skip(1)


190  select Enumerable.Repeat(x, 1);


191  var predictedContinuations = from x in estimated.Skip(1)


192  select Enumerable.Repeat(x, 1);


193  // absolute errors = 1.99, 0.99, 0, 2, 1.01, 2.02, 2.02, 2.02


194  // sum of absolute errors for correctly predicted deltas = 2.97


195  // sum of absolute errors for incorrectly predicted deltas = 3.03


196  double expected = 5.03 / 7.02;


197  OnlineCalculatorError errorState;


198  double actual = OnlineWeightedDirectionalSymmetryCalculator.Calculate(startValues, actualContinuations, predictedContinuations, out errorState);


199  Assert.AreEqual(expected, actual, 1E9);


200  }


201  [TestMethod]


202  public void CalculateTheilsUTest() {


203  var original = new double[] { 0, 0.01, 1.01, 1, 1, 1.01, 2.01, 2, 0 };


204  var estimated = new double[] { 1, 1.01, 0.01, 2, 0, 0.01, 1.01, 3, 1 };


205  // onestep forecast


206  var startValues = original;


207  var actualContinuations = from x in original.Skip(1)


208  select Enumerable.Repeat(x, 1);


209  var predictedContinuations = from x in estimated.Skip(1)


210  select Enumerable.Repeat(x, 1);


211  // Sum of squared errors of model y(t+1) = y(t) = 10.0004


212  // Sum of squared errors of predicted values = 8


213  double expected = Math.Sqrt(8 / 10.0004);


214  OnlineCalculatorError errorState;


215  double actual = OnlineTheilsUStatisticCalculator.Calculate(startValues, actualContinuations, predictedContinuations, out errorState);


216  Assert.AreEqual(expected, actual, 1E9);


217  }


218  [TestMethod]


219  public void CalculateAccuracyTest() {


220  var original = new double[] { 1, 1, 0, 0 };


221  var estimated = new double[] { 1, 0, 1, 0 };


222  double expected = 0.5;


223  OnlineCalculatorError errorState;


224  double actual = OnlineAccuracyCalculator.Calculate(original, estimated, out errorState);


225  Assert.AreEqual(expected, actual, 1E9);


226  }


227 


228  [TestMethod]


229  public void CalculateMeanAbsolutePercentageErrorTest() {


230  var original = new double[] { 1, 2, 3, 1, 5 };


231  var estimated = new double[] { 2, 1, 3, 1, 0 };


232  double expected = 0.5;


233  OnlineCalculatorError errorState;


234  double actual = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(original, estimated, out errorState);


235  Assert.AreEqual(expected, actual, 1E9);


236  Assert.AreEqual(OnlineCalculatorError.None, errorState);


237 


238  // if the original contains zero values the result is not defined


239  var original2 = new double[] { 1, 2, 0, 0, 0 };


240  OnlineMeanAbsolutePercentageErrorCalculator.Calculate(original2, estimated, out errorState);


241  Assert.AreEqual(OnlineCalculatorError.InvalidValueAdded, errorState);


242  }


243  }


244  }

