Changeset 8582 for trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceLinearArd.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/CovarianceLinearArd.cs
r8562 r8582 25 25 using HeuristicLab.Common; 26 26 using HeuristicLab.Core; 27 using HeuristicLab.Data; 27 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 29 … … 31 32 [Item(Name = "CovarianceLinearArd", 32 33 Description = "Linear covariance function with automatic relevance determination for Gaussian processes.")] 33 public class CovarianceLinearArd : Item, ICovarianceFunction { 34 public class CovarianceLinearArd : CovarianceFunction { 35 public IValueParameter<DoubleArray> InverseLengthParameter { 36 get { return inverseLengthParameter; } 37 } 38 39 [Storable] 40 private HyperParameter<DoubleArray> inverseLengthParameter; 41 34 42 [Storable] 35 43 private double[] inverseLength; 36 public double[] InverseLength {37 get {38 if (inverseLength == null) return null;39 double[] res = new double[inverseLength.Length];40 Array.Copy(inverseLength, res, res.Length);41 return res;42 }43 }44 45 public int GetNumberOfParameters(int numberOfVariables) {46 return numberOfVariables;47 }48 44 49 45 [StorableConstructor] … … 51 47 protected CovarianceLinearArd(CovarianceLinearArd original, Cloner cloner) 52 48 : base(original, cloner) { 53 this.inverseLength = original.InverseLength; // array is copied in the getter 49 inverseLengthParameter = cloner.Clone(original.inverseLengthParameter); 50 if (original.inverseLength != null) { 51 this.inverseLength = new double[original.inverseLength.Length]; 52 Array.Copy(original.inverseLength, inverseLength, inverseLength.Length); 53 } 54 55 RegisterEvents(); 54 56 } 55 57 public CovarianceLinearArd() 56 58 : base() { 59 inverseLengthParameter = new HyperParameter<DoubleArray>("InverseLength", 60 "The inverse length parameter for ARD."); 61 Parameters.Add(inverseLengthParameter); 62 RegisterEvents(); 63 } 64 65 [StorableHook(HookType.AfterDeserialization)] 66 private void AfterDeserialization() { 67 RegisterEvents(); 57 68 } 58 69 … … 61 72 } 62 73 63 public void SetParameter(double[] hyp) { 64 inverseLength = hyp.Select(e => 1.0 / Math.Exp(e)).ToArray(); 74 // caching 75 private void RegisterEvents() { 76 AttachArrayChangeHandler<DoubleArray, double>(inverseLengthParameter, () => { inverseLength = inverseLengthParameter.Value.ToArray(); }); 65 77 } 66 78 67 public double GetCovariance(double[,] x, int i, int j) { 79 80 public override int GetNumberOfParameters(int numberOfVariables) { 81 if (!inverseLengthParameter.Fixed) 82 return numberOfVariables; 83 else 84 return 0; 85 } 86 87 public override void SetParameter(double[] hyp) { 88 if (!inverseLengthParameter.Fixed && hyp.Length > 0) { 89 inverseLengthParameter.SetValue(new DoubleArray(hyp.Select(e => 1.0 / Math.Exp(e)).ToArray())); 90 } else throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceLinearArd", "hyp"); 91 } 92 93 public override double GetCovariance(double[,] x, int i, int j) { 68 94 return Util.ScalarProd(x, i, j, inverseLength); 69 95 } 70 96 71 public IEnumerable<double> GetGradient(double[,] x, int i, int j) {97 public override IEnumerable<double> GetGradient(double[,] x, int i, int j) { 72 98 for (int k = 0; k < inverseLength.Length; k++) { 73 99 yield return -2.0 * x[i, k] * x[j, k] * inverseLength[k] * inverseLength[k]; … … 75 101 } 76 102 77 public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {103 public override double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) { 78 104 return Util.ScalarProd(x, i, xt, j, inverseLength); 79 105 }
Note: See TracChangeset
for help on using the changeset viewer.