Changeset 8982 for trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceConst.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/CovarianceConst.cs
r8929 r8982 25 25 using HeuristicLab.Core; 26 26 using HeuristicLab.Data; 27 using HeuristicLab.Parameters; 27 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 29 … … 32 33 Description = "Constant covariance function for Gaussian processes.")] 33 34 public sealed class CovarianceConst : ParameterizedNamedItem, ICovarianceFunction { 34 35 [Storable]36 private double scale;37 [Storable]38 private readonly HyperParameter<DoubleValue> scaleParameter;39 35 public IValueParameter<DoubleValue> ScaleParameter { 40 get { return scaleParameter; }36 get { return (IValueParameter<DoubleValue>)Parameters["Scale"]; } 41 37 } 42 38 … … 48 44 private CovarianceConst(CovarianceConst original, Cloner cloner) 49 45 : base(original, cloner) { 50 this.scaleParameter = cloner.Clone(original.scaleParameter);51 this.scale = original.scale;52 53 RegisterEvents();54 46 } 55 47 … … 59 51 Description = ItemDescription; 60 52 61 scaleParameter = new HyperParameter<DoubleValue>("Scale", "The scale of the constant covariance function."); 62 Parameters.Add(scaleParameter); 63 RegisterEvents(); 53 Parameters.Add(new OptionalValueParameter<DoubleValue>("Scale", "The scale of the constant covariance function.")); 64 54 } 65 66 [StorableHook(HookType.AfterDeserialization)]67 private void AfterDeserialization() {68 RegisterEvents();69 }70 71 // caching72 private void RegisterEvents() {73 Util.AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { scale = scaleParameter.Value.Value; });74 }75 76 55 77 56 public override IDeepCloneable Clone(Cloner cloner) { … … 80 59 81 60 public int GetNumberOfParameters(int numberOfVariables) { 82 return scaleParameter.Fixed? 0 : 1;61 return ScaleParameter.Value != null ? 0 : 1; 83 62 } 84 63 85 public void SetParameter(double[] hyp) { 86 if (!scaleParameter.Fixed && hyp.Length == 1) { 87 scaleParameter.SetValue(new DoubleValue(Math.Exp(2 * hyp[0]))); 88 } else { 89 throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceConst", "hyp"); 90 } 64 public void SetParameter(double[] p) { 65 double scale; 66 GetParameterValues(p, out scale); 67 ScaleParameter.Value = new DoubleValue(scale); 91 68 } 92 69 93 public double GetCovariance(double[,] x, int i, int j, IEnumerable<int> columnIndices) { 94 return scale; 70 private void GetParameterValues(double[] p, out double scale) { 71 int c = 0; 72 // gather parameter values 73 if (ScaleParameter.Value != null) { 74 scale = ScaleParameter.Value.Value; 75 } else { 76 scale = Math.Exp(2 * p[c]); 77 c++; 78 } 79 if (p.Length != c) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceConst", "p"); 95 80 } 96 81 97 public IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices) { 98 yield return 2.0 * scale; 82 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int> columnIndices) { 83 double scale; 84 GetParameterValues(p, out scale); 85 // create functions 86 var cov = new ParameterizedCovarianceFunction(); 87 cov.Covariance = (x, i, j) => scale; 88 cov.CrossCovariance = (x, xt, i, j) => scale; 89 cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, scale, columnIndices); 90 return cov; 99 91 } 100 92 101 p ublic double GetCrossCovariance(double[,] x, double[,] xt, int i, int j, IEnumerable<int> columnIndices) {102 returnscale;93 private static IEnumerable<double> GetGradient(double[,] x, int i, int j, double scale, IEnumerable<int> columnIndices) { 94 yield return 2.0 * scale; 103 95 } 104 96 }
Note: See TracChangeset
for help on using the changeset viewer.