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 HeuristicLab.Common;


25  using HeuristicLab.Core;


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


27 


28  namespace HeuristicLab.Algorithms.DataAnalysis {


29  [StorableClass]


30  [Item(Name = "CovariancePeriodic", Description = "Periodic covariance function for Gaussian processes.")]


31  public class CovariancePeriodic : Item, ICovarianceFunction {


32  [Storable]


33  private double sf2;


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


35  [Storable]


36  private double inverseLength;


37  public double InverseLength { get { return inverseLength; } }


38  [Storable]


39  private double p;


40  public double Period { get { return p; } }


41 


42  public int GetNumberOfParameters(int numberOfVariables) {


43  return 3;


44  }


45  [StorableConstructor]


46  protected CovariancePeriodic(bool deserializing) : base(deserializing) { }


47  protected CovariancePeriodic(CovariancePeriodic original, Cloner cloner)


48  : base(original, cloner) {


49  sf2 = original.sf2;


50  inverseLength = original.inverseLength;


51  p = original.p;


52  }


53  public CovariancePeriodic()


54  : base() {


55  }


56 


57  public override IDeepCloneable Clone(Cloner cloner) {


58  return new CovariancePeriodic(this, cloner);


59  }


60 


61  public void SetParameter(double[] hyp) {


62  if (hyp.Length != 3) throw new ArgumentException();


63  this.inverseLength = 1.0 / Math.Exp(hyp[0]);


64  this.p = Math.Exp(hyp[1]);


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


66  }


67 


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


69  double k = i == j ? 0.0 : GetDistance(x, x, i, j);


70  k = Math.PI * k / p;


71  k = Math.Sin(k) * inverseLength;


72  k = k * k;


73 


74  return sf2 * Math.Exp(2.0 * k);


75  }


76 


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


78  double v = i == j ? 0.0 : Math.PI * GetDistance(x, x, i, j) / p;


79  double gradient = Math.Sin(v) * inverseLength;


80  gradient *= gradient;


81  yield return 4.0 * sf2 * Math.Exp(2.0 * gradient) * gradient;


82  double r = Math.Sin(v) * inverseLength;


83  yield return 4.0 * sf2 * inverseLength * Math.Exp(2 * r * r) * r * Math.Cos(v) * v;


84  yield return 2.0 * sf2 * Math.Exp(2 * gradient);


85  }


86 


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


88  double k = GetDistance(x, xt, i, j);


89  k = Math.PI * k / p;


90  k = Math.Sin(k) * inverseLength;


91  k = k * k;


92 


93  return sf2 * Math.Exp(2.0 * k);


94  }


95 


96  private double GetDistance(double[,] x, double[,] xt, int i, int j) {


97  return Math.Sqrt(Util.SqrDist(x, i, xt, j));


98  }


99  }


100  }

