Changeset 8982 for trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovariancePeriodic.cs
- Timestamp:
- 12/01/12 19:02:47 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovariancePeriodic.cs
r8929 r8982 26 26 using HeuristicLab.Core; 27 27 using HeuristicLab.Data; 28 using HeuristicLab.Parameters; 28 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 29 30 … … 33 34 public sealed class CovariancePeriodic : ParameterizedNamedItem, ICovarianceFunction { 34 35 35 [Storable]36 private double scale;37 [Storable]38 private readonly HyperParameter<DoubleValue> scaleParameter;39 36 public IValueParameter<DoubleValue> ScaleParameter { 40 get { return scaleParameter; }37 get { return (IValueParameter<DoubleValue>)Parameters["Scale"]; } 41 38 } 42 39 43 [Storable]44 private double inverseLength;45 [Storable]46 private readonly HyperParameter<DoubleValue> inverseLengthParameter;47 40 public IValueParameter<DoubleValue> InverseLengthParameter { 48 get { return inverseLengthParameter; }41 get { return (IValueParameter<DoubleValue>)Parameters["InverseLength"]; } 49 42 } 50 43 51 [Storable]52 private double period;53 [Storable]54 private readonly HyperParameter<DoubleValue> periodParameter;55 44 public IValueParameter<DoubleValue> PeriodParameter { 56 get { return periodParameter; }45 get { return (IValueParameter<DoubleValue>)Parameters["Period"]; } 57 46 } 58 47 … … 62 51 private CovariancePeriodic(CovariancePeriodic original, Cloner cloner) 63 52 : base(original, cloner) { 64 this.scaleParameter = cloner.Clone(original.scaleParameter);65 this.inverseLengthParameter = cloner.Clone(original.inverseLengthParameter);66 this.periodParameter = cloner.Clone(original.periodParameter);67 this.scale = original.scale;68 this.inverseLength = original.inverseLength;69 this.period = original.period;70 71 RegisterEvents();72 53 } 73 54 … … 77 58 Description = ItemDescription; 78 59 79 scaleParameter = new HyperParameter<DoubleValue>("Scale", "The scale of the periodic covariance function."); 80 inverseLengthParameter = new HyperParameter<DoubleValue>("InverseLength", "The inverse length parameter for the periodic covariance function."); 81 periodParameter = new HyperParameter<DoubleValue>("Period", "The period parameter for the periodic covariance function."); 82 Parameters.Add(scaleParameter); 83 Parameters.Add(inverseLengthParameter); 84 Parameters.Add(periodParameter); 85 86 RegisterEvents(); 87 } 88 89 [StorableHook(HookType.AfterDeserialization)] 90 private void AfterDeserialization() { 91 RegisterEvents(); 60 Parameters.Add(new OptionalValueParameter<DoubleValue>("Scale", "The scale of the periodic covariance function.")); 61 Parameters.Add(new OptionalValueParameter<DoubleValue>("InverseLength", "The inverse length parameter for the periodic covariance function.")); 62 Parameters.Add(new OptionalValueParameter<DoubleValue>("Period", "The period parameter for the periodic covariance function.")); 92 63 } 93 64 … … 96 67 } 97 68 98 // caching 99 private void RegisterEvents() { 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; }); 69 public int GetNumberOfParameters(int numberOfVariables) { 70 return (ScaleParameter.Value != null ? 0 : 1) + 71 (PeriodParameter.Value != null ? 0 : 1) + 72 (InverseLengthParameter.Value != null ? 0 : 1); 103 73 } 104 74 105 public int GetNumberOfParameters(int numberOfVariables) { 106 return 107 (new[] { scaleParameter, inverseLengthParameter, periodParameter }).Count(p => !p.Fixed); 75 public void SetParameter(double[] p) { 76 double scale, inverseLength, period; 77 GetParameterValues(p, out scale, out period, out inverseLength); 78 ScaleParameter.Value = new DoubleValue(scale); 79 PeriodParameter.Value = new DoubleValue(period); 80 InverseLengthParameter.Value = new DoubleValue(inverseLength); 108 81 } 109 82 110 public void SetParameter(double[] hyp) { 111 int i = 0; 112 if (!inverseLengthParameter.Fixed) { 113 inverseLengthParameter.SetValue(new DoubleValue(1.0 / Math.Exp(hyp[i]))); 114 i++; 83 84 private void GetParameterValues(double[] p, out double scale, out double period, out double inverseLength) { 85 // gather parameter values 86 int c = 0; 87 if (InverseLengthParameter.Value != null) { 88 inverseLength = InverseLengthParameter.Value.Value; 89 } else { 90 inverseLength = 1.0 / Math.Exp(p[c]); 91 c++; 115 92 } 116 if (!periodParameter.Fixed) { 117 periodParameter.SetValue(new DoubleValue(Math.Exp(hyp[i]))); 118 i++; 93 if (PeriodParameter.Value != null) { 94 period = PeriodParameter.Value.Value; 95 } else { 96 period = Math.Exp(p[c]); 97 c++; 119 98 } 120 if (!scaleParameter.Fixed) { 121 scaleParameter.SetValue(new DoubleValue(Math.Exp(2 * hyp[i]))); 122 i++; 99 if (ScaleParameter.Value != null) { 100 scale = ScaleParameter.Value.Value; 101 } else { 102 scale = Math.Exp(2 * p[c]); 103 c++; 123 104 } 124 if ( hyp.Length != i) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovariancePeriod", "hyp");105 if (p.Length != c) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovariancePeriodic", "p"); 125 106 } 126 107 127 public double GetCovariance(double[,] x, int i, int j, IEnumerable<int> columnIndices) { 128 double k = i == j ? 0.0 : GetDistance(x, x, i, j, columnIndices); 129 k = Math.PI * k / period; 130 k = Math.Sin(k) * inverseLength; 131 k = k * k; 108 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int> columnIndices) { 109 double inverseLength, period, scale; 110 GetParameterValues(p, out scale, out period, out inverseLength); 111 // create functions 112 var cov = new ParameterizedCovarianceFunction(); 113 cov.Covariance = (x, i, j) => { 114 double k = i == j ? 0.0 : GetDistance(x, x, i, j, columnIndices); 115 k = Math.PI * k / period; 116 k = Math.Sin(k) * inverseLength; 117 k = k * k; 132 118 133 return scale * Math.Exp(-2.0 * k); 119 return scale * Math.Exp(-2.0 * k); 120 }; 121 cov.CrossCovariance = (x, xt, i, j) => { 122 double k = GetDistance(x, xt, i, j, columnIndices); 123 k = Math.PI * k / period; 124 k = Math.Sin(k) * inverseLength; 125 k = k * k; 126 127 return scale * Math.Exp(-2.0 * k); 128 }; 129 cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, period, inverseLength); 130 return cov; 134 131 } 135 132 136 public IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices) { 133 134 private static IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices, double scale, double period, double inverseLength) { 137 135 double v = i == j ? 0.0 : Math.PI * GetDistance(x, x, i, j, columnIndices) / period; 138 136 double gradient = Math.Sin(v) * inverseLength; … … 144 142 } 145 143 146 public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j, IEnumerable<int> columnIndices) { 147 double k = GetDistance(x, xt, i, j, columnIndices); 148 k = Math.PI * k / period; 149 k = Math.Sin(k) * inverseLength; 150 k = k * k; 151 152 return scale * Math.Exp(-2.0 * k); 153 } 154 155 private double GetDistance(double[,] x, double[,] xt, int i, int j, IEnumerable<int> columnIndices) { 144 private static double GetDistance(double[,] x, double[,] xt, int i, int j, IEnumerable<int> columnIndices) { 156 145 return Math.Sqrt(Util.SqrDist(x, i, xt, j, 1, columnIndices)); 157 146 }
Note: See TracChangeset
for help on using the changeset viewer.