Changeset 8612
- Timestamp:
- 09/10/12 13:28:55 (12 years ago)
- Location:
- trunk/sources
- Files:
-
- 1 deleted
- 23 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceConst.cs
r8582 r8612 31 31 [Item(Name = "CovarianceConst", 32 32 Description = "Constant covariance function for Gaussian processes.")] 33 public class CovarianceConst :CovarianceFunction {33 public sealed class CovarianceConst : ParameterizedNamedItem, ICovarianceFunction { 34 34 35 [Storable] 36 private double scale; 37 [Storable] 38 private readonly HyperParameter<DoubleValue> scaleParameter; 35 39 public IValueParameter<DoubleValue> ScaleParameter { 36 40 get { return scaleParameter; } 37 41 } 38 42 39 [Storable]40 private readonly HyperParameter<DoubleValue> scaleParameter;41 42 [Storable]43 private double scale;44 45 43 [StorableConstructor] 46 pr otectedCovarianceConst(bool deserializing)44 private CovarianceConst(bool deserializing) 47 45 : base(deserializing) { 48 46 } 49 47 50 pr otectedCovarianceConst(CovarianceConst original, Cloner cloner)48 private CovarianceConst(CovarianceConst original, Cloner cloner) 51 49 : base(original, cloner) { 52 50 this.scaleParameter = cloner.Clone(original.scaleParameter); … … 58 56 public CovarianceConst() 59 57 : base() { 58 Name = ItemName; 59 Description = ItemDescription; 60 60 61 scaleParameter = new HyperParameter<DoubleValue>("Scale", "The scale of the constant covariance function."); 61 62 Parameters.Add(scaleParameter); … … 70 71 // caching 71 72 private void RegisterEvents() { 72 AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { scale = scaleParameter.Value.Value; });73 Util.AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { scale = scaleParameter.Value.Value; }); 73 74 } 74 75 … … 78 79 } 79 80 80 public overrideint GetNumberOfParameters(int numberOfVariables) {81 public int GetNumberOfParameters(int numberOfVariables) { 81 82 return scaleParameter.Fixed ? 0 : 1; 82 83 } 83 84 84 public overridevoid SetParameter(double[] hyp) {85 public void SetParameter(double[] hyp) { 85 86 if (!scaleParameter.Fixed && hyp.Length == 1) { 86 87 scaleParameter.SetValue(new DoubleValue(Math.Exp(2 * hyp[0]))); … … 90 91 } 91 92 92 public overridedouble GetCovariance(double[,] x, int i, int j) {93 public double GetCovariance(double[,] x, int i, int j) { 93 94 return scale; 94 95 } 95 96 96 public overrideIEnumerable<double> GetGradient(double[,] x, int i, int j) {97 public IEnumerable<double> GetGradient(double[,] x, int i, int j) { 97 98 yield return 2.0 * scale; 98 99 } 99 100 100 public overridedouble GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {101 public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) { 101 102 return scale; 102 103 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceLinear.cs
r8582 r8612 29 29 [StorableClass] 30 30 [Item(Name = "CovarianceLinear", Description = "Linear covariance function for Gaussian processes.")] 31 public class CovarianceLinear : CovarianceFunction { 32 public override int GetNumberOfParameters(int numberOfVariables) { 33 return 0; 34 } 31 public sealed class CovarianceLinear : Item, ICovarianceFunction { 35 32 [StorableConstructor] 36 pr otectedCovarianceLinear(bool deserializing) : base(deserializing) { }37 pr otectedCovarianceLinear(CovarianceLinear original, Cloner cloner)33 private CovarianceLinear(bool deserializing) : base(deserializing) { } 34 private CovarianceLinear(CovarianceLinear original, Cloner cloner) 38 35 : base(original, cloner) { 39 36 } … … 46 43 } 47 44 48 public override void SetParameter(double[] hyp) { 45 public int GetNumberOfParameters(int numberOfVariables) { 46 return 0; 47 } 48 49 public void SetParameter(double[] hyp) { 49 50 if (hyp.Length > 0) throw new ArgumentException("No hyperparameters are allowed for the linear covariance function."); 50 51 } 51 52 52 public overridedouble GetCovariance(double[,] x, int i, int j) {53 public double GetCovariance(double[,] x, int i, int j) { 53 54 return Util.ScalarProd(x, i, j); 54 55 } 55 56 56 public overrideIEnumerable<double> GetGradient(double[,] x, int i, int j) {57 public IEnumerable<double> GetGradient(double[,] x, int i, int j) { 57 58 yield break; 58 59 } 59 60 60 public overridedouble GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {61 public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) { 61 62 return Util.ScalarProd(x, i, xt, j); 62 63 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceLinearArd.cs
r8582 r8612 32 32 [Item(Name = "CovarianceLinearArd", 33 33 Description = "Linear covariance function with automatic relevance determination for Gaussian processes.")] 34 public class CovarianceLinearArd : CovarianceFunction { 34 public sealed class CovarianceLinearArd : ParameterizedNamedItem, ICovarianceFunction { 35 [Storable] 36 private double[] inverseLength; 37 [Storable] 38 private readonly HyperParameter<DoubleArray> inverseLengthParameter; 35 39 public IValueParameter<DoubleArray> InverseLengthParameter { 36 40 get { return inverseLengthParameter; } 37 41 } 38 42 39 [Storable]40 private HyperParameter<DoubleArray> inverseLengthParameter;41 42 [Storable]43 private double[] inverseLength;44 45 43 [StorableConstructor] 46 pr otectedCovarianceLinearArd(bool deserializing) : base(deserializing) { }47 pr otectedCovarianceLinearArd(CovarianceLinearArd original, Cloner cloner)44 private CovarianceLinearArd(bool deserializing) : base(deserializing) { } 45 private CovarianceLinearArd(CovarianceLinearArd original, Cloner cloner) 48 46 : base(original, cloner) { 49 47 inverseLengthParameter = cloner.Clone(original.inverseLengthParameter); … … 57 55 public CovarianceLinearArd() 58 56 : base() { 57 Name = ItemName; 58 Description = ItemDescription; 59 59 60 inverseLengthParameter = new HyperParameter<DoubleArray>("InverseLength", 60 61 "The inverse length parameter for ARD."); … … 74 75 // caching 75 76 private void RegisterEvents() { 76 AttachArrayChangeHandler<DoubleArray, double>(inverseLengthParameter, () => { inverseLength = inverseLengthParameter.Value.ToArray(); });77 Util.AttachArrayChangeHandler<DoubleArray, double>(inverseLengthParameter, () => { inverseLength = inverseLengthParameter.Value.ToArray(); }); 77 78 } 78 79 79 80 80 public overrideint GetNumberOfParameters(int numberOfVariables) {81 public int GetNumberOfParameters(int numberOfVariables) { 81 82 if (!inverseLengthParameter.Fixed) 82 83 return numberOfVariables; … … 85 86 } 86 87 87 public overridevoid SetParameter(double[] hyp) {88 public void SetParameter(double[] hyp) { 88 89 if (!inverseLengthParameter.Fixed && hyp.Length > 0) { 89 90 inverseLengthParameter.SetValue(new DoubleArray(hyp.Select(e => 1.0 / Math.Exp(e)).ToArray())); … … 91 92 } 92 93 93 public overridedouble GetCovariance(double[,] x, int i, int j) {94 public double GetCovariance(double[,] x, int i, int j) { 94 95 return Util.ScalarProd(x, i, j, inverseLength); 95 96 } 96 97 97 public overrideIEnumerable<double> GetGradient(double[,] x, int i, int j) {98 public IEnumerable<double> GetGradient(double[,] x, int i, int j) { 98 99 for (int k = 0; k < inverseLength.Length; k++) { 99 100 yield return -2.0 * x[i, k] * x[j, k] * inverseLength[k] * inverseLength[k]; … … 101 102 } 102 103 103 public overridedouble GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {104 public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) { 104 105 return Util.ScalarProd(x, i, xt, j, inverseLength); 105 106 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceMaternIso.cs
r8583 r8612 33 33 [Item(Name = "CovarianceMaternIso", 34 34 Description = "Matern covariance function for Gaussian processes.")] 35 public class CovarianceMaternIso : CovarianceFunction { 35 public sealed class CovarianceMaternIso : ParameterizedNamedItem, ICovarianceFunction { 36 [Storable] 37 private double inverseLength; 38 [Storable] 39 private readonly HyperParameter<DoubleValue> inverseLengthParameter; 40 public IValueParameter<DoubleValue> InverseLengthParameter { 41 get { return inverseLengthParameter; } 42 } 43 44 [Storable] 45 private double sf2; 46 [Storable] 47 private readonly HyperParameter<DoubleValue> scaleParameter; 36 48 public IValueParameter<DoubleValue> ScaleParameter { 37 49 get { return scaleParameter; } 38 50 } 39 public IValueParameter<DoubleValue> InverseLengthParameter { 40 get { return inverseLengthParameter; } 41 } 51 52 [Storable] 53 private int d; 54 [Storable] 55 private readonly ConstrainedValueParameter<IntValue> dParameter; 42 56 public IConstrainedValueParameter<IntValue> DParameter { 43 57 get { return dParameter; } 44 58 } 45 59 46 [Storable]47 private readonly HyperParameter<DoubleValue> inverseLengthParameter;48 [Storable]49 private readonly HyperParameter<DoubleValue> scaleParameter;50 [Storable]51 private readonly ConstrainedValueParameter<IntValue> dParameter;52 53 [Storable]54 private double inverseLength;55 [Storable]56 private double sf2;57 [Storable]58 private int d;59 60 60 61 [StorableConstructor] 61 pr otectedCovarianceMaternIso(bool deserializing)62 private CovarianceMaternIso(bool deserializing) 62 63 : base(deserializing) { 63 64 } 64 65 65 pr otectedCovarianceMaternIso(CovarianceMaternIso original, Cloner cloner)66 private CovarianceMaternIso(CovarianceMaternIso original, Cloner cloner) 66 67 : base(original, cloner) { 67 68 this.scaleParameter = cloner.Clone(original.scaleParameter); … … 76 77 public CovarianceMaternIso() 77 78 : base() { 79 Name = ItemName; 80 Description = ItemDescription; 81 78 82 inverseLengthParameter = new HyperParameter<DoubleValue>("InverseLength", "The inverse length parameter of the isometric Matern covariance function."); 79 83 scaleParameter = new HyperParameter<DoubleValue>("Scale", "The scale parameter of the isometric Matern covariance function."); … … 103 107 // caching 104 108 private void RegisterEvents() { 105 AttachValueChangeHandler<DoubleValue, double>(inverseLengthParameter, () => { inverseLength = inverseLengthParameter.Value.Value; });106 AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { sf2 = scaleParameter.Value.Value; });107 AttachValueChangeHandler<IntValue, int>(dParameter, () => { d = dParameter.Value.Value; });109 Util.AttachValueChangeHandler<DoubleValue, double>(inverseLengthParameter, () => { inverseLength = inverseLengthParameter.Value.Value; }); 110 Util.AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { sf2 = scaleParameter.Value.Value; }); 111 Util.AttachValueChangeHandler<IntValue, int>(dParameter, () => { d = dParameter.Value.Value; }); 108 112 } 109 113 110 public overrideint GetNumberOfParameters(int numberOfVariables) {114 public int GetNumberOfParameters(int numberOfVariables) { 111 115 return 112 116 (inverseLengthParameter.Fixed ? 0 : 1) + … … 114 118 } 115 119 116 public overridevoid SetParameter(double[] hyp) {120 public void SetParameter(double[] hyp) { 117 121 int i = 0; 118 122 if (!inverseLengthParameter.Fixed) { … … 150 154 } 151 155 152 public overridedouble GetCovariance(double[,] x, int i, int j) {156 public double GetCovariance(double[,] x, int i, int j) { 153 157 double dist = i == j 154 158 ? 0.0 … … 157 161 } 158 162 159 public overrideIEnumerable<double> GetGradient(double[,] x, int i, int j) {163 public IEnumerable<double> GetGradient(double[,] x, int i, int j) { 160 164 double dist = i == j 161 165 ? 0.0 … … 166 170 } 167 171 168 public overridedouble GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {172 public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) { 169 173 double dist = Math.Sqrt(Util.SqrDist(x, i, xt, j, Math.Sqrt(d) * inverseLength)); 170 174 return sf2 * m(dist); -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceNoise.cs
r8484 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 = "CovarianceNoise", 31 32 Description = "Noise covariance function for Gaussian processes.")] 32 public class CovarianceNoise : Item, ICovarianceFunction { 33 public sealed class CovarianceNoise : ParameterizedNamedItem, ICovarianceFunction { 34 35 33 36 [Storable] 34 37 private double sf2; 35 public double Scale { get { return sf2; } } 38 [Storable] 39 private readonly HyperParameter<DoubleValue> scaleParameter; 40 public IValueParameter<DoubleValue> ScaleParameter { 41 get { return scaleParameter; } 42 } 36 43 37 44 [StorableConstructor] 38 pr otectedCovarianceNoise(bool deserializing)45 private CovarianceNoise(bool deserializing) 39 46 : base(deserializing) { 40 47 } 41 48 42 pr otectedCovarianceNoise(CovarianceNoise original, Cloner cloner)49 private CovarianceNoise(CovarianceNoise original, Cloner cloner) 43 50 : base(original, cloner) { 51 this.scaleParameter = cloner.Clone(original.scaleParameter); 44 52 this.sf2 = original.sf2; 53 RegisterEvents(); 45 54 } 46 55 47 56 public CovarianceNoise() 48 57 : base() { 58 Name = ItemName; 59 Description = ItemDescription; 60 61 this.scaleParameter = new HyperParameter<DoubleValue>("Scale", "The scale of noise."); 62 Parameters.Add(this.scaleParameter); 63 64 RegisterEvents(); 49 65 } 50 66 … … 53 69 } 54 70 71 [StorableHook(HookType.AfterDeserialization)] 72 private void AfterDeserialization() { 73 RegisterEvents(); 74 } 75 76 private void RegisterEvents() { 77 Util.AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { sf2 = scaleParameter.Value.Value; }); 78 } 79 55 80 public int GetNumberOfParameters(int numberOfVariables) { 56 return 1;81 return scaleParameter.Fixed ? 0 : 1; 57 82 } 58 83 59 84 public void SetParameter(double[] hyp) { 60 this.sf2 = Math.Exp(2 * hyp[0]); 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 } 61 90 } 62 91 -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovariancePeriodic.cs
r8582 r8612 31 31 [StorableClass] 32 32 [Item(Name = "CovariancePeriodic", Description = "Periodic covariance function for Gaussian processes.")] 33 public class CovariancePeriodic : CovarianceFunction { 33 public sealed class CovariancePeriodic : ParameterizedNamedItem, ICovarianceFunction { 34 35 [Storable] 36 private double scale; 37 [Storable] 38 private readonly HyperParameter<DoubleValue> scaleParameter; 34 39 public IValueParameter<DoubleValue> ScaleParameter { 35 40 get { return scaleParameter; } 36 41 } 42 43 [Storable] 44 private double inverseLength; 45 [Storable] 46 private readonly HyperParameter<DoubleValue> inverseLengthParameter; 37 47 public IValueParameter<DoubleValue> InverseLengthParameter { 38 48 get { return inverseLengthParameter; } 39 49 } 50 51 [Storable] 52 private double period; 53 [Storable] 54 private readonly HyperParameter<DoubleValue> periodParameter; 40 55 public IValueParameter<DoubleValue> PeriodParameter { 41 56 get { return periodParameter; } 42 57 } 43 58 44 [Storable]45 private HyperParameter<DoubleValue> scaleParameter;46 [Storable]47 private HyperParameter<DoubleValue> inverseLengthParameter;48 [Storable]49 private HyperParameter<DoubleValue> periodParameter;50 51 [Storable]52 private double scale;53 [Storable]54 private double inverseLength;55 [Storable]56 private double period;57 58 59 59 60 [StorableConstructor] 60 pr otectedCovariancePeriodic(bool deserializing) : base(deserializing) { }61 pr otectedCovariancePeriodic(CovariancePeriodic original, Cloner cloner)61 private CovariancePeriodic(bool deserializing) : base(deserializing) { } 62 private CovariancePeriodic(CovariancePeriodic original, Cloner cloner) 62 63 : base(original, cloner) { 63 64 this.scaleParameter = cloner.Clone(original.scaleParameter); … … 73 74 public CovariancePeriodic() 74 75 : base() { 76 Name = ItemName; 77 Description = ItemDescription; 78 75 79 scaleParameter = new HyperParameter<DoubleValue>("Scale", "The scale of the periodic covariance function."); 76 80 inverseLengthParameter = new HyperParameter<DoubleValue>("InverseLength", "The inverse length parameter for the periodic covariance function."); … … 94 98 // caching 95 99 private void RegisterEvents() { 96 AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { scale = scaleParameter.Value.Value; });97 AttachValueChangeHandler<DoubleValue, double>(inverseLengthParameter, () => { inverseLength = inverseLengthParameter.Value.Value; });98 AttachValueChangeHandler<DoubleValue, double>(periodParameter, () => { period = periodParameter.Value.Value; });100 Util.AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { scale = scaleParameter.Value.Value; }); 101 Util.AttachValueChangeHandler<DoubleValue, double>(inverseLengthParameter, () => { inverseLength = inverseLengthParameter.Value.Value; }); 102 Util.AttachValueChangeHandler<DoubleValue, double>(periodParameter, () => { period = periodParameter.Value.Value; }); 99 103 } 100 104 101 public overrideint GetNumberOfParameters(int numberOfVariables) {105 public int GetNumberOfParameters(int numberOfVariables) { 102 106 return 103 107 (new[] { scaleParameter, inverseLengthParameter, periodParameter }).Count(p => !p.Fixed); 104 108 } 105 109 106 public overridevoid SetParameter(double[] hyp) {110 public void SetParameter(double[] hyp) { 107 111 int i = 0; 108 112 if (!inverseLengthParameter.Fixed) { … … 121 125 } 122 126 123 public overridedouble GetCovariance(double[,] x, int i, int j) {127 public double GetCovariance(double[,] x, int i, int j) { 124 128 double k = i == j ? 0.0 : GetDistance(x, x, i, j); 125 129 k = Math.PI * k / period; … … 130 134 } 131 135 132 public overrideIEnumerable<double> GetGradient(double[,] x, int i, int j) {136 public IEnumerable<double> GetGradient(double[,] x, int i, int j) { 133 137 double v = i == j ? 0.0 : Math.PI * GetDistance(x, x, i, j) / period; 134 138 double gradient = Math.Sin(v) * inverseLength; … … 140 144 } 141 145 142 public overridedouble GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {146 public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) { 143 147 double k = GetDistance(x, xt, i, j); 144 148 k = Math.PI * k / period; -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceProd.cs
r8489 r8612 31 31 [Item(Name = "CovarianceProd", 32 32 Description = "Product covariance function for Gaussian processes.")] 33 public class CovarianceProd : Item, ICovarianceFunction {33 public sealed class CovarianceProd : Item, ICovarianceFunction { 34 34 [Storable] 35 35 private ItemList<ICovarianceFunction> factors; … … 42 42 43 43 [StorableConstructor] 44 pr otectedCovarianceProd(bool deserializing)44 private CovarianceProd(bool deserializing) 45 45 : base(deserializing) { 46 46 } 47 47 48 pr otectedCovarianceProd(CovarianceProd original, Cloner cloner)48 private CovarianceProd(CovarianceProd original, Cloner cloner) 49 49 : base(original, cloner) { 50 50 this.factors = cloner.Clone(original.factors); -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceRQArd.cs
r8565 r8612 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 = "CovarianceRQArd", 32 33 Description = "Rational quadratic covariance function with automatic relevance determination for Gaussian processes.")] 33 public class CovarianceRQArd :Item, ICovarianceFunction {34 public sealed class CovarianceRQArd : ParameterizedNamedItem, ICovarianceFunction { 34 35 [Storable] 35 36 private double sf2; 36 public double Scale { get { return sf2; } } 37 [Storable] 38 private readonly HyperParameter<DoubleValue> scaleParameter; 39 public IValueParameter<DoubleValue> ScaleParameter { 40 get { return scaleParameter; } 41 } 42 37 43 [Storable] 38 44 private double[] inverseLength; 39 public double[] InverseLength { 40 get { 41 if (inverseLength == null) return null; 42 double[] res = new double[inverseLength.Length]; 43 Array.Copy(inverseLength, res, res.Length); 44 return res; 45 } 45 [Storable] 46 private readonly HyperParameter<DoubleArray> inverseLengthParameter; 47 public IValueParameter<DoubleArray> InverseLengthParameter { 48 get { return inverseLengthParameter; } 46 49 } 50 47 51 [Storable] 48 private double alpha; 49 public double Shape { get { return alpha; } } 52 private double shape; 53 [Storable] 54 private readonly HyperParameter<DoubleValue> shapeParameter; 55 public IValueParameter<DoubleValue> ShapeParameter { 56 get { return shapeParameter; } 57 } 50 58 51 59 [StorableConstructor] 52 pr otectedCovarianceRQArd(bool deserializing)60 private CovarianceRQArd(bool deserializing) 53 61 : base(deserializing) { 54 62 } 55 63 56 pr otectedCovarianceRQArd(CovarianceRQArd original, Cloner cloner)64 private CovarianceRQArd(CovarianceRQArd original, Cloner cloner) 57 65 : base(original, cloner) { 66 this.scaleParameter = cloner.Clone(original.scaleParameter); 58 67 this.sf2 = original.sf2; 59 this.inverseLength = original.InverseLength; // array is cloned in the getter 60 this.alpha = original.alpha; 68 69 this.inverseLengthParameter = cloner.Clone(original.inverseLengthParameter); 70 if (original.inverseLength != null) { 71 this.inverseLength = new double[original.inverseLength.Length]; 72 Array.Copy(original.inverseLength, inverseLength, inverseLength.Length); 73 } 74 75 this.shapeParameter = cloner.Clone(original.shapeParameter); 76 this.shape = original.shape; 77 78 RegisterEvents(); 61 79 } 62 80 63 81 public CovarianceRQArd() 64 82 : base() { 83 Name = ItemName; 84 Description = ItemDescription; 85 86 this.scaleParameter = new HyperParameter<DoubleValue>("Scale", "The scale parameter of the rational quadratic covariance function with ARD."); 87 this.inverseLengthParameter = new HyperParameter<DoubleArray>("InverseLength", "The inverse length parameter for automatic relevance determination."); 88 this.shapeParameter = new HyperParameter<DoubleValue>("Shape", "The shape parameter (alpha) of the rational quadratic covariance function with ARD."); 89 90 Parameters.Add(scaleParameter); 91 Parameters.Add(inverseLengthParameter); 92 Parameters.Add(shapeParameter); 93 94 RegisterEvents(); 65 95 } 66 96 … … 69 99 } 70 100 101 [StorableHook(HookType.AfterDeserialization)] 102 private void AfterDeserialization() { 103 RegisterEvents(); 104 } 105 106 private void RegisterEvents() { 107 Util.AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { sf2 = scaleParameter.Value.Value; }); 108 Util.AttachValueChangeHandler<DoubleValue, double>(shapeParameter, () => { shape = shapeParameter.Value.Value; }); 109 Util.AttachArrayChangeHandler<DoubleArray, double>(inverseLengthParameter, () => { inverseLength = inverseLengthParameter.Value.ToArray(); }); 110 } 111 71 112 public int GetNumberOfParameters(int numberOfVariables) { 72 return numberOfVariables + 2; 113 return 114 (scaleParameter.Fixed ? 0 : 1) + 115 (shapeParameter.Fixed ? 0 : 1) + 116 (inverseLengthParameter.Fixed ? 0 : numberOfVariables); 73 117 } 74 118 75 119 public void SetParameter(double[] hyp) { 76 this.inverseLength = hyp.Take(hyp.Length - 2).Select(e => 1.0 / Math.Exp(e)).ToArray(); 77 this.sf2 = Math.Exp(2 * hyp[hyp.Length - 2]); 78 this.alpha = Math.Exp(hyp[hyp.Length - 1]); 120 int i = 0; 121 if (!scaleParameter.Fixed) { 122 scaleParameter.SetValue(new DoubleValue(Math.Exp(2 * hyp[i]))); 123 i++; 124 } 125 if (!shapeParameter.Fixed) { 126 shapeParameter.SetValue(new DoubleValue(Math.Exp(hyp[i]))); 127 i++; 128 } 129 if (!inverseLengthParameter.Fixed) { 130 inverseLengthParameter.SetValue(new DoubleArray(hyp.Skip(i).Select(e => 1.0 / Math.Exp(e)).ToArray())); 131 i += hyp.Skip(i).Count(); 132 } 133 if (hyp.Length != i) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceRQArd", "hyp"); 79 134 } 80 135 … … 84 139 ? 0.0 85 140 : Util.SqrDist(x, i, j, inverseLength); 86 return sf2 * Math.Pow(1 + 0.5 * d / alpha, -alpha);141 return sf2 * Math.Pow(1 + 0.5 * d / shape, -shape); 87 142 } 88 143 … … 91 146 ? 0.0 92 147 : Util.SqrDist(x, i, j, inverseLength); 93 double b = 1 + 0.5 * d / alpha;148 double b = 1 + 0.5 * d / shape; 94 149 for (int k = 0; k < inverseLength.Length; k++) { 95 yield return sf2 * Math.Pow(b, - alpha- 1) * Util.SqrDist(x[i, k] * inverseLength[k], x[j, k] * inverseLength[k]);150 yield return sf2 * Math.Pow(b, -shape - 1) * Util.SqrDist(x[i, k] * inverseLength[k], x[j, k] * inverseLength[k]); 96 151 } 97 yield return 2 * sf2 * Math.Pow(b, - alpha);98 yield return sf2 * Math.Pow(b, - alpha) * (0.5 * d / b - alpha* Math.Log(b));152 yield return 2 * sf2 * Math.Pow(b, -shape); 153 yield return sf2 * Math.Pow(b, -shape) * (0.5 * d / b - shape * Math.Log(b)); 99 154 } 100 155 101 156 public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) { 102 157 double d = Util.SqrDist(x, i, xt, j, inverseLength); 103 return sf2 * Math.Pow(1 + 0.5 * d / alpha, -alpha);158 return sf2 * Math.Pow(1 + 0.5 * d / shape, -shape); 104 159 } 105 160 } -
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 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceSEard.cs
r8565 r8612 25 25 using HeuristicLab.Common; 26 26 using HeuristicLab.Core; 27 using HeuristicLab.Data; 27 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 29 … … 30 31 [StorableClass] 31 32 [Item(Name = "CovarianceSEard", Description = "Squared exponential covariance function with automatic relevance determination for Gaussian processes.")] 32 public class CovarianceSEard :Item, ICovarianceFunction {33 public sealed class CovarianceSEard : 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; } } 36 39 37 40 [Storable] 38 41 private double[] inverseLength; 39 public double[] InverseLength { 40 get { 41 if (inverseLength == null) return new double[0]; 42 var copy = new double[inverseLength.Length]; 43 Array.Copy(inverseLength, copy, copy.Length); 44 return copy; 42 [Storable] 43 private readonly HyperParameter<DoubleArray> inverseLengthParameter; 44 public IValueParameter<DoubleArray> InverseLengthParameter { get { return inverseLengthParameter; } } 45 46 [StorableConstructor] 47 private CovarianceSEard(bool deserializing) : base(deserializing) { } 48 private CovarianceSEard(CovarianceSEard original, Cloner cloner) 49 : base(original, cloner) { 50 this.sf2 = original.sf2; 51 this.scaleParameter = cloner.Clone(original.scaleParameter); 52 53 if (original.inverseLength != null) { 54 this.inverseLength = new double[original.inverseLength.Length]; 55 Array.Copy(original.inverseLength, this.inverseLength, this.inverseLength.Length); 45 56 } 46 }57 this.inverseLengthParameter = cloner.Clone(original.inverseLengthParameter); 47 58 48 public int GetNumberOfParameters(int numberOfVariables) { 49 return numberOfVariables + 1; 50 } 51 [StorableConstructor] 52 protected CovarianceSEard(bool deserializing) : base(deserializing) { } 53 protected CovarianceSEard(CovarianceSEard original, Cloner cloner) 54 : base(original, cloner) { 55 this.inverseLength = original.InverseLength; // array is cloned in the getter 56 this.sf2 = original.sf2; 59 RegisterEvents(); 57 60 } 58 61 public CovarianceSEard() 59 62 : base() { 63 Name = ItemName; 64 Description = ItemDescription; 65 66 this.scaleParameter = new HyperParameter<DoubleValue>("Scale", "The scale parameter of the squared exponential covariance function with ARD."); 67 this.inverseLengthParameter = new HyperParameter<DoubleArray>("InverseLength", "The inverse length parameter for automatic relevance determination."); 68 69 Parameters.Add(scaleParameter); 70 Parameters.Add(inverseLengthParameter); 71 72 RegisterEvents(); 60 73 } 61 74 … … 64 77 } 65 78 79 [StorableHook(HookType.AfterDeserialization)] 80 private void AfterDeserialization() { 81 RegisterEvents(); 82 } 83 84 private void RegisterEvents() { 85 Util.AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { sf2 = scaleParameter.Value.Value; }); 86 Util.AttachArrayChangeHandler<DoubleArray, double>(inverseLengthParameter, () => { 87 inverseLength = 88 inverseLengthParameter.Value.ToArray(); 89 }); 90 } 91 92 public int GetNumberOfParameters(int numberOfVariables) { 93 return 94 (scaleParameter.Fixed ? 0 : 1) + 95 (inverseLengthParameter.Fixed ? 0 : numberOfVariables); 96 } 97 98 66 99 public void SetParameter(double[] hyp) { 67 this.inverseLength = hyp.Take(hyp.Length - 1).Select(p => 1.0 / Math.Exp(p)).ToArray(); 68 this.sf2 = Math.Exp(2 * hyp[hyp.Length - 1]); 100 int i = 0; 101 if (!scaleParameter.Fixed) { 102 scaleParameter.SetValue(new DoubleValue(Math.Exp(2 * hyp[i]))); 103 i++; 104 } 105 if (!inverseLengthParameter.Fixed) { 106 inverseLengthParameter.SetValue(new DoubleArray(hyp.Skip(i).Select(e => 1.0 / Math.Exp(e)).ToArray())); 107 i += hyp.Skip(i).Count(); 108 } 109 if (hyp.Length != i) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovariancSEard", "hyp"); 69 110 } 70 111 -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceSEiso.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 = "CovarianceSEiso", 31 32 Description = "Isotropic squared exponential covariance function for Gaussian processes.")] 32 public class CovarianceSEiso :Item, ICovarianceFunction {33 public sealed class CovarianceSEiso : 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; } } 42 [Storable] 43 private readonly HyperParameter<DoubleValue> inverseLengthParameter; 44 public IValueParameter<DoubleValue> InverseLengthParameter { get { return inverseLengthParameter; } } 39 45 40 46 [StorableConstructor] 41 pr otectedCovarianceSEiso(bool deserializing)47 private CovarianceSEiso(bool deserializing) 42 48 : base(deserializing) { 43 49 } 44 50 45 pr otectedCovarianceSEiso(CovarianceSEiso original, Cloner cloner)51 private CovarianceSEiso(CovarianceSEiso original, Cloner cloner) 46 52 : base(original, cloner) { 47 53 this.sf2 = original.sf2; 54 this.scaleParameter = cloner.Clone(original.scaleParameter); 55 48 56 this.inverseLength = original.inverseLength; 57 this.inverseLengthParameter = cloner.Clone(original.inverseLengthParameter); 58 59 RegisterEvents(); 49 60 } 50 61 51 62 public CovarianceSEiso() 52 63 : base() { 64 Name = ItemName; 65 Description = ItemDescription; 66 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(); 53 74 } 54 75 … … 57 78 } 58 79 80 [StorableHook(HookType.AfterDeserialization)] 81 private void AfterDeserialization() { 82 RegisterEvents(); 83 } 84 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 59 90 public int GetNumberOfParameters(int numberOfVariables) { 60 return 2; 91 return 92 (scaleParameter.Fixed ? 0 : 1) + 93 (inverseLengthParameter.Fixed ? 0 : 1); 61 94 } 62 95 63 96 public void SetParameter(double[] hyp) { 64 if (hyp.Length != 2) throw new ArgumentException("CovarianceSEiso has two hyperparameters", "k"); 65 this.inverseLength = 1.0 / Math.Exp(hyp[0]); 66 this.sf2 = Math.Exp(2 * hyp[1]); 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 CovarianceSEiso", "hyp"); 67 107 } 68 108 -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceSum.cs
r8489 r8612 31 31 [Item(Name = "CovarianceSum", 32 32 Description = "Sum covariance function for Gaussian processes.")] 33 public class CovarianceSum : Item, ICovarianceFunction {33 public sealed class CovarianceSum : Item, ICovarianceFunction { 34 34 [Storable] 35 35 private ItemList<ICovarianceFunction> terms; … … 42 42 43 43 [StorableConstructor] 44 pr otectedCovarianceSum(bool deserializing)44 private CovarianceSum(bool deserializing) 45 45 : base(deserializing) { 46 46 } 47 47 48 pr otectedCovarianceSum(CovarianceSum original, Cloner cloner)48 private CovarianceSum(CovarianceSum original, Cloner cloner) 49 49 : base(original, cloner) { 50 50 this.terms = cloner.Clone(original.terms); -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/GaussianProcessModel.cs
r8582 r8612 137 137 l = new double[n, n]; 138 138 139 meanFunction.SetData(x);140 141 139 // calculate means and covariances 142 140 double[] m = meanFunction.GetMean(x); … … 235 233 int n = x.GetLength(0); 236 234 var Ks = new double[newN, n]; 237 meanFunction.SetData(newX);238 235 var ms = meanFunction.GetMean(newX); 239 236 for (int i = 0; i < newN; i++) { -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/HyperParameter.cs
r8592 r8612 29 29 [StorableClass] 30 30 [Item(Name = "HyperParameter", Description = "Represents a hyperparameter for Gaussian processes.")] 31 publicclass HyperParameter<T> : OptionalValueParameter<T>, IValueParameter<T> where T : class, IItem {31 internal class HyperParameter<T> : OptionalValueParameter<T>, IValueParameter<T> where T : class, IItem { 32 32 33 33 [Storable] -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/IMeanFunction.cs
r8416 r8612 25 25 int GetNumberOfParameters(int numberOfVariables); 26 26 void SetParameter(double[] hyp); 27 void SetData(double[,] x);28 27 double[] GetMean(double[,] x); 29 28 double[] GetGradients(int k, double[,] x); -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanConst.cs
r8473 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 … … 29 30 [StorableClass] 30 31 [Item(Name = "MeanConst", Description = "Constant mean function for Gaussian processes.")] 31 public class MeanConst :Item, IMeanFunction {32 public sealed class MeanConst : ParameterizedNamedItem, IMeanFunction { 32 33 [Storable] 33 34 private double c; 34 public double Value { get { return c; } } 35 [Storable] 36 private readonly HyperParameter<DoubleValue> valueParameter; 37 public IValueParameter<DoubleValue> ValueParameter { get { return valueParameter; } } 35 38 36 public int GetNumberOfParameters(int numberOfVariables) {37 return 1;38 }39 39 [StorableConstructor] 40 pr otectedMeanConst(bool deserializing) : base(deserializing) { }41 pr otectedMeanConst(MeanConst original, Cloner cloner)40 private MeanConst(bool deserializing) : base(deserializing) { } 41 private MeanConst(MeanConst original, Cloner cloner) 42 42 : base(original, cloner) { 43 43 this.c = original.c; 44 this.valueParameter = cloner.Clone(original.valueParameter); 45 RegisterEvents(); 44 46 } 45 47 public MeanConst() 46 48 : base() { 49 this.name = ItemName; 50 this.description = ItemDescription; 51 52 this.valueParameter = new HyperParameter<DoubleValue>("Value", "The constant value for the constant mean function."); 53 Parameters.Add(valueParameter); 54 RegisterEvents(); 55 } 56 57 public override IDeepCloneable Clone(Cloner cloner) { 58 return new MeanConst(this, cloner); 59 } 60 61 [StorableHook(HookType.AfterDeserialization)] 62 private void AfterDeserialization() { 63 RegisterEvents(); 64 } 65 66 private void RegisterEvents() { 67 Util.AttachValueChangeHandler<DoubleValue, double>(valueParameter, () => { c = valueParameter.Value.Value; }); 68 } 69 70 public int GetNumberOfParameters(int numberOfVariables) { 71 return valueParameter.Fixed ? 0 : 1; 47 72 } 48 73 49 74 public void SetParameter(double[] hyp) { 50 if (hyp.Length != 1) throw new ArgumentException("Only one hyper-parameter allowed for constant mean function.", "hyp"); 51 this.c = hyp[0]; 52 } 53 public void SetData(double[,] x) { 54 // nothing to do 75 if (!valueParameter.Fixed) { 76 valueParameter.SetValue(new DoubleValue(hyp[0])); 77 } else if (hyp.Length > 0) 78 throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for the constant mean function.", "hyp"); 55 79 } 56 80 … … 63 87 return Enumerable.Repeat(1.0, x.GetLength(0)).ToArray(); 64 88 } 65 66 public override IDeepCloneable Clone(Cloner cloner) {67 return new MeanConst(this, cloner);68 }69 89 } 70 90 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanLinear.cs
r8473 r8612 19 19 */ 20 20 #endregion 21 21 22 using System; 22 23 using System.Linq; 23 24 using HeuristicLab.Common; 24 25 using HeuristicLab.Core; 26 using HeuristicLab.Data; 25 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 26 28 … … 28 30 [StorableClass] 29 31 [Item(Name = "MeanLinear", Description = "Linear mean function for Gaussian processes.")] 30 public class MeanLinear :Item, IMeanFunction {32 public sealed class MeanLinear : ParameterizedNamedItem, IMeanFunction { 31 33 [Storable] 32 private double[] alpha; 33 public double[] Weights { 34 get { 35 if (alpha == null) return new double[0]; 36 var copy = new double[alpha.Length]; 37 Array.Copy(alpha, copy, copy.Length); 38 return copy; 34 private double[] weights; 35 [Storable] 36 private readonly HyperParameter<DoubleArray> weightsParameter; 37 public IValueParameter<DoubleArray> WeightsParameter { get { return weightsParameter; } } 38 39 [StorableConstructor] 40 private MeanLinear(bool deserializing) : base(deserializing) { } 41 private MeanLinear(MeanLinear original, Cloner cloner) 42 : base(original, cloner) { 43 if (original.weights != null) { 44 this.weights = new double[original.weights.Length]; 45 Array.Copy(original.weights, weights, original.weights.Length); 39 46 } 40 } 41 public int GetNumberOfParameters(int numberOfVariables) { 42 return numberOfVariables; 43 } 44 [StorableConstructor] 45 protected MeanLinear(bool deserializing) : base(deserializing) { } 46 protected MeanLinear(MeanLinear original, Cloner cloner) 47 : base(original, cloner) { 48 if (original.alpha != null) { 49 this.alpha = new double[original.alpha.Length]; 50 Array.Copy(original.alpha, alpha, original.alpha.Length); 51 } 47 weightsParameter = cloner.Clone(original.weightsParameter); 48 RegisterEvents(); 52 49 } 53 50 public MeanLinear() 54 51 : base() { 52 this.weightsParameter = new HyperParameter<DoubleArray>("Weights", "The weights parameter for the linear mean function."); 53 Parameters.Add(weightsParameter); 54 RegisterEvents(); 55 } 56 57 public override IDeepCloneable Clone(Cloner cloner) { 58 return new MeanLinear(this, cloner); 59 } 60 61 [StorableHook(HookType.AfterDeserialization)] 62 private void AfterDeserialization() { 63 RegisterEvents(); 64 } 65 66 private void RegisterEvents() { 67 Util.AttachArrayChangeHandler<DoubleArray, double>(weightsParameter, () => { 68 weights = weightsParameter.Value.ToArray(); 69 }); 70 } 71 72 public int GetNumberOfParameters(int numberOfVariables) { 73 return weightsParameter.Fixed ? 0 : numberOfVariables; 55 74 } 56 75 57 76 public void SetParameter(double[] hyp) { 58 this.alpha = new double[hyp.Length]; 59 Array.Copy(hyp, alpha, hyp.Length); 60 } 61 public void SetData(double[,] x) { 62 // nothing to do 77 if (!weightsParameter.Fixed) { 78 weightsParameter.SetValue(new DoubleArray(hyp)); 79 } else if (hyp.Length != 0) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for the linear mean function.", "hyp"); 63 80 } 64 81 65 82 public double[] GetMean(double[,] x) { 66 83 // sanity check 67 if ( alpha.Length != x.GetLength(1)) throw new ArgumentException("The number of hyperparameters must match the number of variables for the linear mean function.");84 if (weights.Length != x.GetLength(1)) throw new ArgumentException("The number of hyperparameters must match the number of variables for the linear mean function."); 68 85 int cols = x.GetLength(1); 69 86 int n = x.GetLength(0); 70 87 return (from i in Enumerable.Range(0, n) 71 let rowVector = from j in Enumerable.Range(0, cols) 72 select x[i, j] 73 select Util.ScalarProd(alpha, rowVector)) 88 let rowVector = Enumerable.Range(0, cols).Select(j => x[i, j]) 89 select Util.ScalarProd(weights, rowVector)) 74 90 .ToArray(); 75 91 } … … 79 95 int n = x.GetLength(0); 80 96 if (k > cols) throw new ArgumentException(); 81 return (from r in Enumerable.Range(0, n) 82 select x[r, k]).ToArray(); 83 } 84 85 public override IDeepCloneable Clone(Cloner cloner) { 86 return new MeanLinear(this, cloner); 97 return (Enumerable.Range(0, n).Select(r => x[r, k])).ToArray(); 87 98 } 88 99 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanProd.cs
r8463 r8612 27 27 [StorableClass] 28 28 [Item(Name = "MeanProd", Description = "Product of mean functions for Gaussian processes.")] 29 public class MeanProd : Item, IMeanFunction {29 public sealed class MeanProd : Item, IMeanFunction { 30 30 [Storable] 31 31 private ItemList<IMeanFunction> factors; … … 38 38 } 39 39 40 public int GetNumberOfParameters(int numberOfVariables) {41 this.numberOfVariables = numberOfVariables;42 return factors.Select(t => t.GetNumberOfParameters(numberOfVariables)).Sum();43 }44 45 40 [StorableConstructor] 46 pr otectedMeanProd(bool deserializing)41 private MeanProd(bool deserializing) 47 42 : base(deserializing) { 48 43 } 49 44 50 pr otectedMeanProd(MeanProd original, Cloner cloner)45 private MeanProd(MeanProd original, Cloner cloner) 51 46 : base(original, cloner) { 52 47 this.factors = cloner.Clone(original.factors); … … 56 51 public MeanProd() { 57 52 this.factors = new ItemList<IMeanFunction>(); 53 } 54 public override IDeepCloneable Clone(Cloner cloner) { 55 return new MeanProd(this, cloner); 56 } 57 58 public int GetNumberOfParameters(int numberOfVariables) { 59 this.numberOfVariables = numberOfVariables; 60 return factors.Select(t => t.GetNumberOfParameters(numberOfVariables)).Sum(); 58 61 } 59 62 … … 65 68 offset += numberOfParameters; 66 69 } 67 }68 69 public void SetData(double[,] x) {70 foreach (var t in factors) t.SetData(x);71 70 } 72 71 … … 102 101 return res; 103 102 } 104 105 public override IDeepCloneable Clone(Cloner cloner) {106 return new MeanProd(this, cloner);107 }108 103 } 109 104 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanSum.cs
r8439 r8612 27 27 [StorableClass] 28 28 [Item(Name = "MeanSum", Description = "Sum of mean functions for Gaussian processes.")] 29 public class MeanSum : Item, IMeanFunction {29 public sealed class MeanSum : Item, IMeanFunction { 30 30 [Storable] 31 31 private ItemList<IMeanFunction> terms; … … 37 37 } 38 38 39 public int GetNumberOfParameters(int numberOfVariables) {40 this.numberOfVariables = numberOfVariables;41 return terms.Select(t => t.GetNumberOfParameters(numberOfVariables)).Sum();42 }43 39 [StorableConstructor] 44 pr otectedMeanSum(bool deserializing) : base(deserializing) { }45 pr otectedMeanSum(MeanSum original, Cloner cloner)40 private MeanSum(bool deserializing) : base(deserializing) { } 41 private MeanSum(MeanSum original, Cloner cloner) 46 42 : base(original, cloner) { 47 43 this.terms = cloner.Clone(original.terms); … … 50 46 public MeanSum() { 51 47 this.terms = new ItemList<IMeanFunction>(); 48 } 49 50 public override IDeepCloneable Clone(Cloner cloner) { 51 return new MeanSum(this, cloner); 52 } 53 54 public int GetNumberOfParameters(int numberOfVariables) { 55 this.numberOfVariables = numberOfVariables; 56 return terms.Select(t => t.GetNumberOfParameters(numberOfVariables)).Sum(); 52 57 } 53 58 … … 59 64 offset += numberOfParameters; 60 65 } 61 }62 63 public void SetData(double[,] x) {64 foreach (var t in terms) t.SetData(x);65 66 } 66 67 … … 82 83 return terms[i].GetGradients(k, x); 83 84 } 84 85 public override IDeepCloneable Clone(Cloner cloner) {86 return new MeanSum(this, cloner);87 }88 85 } 89 86 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanZero.cs
r8416 r8612 28 28 [StorableClass] 29 29 [Item(Name = "MeanZero", Description = "Constant zero mean function for Gaussian processes.")] 30 public class MeanZero : Item, IMeanFunction { 31 public int GetNumberOfParameters(int numberOfVariables) { 32 return 0; 33 } 30 public sealed class MeanZero : Item, IMeanFunction { 34 31 [StorableConstructor] 35 pr otectedMeanZero(bool deserializing) : base(deserializing) { }36 pr otectedMeanZero(MeanZero original, Cloner cloner)32 private MeanZero(bool deserializing) : base(deserializing) { } 33 private MeanZero(MeanZero original, Cloner cloner) 37 34 : base(original, cloner) { 38 35 } … … 40 37 } 41 38 39 public override IDeepCloneable Clone(Cloner cloner) { 40 return new MeanZero(this, cloner); 41 } 42 43 public int GetNumberOfParameters(int numberOfVariables) { 44 return 0; 45 } 46 42 47 public void SetParameter(double[] hyp) { 43 48 if (hyp.Length > 0) throw new ArgumentException("No hyper-parameters allowed for zero mean function.", "hyp"); 44 }45 46 public void SetData(double[,] x) {47 // do nothing48 49 } 49 50 … … 56 57 return Enumerable.Repeat(0.0, x.GetLength(0)).ToArray(); 57 58 } 58 public override IDeepCloneable Clone(Cloner cloner) {59 return new MeanZero(this, cloner);60 }61 59 } 62 60 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/Util.cs
r8562 r8612 20 20 #endregion 21 21 22 using System; 22 23 using System.Collections.Generic; 23 24 using System.Linq; 25 using HeuristicLab.Core; 26 using HeuristicLab.Data; 24 27 25 28 namespace HeuristicLab.Algorithms.DataAnalysis { 26 publicstatic class Util {29 internal static class Util { 27 30 public static double ScalarProd(IEnumerable<double> v, IEnumerable<double> u) { 28 31 return v.Zip(u, (vi, ui) => vi * ui).Sum(); … … 94 97 return Enumerable.Range(0, rows).Select(r => x[r, c]); 95 98 } 99 100 101 public static void AttachValueChangeHandler<T, U>(IValueParameter<T> parameter, Action action) 102 where T : ValueTypeValue<U> 103 where U : struct { 104 parameter.ValueChanged += (sender, args) => { 105 if (parameter.Value != null) { 106 parameter.Value.ValueChanged += (s, a) => action(); 107 action(); 108 } 109 }; 110 if (parameter.Value != null) { 111 parameter.Value.ValueChanged += (s, a) => action(); 112 } 113 } 114 115 public static void AttachArrayChangeHandler<T, U>(IValueParameter<T> parameter, Action action) 116 where T : ValueTypeArray<U> 117 where U : struct { 118 parameter.ValueChanged += (sender, args) => { 119 if (parameter.Value != null) { 120 parameter.Value.ItemChanged += (s, a) => action(); 121 parameter.Value.Reset += (s, a) => action(); 122 action(); 123 } 124 }; 125 if (parameter.Value != null) { 126 parameter.Value.ItemChanged += (s, a) => action(); 127 parameter.Value.Reset += (s, a) => action(); 128 } 129 } 96 130 } 97 131 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/HeuristicLab.Algorithms.DataAnalysis-3.4.csproj
r8609 r8612 121 121 <Compile Include="FixedDataAnalysisAlgorithm.cs" /> 122 122 <Compile Include="GaussianProcess\HyperParameter.cs" /> 123 <Compile Include="GaussianProcess\CovarianceFunction.cs" />124 123 <Compile Include="GaussianProcess\CovarianceRQArd.cs" /> 125 124 <Compile Include="GaussianProcess\CovarianceMaternIso.cs" /> -
trunk/sources/HeuristicLab.Tests/HeuristicLab.Algorithms.DataAnalysis-3.4/GaussianProcessFunctionsTest.cs
r8583 r8612 1509 1509 var hyp = Enumerable.Repeat(hypValue, nHyp).ToArray(); 1510 1510 mf.SetParameter(hyp); 1511 mf.SetData(x);1512 1511 1513 1512 var m = mf.GetMean(xt);
Note: See TracChangeset
for help on using the changeset viewer.