Changeset 8612 for trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceSEiso.cs
- Timestamp:
- 09/10/12 13:28:55 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceSEiso.cs
r8491 r8612 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 = "CovarianceSEiso", 31 32 Description = "Isotropic squared exponential covariance function for Gaussian processes.")] 32 public class CovarianceSEiso :Item, ICovarianceFunction {33 public sealed class CovarianceSEiso : ParameterizedNamedItem, ICovarianceFunction { 33 34 [Storable] 34 35 private double sf2; 35 public double Scale { get { return sf2; } } 36 [Storable] 37 private readonly HyperParameter<DoubleValue> scaleParameter; 38 public IValueParameter<DoubleValue> ScaleParameter { get { return scaleParameter; } } 39 36 40 [Storable] 37 41 private double inverseLength; 38 public double InverseLength { get { return inverseLength; } } 42 [Storable] 43 private readonly HyperParameter<DoubleValue> inverseLengthParameter; 44 public IValueParameter<DoubleValue> InverseLengthParameter { get { return inverseLengthParameter; } } 39 45 40 46 [StorableConstructor] 41 pr otectedCovarianceSEiso(bool deserializing)47 private CovarianceSEiso(bool deserializing) 42 48 : base(deserializing) { 43 49 } 44 50 45 pr otectedCovarianceSEiso(CovarianceSEiso original, Cloner cloner)51 private CovarianceSEiso(CovarianceSEiso original, Cloner cloner) 46 52 : base(original, cloner) { 47 53 this.sf2 = original.sf2; 54 this.scaleParameter = cloner.Clone(original.scaleParameter); 55 48 56 this.inverseLength = original.inverseLength; 57 this.inverseLengthParameter = cloner.Clone(original.inverseLengthParameter); 58 59 RegisterEvents(); 49 60 } 50 61 51 62 public CovarianceSEiso() 52 63 : base() { 64 Name = ItemName; 65 Description = ItemDescription; 66 67 this.scaleParameter = new HyperParameter<DoubleValue>("Scale", "The scale parameter of the isometric squared exponential covariance function."); 68 this.inverseLengthParameter = new HyperParameter<DoubleValue>("InverseLength", "The inverse length parameter of the isometric squared exponential covariance function."); 69 70 Parameters.Add(scaleParameter); 71 Parameters.Add(inverseLengthParameter); 72 73 RegisterEvents(); 53 74 } 54 75 … … 57 78 } 58 79 80 [StorableHook(HookType.AfterDeserialization)] 81 private void AfterDeserialization() { 82 RegisterEvents(); 83 } 84 85 private void RegisterEvents() { 86 Util.AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { sf2 = scaleParameter.Value.Value; }); 87 Util.AttachValueChangeHandler<DoubleValue, double>(inverseLengthParameter, () => { inverseLength = inverseLengthParameter.Value.Value; }); 88 } 89 59 90 public int GetNumberOfParameters(int numberOfVariables) { 60 return 2; 91 return 92 (scaleParameter.Fixed ? 0 : 1) + 93 (inverseLengthParameter.Fixed ? 0 : 1); 61 94 } 62 95 63 96 public void SetParameter(double[] hyp) { 64 if (hyp.Length != 2) throw new ArgumentException("CovarianceSEiso has two hyperparameters", "k"); 65 this.inverseLength = 1.0 / Math.Exp(hyp[0]); 66 this.sf2 = Math.Exp(2 * hyp[1]); 97 int i = 0; 98 if (!inverseLengthParameter.Fixed) { 99 inverseLengthParameter.SetValue(new DoubleValue(1.0 / Math.Exp(hyp[i]))); 100 i++; 101 } 102 if (!scaleParameter.Fixed) { 103 scaleParameter.SetValue(new DoubleValue(Math.Exp(2 * hyp[i]))); 104 i++; 105 } 106 if (hyp.Length != i) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceSEiso", "hyp"); 67 107 } 68 108
Note: See TracChangeset
for help on using the changeset viewer.