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