Changeset 8612 for trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceRQiso.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/CovarianceRQiso.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 = "CovarianceRQiso", 31 32 Description = "Isotropic rational quadratic covariance function for Gaussian processes.")] 32 public class CovarianceRQiso :Item, ICovarianceFunction {33 public sealed class CovarianceRQiso : 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; } }39 42 [Storable] 40 private double alpha; 41 public double Shape { get { return alpha; } } 43 private readonly HyperParameter<DoubleValue> inverseLengthParameter; 44 public IValueParameter<DoubleValue> InverseLengthParameter { get { return inverseLengthParameter; } } 45 46 [Storable] 47 private double shape; 48 [Storable] 49 private readonly HyperParameter<DoubleValue> shapeParameter; 50 public IValueParameter<DoubleValue> ShapeParameter { get { return shapeParameter; } } 42 51 43 52 [StorableConstructor] 44 pr otectedCovarianceRQiso(bool deserializing)53 private CovarianceRQiso(bool deserializing) 45 54 : base(deserializing) { 46 55 } 47 56 48 pr otectedCovarianceRQiso(CovarianceRQiso original, Cloner cloner)57 private CovarianceRQiso(CovarianceRQiso original, Cloner cloner) 49 58 : base(original, cloner) { 50 59 this.sf2 = original.sf2; 60 this.scaleParameter = cloner.Clone(original.scaleParameter); 61 51 62 this.inverseLength = original.inverseLength; 52 this.alpha = original.alpha; 63 this.inverseLengthParameter = cloner.Clone(original.inverseLengthParameter); 64 65 this.shape = original.shape; 66 this.shapeParameter = cloner.Clone(original.shapeParameter); 67 68 RegisterEvents(); 53 69 } 54 70 55 71 public CovarianceRQiso() 56 72 : base() { 73 Name = ItemName; 74 Description = ItemDescription; 75 76 this.scaleParameter = new HyperParameter<DoubleValue>("Scale", "The scale parameter of the isometric rational quadratic covariance function."); 77 this.inverseLengthParameter = new HyperParameter<DoubleValue>("InverseLength", "The inverse length parameter of the isometric rational quadratic covariance function."); 78 this.shapeParameter = new HyperParameter<DoubleValue>("Shape", "The shape parameter (alpha) of the isometric rational quadratic covariance function."); 79 80 Parameters.Add(scaleParameter); 81 Parameters.Add(inverseLengthParameter); 82 Parameters.Add(shapeParameter); 83 84 RegisterEvents(); 57 85 } 58 86 … … 61 89 } 62 90 91 [StorableHook(HookType.AfterDeserialization)] 92 private void AfterDeserialization() { 93 RegisterEvents(); 94 } 95 96 private void RegisterEvents() { 97 Util.AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { sf2 = scaleParameter.Value.Value; }); 98 Util.AttachValueChangeHandler<DoubleValue, double>(inverseLengthParameter, () => { inverseLength = inverseLengthParameter.Value.Value; }); 99 Util.AttachValueChangeHandler<DoubleValue, double>(shapeParameter, () => { shape = shapeParameter.Value.Value; }); 100 } 101 63 102 public int GetNumberOfParameters(int numberOfVariables) { 64 return 3; 103 return 104 (scaleParameter.Fixed ? 0 : 1) + 105 (inverseLengthParameter.Fixed ? 0 : 1) + 106 (shapeParameter.Fixed ? 0 : 1); 65 107 } 66 108 67 109 public void SetParameter(double[] hyp) { 68 if (hyp.Length != 3) throw new ArgumentException("CovarianceRQiso has three hyperparameters", "k"); 69 this.inverseLength = 1.0 / Math.Exp(hyp[0]); 70 this.sf2 = Math.Exp(2 * hyp[1]); 71 this.alpha = Math.Exp(hyp[2]); 110 int i = 0; 111 if (!scaleParameter.Fixed) { 112 scaleParameter.SetValue(new DoubleValue(Math.Exp(2 * hyp[i]))); 113 i++; 114 } 115 if (!shapeParameter.Fixed) { 116 shapeParameter.SetValue(new DoubleValue(Math.Exp(hyp[i]))); 117 i++; 118 } 119 if (!inverseLengthParameter.Fixed) { 120 inverseLengthParameter.SetValue(new DoubleValue(1.0 / Math.Exp(hyp[i]))); 121 i++; 122 } 123 if (hyp.Length != i) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceRQiso", "hyp"); 72 124 } 73 125 … … 77 129 ? 0.0 78 130 : Util.SqrDist(x, i, j, inverseLength); 79 return sf2 * Math.Pow(1 + 0.5 * d / alpha, -alpha);131 return sf2 * Math.Pow(1 + 0.5 * d / shape, -shape); 80 132 } 81 133 … … 85 137 : Util.SqrDist(x, i, j, inverseLength); 86 138 87 double b = 1 + 0.5 * d / alpha;88 yield return sf2 * Math.Pow(b, - alpha- 1) * d;89 yield return 2 * sf2 * Math.Pow(b, - alpha);90 yield return sf2 * Math.Pow(b, - alpha) * (0.5 * d / b - alpha* Math.Log(b));139 double b = 1 + 0.5 * d / shape; 140 yield return sf2 * Math.Pow(b, -shape - 1) * d; 141 yield return 2 * sf2 * Math.Pow(b, -shape); 142 yield return sf2 * Math.Pow(b, -shape) * (0.5 * d / b - shape * Math.Log(b)); 91 143 } 92 144 93 145 public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) { 94 146 double d = Util.SqrDist(x, i, xt, j, inverseLength); 95 return sf2 * Math.Pow(1 + 0.5 * d / alpha, -alpha);147 return sf2 * Math.Pow(1 + 0.5 * d / shape, -shape); 96 148 } 97 149 }
Note: See TracChangeset
for help on using the changeset viewer.