Changeset 8582 for trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovariancePeriodic.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/CovariancePeriodic.cs
r8491 r8582 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Linq; 24 25 using HeuristicLab.Common; 25 26 using HeuristicLab.Core; 27 using HeuristicLab.Data; 26 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 29 … … 29 31 [StorableClass] 30 32 [Item(Name = "CovariancePeriodic", Description = "Periodic covariance function for Gaussian processes.")] 31 public class CovariancePeriodic : Item, ICovarianceFunction { 33 public class CovariancePeriodic : CovarianceFunction { 34 public IValueParameter<DoubleValue> ScaleParameter { 35 get { return scaleParameter; } 36 } 37 public IValueParameter<DoubleValue> InverseLengthParameter { 38 get { return inverseLengthParameter; } 39 } 40 public IValueParameter<DoubleValue> PeriodParameter { 41 get { return periodParameter; } 42 } 43 32 44 [Storable] 33 private double sf2; 34 public double Scale { get { return sf2; } } 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; 35 53 [Storable] 36 54 private double inverseLength; 37 public double InverseLength { get { return inverseLength; } }38 55 [Storable] 39 private double p; 40 public double Period { get { return p; } } 56 private double period; 41 57 42 public int GetNumberOfParameters(int numberOfVariables) { 43 return 3; 44 } 58 45 59 [StorableConstructor] 46 60 protected CovariancePeriodic(bool deserializing) : base(deserializing) { } 47 61 protected CovariancePeriodic(CovariancePeriodic original, Cloner cloner) 48 62 : base(original, cloner) { 49 sf2 = original.sf2; 50 inverseLength = original.inverseLength; 51 p = original.p; 63 this.scaleParameter = cloner.Clone(original.scaleParameter); 64 this.inverseLengthParameter = cloner.Clone(original.inverseLengthParameter); 65 this.periodParameter = cloner.Clone(original.periodParameter); 66 this.scale = original.scale; 67 this.inverseLength = original.inverseLength; 68 this.period = original.period; 69 70 RegisterEvents(); 52 71 } 72 53 73 public CovariancePeriodic() 54 74 : base() { 75 scaleParameter = new HyperParameter<DoubleValue>("Scale", "The scale of the periodic covariance function."); 76 inverseLengthParameter = new HyperParameter<DoubleValue>("InverseLength", "The inverse length parameter for the periodic covariance function."); 77 periodParameter = new HyperParameter<DoubleValue>("Period", "The period parameter for the periodic covariance function."); 78 Parameters.Add(scaleParameter); 79 Parameters.Add(inverseLengthParameter); 80 Parameters.Add(periodParameter); 81 82 RegisterEvents(); 83 } 84 85 [StorableHook(HookType.AfterDeserialization)] 86 private void AfterDeserialization() { 87 RegisterEvents(); 55 88 } 56 89 … … 59 92 } 60 93 61 public void SetParameter(double[] hyp) {62 if (hyp.Length != 3) throw new ArgumentException();63 this.inverseLength = 1.0 / Math.Exp(hyp[0]);64 this.p = Math.Exp(hyp[1]);65 this.sf2 = Math.Exp(2 * hyp[2]);94 // caching 95 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; }); 66 99 } 67 100 68 public double GetCovariance(double[,] x, int i, int j) { 101 public override int GetNumberOfParameters(int numberOfVariables) { 102 return 103 (new[] { scaleParameter, inverseLengthParameter, periodParameter }).Count(p => !p.Fixed); 104 } 105 106 public override void SetParameter(double[] hyp) { 107 int i = 0; 108 if (!inverseLengthParameter.Fixed) { 109 inverseLengthParameter.SetValue(new DoubleValue(1.0 / Math.Exp(hyp[i]))); 110 i++; 111 } 112 if (!periodParameter.Fixed) { 113 periodParameter.SetValue(new DoubleValue(Math.Exp(hyp[i]))); 114 i++; 115 } 116 if (!scaleParameter.Fixed) { 117 scaleParameter.SetValue(new DoubleValue(Math.Exp(2 * hyp[i]))); 118 i++; 119 } 120 if (hyp.Length != i) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovariancePeriod", "hyp"); 121 } 122 123 public override double GetCovariance(double[,] x, int i, int j) { 69 124 double k = i == j ? 0.0 : GetDistance(x, x, i, j); 70 k = Math.PI * k / p ;125 k = Math.PI * k / period; 71 126 k = Math.Sin(k) * inverseLength; 72 127 k = k * k; 73 128 74 return s f2* Math.Exp(-2.0 * k);129 return scale * Math.Exp(-2.0 * k); 75 130 } 76 131 77 public IEnumerable<double> GetGradient(double[,] x, int i, int j) {78 double v = i == j ? 0.0 : Math.PI * GetDistance(x, x, i, j) / p ;132 public override IEnumerable<double> GetGradient(double[,] x, int i, int j) { 133 double v = i == j ? 0.0 : Math.PI * GetDistance(x, x, i, j) / period; 79 134 double gradient = Math.Sin(v) * inverseLength; 80 135 gradient *= gradient; 81 yield return 4.0 * s f2* Math.Exp(-2.0 * gradient) * gradient;136 yield return 4.0 * scale * Math.Exp(-2.0 * gradient) * gradient; 82 137 double r = Math.Sin(v) * inverseLength; 83 yield return 4.0 * s f2* inverseLength * Math.Exp(-2 * r * r) * r * Math.Cos(v) * v;84 yield return 2.0 * s f2* Math.Exp(-2 * gradient);138 yield return 4.0 * scale * inverseLength * Math.Exp(-2 * r * r) * r * Math.Cos(v) * v; 139 yield return 2.0 * scale * Math.Exp(-2 * gradient); 85 140 } 86 141 87 public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {142 public override double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) { 88 143 double k = GetDistance(x, xt, i, j); 89 k = Math.PI * k / p ;144 k = Math.PI * k / period; 90 145 k = Math.Sin(k) * inverseLength; 91 146 k = k * k; 92 147 93 return s f2* Math.Exp(-2.0 * k);148 return scale * Math.Exp(-2.0 * k); 94 149 } 95 150
Note: See TracChangeset
for help on using the changeset viewer.