Free cookie consent management tool by TermsFeed Policy Generator

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/HeuristicLab.Algorithms.DataAnalysis/3.4
Files:
1 added
12 edited

Legend:

Unmodified
Added
Removed
  • 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}
Note: See TracChangeset for help on using the changeset viewer.