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.Collections.Generic;


24  using System.Linq;


25  using HeuristicLab.Common;


26  using HeuristicLab.Core;


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


28 


29  namespace HeuristicLab.Algorithms.DataAnalysis {


30  [StorableClass]


31  [Item(Name = "CovarianceRQiso",


32  Description = "Isotropic rational quadratic covariance function for Gaussian processes.")]


33  public class CovarianceRQiso : Item, ICovarianceFunction {


34  [Storable]


35  private double sf2;


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


37  [Storable]


38  private double l;


39  public double Length { get { return l; } }


40  [Storable]


41  private double alpha;


42  public double Shape { get { return alpha; } }


43 


44  [StorableConstructor]


45  protected CovarianceRQiso(bool deserializing)


46  : base(deserializing) {


47  }


48 


49  protected CovarianceRQiso(CovarianceRQiso original, Cloner cloner)


50  : base(original, cloner) {


51  this.sf2 = original.sf2;


52  this.l = original.l;


53  this.alpha = original.alpha;


54  }


55 


56  public CovarianceRQiso()


57  : base() {


58  }


59 


60  public override IDeepCloneable Clone(Cloner cloner) {


61  return new CovarianceRQiso(this, cloner);


62  }


63 


64  public int GetNumberOfParameters(int numberOfVariables) {


65  return 3;


66  }


67 


68  public void SetParameter(double[] hyp) {


69  if (hyp.Length != 3) throw new ArgumentException("CovarianceRQiso has three hyperparameters", "k");


70  this.l = Math.Exp(hyp[0]);


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


72  this.alpha = Math.Exp(hyp[2]);


73  }


74 


75 


76  public double GetCovariance(double[,] x, int i, int j) {


77  double lInv = 1.0 / l;


78  double d = i == j


79  ? 0.0


80  : Util.SqrDist(Util.GetRow(x, i).Select(e => e * lInv), Util.GetRow(x, j).Select(e => e * lInv));


81  return sf2 * Math.Pow(1 + 0.5 * d / alpha, alpha);


82  }


83 


84  public IEnumerable<double> GetGradient(double[,] x, int i, int j) {


85  double lInv = 1.0 / l;


86  double d = i == j


87  ? 0.0


88  : Util.SqrDist(Util.GetRow(x, i).Select(e => e * lInv), Util.GetRow(x, j).Select(e => e * lInv));


89 


90  double b = 1 + 0.5 * d / alpha;


91  yield return sf2 * Math.Pow(b, alpha  1) * d;


92  yield return 2 * sf2 * Math.Pow(b, alpha);


93  yield return sf2 * Math.Pow(b, alpha) * (0.5 * d / b  alpha * Math.Log(b));


94  }


95 


96  public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {


97  double lInv = 1.0 / l;


98  double d = Util.SqrDist(Util.GetRow(x, i).Select(e => e * lInv), Util.GetRow(xt, j).Select(e => e * lInv));


99  return sf2 * Math.Pow(1 + 0.5 * d / alpha, alpha);


100  }


101  }


102  }

