1  #region License Information


2  /* HeuristicLab


3  * Copyright (C) 20022012 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.Linq;


24  using HeuristicLab.Common;


25  using HeuristicLab.Core;


26  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;


27 


28  namespace HeuristicLab.Algorithms.DataAnalysis {


29  [StorableClass]


30  [Item(Name = "CovarianceSEard", Description = "Squared exponential covariance function with automatic relevance determination for Gaussian processes.")]


31  public class CovarianceSEard : Item, ICovarianceFunction {


32  [Storable]


33  private double[,] x;


34  [Storable]


35  private double[,] xt;


36  [Storable]


37  private double sf2;


38  public double Scale { get { return sf2; } }


39 


40  [Storable]


41  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  }


50 


51  private double[,] sd;


52  private bool symmetric;


53 


54  public int GetNumberOfParameters(int numberOfVariables) {


55  return numberOfVariables + 1;


56  }


57  [StorableConstructor]


58  protected CovarianceSEard(bool deserializing) : base(deserializing) { }


59  protected CovarianceSEard(CovarianceSEard original, Cloner cloner)


60  : base(original, cloner) {


61  if (original.x != null) {


62  this.x = new double[original.x.GetLength(0), original.x.GetLength(1)];


63  Array.Copy(original.x, this.x, x.Length);


64 


65  this.xt = new double[original.xt.GetLength(0), original.xt.GetLength(1)];


66  Array.Copy(original.xt, this.xt, xt.Length);


67 


68  this.sd = new double[original.sd.GetLength(0), original.sd.GetLength(1)];


69  Array.Copy(original.sd, this.sd, sd.Length);


70 


71  this.l = new double[original.l.Length];


72  Array.Copy(original.l, this.l, l.Length);


73  }


74  this.sf2 = original.sf2;


75  this.symmetric = original.symmetric;


76  }


77  public CovarianceSEard()


78  : base() {


79  }


80 


81  public override IDeepCloneable Clone(Cloner cloner) {


82  return new CovarianceSEard(this, cloner);


83  }


84 


85  public void SetParameter(double[] hyp) {


86  this.l = hyp.Take(hyp.Length  1).Select(Math.Exp).ToArray();


87  this.sf2 = Math.Exp(2 * hyp[hyp.Length  1]);


88  // sf2 = Math.Min(10E6, sf2); // upper limit for the scale


89 


90  sd = null;


91  }


92 


93  public void SetData(double[,] x) {


94  SetData(x, x);


95  this.symmetric = true;


96  }


97 


98  public void SetData(double[,] x, double[,] xt) {


99  this.x = x;


100  this.xt = xt;


101  this.symmetric = false;


102 


103  sd = null;


104  }


105 


106  public double GetCovariance(int i, int j) {


107  if (sd == null) CalculateSquaredDistances();


108  return sf2 * Math.Exp(sd[i, j] / 2.0);


109  }


110 


111  public double GetGradient(int i, int j, int k) {


112  if (k < l.Length) {


113  double sqrDist = Util.SqrDist(x[i, k] / l[k], xt[j, k] / l[k]);


114  return sf2 * Math.Exp(sd[i, j] / 2.0) * sqrDist;


115  } else if (k == l.Length) {


116  return 2.0 * sf2 * Math.Exp(sd[i, j] / 2.0);


117  } else {


118  throw new ArgumentException("CovarianceSEard has dimension+1 hyperparameters.", "k");


119  }


120  }


121 


122 


123  private void CalculateSquaredDistances() {


124  if (x.GetLength(1) != xt.GetLength(1)) throw new InvalidOperationException();


125  int rows = x.GetLength(0);


126  int cols = xt.GetLength(0);


127  sd = new double[rows, cols];


128  if (symmetric) {


129  for (int i = 0; i < rows; i++) {


130  for (int j = i; j < cols; j++) {


131  sd[i, j] = Util.SqrDist(Util.GetRow(x, i).Select((e, k) => e / l[k]),


132  Util.GetRow(xt, j).Select((e, k) => e / l[k]));


133  sd[j, i] = sd[i, j];


134  }


135  }


136  } else {


137  for (int i = 0; i < rows; i++) {


138  for (int j = 0; j < cols; j++) {


139  sd[i, j] = Util.SqrDist(Util.GetRow(x, i).Select((e, k) => e / l[k]),


140  Util.GetRow(xt, j).Select((e, k) => e / l[k]));


141  }


142  }


143  }


144  }


145  }


146  }

