Changeset 8612 for trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceSEard.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/CovarianceSEard.cs
r8565 r8612 25 25 using HeuristicLab.Common; 26 26 using HeuristicLab.Core; 27 using HeuristicLab.Data; 27 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 29 … … 30 31 [StorableClass] 31 32 [Item(Name = "CovarianceSEard", Description = "Squared exponential covariance function with automatic relevance determination for Gaussian processes.")] 32 public class CovarianceSEard :Item, ICovarianceFunction {33 public sealed class CovarianceSEard : 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; } } 36 39 37 40 [Storable] 38 41 private double[] inverseLength; 39 public double[] InverseLength { 40 get { 41 if (inverseLength == null) return new double[0]; 42 var copy = new double[inverseLength.Length]; 43 Array.Copy(inverseLength, copy, copy.Length); 44 return copy; 42 [Storable] 43 private readonly HyperParameter<DoubleArray> inverseLengthParameter; 44 public IValueParameter<DoubleArray> InverseLengthParameter { get { return inverseLengthParameter; } } 45 46 [StorableConstructor] 47 private CovarianceSEard(bool deserializing) : base(deserializing) { } 48 private CovarianceSEard(CovarianceSEard original, Cloner cloner) 49 : base(original, cloner) { 50 this.sf2 = original.sf2; 51 this.scaleParameter = cloner.Clone(original.scaleParameter); 52 53 if (original.inverseLength != null) { 54 this.inverseLength = new double[original.inverseLength.Length]; 55 Array.Copy(original.inverseLength, this.inverseLength, this.inverseLength.Length); 45 56 } 46 }57 this.inverseLengthParameter = cloner.Clone(original.inverseLengthParameter); 47 58 48 public int GetNumberOfParameters(int numberOfVariables) { 49 return numberOfVariables + 1; 50 } 51 [StorableConstructor] 52 protected CovarianceSEard(bool deserializing) : base(deserializing) { } 53 protected CovarianceSEard(CovarianceSEard original, Cloner cloner) 54 : base(original, cloner) { 55 this.inverseLength = original.InverseLength; // array is cloned in the getter 56 this.sf2 = original.sf2; 59 RegisterEvents(); 57 60 } 58 61 public CovarianceSEard() 59 62 : base() { 63 Name = ItemName; 64 Description = ItemDescription; 65 66 this.scaleParameter = new HyperParameter<DoubleValue>("Scale", "The scale parameter of the squared exponential covariance function with ARD."); 67 this.inverseLengthParameter = new HyperParameter<DoubleArray>("InverseLength", "The inverse length parameter for automatic relevance determination."); 68 69 Parameters.Add(scaleParameter); 70 Parameters.Add(inverseLengthParameter); 71 72 RegisterEvents(); 60 73 } 61 74 … … 64 77 } 65 78 79 [StorableHook(HookType.AfterDeserialization)] 80 private void AfterDeserialization() { 81 RegisterEvents(); 82 } 83 84 private void RegisterEvents() { 85 Util.AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { sf2 = scaleParameter.Value.Value; }); 86 Util.AttachArrayChangeHandler<DoubleArray, double>(inverseLengthParameter, () => { 87 inverseLength = 88 inverseLengthParameter.Value.ToArray(); 89 }); 90 } 91 92 public int GetNumberOfParameters(int numberOfVariables) { 93 return 94 (scaleParameter.Fixed ? 0 : 1) + 95 (inverseLengthParameter.Fixed ? 0 : numberOfVariables); 96 } 97 98 66 99 public void SetParameter(double[] hyp) { 67 this.inverseLength = hyp.Take(hyp.Length - 1).Select(p => 1.0 / Math.Exp(p)).ToArray(); 68 this.sf2 = Math.Exp(2 * hyp[hyp.Length - 1]); 100 int i = 0; 101 if (!scaleParameter.Fixed) { 102 scaleParameter.SetValue(new DoubleValue(Math.Exp(2 * hyp[i]))); 103 i++; 104 } 105 if (!inverseLengthParameter.Fixed) { 106 inverseLengthParameter.SetValue(new DoubleArray(hyp.Skip(i).Select(e => 1.0 / Math.Exp(e)).ToArray())); 107 i += hyp.Skip(i).Count(); 108 } 109 if (hyp.Length != i) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovariancSEard", "hyp"); 69 110 } 70 111
Note: See TracChangeset
for help on using the changeset viewer.