Free cookie consent management tool by TermsFeed Policy Generator

Changeset 8473


Ignore:
Timestamp:
08/10/12 20:42:30 (12 years ago)
Author:
gkronber
Message:

#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)

Location:
trunk/sources
Files:
3 added
17 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis.Views/3.4/HeuristicLab.Algorithms.DataAnalysis.Views-3.4.csproj

    r8471 r8473  
    112112    <Reference Include="System.Drawing" />
    113113    <Reference Include="System.Windows.Forms" />
     114    <Reference Include="System.Windows.Forms.DataVisualization" />
    114115    <Reference Include="System.Xml.Linq" />
    115116    <Reference Include="System.Data.DataSetExtensions" />
     
    161162    </Compile>
    162163    <Compile Include="Plugin.cs" />
     164    <Compile Include="GaussianProcessRegressionSolutionLineChartView.cs">
     165      <SubType>UserControl</SubType>
     166    </Compile>
     167    <Compile Include="GaussianProcessRegressionSolutionLineChartView.Designer.cs">
     168      <DependentUpon>GaussianProcessRegressionSolutionLineChartView.cs</DependentUpon>
     169    </Compile>
    163170    <Compile Include="SupportVectorMachineModelSupportVectorsView.cs">
    164171      <SubType>UserControl</SubType>
     
    265272      <Project>{DF87C13E-A889-46FF-8153-66DCAA8C5674}</Project>
    266273      <Name>HeuristicLab.Problems.DataAnalysis-3.4</Name>
     274      <Private>False</Private>
     275    </ProjectReference>
     276    <ProjectReference Include="..\..\HeuristicLab.Visualization.ChartControlsExtensions\3.3\HeuristicLab.Visualization.ChartControlsExtensions-3.3.csproj">
     277      <Project>{315BDA09-3F4F-49B3-9790-B37CFC1C5750}</Project>
     278      <Name>HeuristicLab.Visualization.ChartControlsExtensions-3.3</Name>
    267279      <Private>False</Private>
    268280    </ProjectReference>
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis.Views/3.4/Plugin.cs.frame

    r8471 r8473  
    4444  [PluginDependency("HeuristicLab.Problems.DataAnalysis", "3.4")]
    4545  [PluginDependency("HeuristicLab.Problems.DataAnalysis.Views", "3.4")]
     46  [PluginDependency("HeuristicLab.Visualization.ChartControlsExtensions", "3.3")]
    4647  public class HeuristicLabAlgorithmsDataAnalysisViewsPlugin : PluginBase {
    4748  }
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceConst.cs

    r8464 r8473  
    3232    [Storable]
    3333    private double sf2;
     34    public double Scale { get { return sf2; } }
    3435
    3536    [StorableConstructor]
     
    5657
    5758    public void SetParameter(double[] hyp) {
    58       this.sf2 = Math.Min(1E6, Math.Exp(2 * hyp[0])); // upper limit for scale
     59      this.sf2 = Math.Exp(2 * hyp[0]);
    5960    }
    6061    public void SetData(double[,] x) {
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceNoise.cs

    r8464 r8473  
    3232    [Storable]
    3333    private double sf2;
     34    public double Scale { get { return sf2; } }
    3435
    3536    [StorableConstructor]
     
    5657
    5758    public void SetParameter(double[] hyp) {
    58       this.sf2 = Math.Min(1E6, Math.Exp(2 * hyp[0])); // upper limit for scale
     59      this.sf2 = Math.Exp(2 * hyp[0]);
    5960    }
    6061    public void SetData(double[,] x) {
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovariancePeriodic.cs

    r8463 r8473  
    3535    [Storable]
    3636    private double sf2;
     37    public double Scale { get { return sf2; } }
    3738    [Storable]
    3839    private double l;
     40    public double Length { get { return l; } }
    3941    [Storable]
    4042    private double p;
     43    public double Period { get { return p; } }
    4144
    4245    private bool symmetric;
     
    7477      this.p = Math.Exp(hyp[1]);
    7578      this.sf2 = Math.Exp(2 * hyp[2]);
    76 
    77       sf2 = Math.Min(10E6, sf2); // upper limit for the scale
     79      // sf2 = Math.Min(10E6, sf2); // upper limit for the scale
    7880
    7981      sd = null;
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceSEard.cs

    r8455 r8473  
    3636    [Storable]
    3737    private double sf2;
     38    public double Scale { get { return sf2; } }
     39
    3840    [Storable]
    3941    private double[] l;
     42    public double[] Length {
     43      get {
     44        if (l == null) return new double[0];
     45        var copy = new double[l.Length];
     46        Array.Copy(l, copy, copy.Length);
     47        return copy;
     48      }
     49    }
    4050
    4151    private double[,] sd;
     
    7686      this.l = hyp.Take(hyp.Length - 1).Select(Math.Exp).ToArray();
    7787      this.sf2 = Math.Exp(2 * hyp[hyp.Length - 1]);
    78       sf2 = Math.Min(10E6, sf2); // upper limit for the scale
     88      // sf2 = Math.Min(10E6, sf2); // upper limit for the scale
    7989
    8090      sd = null;
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceSEiso.cs

    r8463 r8473  
    3737    [Storable]
    3838    private double sf2;
     39    public double Scale { get { return sf2; } }
    3940    [Storable]
    4041    private double l;
     42    public double Length { get { return l; } }
    4143    [Storable]
    4244    private bool symmetric;
     
    8082    public void SetParameter(double[] hyp) {
    8183      this.l = Math.Exp(hyp[0]);
    82       this.sf2 = Math.Min(1E6, Math.Exp(2 * hyp[1])); // upper limit for scale
     84      this.sf2 = Math.Exp(2 * hyp[1]);
    8385      sd = null;
    8486    }
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/GaussianProcessModel.cs

    r8463 r8473  
    7474    [Storable]
    7575    private Scaling inputScaling;
    76     [Storable]
    77     private Scaling targetScaling;
    7876
    7977
     
    8583      this.covarianceFunction = cloner.Clone(original.covarianceFunction);
    8684      this.inputScaling = cloner.Clone(original.inputScaling);
    87       this.targetScaling = cloner.Clone(original.targetScaling);
    8885      this.negativeLogLikelihood = original.negativeLogLikelihood;
    8986      this.targetVariable = original.targetVariable;
     
    106103      this.allowedInputVariables = allowedInputVariables.ToArray();
    107104
    108       sqrSigmaNoise = Math.Exp(2.0 * hyp.First());
    109       sqrSigmaNoise = Math.Max(10E-6, sqrSigmaNoise); // lower limit for the noise level
    110105
    111106      int nVariables = this.allowedInputVariables.Length;
    112       this.meanFunction.SetParameter(hyp.Skip(1)
     107      this.meanFunction.SetParameter(hyp
    113108        .Take(this.meanFunction.GetNumberOfParameters(nVariables))
    114109        .ToArray());
    115       this.covarianceFunction.SetParameter(hyp.Skip(1 + this.meanFunction.GetNumberOfParameters(nVariables))
     110      this.covarianceFunction.SetParameter(hyp.Skip(this.meanFunction.GetNumberOfParameters(nVariables))
    116111        .Take(this.covarianceFunction.GetNumberOfParameters(nVariables))
    117112        .ToArray());
     113      sqrSigmaNoise = Math.Exp(2.0 * hyp.Last());
    118114
    119115      CalculateModel(ds, rows);
     
    123119      inputScaling = new Scaling(ds, allowedInputVariables, rows);
    124120      x = AlglibUtil.PrepareAndScaleInputMatrix(ds, allowedInputVariables, rows, inputScaling);
    125 
    126 
    127       targetScaling = new Scaling(ds, new string[] { targetVariable }, rows);
    128       var y = targetScaling.GetScaledValues(ds, targetVariable, rows);
     121      var y = ds.GetDoubleValues(targetVariable, rows);
    129122
    130123      int n = x.GetLength(0);
     
    149142
    150143      var res = alglib.trfac.spdmatrixcholesky(ref l, n, false);
    151       if (!res) throw new InvalidOperationException("Matrix is not positive semidefinite");
     144      if (!res) throw new ArgumentException("Matrix is not positive semidefinite");
    152145
    153146      // calculate sum of diagonal elements for likelihood
     
    198191      }
    199192
    200       return new double[] { noiseGradient }
    201         .Concat(meanGradients)
    202         .Concat(covGradients).ToArray();
     193      return
     194        meanGradients
     195        .Concat(covGradients)
     196        .Concat(new double[] { noiseGradient }).ToArray();
    203197    }
    204198
     
    231225      // var kss = new double[newN];
    232226      var Ks = new double[newN, n];
    233       double[,] sWKs = new double[n, newN];
     227      //double[,] sWKs = new double[n, newN];
    234228      // double[,] v;
    235229
     
    243237      var ms = meanFunction.GetMean(newX);
    244238      for (int i = 0; i < newN; i++) {
    245 
    246239        for (int j = 0; j < n; j++) {
    247240          Ks[i, j] = covarianceFunction.GetCovariance(j, i);
    248           sWKs[j, i] = Ks[i, j] / Math.Sqrt(sqrSigmaNoise);
     241          //sWKs[j, i] = Ks[i, j] / Math.Sqrt(sqrSigmaNoise);
    249242        }
    250243      }
     
    253246      // alglib.rmatrixsolvem(l, n, sWKs, newN, true, out info, out denseSolveRep, out v);
    254247
    255       double targetScaleMin, targetScaleMax;
    256       targetScaling.GetScalingParameters(targetVariable, out targetScaleMin, out targetScaleMax);
    257248      return Enumerable.Range(0, newN)
    258         .Select(i => ms[i] + Util.ScalarProd(Util.GetRow(Ks, i), alpha))
    259         .Select(m => m * (targetScaleMax - targetScaleMin) + targetScaleMin);
     249        .Select(i => ms[i] + Util.ScalarProd(Util.GetRow(Ks, i), alpha));
    260250      //for (int i = 0; i < newN; i++) {
    261251      //  // predMean[i] = ms[i] + prod(GetRow(Ks, i), alpha);
     
    265255
    266256    }
     257
     258    public IEnumerable<double> GetEstimatedVariance(Dataset dataset, IEnumerable<int> rows) {
     259      var newX = AlglibUtil.PrepareAndScaleInputMatrix(dataset, allowedInputVariables, rows, inputScaling);
     260      int newN = newX.GetLength(0);
     261      int n = x.GetLength(0);
     262
     263      var kss = new double[newN];
     264      double[,] sWKs = new double[n, newN];
     265
     266
     267      // for stddev
     268      covarianceFunction.SetData(newX);
     269      for (int i = 0; i < newN; i++)
     270        kss[i] = covarianceFunction.GetCovariance(i, i);
     271
     272      covarianceFunction.SetData(x, newX);
     273      for (int i = 0; i < n; i++) {
     274        for (int j = 0; j < newN; j++) {
     275          sWKs[i, j] = covarianceFunction.GetCovariance(i, j) / Math.Sqrt(sqrSigmaNoise);
     276        }
     277      }
     278
     279      // for stddev
     280      int info;
     281      alglib.densesolverreport denseSolveRep;
     282      double[,] v;
     283      double[,] lTrans = new double[l.GetLength(1), l.GetLength(0)];
     284      for (int i = 0; i < lTrans.GetLength(0); i++)
     285        for (int j = 0; j < lTrans.GetLength(1); j++)
     286          lTrans[i, j] = l[j, i];
     287      alglib.rmatrixsolvem(lTrans, n, sWKs, newN, true, out info, out denseSolveRep, out v); // not working!
     288      // alglib.spdmatrixcholeskysolvem(lTrans, n, true, sWKs, newN, out info, out denseSolveRep, out v);
     289
     290      for (int i = 0; i < newN; i++) {
     291        var sumV2 = Util.ScalarProd(Util.GetCol(v, i), Util.GetCol(v, i));
     292        yield return kss[i] - sumV2;
     293      }
     294    }
    267295  }
    268296}
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/GaussianProcessRegressionModelCreator.cs

    r8401 r8473  
    2020#endregion
    2121
     22using System;
    2223using System.Linq;
    2324using HeuristicLab.Common;
     
    6061
    6162    public override IOperation Apply() {
    62       var model = Create(ProblemData, Hyperparameter.ToArray(), MeanFunction, CovarianceFunction);
    63       ModelParameter.ActualValue = model;
    64       NegativeLogLikelihoodParameter.ActualValue = new DoubleValue(model.NegativeLogLikelihood);
    65       HyperparameterGradientsParameter.ActualValue = new RealVector(model.GetHyperparameterGradients());
     63      try {
     64        var model = Create(ProblemData, Hyperparameter.ToArray(), MeanFunction, CovarianceFunction);
     65        ModelParameter.ActualValue = model;
     66        NegativeLogLikelihoodParameter.ActualValue = new DoubleValue(model.NegativeLogLikelihood);
     67        HyperparameterGradientsParameter.ActualValue = new RealVector(model.GetHyperparameterGradients());
     68        return base.Apply();
     69      }
     70      catch (ArgumentException) { }
     71      catch (alglib.alglibexception) { }
     72      NegativeLogLikelihoodParameter.ActualValue = new DoubleValue(1E300);
     73      HyperparameterGradientsParameter.ActualValue = new RealVector(Hyperparameter.Count());
    6674      return base.Apply();
    6775    }
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/GaussianProcessRegressionSolution.cs

    r8371 r8473  
    2020#endregion
    2121
     22using System.Collections.Generic;
     23using System.Linq;
    2224using HeuristicLab.Common;
    2325using HeuristicLab.Core;
     
    5153      return new GaussianProcessRegressionSolution(this, cloner);
    5254    }
     55
     56    public IEnumerable<double> EstimatedVariance {
     57      get { return GetEstimatedVariance(Enumerable.Range(0, ProblemData.Dataset.Rows)); }
     58    }
     59    public IEnumerable<double> EstimatedTrainingVariance {
     60      get { return GetEstimatedVariance(ProblemData.TrainingIndices); }
     61    }
     62    public IEnumerable<double> EstimatedTestVariance {
     63      get { return GetEstimatedVariance(ProblemData.TestIndices); }
     64    }
     65
     66    public IEnumerable<double> GetEstimatedVariance(IEnumerable<int> rows) {
     67      return Model.GetEstimatedVariance(ProblemData.Dataset, rows);
     68    }
    5369  }
    5470}
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanConst.cs

    r8416 r8473  
    3232    [Storable]
    3333    private double c;
     34    public double Value { get { return c; } }
     35
    3436    public int GetNumberOfParameters(int numberOfVariables) {
    3537      return 1;
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanLinear.cs

    r8416 r8473  
    3131    [Storable]
    3232    private double[] alpha;
     33    public double[] Weights {
     34      get {
     35        if (alpha == null) return new double[0];
     36        var copy = new double[alpha.Length];
     37        Array.Copy(alpha, copy, copy.Length);
     38        return copy;
     39      }
     40    }
    3341    public int GetNumberOfParameters(int numberOfVariables) {
    3442      return numberOfVariables;
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/HeuristicLab.Algorithms.DataAnalysis-3.4.csproj

    r8471 r8473  
    120120    </Compile>
    121121    <Compile Include="FixedDataAnalysisAlgorithm.cs" />
     122    <Compile Include="GaussianProcess\CovarianceRQiso.cs" />
    122123    <Compile Include="GaussianProcess\CovarianceNoise.cs" />
    123124    <Compile Include="GaussianProcess\CovarianceConst.cs" />
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/Interfaces/IGaussianProcessModel.cs

    r8416 r8473  
    2020#endregion
    2121
     22using System.Collections.Generic;
    2223using HeuristicLab.Problems.DataAnalysis;
    2324
     
    3132    ICovarianceFunction CovarianceFunction { get; }
    3233    double[] GetHyperparameterGradients();
     34
     35    IEnumerable<double> GetEstimatedVariance(Dataset ds, IEnumerable<int> rows);
    3336  }
    3437}
  • trunk/sources/HeuristicLab.Algorithms.GradientDescent/3.3/LbfgsInitializer.cs

    r8401 r8473  
    8787      alglib.minlbfgs.minlbfgsstate state = new alglib.minlbfgs.minlbfgsstate();
    8888      if (ApproximateGradients.Value) {
    89         alglib.minlbfgs.minlbfgscreatef(n, Math.Min(n, 7), initialPoint, 1E-5, state);
     89        alglib.minlbfgs.minlbfgscreatef(n, Math.Min(n, 10), initialPoint, 1E-5, state);
    9090      } else {
    91         alglib.minlbfgs.minlbfgscreate(n, Math.Min(n, 7), initialPoint, state);
     91        alglib.minlbfgs.minlbfgscreate(n, Math.Min(n, 10), initialPoint, state);
    9292      }
    93       alglib.minlbfgs.minlbfgssetcond(state, 0, 0, 0, Iterations.Value);
     93      alglib.minlbfgs.minlbfgssetcond(state, 0.0, 0, 0, Iterations.Value);
    9494      alglib.minlbfgs.minlbfgssetxrep(state, true);
    9595
  • trunk/sources/HeuristicLab.Tests/HeuristicLab.Algorithms.DataAnalysis-3.4/GaussianProcessFunctionsTest.cs

    r8463 r8473  
    254254    }
    255255
     256    [TestMethod]
     257    public void CovRQIsoTest() {
     258      TestCovarianceFunction(new CovarianceRQiso(), 0,
     259        new double[,]
     260          {
     261{    0.6452,    0.6190,    0.8662,    0.6338,    0.7802,    0.5893,    0.7102,    0.7753,    0.7037,    0.8331},
     262{    0.6771,    0.7392,    0.7442,    0.6630,    0.7603,    0.6144,    0.7145,    0.8346,    0.7492,    0.8200},
     263{    0.7963,    0.7777,    0.8574,    0.7266,    0.6362,    0.8060,    0.8519,    0.7084,    0.7810,    0.8459},
     264{    0.7988,    0.6268,    0.8004,    0.7500,    0.5583,    0.6524,    0.8968,    0.8864,    0.8739,    0.8382},
     265{    0.8902,    0.6571,    0.7919,    0.9030,    0.5304,    0.5727,    0.8079,    0.9232,    0.9117,    0.8733},
     266{    0.8819,    0.7639,    0.7387,    0.8449,    0.5785,    0.6563,    0.8783,    0.9519,    0.9535,    0.8742},
     267{    0.6716,    0.7814,    0.7590,    0.6437,    0.8797,    0.6366,    0.7012,    0.6810,    0.7090,    0.8339},
     268{    0.6702,    0.6823,    0.8268,    0.6250,    0.7456,    0.6782,    0.7331,    0.6205,    0.6786,    0.8033},
     269{    0.6910,    0.7332,    0.8750,    0.6697,    0.8119,    0.6608,    0.7235,    0.7261,    0.7142,    0.8595},
     270{    0.6467,    0.7493,    0.6821,    0.6133,    0.6857,    0.6692,    0.7265,    0.8252,    0.7259,    0.7506},
     271          },
     272        new double[][,]
     273          {
     274            new double[,] {
     275{    0.4578,    0.4717,    0.2318,    0.4642,    0.3430,    0.4841,    0.4117,    0.3485,    0.4170,    0.2781},
     276{    0.4373,    0.3856,    0.3808,    0.4468,    0.3645,    0.4738,    0.4080,    0.2761,    0.3758,    0.2952},
     277{    0.3244,    0.3458,    0.2445,    0.3973,    0.4629,    0.3127,    0.2524,    0.4132,    0.3421,    0.2607},
     278{    0.3214,    0.4679,    0.3195,    0.3750,    0.4932,    0.4535,    0.1851,    0.2015,    0.2204,    0.2712},
     279{    0.1955,    0.4507,    0.3295,    0.1752,    0.4981,    0.4894,    0.3104,    0.1418,    0.1610,    0.2213},
     280{    0.2084,    0.3607,    0.3861,    0.2621,    0.4877,    0.4512,    0.2138,    0.0915,    0.0887,    0.2199},
     281{    0.4411,    0.3416,    0.3658,    0.4587,    0.2116,    0.4627,    0.4191,    0.4345,    0.4127,    0.2770},
     282{    0.4421,    0.4335,    0.2864,    0.4687,    0.3793,    0.4365,    0.3913,    0.4710,    0.4362,    0.3160},
     283{    0.4270,    0.3912,    0.2187,    0.4424,    0.3054,    0.4483,    0.4001,    0.3977,    0.4082,    0.2415},
     284{    0.4570,    0.3757,    0.4337,    0.4743,    0.4310,    0.4427,    0.3974,    0.2885,    0.3980,    0.3744},
     285            },
     286            new double[,] {
     287{    1.2905,    1.2380,    1.7324,    1.2677,    1.5604,    1.1785,    1.4203,    1.5505,    1.4074,    1.6661},
     288{    1.3541,    1.4784,    1.4883,    1.3260,    1.5205,    1.2287,    1.4290,    1.6691,    1.4983,    1.6400},
     289{    1.5926,    1.5553,    1.7148,    1.4532,    1.2723,    1.6120,    1.7037,    1.4168,    1.5620,    1.6918},
     290{    1.5976,    1.2535,    1.6008,    1.5000,    1.1166,    1.3049,    1.7936,    1.7727,    1.7478,    1.6765},
     291{    1.7803,    1.3141,    1.5839,    1.8060,    1.0609,    1.1453,    1.6157,    1.8464,    1.8234,    1.7466},
     292{    1.7637,    1.5278,    1.4774,    1.6898,    1.1570,    1.3125,    1.7566,    1.9039,    1.9070,    1.7485},
     293{    1.3433,    1.5628,    1.5180,    1.2873,    1.7594,    1.2733,    1.4024,    1.3619,    1.4179,    1.6679},
     294{    1.3404,    1.3646,    1.6537,    1.2501,    1.4913,    1.3564,    1.4662,    1.2410,    1.3572,    1.6066},
     295{    1.3820,    1.4665,    1.7501,    1.3395,    1.6239,    1.3216,    1.4470,    1.4522,    1.4285,    1.7190},
     296{    1.2934,    1.4986,    1.3642,    1.2267,    1.3714,    1.3384,    1.4531,    1.6503,    1.4517,    1.5012},
     297          },
     298                      new double[,] {
     299{   -0.0538,   -0.0611,   -0.0085,   -0.0569,   -0.0222,   -0.0696,   -0.0372,   -0.0231,   -0.0388,   -0.0131},
     300{   -0.0454,   -0.0306,   -0.0295,   -0.0490,   -0.0261,   -0.0624,   -0.0362,   -0.0129,   -0.0284,   -0.0151},
     301{   -0.0192,   -0.0226,   -0.0096,   -0.0334,   -0.0563,   -0.0175,   -0.0104,   -0.0377,   -0.0220,   -0.0112},
     302{   -0.0187,   -0.0589,   -0.0184,   -0.0283,   -0.0788,   -0.0519,   -0.0051,   -0.0062,   -0.0076,   -0.0123},
     303{   -0.0058,   -0.0506,   -0.0200,   -0.0045,   -0.0873,   -0.0745,   -0.0171,   -0.0029,   -0.0038,   -0.0077},
     304{   -0.0067,   -0.0254,   -0.0307,   -0.0114,   -0.0728,   -0.0508,   -0.0071,   -0.0011,   -0.0011,   -0.0076},
     305{   -0.0468,   -0.0219,   -0.0264,   -0.0542,   -0.0069,   -0.0561,   -0.0394,   -0.0444,   -0.0375,   -0.0130},
     306{   -0.0472,   -0.0441,   -0.0140,   -0.0594,   -0.0292,   -0.0451,   -0.0319,   -0.0606,   -0.0450,   -0.0179},
     307{   -0.0419,   -0.0319,   -0.0075,   -0.0473,   -0.0165,   -0.0496,   -0.0341,   -0.0335,   -0.0363,   -0.0094},
     308{   -0.0534,   -0.0284,   -0.0441,   -0.0627,   -0.0432,   -0.0474,   -0.0334,   -0.0143,   -0.0336,   -0.0281},
     309          }
     310          }
     311      );
     312      TestCovarianceFunction(new CovarianceRQiso(), 1,
     313         new double[,]
     314           {
     315{    6.8660,    6.8070,    7.2367,    6.8409,    7.1145,    6.7347,    6.9959,    7.1068,    6.9839,    7.1923},
     316{    6.9324,    7.0474,    7.0559,    6.9038,    7.0827,    6.7961,    7.0038,    7.1944,    7.0644,    7.1739},
     317{    7.1392,    7.1106,    7.2253,    7.0255,    6.8461,    7.1536,    7.2179,    6.9926,    7.1158,    7.2099},
     318{    7.1429,    6.8249,    7.1453,    7.0658,    6.6525,    6.8816,    7.2752,    7.2623,    7.2467,    7.1995},
     319{    7.2671,    6.8913,    7.1326,    7.2827,    6.5716,    6.6915,    7.1564,    7.3065,    7.2931,    7.2459},
     320{    7.2567,    7.0886,    7.0466,    7.2086,    6.7069,    6.8897,    7.2522,    7.3388,    7.3405,    7.2471},
     321{    6.9215,    7.1164,    7.0807,    6.8626,    7.2540,    6.8471,    6.9792,    6.9401,    6.9937,    7.1935},
     322{    6.9185,    6.9428,    7.1836,    6.8210,    7.0584,    6.9346,    7.0369,    6.8105,    6.9354,    7.1496},
     323{    6.9598,    7.0372,    7.2481,    6.9176,    7.1623,    6.8992,    7.0200,    7.0247,    7.0033,    7.2280},
     324{    6.8692,    7.0646,    6.9424,    6.7937,    6.9495,    6.9165,    7.0254,    7.1813,    7.0242,    7.0668},
     325           },
     326         new double[][,]
     327          {
     328            new double[,] {
     329{    0.9946,    1.1004,    0.3003,    1.0398,    0.5350,    1.2280,    0.7575,    0.5497,    0.7796,    0.3863},
     330{    0.8743,    0.6614,    0.6455,    0.9263,    0.5952,    1.1197,    0.7428,    0.3823,    0.6297,    0.4217},
     331{    0.4881,    0.5425,    0.3225,    0.7023,    1.0304,    0.4606,    0.3369,    0.7635,    0.5326,    0.3523},
     332{    0.4810,    1.0684,    0.4764,    0.6270,    1.3705,    0.9666,    0.2253,    0.2504,    0.2810,    0.3725},
     333{    0.2412,    0.9489,    0.5007,    0.2106,    1.5081,    1.3031,    0.4553,    0.1639,    0.1902,    0.2825},
     334{    0.2614,    0.5840,    0.6630,    0.3549,    1.2764,    0.9519,    0.2702,    0.1000,    0.0967,    0.2802},
     335{    0.8941,    0.5314,    0.5990,    1.0007,    0.2666,    1.0286,    0.7883,    0.8600,    0.7615,    0.3839},
     336{    0.8995,    0.8552,    0.4030,    1.0754,    0.6409,    0.8701,    0.6811,    1.0941,    0.8687,    0.4682},
     337{    0.8240,    0.6807,    0.2782,    0.9012,    0.4439,    0.9346,    0.7126,    0.7040,    0.7436,    0.3172},
     338{    0.9889,    0.6293,    0.8560,    1.1240,    0.8429,    0.9031,    0.7025,    0.4076,    0.7048,    0.6252},
     339            },
     340            new double[,] {
     341{   13.7321,   13.6139,   14.4735,   13.6818,   14.2291,   13.4693,   13.9917,   14.2135,   13.9678,   14.3846},
     342{   13.8647,   14.0949,   14.1118,   13.8076,   14.1654,   13.5922,   14.0076,   14.3888,   14.1287,   14.3478},
     343{   14.2784,   14.2211,   14.4506,   14.0511,   13.6922,   14.3072,   14.4358,   13.9852,   14.2316,   14.4199},
     344{   14.2859,   13.6498,   14.2906,   14.1315,   13.3049,   13.7631,   14.5504,   14.5247,   14.4933,   14.3990},
     345{   14.5341,   13.7827,   14.2652,   14.5654,   13.1433,   13.3830,   14.3128,   14.6130,   14.5862,   14.4918},
     346{   14.5134,   14.1773,   14.0932,   14.4172,   13.4138,   13.7793,   14.5045,   14.6776,   14.6810,   14.4942},
     347{   13.8429,   14.2328,   14.1613,   13.7253,   14.5081,   13.6943,   13.9583,   13.8803,   13.9873,   14.3871},
     348{   13.8370,   13.8855,   14.3672,   13.6420,   14.1168,   13.8693,   14.0738,   13.6210,   13.8709,   14.2993},
     349{   13.9195,   14.0743,   14.4962,   13.8352,   14.3246,   13.7985,   14.0400,   14.0493,   14.0067,   14.4561},
     350{   13.7384,   14.1291,   13.8847,   13.5873,   13.8990,   13.8331,   14.0508,   14.3625,   14.0484,   14.1335},
     351            },
     352            new double[,] {
     353{   -0.0067,   -0.0083,   -0.0006,   -0.0074,   -0.0019,   -0.0105,   -0.0038,   -0.0020,   -0.0041,   -0.0010},
     354{   -0.0051,   -0.0029,   -0.0027,   -0.0058,   -0.0023,   -0.0087,   -0.0037,   -0.0009,   -0.0026,   -0.0011},
     355{   -0.0015,   -0.0019,   -0.0007,   -0.0033,   -0.0073,   -0.0014,   -0.0007,   -0.0039,   -0.0019,   -0.0008},
     356{   -0.0015,   -0.0078,   -0.0015,   -0.0026,   -0.0133,   -0.0064,   -0.0003,   -0.0004,   -0.0005,   -0.0009},
     357{   -0.0004,   -0.0061,   -0.0016,   -0.0003,   -0.0164,   -0.0120,   -0.0013,   -0.0002,   -0.0002,   -0.0005},
     358{   -0.0004,   -0.0022,   -0.0029,   -0.0008,   -0.0114,   -0.0062,   -0.0005,   -0.0001,   -0.0001,   -0.0005},
     359{   -0.0054,   -0.0018,   -0.0024,   -0.0068,   -0.0005,   -0.0072,   -0.0042,   -0.0050,   -0.0039,   -0.0009},
     360{   -0.0055,   -0.0049,   -0.0010,   -0.0080,   -0.0027,   -0.0051,   -0.0031,   -0.0082,   -0.0051,   -0.0014},
     361{   -0.0046,   -0.0031,   -0.0005,   -0.0055,   -0.0013,   -0.0059,   -0.0034,   -0.0033,   -0.0037,   -0.0006},
     362{   -0.0067,   -0.0026,   -0.0049,   -0.0087,   -0.0048,   -0.0055,   -0.0033,   -0.0011,   -0.0033,   -0.0026},                             
     363                           }
     364          }
     365       );
     366    }
     367
    256368
    257369    [TestMethod]
     
    335447              },
    336448          }
    337       );
     449      , 5e-3);
    338450      TestCovarianceFunction(new CovariancePeriodic(), 1,
    339451        new double[,]
     
    393505              },
    394506          }
    395       );
     507      , 5e-3);
    396508    }
    397509
     
    781893              },
    782894          }
    783       );
    784     }
    785 
    786 
    787     private void TestCovarianceFunction(ICovarianceFunction cf, double hypValue, double[,] expectedCov, double[][,] expectedGradients) {
     895      , 5e-3);
     896    }
     897
     898
     899    private void TestCovarianceFunction(ICovarianceFunction cf, double hypValue, double[,] expectedCov, double[][,] expectedGradients, double delta = 1E-3) {
    788900      var x = GetData();
    789901      var xt = GetDataTest();
     
    801913          actualCov[i, j] = cf.GetCovariance(i, j);
    802914
    803       AssertEqual(expectedCov, actualCov);
     915      AssertEqual(expectedCov, actualCov, delta);
    804916
    805917      for (int i = 0; i < rows0; i++)
    806918        for (int j = 0; j < rows1; j++) {
    807919          for (int k = 0; k < nHyp; k++)
    808             Assert.AreEqual(expectedGradients[k][i, j], cf.GetGradient(i, j, k), 5E-3);
     920            Assert.AreEqual(expectedGradients[k][i, j], cf.GetGradient(i, j, k), delta);
    809921        }
    810922    }
     
    836948        Assert.AreEqual(expected[i], actual[i], 1E-3);
    837949    }
    838     private void AssertEqual(double[,] expected, double[,] actual) {
     950    private void AssertEqual(double[,] expected, double[,] actual, double delta = 5e-3) {
    839951      Assert.AreEqual(expected.Length, actual.Length);
    840952      for (int i = 0; i < expected.GetLength(0); i++)
    841953        for (int j = 0; j < expected.GetLength(1); j++)
    842           Assert.AreEqual(expected[i, j], actual[i, j], 5E-3);
     954          Assert.AreEqual(expected[i, j], actual[i, j], delta);
    843955    }
    844956
  • trunk/sources/HeuristicLab.Tests/HeuristicLab.Algorithms.DataAnalysis-3.4/GaussianProcessModelTest.cs

    r8463 r8473  
    5656
    5757        var dHyp = model.GetHyperparameterGradients();
    58         Assert.AreEqual(-2.0171e+003, dHyp[0], 1);
    59         Assert.AreEqual(-248.7932, dHyp[1], 1E-2);
     58        Assert.AreEqual(-248.7932, dHyp[0], 1E-2);
    6059        var dHypCovExpected = new double[] { -0.5550e4, -5.5533e4, -0.2511e4, -2.7625e4, -1.3033e4, 0.0289e4, -2.7625e4 };
    61         AssertEqual(dHypCovExpected, dHyp.Skip(2).ToArray(), 1);
     60        AssertEqual(dHypCovExpected, dHyp.Skip(1).Take(7).ToArray(), 1);
     61        Assert.AreEqual(-2.0171e+003, dHyp.Last(), 1);
     62
    6263
    6364        var predTrain = model.GetEstimatedValues(problemData.Dataset, new int[] { 0, 400 }).ToArray();
    6465        Assert.AreEqual(310.5930, predTrain[0], 1e-3);
    6566        Assert.AreEqual(347.9993, predTrain[1], 1e-3);
     67
     68        var predTrainVar = model.GetEstimatedVariance(problemData.Dataset, problemData.TrainingIndices).ToArray();
    6669      }
    6770
    6871      {
    69         var hyp = new double[] { 0.716427415979145, 0.029973094285941, 0.455535210579926, 3.438647883940457, 1.464114485889487, 3.001788584487478, 3.815289323309630, 4.374914122810222, 3.001788584487478 };
     72        var hyp = new double[] { 0.029973094285941, 0.455535210579926, 3.438647883940457, 1.464114485889487, 3.001788584487478, 3.815289323309630, 4.374914122810222, 3.001788584487478, 0.716427415979145 };
    7073        var model = new GaussianProcessModel(problemData.Dataset, targetVariable, allowedInputVariables, rows, hyp,
    7174                                             meanFunction,
     
    7477
    7578        var dHyp = model.GetHyperparameterGradients();
    76         Assert.AreEqual(0.8621, dHyp[0], 1e-3);
    77         Assert.AreEqual(-0.0046, dHyp[1], 1e-3);
     79        Assert.AreEqual(-0.0046, dHyp[0], 1e-3);
    7880        var dHypCovExpected = new double[] { 0.2652, -0.2386, 0.1706, -0.1744, 0.0000, 0.0000, -0.1744 };
    79         AssertEqual(dHypCovExpected, dHyp.Skip(2).ToArray(), 1e-3);
     81        AssertEqual(dHypCovExpected, dHyp.Skip(1).Take(7).ToArray(), 1e-3);
     82        Assert.AreEqual(0.8621, dHyp.Last(), 1e-3);
    8083
    8184        var predTrain = model.GetEstimatedValues(problemData.Dataset, new int[] { 0, 400 }).ToArray();
     
    8386        Assert.AreEqual(356.6076, predTrain[1], 1e-3);
    8487      }
     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       */
    85125    }
    86126
Note: See TracChangeset for help on using the changeset viewer.