Changeset 8582 for trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceConst.cs
- Timestamp:
- 09/05/12 17:04:30 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceConst.cs
r8484 r8582 24 24 using HeuristicLab.Common; 25 25 using HeuristicLab.Core; 26 using HeuristicLab.Data; 26 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 28 … … 30 31 [Item(Name = "CovarianceConst", 31 32 Description = "Constant covariance function for Gaussian processes.")] 32 public class CovarianceConst : Item, ICovarianceFunction { 33 public class CovarianceConst : CovarianceFunction { 34 35 public IValueParameter<DoubleValue> ScaleParameter { 36 get { return scaleParameter; } 37 } 38 33 39 [Storable] 34 private double sf2; 35 public double Scale { get { return sf2; } } 40 private readonly HyperParameter<DoubleValue> scaleParameter; 41 42 [Storable] 43 private double scale; 36 44 37 45 [StorableConstructor] … … 42 50 protected CovarianceConst(CovarianceConst original, Cloner cloner) 43 51 : base(original, cloner) { 44 this.sf2 = original.sf2; 52 this.scaleParameter = cloner.Clone(original.scaleParameter); 53 this.scale = original.scale; 54 55 RegisterEvents(); 45 56 } 46 57 47 58 public CovarianceConst() 48 59 : base() { 60 scaleParameter = new HyperParameter<DoubleValue>("Scale", "The scale of the constant covariance function."); 61 Parameters.Add(scaleParameter); 62 RegisterEvents(); 49 63 } 64 65 [StorableHook(HookType.AfterDeserialization)] 66 private void AfterDeserialization() { 67 RegisterEvents(); 68 } 69 70 // caching 71 private void RegisterEvents() { 72 AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { scale = scaleParameter.Value.Value; }); 73 } 74 50 75 51 76 public override IDeepCloneable Clone(Cloner cloner) { … … 53 78 } 54 79 55 public int GetNumberOfParameters(int numberOfVariables) {56 return 1;80 public override int GetNumberOfParameters(int numberOfVariables) { 81 return scaleParameter.Fixed ? 0 : 1; 57 82 } 58 83 59 public void SetParameter(double[] hyp) { 60 if (hyp.Length != 1) throw new ArgumentException("CovarianceConst has only one hyperparameter", "k"); 61 this.sf2 = Math.Exp(2 * hyp[0]); 84 public override void SetParameter(double[] hyp) { 85 if (!scaleParameter.Fixed && hyp.Length == 1) { 86 scaleParameter.SetValue(new DoubleValue(Math.Exp(2 * hyp[0]))); 87 } else { 88 throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceConst", "hyp"); 89 } 62 90 } 63 91 64 65 public double GetCovariance(double[,] x, int i, int j) { 66 return sf2; 92 public override double GetCovariance(double[,] x, int i, int j) { 93 return scale; 67 94 } 68 95 69 public IEnumerable<double> GetGradient(double[,] x, int i, int j) {70 yield return 2 * sf2;96 public override IEnumerable<double> GetGradient(double[,] x, int i, int j) { 97 yield return 2.0 * scale; 71 98 } 72 99 73 public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {74 return s f2;100 public override double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) { 101 return scale; 75 102 } 76 103 }
Note: See TracChangeset
for help on using the changeset viewer.