Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Tests/HeuristicLab.Algorithms.DataAnalysis-3.4/GaussianProcessModelTest.cs @ 8473

Last change on this file since 8473 was 8473, checked in by gkronber, 12 years ago

#1902 worked on GPR: added line chart, made parameters of mean and covariance functions readable, removed target variable scaling, moved noise hyperparameter for likelihood function to the end of the parameter list, added methods to calculate the predicted variance, removed limits for scale of covariance functions and introduced exception handling to catch non-spd or singular cov matrixes, implemented rational quadratic covariance function, added unit test case from GBML book (however it does not work as the book seemingly uses a noise-less likelihood function)

File size: 5.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Linq;
23using HeuristicLab.Algorithms.DataAnalysis;
24using HeuristicLab.Problems.Instances.DataAnalysis;
25using Microsoft.VisualStudio.TestTools.UnitTesting;
26
27namespace HeuristicLab.Algorithms.DataAnalysis_34.Tests {
28  [TestClass]
29
30  // reference values calculated with Rasmussen's GPML MATLAB package
31  public class GaussianProcessModelTest {
32    [TestMethod]
33    [DeploymentItem(@"HeuristicLab.Algorithms.DataAnalysis-3.4/co2.txt")]
34    public void GaussianProcessModelOutputTest() {
35      var provider = new RegressionCSVInstanceProvider();
36      var problemData = provider.ImportData("co2.txt");
37
38      var targetVariable = "interpolated";
39      var allowedInputVariables = new string[] { "decimal date" };
40      var rows = Enumerable.Range(0, 401);
41
42      var meanFunction = new MeanConst();
43      var covarianceFunction = new CovarianceSum();
44      covarianceFunction.Terms.Add(new CovarianceSEiso());
45      var prod = new CovarianceProd();
46      prod.Factors.Add(new CovarianceSEiso());
47      prod.Factors.Add(new CovariancePeriodic());
48      covarianceFunction.Terms.Add(prod);
49
50      {
51        var hyp = new double[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
52        var model = new GaussianProcessModel(problemData.Dataset, targetVariable, allowedInputVariables, rows, hyp,
53                                             meanFunction,
54                                             covarianceFunction);
55        Assert.AreEqual(4.3170e+004, model.NegativeLogLikelihood, 1);
56
57        var dHyp = model.GetHyperparameterGradients();
58        Assert.AreEqual(-248.7932, dHyp[0], 1E-2);
59        var dHypCovExpected = new double[] { -0.5550e4, -5.5533e4, -0.2511e4, -2.7625e4, -1.3033e4, 0.0289e4, -2.7625e4 };
60        AssertEqual(dHypCovExpected, dHyp.Skip(1).Take(7).ToArray(), 1);
61        Assert.AreEqual(-2.0171e+003, dHyp.Last(), 1);
62
63
64        var predTrain = model.GetEstimatedValues(problemData.Dataset, new int[] { 0, 400 }).ToArray();
65        Assert.AreEqual(310.5930, predTrain[0], 1e-3);
66        Assert.AreEqual(347.9993, predTrain[1], 1e-3);
67
68        var predTrainVar = model.GetEstimatedVariance(problemData.Dataset, problemData.TrainingIndices).ToArray();
69      }
70
71      {
72        var hyp = new double[] { 0.029973094285941, 0.455535210579926, 3.438647883940457, 1.464114485889487, 3.001788584487478, 3.815289323309630, 4.374914122810222, 3.001788584487478, 0.716427415979145 };
73        var model = new GaussianProcessModel(problemData.Dataset, targetVariable, allowedInputVariables, rows, hyp,
74                                             meanFunction,
75                                             covarianceFunction);
76        Assert.AreEqual(872.8448, model.NegativeLogLikelihood, 1e-3);
77
78        var dHyp = model.GetHyperparameterGradients();
79        Assert.AreEqual(-0.0046, dHyp[0], 1e-3);
80        var dHypCovExpected = new double[] { 0.2652, -0.2386, 0.1706, -0.1744, 0.0000, 0.0000, -0.1744 };
81        AssertEqual(dHypCovExpected, dHyp.Skip(1).Take(7).ToArray(), 1e-3);
82        Assert.AreEqual(0.8621, dHyp.Last(), 1e-3);
83
84        var predTrain = model.GetEstimatedValues(problemData.Dataset, new int[] { 0, 400 }).ToArray();
85        Assert.AreEqual(315.3692, predTrain[0], 1e-3);
86        Assert.AreEqual(356.6076, predTrain[1], 1e-3);
87      }
88
89      /*
90      {
91        // example from GPML book
92        var hyp = new double[] {
93          341.0, // mean 341 ppm
94          // SE iso (long term trend)
95          Math.Log(67.0 / 45.0), // length scale 67 years
96          Math.Log(Math.Sqrt(66)), // magnitude 66ppm
97                   
98          // product of SEiso and periodic
99          Math.Log(90.0 / 45.0), // decay-time 90 years
100          Math.Log(Math.Sqrt(2.4)), // magnitude 2.4ppm
101
102          Math.Log(1.3), // smoothness
103          Math.Log(1), // period 1 year
104          Math.Log(Math.Sqrt(2.4)), // magnitude 2.4ppm
105
106          // short term variation
107          Math.Log(1.2 / 45.0), // typical length 1.2 years
108          Math.Log(Math.Sqrt(0.66)), // magnitude 0.66ppm
109          Math.Log(0.78), // shape (very small)
110
111          // SEiso (correlated noise)
112          Math.Log(1.6 / 45.0 / 12.0), // 1.6 months
113          Math.Log(Math.Sqrt(0.18)), // amplitude of correlated noise 0.18ppm
114          Math.Log(Math.Sqrt(0.19)),  // theta11 0.19ppm noise
115          };
116
117        covarianceFunction.Terms.Add(new CovarianceRQiso());
118        covarianceFunction.Terms.Add(new CovarianceSEiso()); // correlated noise
119        var model = new GaussianProcessModel(problemData.Dataset, targetVariable, allowedInputVariables, Enumerable.Range(0, 545), hyp,
120                                             new MeanConst(),
121                                             covarianceFunction);
122        Assert.AreEqual(-108.5, model.NegativeLogLikelihood, 1);
123      }
124       */
125    }
126
127
128    private void AssertEqual(double[] expected, double[] actual, double delta = 1E-3) {
129      Assert.AreEqual(expected.Length, actual.Length);
130      for (int i = 0; i < expected.Length; i++)
131        Assert.AreEqual(expected[i], actual[i], delta);
132    }
133  }
134}
Note: See TracBrowser for help on using the repository browser.