Changeset 8982 for trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceSquaredExponentialIso.cs
- Timestamp:
- 12/01/12 19:02:47 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceSquaredExponentialIso.cs
r8929 r8982 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Linq.Expressions; 24 25 using HeuristicLab.Common; 25 26 using HeuristicLab.Core; 26 27 using HeuristicLab.Data; 28 using HeuristicLab.Parameters; 27 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 30 … … 32 34 Description = "Isotropic squared exponential covariance function for Gaussian processes.")] 33 35 public sealed class CovarianceSquaredExponentialIso : ParameterizedNamedItem, ICovarianceFunction { 34 [Storable] 35 private double sf2; 36 [Storable] 37 private readonly HyperParameter<DoubleValue> scaleParameter; 38 public IValueParameter<DoubleValue> ScaleParameter { get { return scaleParameter; } } 36 public IValueParameter<DoubleValue> ScaleParameter { 37 get { return (IValueParameter<DoubleValue>)Parameters["Scale"]; } 38 } 39 39 40 [Storable] 41 private double inverseLength; 42 [Storable] 43 private readonly HyperParameter<DoubleValue> inverseLengthParameter; 44 public IValueParameter<DoubleValue> InverseLengthParameter { get { return inverseLengthParameter; } } 40 public IValueParameter<DoubleValue> InverseLengthParameter { 41 get { return (IValueParameter<DoubleValue>)Parameters["InverseLength"]; } 42 } 45 43 46 44 [StorableConstructor] … … 51 49 private CovarianceSquaredExponentialIso(CovarianceSquaredExponentialIso original, Cloner cloner) 52 50 : base(original, cloner) { 53 this.sf2 = original.sf2;54 this.scaleParameter = cloner.Clone(original.scaleParameter);55 56 this.inverseLength = original.inverseLength;57 this.inverseLengthParameter = cloner.Clone(original.inverseLengthParameter);58 59 RegisterEvents();60 51 } 61 52 … … 65 56 Description = ItemDescription; 66 57 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(); 58 Parameters.Add(new OptionalValueParameter<DoubleValue>("Scale", "The scale parameter of the isometric squared exponential covariance function.")); 59 Parameters.Add(new OptionalValueParameter<DoubleValue>("InverseLength", "The inverse length parameter of the isometric squared exponential covariance function.")); 74 60 } 75 61 … … 78 64 } 79 65 80 [StorableHook(HookType.AfterDeserialization)] 81 private void AfterDeserialization() { 82 RegisterEvents(); 66 public int GetNumberOfParameters(int numberOfVariables) { 67 return 68 (ScaleParameter.Value != null ? 0 : 1) + 69 (InverseLengthParameter.Value != null ? 0 : 1); 83 70 } 84 71 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 90 public int GetNumberOfParameters(int numberOfVariables) { 91 return 92 (scaleParameter.Fixed ? 0 : 1) + 93 (inverseLengthParameter.Fixed ? 0 : 1); 94 } 95 96 public void SetParameter(double[] hyp) { 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 CovarianceSquaredExponentialIso", "hyp"); 72 public void SetParameter(double[] p) { 73 double scale, inverseLength; 74 GetParameterValues(p, out scale, out inverseLength); 75 ScaleParameter.Value = new DoubleValue(scale); 76 InverseLengthParameter.Value = new DoubleValue(inverseLength); 107 77 } 108 78 109 79 110 public double GetCovariance(double[,] x, int i, int j, IEnumerable<int> columnIndices) { 111 double d = i == j 112 ? 0.0 113 : Util.SqrDist(x, i, j, inverseLength, columnIndices); 114 return sf2 * Math.Exp(-d / 2.0); 80 private void GetParameterValues(double[] p, out double scale, out double inverseLength) { 81 // gather parameter values 82 int c = 0; 83 if (InverseLengthParameter.Value != null) { 84 inverseLength = InverseLengthParameter.Value.Value; 85 } else { 86 inverseLength = 1.0 / Math.Exp(p[c]); 87 c++; 88 } 89 90 if (ScaleParameter.Value != null) { 91 scale = ScaleParameter.Value.Value; 92 } else { 93 scale = Math.Exp(2 * p[c]); 94 c++; 95 } 96 if (p.Length != c) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceSquaredExponentialIso", "p"); 115 97 } 116 98 117 public IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices) { 99 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int> columnIndices) { 100 double inverseLength, scale; 101 GetParameterValues(p, out scale, out inverseLength); 102 // create functions 103 var cov = new ParameterizedCovarianceFunction(); 104 cov.Covariance = (x, i, j) => { 105 double d = i == j 106 ? 0.0 107 : Util.SqrDist(x, i, j, inverseLength, columnIndices); 108 return scale * Math.Exp(-d / 2.0); 109 }; 110 cov.CrossCovariance = (x, xt, i, j) => { 111 double d = Util.SqrDist(x, i, xt, j, inverseLength, columnIndices); 112 return scale * Math.Exp(-d / 2.0); 113 }; 114 cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, scale, inverseLength, columnIndices); 115 return cov; 116 } 117 118 private static IEnumerable<double> GetGradient(double[,] x, int i, int j, double sf2, double inverseLength, IEnumerable<int> columnIndices) { 118 119 double d = i == j 119 120 ? 0.0 … … 123 124 yield return 2.0 * sf2 * g; 124 125 } 125 126 public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j, IEnumerable<int> columnIndices) {127 double d = Util.SqrDist(x, i, xt, j, inverseLength, columnIndices);128 return sf2 * Math.Exp(-d / 2.0);129 }130 126 } 131 127 }
Note: See TracChangeset
for help on using the changeset viewer.