Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/29/11 19:52:22 (12 years ago)
Author:
gkronber
Message:

#1081: merged old changesets r6802, r6807:6808, r6811, r6974, r7058 from the trunk into the TimeSeries branch to bring it to version r7096.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis-3.4/StatisticCalculatorsTest.cs

    r7097 r7099  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    2324using System.Linq;
     
    139140      }
    140141    }
     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(t-1): +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      // one-step 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, 1E-9);
     182    }
     183
     184    [TestMethod]
     185    public void CalculateMultiStepDirectionalSymmetryTest() {
     186      // delta: +0.01, +1, -0.01, -2, -0.01, -1, +0.01, +2
     187      var original = new double[] { 0, 0.01, 1.01, 1, -1, -1.01, -2.01, -2, 0 };
     188      {
     189        var estimated = new double[][]
     190                          {
     191                            new double[] {0.01, 1.01, 1, -1, -1.01},
     192                            new double[] {1.01, 1, -1, -1.01, -2.01},
     193                            new double[] {1, -1, -1.01, -2.01, -2},
     194                            new double[] {-1, -1.01, -2.01, -2, 0}
     195                          };
     196
     197        // 5-step forecast
     198        var startValues = original.Take(4);
     199        var actualContinuations = from i in Enumerable.Range(1, original.Count() - 5)
     200                                  select original.Skip(i).Take(5);
     201        var predictedContinuations = estimated;
     202        double expected = 1; // predictions are 100% correct
     203        OnlineCalculatorError errorState;
     204        double actual = OnlineDirectionalSymmetryCalculator.Calculate(startValues, actualContinuations,
     205                                                                      predictedContinuations, out errorState);
     206        Assert.AreEqual(expected, actual, 1E-9);
     207      }
     208      {
     209        // only the direction is relevant
     210        var estimated = new double[][]
     211                          {
     212                            new double[] {0.01, 0.01, 0.01, -0.01, -0.01},  // start=0, original deltas: 0.01, 1.01, 1.00, -1.00, -1.01
     213                            new double[] {0.02, 0.02, 0.00, 0.00, 0.00}, // start=0.01, original deltas: 1.00, 0.90, -1.01, -1.02, -2.02,
     214                            new double[] { 1.00, 1.00, 1.00, 1.00, 1.00}, // start=1.01, original deltas: -0.01, -2.01, -2.02, -3.02, -3.01
     215                            new double[] { 0.90, 0.90, 0.90, 0.90, 0.90}  // start=1, original deltas: -2.00, -0.01, -3.01, -3.00, -1.00
     216                          };
     217
     218        // 5-step forecast
     219        var startValues = original.Take(4);
     220        var actualContinuations = from i in Enumerable.Range(1, original.Count() - 5)
     221                                  select original.Skip(i).Take(5);
     222        var predictedContinuations = estimated;
     223        double expected = 1; // half of the predicted deltas are correct
     224        OnlineCalculatorError errorState;
     225        double actual = OnlineDirectionalSymmetryCalculator.Calculate(startValues, actualContinuations,
     226                                                                      predictedContinuations, out errorState);
     227        Assert.AreEqual(expected, actual, 1E-9);
     228      }
     229      {
     230        // also check incorrectly predicted directions
     231        var estimated = new double[][]
     232                          {
     233                            new double[] {0.01, 0.01, 0.01, +0.01, +0.01},  // start=0, original deltas: 0.01, 1.01, 1.00, -1.00, -1.01
     234                            new double[] {0.02, 0.00, 0.02, 0.00, 0.00}, // start=0.01, original deltas: 1.00, 0.90, -1.01, -1.02, -2.02,
     235                            new double[] { 1.02, 1.00, 1.02, 1.00, 1.02}, // start=1.01, original deltas: -0.01, -2.01, -2.02, -3.02, -3.01
     236                            new double[] { 0.90, 0.90, 0.90, 0.90, 0.90}  // start=1, original deltas: -2.00, -0.01, -3.01, -3.00, -1.00
     237                          };
     238
     239        // 5-step forecast
     240        var startValues = original.Take(4);
     241        var actualContinuations = from i in Enumerable.Range(1, original.Count() - 5)
     242                                  select original.Skip(i).Take(5);
     243        var predictedContinuations = estimated;
     244        double expected = (20 - 7) / 20.0; // half of the predicted deltas are correct
     245        OnlineCalculatorError errorState;
     246        double actual = OnlineDirectionalSymmetryCalculator.Calculate(startValues, actualContinuations,
     247                                                                      predictedContinuations, out errorState);
     248        Assert.AreEqual(expected, actual, 1E-9);
     249      }
     250    }
     251
     252
     253    [TestMethod]
     254    public void CalculateWeightedDirectionalSymmetryTest() {
     255      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
     256      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
     257      // one-step forecast
     258      var startValues = original;
     259      var actualContinuations = from x in original.Skip(1)
     260                                select Enumerable.Repeat(x, 1);
     261      var predictedContinuations = from x in estimated.Skip(1)
     262                                   select Enumerable.Repeat(x, 1);
     263      // absolute errors = 1.99, 0.99, 0, 2, 1.01, 2.02, 2.02, 2.02     
     264      // sum of absolute errors for correctly predicted deltas = 2.97
     265      // sum of absolute errors for incorrectly predicted deltas = 3.03
     266      double expected = 5.03 / 7.02;
     267      OnlineCalculatorError errorState;
     268      double actual = OnlineWeightedDirectionalSymmetryCalculator.Calculate(startValues, actualContinuations, predictedContinuations, out errorState);
     269      Assert.AreEqual(expected, actual, 1E-9);
     270    }
     271
     272    [TestMethod]
     273    public void CalculateTheilsUTest() {
     274      var original = new double[] { 0, 0.01, 1.01, 1, -1, -1.01, -2.01, -2, 0 };
     275      var estimated = new double[] { 1, 1.01, 0.01, 2, 0, -0.01, -1.01, -3, 1 };
     276      // one-step forecast
     277      var startValues = original;
     278      var actualContinuations = from x in original.Skip(1)
     279                                select Enumerable.Repeat(x, 1);
     280      var predictedContinuations = from x in estimated.Skip(1)
     281                                   select Enumerable.Repeat(x, 1);
     282      // Sum of squared errors of model y(t+1) = y(t) = 10.0004
     283      // Sum of squared errors of predicted values = 8 
     284      double expected = Math.Sqrt(8 / 10.0004);
     285      OnlineCalculatorError errorState;
     286      double actual = OnlineTheilsUStatisticCalculator.Calculate(startValues, actualContinuations, predictedContinuations, out errorState);
     287      Assert.AreEqual(expected, actual, 1E-9);
     288    }
     289
     290    [TestMethod]
     291    public void CalculateMultiStepTheilsUTest() {
     292      var original = new double[] { 0, 0.01, 1.01, 1, -1, -1.01, -2.01, -2, 0 };
     293      {
     294        // prefect prediction
     295        var estimated = new double[][]
     296                          {
     297                            new double[] {0.01, 1.01, 1, -1, -1.01},
     298                            new double[] {1.01, 1, -1, -1.01, -2.01},
     299                            new double[] {1, -1, -1.01, -2.01, -2},
     300                            new double[] {-1, -1.01, -2.01, -2, 0}
     301                          };
     302        // 5-step forecast
     303        var startValues = original.Take(4);
     304        var actualContinuations = from i in Enumerable.Range(1, original.Count() - 5)
     305                                  select original.Skip(i).Take(5);
     306        var predictedContinuations = estimated;
     307
     308        double expected = 0;
     309        OnlineCalculatorError errorState;
     310        double actual = OnlineTheilsUStatisticCalculator.Calculate(startValues, actualContinuations,
     311                                                                   predictedContinuations, out errorState);
     312        Assert.AreEqual(expected, actual, 1E-9);
     313      }
     314      {
     315        // naive prediction
     316        var estimated = new double[][]
     317                          {
     318                            new double[] {0, 0, 0, 0, 0},
     319                            new double[] {0.01, 0.01, 0.01, 0.01, 0.01},
     320                            new double[] {1.01, 1.01, 1.01, 1.01, 1.01},
     321                            new double[] {1, 1, 1, 1, 1}
     322                          };
     323        // 5-step forecast
     324        var startValues = original.Take(4);
     325        var actualContinuations = from i in Enumerable.Range(1, original.Count() - 5)
     326                                  select original.Skip(i).Take(5);
     327        var predictedContinuations = estimated;
     328
     329        double expected = 1;
     330        OnlineCalculatorError errorState;
     331        double actual = OnlineTheilsUStatisticCalculator.Calculate(startValues, actualContinuations,
     332                                                                   predictedContinuations, out errorState);
     333        Assert.AreEqual(expected, actual, 1E-9);
     334      }
     335      {
     336        // realistic prediction
     337        var estimated = new double[][]
     338                          {
     339                            new double[] {0.005, 0.5, 0.5, -0.5, -0.5},    // start = 0
     340                            new double[] {0.60, 0.5, -0.5, -0.5, -1},   // start = 0.01
     341                            new double[] {-0.005, 0, 0, -0.5, -1},     // start = 1.01
     342                            new double[] {-0, 0, -0.5, -1, 0.5}      // start = 1
     343                          };
     344        // 5-step forecast
     345        var startValues = original.Take(4);
     346        var actualContinuations = from i in Enumerable.Range(1, original.Count() - 5)
     347                                  select original.Skip(i).Take(5);
     348        var predictedContinuations = estimated;
     349
     350        double expected = 0.47558387;
     351        OnlineCalculatorError errorState;
     352        double actual = OnlineTheilsUStatisticCalculator.Calculate(startValues, actualContinuations,
     353                                                                   predictedContinuations, out errorState);
     354        Assert.AreEqual(expected, actual, 1E-6);
     355      }
     356    }
     357
     358    [TestMethod]
     359    public void CalculateAccuracyTest() {
     360      var original = new double[] { 1, 1, 0, 0 };
     361      var estimated = new double[] { 1, 0, 1, 0 };
     362      double expected = 0.5;
     363      OnlineCalculatorError errorState;
     364      double actual = OnlineAccuracyCalculator.Calculate(original, estimated, out errorState);
     365      Assert.AreEqual(expected, actual, 1E-9);
     366    }
     367
     368    [TestMethod]
     369    public void CalculateMeanAbsolutePercentageErrorTest() {
     370      var original = new double[] { 1, 2, 3, 1, 5 };
     371      var estimated = new double[] { 2, 1, 3, 1, 0 };
     372      double expected = 0.5;
     373      OnlineCalculatorError errorState;
     374      double actual = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(original, estimated, out errorState);
     375      Assert.AreEqual(expected, actual, 1E-9);
     376      Assert.AreEqual(OnlineCalculatorError.None, errorState);
     377
     378      // if the original contains zero values the result is not defined
     379      var original2 = new double[] { 1, 2, 0, 0, 0 };
     380      OnlineMeanAbsolutePercentageErrorCalculator.Calculate(original2, estimated, out errorState);
     381      Assert.AreEqual(OnlineCalculatorError.InvalidValueAdded, errorState);
     382    }
    141383  }
    142384}
Note: See TracChangeset for help on using the changeset viewer.