Changeset 10489 for trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions
- Timestamp:
- 02/20/14 17:45:06 (11 years ago)
- Location:
- trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions
- Files:
-
- 14 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceConst.cs
r9456 r10489 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Linq; 24 25 using HeuristicLab.Common; 25 26 using HeuristicLab.Core; … … 36 37 get { return (IValueParameter<DoubleValue>)Parameters["Scale"]; } 37 38 } 38 39 private bool HasFixedScaleParameter { 40 get { return ScaleParameter.Value != null; } 41 } 39 42 [StorableConstructor] 40 43 private CovarianceConst(bool deserializing) … … 59 62 60 63 public int GetNumberOfParameters(int numberOfVariables) { 61 return ScaleParameter.Value != null? 0 : 1;64 return HasFixedScaleParameter ? 0 : 1; 62 65 } 63 66 … … 71 74 int c = 0; 72 75 // gather parameter values 73 if ( ScaleParameter.Value != null) {76 if (HasFixedScaleParameter) { 74 77 scale = ScaleParameter.Value.Value; 75 78 } else { … … 87 90 cov.Covariance = (x, i, j) => scale; 88 91 cov.CrossCovariance = (x, xt, i, j) => scale; 89 cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, scale, columnIndices); 92 if (HasFixedScaleParameter) { 93 cov.CovarianceGradient = (x, i, j) => Enumerable.Empty<double>(); 94 } else { 95 cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, scale, columnIndices); 96 } 90 97 return cov; 91 98 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceLinearArd.cs
r9456 r10489 37 37 get { return (IValueParameter<DoubleArray>)Parameters["InverseLength"]; } 38 38 } 39 private bool HasFixedInverseLengthParameter { 40 get { return InverseLengthParameter.Value != null; } 41 } 39 42 40 43 [StorableConstructor] … … 57 60 58 61 public int GetNumberOfParameters(int numberOfVariables) { 59 if (InverseLengthParameter.Value == null) 62 if (HasFixedInverseLengthParameter) 63 return 0; 64 else 60 65 return numberOfVariables; 61 else62 return 0;63 66 } 64 67 … … 71 74 private void GetParameterValues(double[] p, out double[] inverseLength) { 72 75 // gather parameter values 73 if ( InverseLengthParameter.Value != null) {76 if (HasFixedInverseLengthParameter) { 74 77 inverseLength = InverseLengthParameter.Value.ToArray(); 75 78 } else { … … 81 84 double[] inverseLength; 82 85 GetParameterValues(p, out inverseLength); 86 var fixedInverseLength = HasFixedInverseLengthParameter; 83 87 // create functions 84 88 var cov = new ParameterizedCovarianceFunction(); 85 89 cov.Covariance = (x, i, j) => Util.ScalarProd(x, i, j, inverseLength, columnIndices); 86 90 cov.CrossCovariance = (x, xt, i, j) => Util.ScalarProd(x, i, xt, j, inverseLength, columnIndices); 87 cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, inverseLength, columnIndices); 91 if (fixedInverseLength) 92 cov.CovarianceGradient = (x, i, j) => Enumerable.Empty<double>(); 93 else 94 cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, inverseLength, columnIndices); 88 95 return cov; 89 96 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceMaternIso.cs
r9456 r10489 45 45 get { return (IConstrainedValueParameter<IntValue>)Parameters["D"]; } 46 46 } 47 47 private bool HasFixedScaleParameter { 48 get { return ScaleParameter.Value != null; } 49 } 50 private bool HasFixedInverseLengthParameter { 51 get { return InverseLengthParameter.Value != null; } 52 } 48 53 49 54 [StorableConstructor] … … 76 81 public int GetNumberOfParameters(int numberOfVariables) { 77 82 return 78 ( InverseLengthParameter.Value != null? 0 : 1) +79 ( ScaleParameter.Value != null? 0 : 1);83 (HasFixedInverseLengthParameter ? 0 : 1) + 84 (HasFixedScaleParameter ? 0 : 1); 80 85 } 81 86 … … 90 95 // gather parameter values 91 96 int c = 0; 92 if ( InverseLengthParameter.Value != null) {97 if (HasFixedInverseLengthParameter) { 93 98 inverseLength = InverseLengthParameter.Value.Value; 94 99 } else { … … 97 102 } 98 103 99 if ( ScaleParameter.Value != null) {104 if (HasFixedScaleParameter) { 100 105 scale = ScaleParameter.Value.Value; 101 106 } else { … … 110 115 int d = DParameter.Value.Value; 111 116 GetParameterValues(p, out scale, out inverseLength); 117 var fixedInverseLength = HasFixedInverseLengthParameter; 118 var fixedScale = HasFixedScaleParameter; 112 119 // create functions 113 120 var cov = new ParameterizedCovarianceFunction(); … … 122 129 return scale * m(d, dist); 123 130 }; 124 cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, d, scale, inverseLength, columnIndices );131 cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, d, scale, inverseLength, columnIndices, fixedInverseLength, fixedScale); 125 132 return cov; 126 133 } … … 149 156 150 157 151 private static IEnumerable<double> GetGradient(double[,] x, int i, int j, int d, double scale, double inverseLength, IEnumerable<int> columnIndices) { 158 private static IEnumerable<double> GetGradient(double[,] x, int i, int j, int d, double scale, double inverseLength, IEnumerable<int> columnIndices, 159 bool fixedInverseLength, bool fixedScale) { 152 160 double dist = i == j 153 161 ? 0.0 154 162 : Math.Sqrt(Util.SqrDist(x, i, j, Math.Sqrt(d) * inverseLength, columnIndices)); 155 163 156 yield return scale * dm(d, dist);157 yield return 2 * scale * m(d, dist);164 if (!fixedInverseLength) yield return scale * dm(d, dist); 165 if (!fixedScale) yield return 2 * scale * m(d, dist); 158 166 } 159 167 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceNeuralNetwork.cs
r9542 r10489 43 43 get { return (IValueParameter<DoubleValue>)Parameters["Length"]; } 44 44 } 45 private bool HasFixedScaleParameter { 46 get { return ScaleParameter.Value != null; } 47 } 48 private bool HasFixedLengthParameter { 49 get { return LengthParameter.Value != null; } 50 } 45 51 46 52 [StorableConstructor] … … 68 74 public int GetNumberOfParameters(int numberOfVariables) { 69 75 return 70 ( ScaleParameter.Value != null? 0 : 1) +71 ( LengthParameter.Value != null? 0 : 1);76 (HasFixedScaleParameter ? 0 : 1) + 77 (HasFixedLengthParameter ? 0 : 1); 72 78 } 73 79 … … 83 89 // gather parameter values 84 90 int c = 0; 85 if ( LengthParameter.Value != null) {91 if (HasFixedLengthParameter) { 86 92 length = LengthParameter.Value.Value; 87 93 } else { … … 90 96 } 91 97 92 if ( ScaleParameter.Value != null) {98 if (HasFixedScaleParameter) { 93 99 scale = ScaleParameter.Value.Value; 94 100 } else { … … 110 116 double length, scale; 111 117 GetParameterValues(p, out scale, out length); 112 // create functions 113 AutoDiff.Variable p0 = new AutoDiff.Variable(); 114 AutoDiff.Variable p1 = new AutoDiff.Variable(); 115 var l = TermBuilder.Exp(2.0 * p0); 116 var s = TermBuilder.Exp(2.0 * p1); 117 AutoDiff.Variable[] x1 = new AutoDiff.Variable[columnIndices.Count()]; 118 AutoDiff.Variable[] x2 = new AutoDiff.Variable[columnIndices.Count()]; 119 AutoDiff.Term sx = 1; 120 AutoDiff.Term s1 = 1; 121 AutoDiff.Term s2 = 1; 122 for (int k = 0; k < columnIndices.Count(); k++) { 123 x1[k] = new AutoDiff.Variable(); 124 x2[k] = new AutoDiff.Variable(); 125 sx += x1[k] * x2[k]; 126 s1 += x1[k] * x1[k]; 127 s2 += x2[k] * x2[k]; 128 } 129 130 var parameter = x1.Concat(x2).Concat(new AutoDiff.Variable[] { p0, p1 }).ToArray(); 131 var values = new double[x1.Length + x2.Length + 2]; 132 var c = (s * asin(sx / (sqrt((l + s1) * (l + s2))))).Compile(parameter); 118 var fixedLength = HasFixedLengthParameter; 119 var fixedScale = HasFixedScaleParameter; 133 120 134 121 var cov = new ParameterizedCovarianceFunction(); 135 122 cov.Covariance = (x, i, j) => { 136 int k = 0; 123 double sx = 1.0; 124 double s1 = 1.0; 125 double s2 = 1.0; 137 126 foreach (var col in columnIndices) { 138 values[k] = x[i, col]; 139 k++; 127 sx += x[i, col] * x[j, col]; 128 s1 += x[i, col] * x[i, col]; 129 s2 += x[j, col] * x[j, col]; 140 130 } 141 foreach (var col in columnIndices) { 142 values[k] = x[j, col]; 143 k++; 144 } 145 values[k] = Math.Log(Math.Sqrt(length)); 146 values[k + 1] = Math.Log(Math.Sqrt(scale)); 147 return c.Evaluate(values); 131 132 return (scale * Math.Asin(sx / (Math.Sqrt((length + s1) * (length + s2))))); 148 133 }; 149 134 cov.CrossCovariance = (x, xt, i, j) => { 150 int k = 0; 135 double sx = 1.0; 136 double s1 = 1.0; 137 double s2 = 1.0; 151 138 foreach (var col in columnIndices) { 152 values[k] = x[i, col]; 153 k++; 139 sx += x[i, col] * xt[j, col]; 140 s1 += x[i, col] * x[i, col]; 141 s2 += xt[j, col] * xt[j, col]; 154 142 } 143 144 return (scale * Math.Asin(sx / (Math.Sqrt((length + s1) * (length + s2))))); 145 }; 146 cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, length, scale, columnIndices, fixedLength, fixedScale); 147 return cov; 148 } 149 150 // order of returned gradients must match the order in GetParameterValues! 151 private static IEnumerable<double> GetGradient(double[,] x, int i, int j, double length, double scale, IEnumerable<int> columnIndices, 152 bool fixedLength, bool fixedScale) { 153 { 154 double sx = 1.0; 155 double s1 = 1.0; 156 double s2 = 1.0; 155 157 foreach (var col in columnIndices) { 156 values[k] = xt[j, col]; 157 k++; 158 sx += x[i, col] * x[j, col]; 159 s1 += x[i, col] * x[i, col]; 160 s2 += x[j, col] * x[j, col]; 158 161 } 159 values[k] = Math.Log(Math.Sqrt(length)); 160 values[k + 1] = Math.Log(Math.Sqrt(scale)); 161 return c.Evaluate(values); 162 var h = (length + s1) * (length + s2); 163 var f = sx / Math.Sqrt(h); 164 if (!fixedLength) { 165 yield return -scale / Math.Sqrt(1.0 - f * f) * ((length * sx * (2.0 * length + s1 + s2)) / Math.Pow(h, 3.0 / 2.0)); 166 } 167 if (!fixedScale) { 168 yield return 2.0 * scale * Math.Asin(f); 169 } 170 162 171 }; 163 cov.CovarianceGradient = (x, i, j) => {164 int k = 0;165 foreach (var col in columnIndices) {166 values[k] = x[i, col];167 k++;168 }169 foreach (var col in columnIndices) {170 values[k] = x[j, col];171 k++;172 }173 values[k] = Math.Log(Math.Sqrt(length));174 values[k + 1] = Math.Log(Math.Sqrt(scale));175 return c.Differentiate(values).Item1.Skip(columnIndices.Count() * 2);176 };177 return cov;178 172 } 179 173 -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceNoise.cs
r9544 r10489 37 37 get { return (IValueParameter<DoubleValue>)Parameters["Scale"]; } 38 38 } 39 private bool HasFixedScaleParameter { 40 get { return ScaleParameter.Value != null; } 41 } 39 42 40 43 [StorableConstructor] … … 60 63 61 64 public int GetNumberOfParameters(int numberOfVariables) { 62 return ScaleParameter.Value != null? 0 : 1;65 return HasFixedScaleParameter ? 0 : 1; 63 66 } 64 67 … … 72 75 int c = 0; 73 76 // gather parameter values 74 if ( ScaleParameter.Value != null) {77 if (HasFixedScaleParameter) { 75 78 scale = ScaleParameter.Value.Value; 76 79 } else { … … 84 87 double scale; 85 88 GetParameterValues(p, out scale); 89 var fixedScale = HasFixedScaleParameter; 86 90 // create functions 87 91 var cov = new ParameterizedCovarianceFunction(); 88 92 cov.Covariance = (x, i, j) => i == j ? scale : 0.0; 89 93 cov.CrossCovariance = (x, xt, i, j) => Util.SqrDist(x, i, xt, j, 1.0, columnIndices) < 1e-9 ? scale : 0.0; 90 cov.CovarianceGradient = (x, i, j) => Enumerable.Repeat(i == j ? 2.0 * scale : 0.0, 1); 94 if (fixedScale) 95 cov.CovarianceGradient = (x, i, j) => Enumerable.Empty<double>(); 96 else 97 cov.CovarianceGradient = (x, i, j) => Enumerable.Repeat(i == j ? 2.0 * scale : 0.0, 1); 91 98 return cov; 92 99 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovariancePeriodic.cs
r9456 r10489 46 46 } 47 47 48 private bool HasFixedScaleParameter { 49 get { return ScaleParameter.Value != null; } 50 } 51 private bool HasFixedInverseLengthParameter { 52 get { return InverseLengthParameter.Value != null; } 53 } 54 private bool HasFixedPeriodParameter { 55 get { return PeriodParameter.Value != null; } 56 } 57 48 58 49 59 [StorableConstructor] … … 68 78 69 79 public int GetNumberOfParameters(int numberOfVariables) { 70 return ( ScaleParameter.Value != null? 0 : 1) +71 ( PeriodParameter.Value != null? 0 : 1) +72 ( InverseLengthParameter.Value != null? 0 : 1);80 return (HasFixedScaleParameter ? 0 : 1) + 81 (HasFixedPeriodParameter ? 0 : 1) + 82 (HasFixedInverseLengthParameter ? 0 : 1); 73 83 } 74 84 … … 82 92 83 93 84 private void GetParameterValues(double[] 94 private void GetParameterValues(double[] 85 95 p, out double scale, out double period, out double inverseLength) { 86 96 // gather parameter values 87 97 int c = 0; 88 if ( InverseLengthParameter.Value != null) {98 if (HasFixedInverseLengthParameter) { 89 99 inverseLength = InverseLengthParameter.Value.Value; 90 100 } else { … … 92 102 c++; 93 103 } 94 if ( PeriodParameter.Value != null) {104 if (HasFixedPeriodParameter) { 95 105 period = PeriodParameter.Value.Value; 96 106 } else { … … 98 108 c++; 99 109 } 100 if ( ScaleParameter.Value != null) {110 if (HasFixedScaleParameter) { 101 111 scale = ScaleParameter.Value.Value; 102 112 } else { … … 110 120 double inverseLength, period, scale; 111 121 GetParameterValues(p, out scale, out period, out inverseLength); 122 var fixedInverseLength = HasFixedInverseLengthParameter; 123 var fixedPeriod = HasFixedPeriodParameter; 124 var fixedScale = HasFixedScaleParameter; 112 125 // create functions 113 126 var cov = new ParameterizedCovarianceFunction(); … … 128 141 return scale * Math.Exp(-2.0 * k); 129 142 }; 130 cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, period, inverseLength );143 cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, period, inverseLength, fixedInverseLength, fixedPeriod, fixedScale); 131 144 return cov; 132 145 } 133 146 134 147 135 private static IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices, double scale, double period, double inverseLength) { 148 private static IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices, double scale, double period, double inverseLength, 149 bool fixedInverseLength, bool fixedPeriod, bool fixedScale) { 136 150 double k = i == j ? 0.0 : Math.PI * GetDistance(x, x, i, j, columnIndices) / period; 137 151 double gradient = Math.Sin(k) * inverseLength; 138 152 gradient *= gradient; 139 yield return 4.0 * scale * Math.Exp(-2.0 * gradient) * gradient; 140 double r = Math.Sin(k) * inverseLength; 141 yield return 2.0 * k * scale * Math.Exp(-2 * r * r) *Math.Sin(2*k) * inverseLength * inverseLength; 142 yield return 2.0 * scale * Math.Exp(-2 * gradient); 153 if (!fixedInverseLength) yield return 4.0 * scale * Math.Exp(-2.0 * gradient) * gradient; 154 if (!fixedPeriod) { 155 double r = Math.Sin(k) * inverseLength; 156 yield return 2.0 * k * scale * Math.Exp(-2 * r * r) * Math.Sin(2 * k) * inverseLength * inverseLength; 157 } 158 if (!fixedScale) 159 yield return 2.0 * scale * Math.Exp(-2 * gradient); 143 160 144 161 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovariancePiecewisePolynomial.cs
r9543 r10489 45 45 get { return (IConstrainedValueParameter<IntValue>)Parameters["V"]; } 46 46 } 47 private bool HasFixedLengthParameter { 48 get { return LengthParameter.Value != null; } 49 } 50 private bool HasFixedScaleParameter { 51 get { return ScaleParameter.Value != null; } 52 } 47 53 48 54 [StorableConstructor] … … 77 83 public int GetNumberOfParameters(int numberOfVariables) { 78 84 return 79 ( LengthParameter.Value != null? 0 : 1) +80 ( ScaleParameter.Value != null? 0 : 1);85 (HasFixedLengthParameter ? 0 : 1) + 86 (HasFixedScaleParameter ? 0 : 1); 81 87 } 82 88 … … 91 97 // gather parameter values 92 98 int n = 0; 93 if ( LengthParameter.Value != null) {99 if (HasFixedLengthParameter) { 94 100 length = LengthParameter.Value.Value; 95 101 } else { … … 98 104 } 99 105 100 if ( ScaleParameter.Value != null) {106 if (HasFixedScaleParameter) { 101 107 scale = ScaleParameter.Value.Value; 102 108 } else { … … 111 117 int v = VParameter.Value.Value; 112 118 GetParameterValues(p, out length, out scale); 119 var fixedLength = HasFixedLengthParameter; 120 var fixedScale = HasFixedScaleParameter; 113 121 int exp = (int)Math.Floor(columnIndices.Count() / 2.0) + v + 1; 114 122 … … 147 155 return scale * Math.Pow(Math.Max(1 - k, 0), exp + v) * f(k); 148 156 }; 149 cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, length, scale, v, exp, f, df, columnIndices );157 cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, length, scale, v, exp, f, df, columnIndices, fixedLength, fixedScale); 150 158 return cov; 151 159 } 152 160 153 private static IEnumerable<double> GetGradient(double[,] x, int i, int j, double length, double scale, int v, double exp, Func<double, double> f, Func<double, double> df, IEnumerable<int> columnIndices) { 161 private static IEnumerable<double> GetGradient(double[,] x, int i, int j, double length, double scale, int v, double exp, Func<double, double> f, Func<double, double> df, IEnumerable<int> columnIndices, 162 bool fixedLength, bool fixedScale) { 154 163 double k = Math.Sqrt(Util.SqrDist(x, i, x, j, 1.0 / length, columnIndices)); 155 yield return scale * Math.Pow(Math.Max(1.0 - k, 0), exp + v - 1) * k * ((exp + v) * f(k) - Math.Max(1 - k, 0) * df(k));156 yield return 2.0 * scale * Math.Pow(Math.Max(1 - k, 0), exp + v) * f(k);164 if (!fixedLength) yield return scale * Math.Pow(Math.Max(1.0 - k, 0), exp + v - 1) * k * ((exp + v) * f(k) - Math.Max(1 - k, 0) * df(k)); 165 if (!fixedScale) yield return 2.0 * scale * Math.Pow(Math.Max(1 - k, 0), exp + v) * f(k); 157 166 } 158 167 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovariancePolynomial.cs
r9535 r10489 45 45 get { return (IValueParameter<IntValue>)Parameters["Degree"]; } 46 46 } 47 private bool HasFixedConstParameter { 48 get { return ConstParameter.Value != null; } 49 } 50 private bool HasFixedScaleParameter { 51 get { return ScaleParameter.Value != null; } 52 } 47 53 48 54 [StorableConstructor] … … 71 77 public int GetNumberOfParameters(int numberOfVariables) { 72 78 return 73 ( ConstParameter.Value != null? 0 : 1) +74 ( ScaleParameter.Value != null? 0 : 1);79 (HasFixedConstParameter ? 0 : 1) + 80 (HasFixedScaleParameter ? 0 : 1); 75 81 } 76 82 … … 85 91 // gather parameter values 86 92 int n = 0; 87 if ( ConstParameter.Value != null) {93 if (HasFixedConstParameter) { 88 94 @const = ConstParameter.Value.Value; 89 95 } else { … … 92 98 } 93 99 94 if ( ScaleParameter.Value != null) {100 if (HasFixedScaleParameter) { 95 101 scale = ScaleParameter.Value.Value; 96 102 } else { … … 106 112 if (degree <= 0) throw new ArgumentException("The degree parameter for CovariancePolynomial must be greater than zero."); 107 113 GetParameterValues(p, out @const, out scale); 114 var fixedConst = HasFixedConstParameter; 115 var fixedScale = HasFixedScaleParameter; 108 116 // create functions 109 117 var cov = new ParameterizedCovarianceFunction(); 110 118 cov.Covariance = (x, i, j) => scale * Math.Pow(@const + Util.ScalarProd(x, i, j, 1.0, columnIndices), degree); 111 119 cov.CrossCovariance = (x, xt, i, j) => scale * Math.Pow(@const + Util.ScalarProd(x, i, xt, j, 1.0, columnIndices), degree); 112 cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, @const, scale, degree, columnIndices );120 cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, @const, scale, degree, columnIndices, fixedConst, fixedScale); 113 121 return cov; 114 122 } 115 123 116 private static IEnumerable<double> GetGradient(double[,] x, int i, int j, double c, double scale, int degree, IEnumerable<int> columnIndices) { 124 private static IEnumerable<double> GetGradient(double[,] x, int i, int j, double c, double scale, int degree, IEnumerable<int> columnIndices, 125 bool fixedConst, bool fixedScale) { 117 126 double s = Util.ScalarProd(x, i, j, 1.0, columnIndices); 118 yield return c * degree * scale * Math.Pow(c + s, degree - 1);119 yield return 2 * scale * Math.Pow(c + s, degree);127 if (!fixedConst) yield return c * degree * scale * Math.Pow(c + s, degree - 1); 128 if (!fixedScale) yield return 2 * scale * Math.Pow(c + s, degree); 120 129 } 121 130 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceRationalQuadraticArd.cs
r9456 r10489 45 45 get { return (IValueParameter<DoubleValue>)Parameters["Shape"]; } 46 46 } 47 private bool HasFixedScaleParameter { 48 get { return ScaleParameter.Value != null; } 49 } 50 private bool HasFixedInverseLengthParameter { 51 get { return InverseLengthParameter.Value != null; } 52 } 53 private bool HasFixedShapeParameter { 54 get { return ShapeParameter.Value != null; } 55 } 47 56 48 57 [StorableConstructor] … … 71 80 public int GetNumberOfParameters(int numberOfVariables) { 72 81 return 73 ( ScaleParameter.Value != null? 0 : 1) +74 ( ShapeParameter.Value != null? 0 : 1) +75 ( InverseLengthParameter.Value != null? 0 : numberOfVariables);82 (HasFixedScaleParameter ? 0 : 1) + 83 (HasFixedShapeParameter ? 0 : 1) + 84 (HasFixedInverseLengthParameter ? 0 : numberOfVariables); 76 85 } 77 86 … … 88 97 int c = 0; 89 98 // gather parameter values 90 if ( InverseLengthParameter.Value != null) {99 if (HasFixedInverseLengthParameter) { 91 100 inverseLength = InverseLengthParameter.Value.ToArray(); 92 101 } else { 93 102 int length = p.Length; 94 if ( ScaleParameter.Value == null) length--;95 if ( ShapeParameter.Value == null) length--;103 if (!HasFixedScaleParameter) length--; 104 if (!HasFixedShapeParameter) length--; 96 105 inverseLength = p.Select(e => 1.0 / Math.Exp(e)).Take(length).ToArray(); 97 106 c += inverseLength.Length; 98 107 } 99 if ( ScaleParameter.Value != null) {108 if (HasFixedScaleParameter) { 100 109 scale = ScaleParameter.Value.Value; 101 110 } else { … … 103 112 c++; 104 113 } 105 if ( ShapeParameter.Value != null) {114 if (HasFixedShapeParameter) { 106 115 shape = ShapeParameter.Value.Value; 107 116 } else { … … 116 125 double[] inverseLength; 117 126 GetParameterValues(p, out scale, out shape, out inverseLength); 127 var fixedInverseLength = HasFixedInverseLengthParameter; 128 var fixedScale = HasFixedScaleParameter; 129 var fixedShape = HasFixedShapeParameter; 118 130 // create functions 119 131 var cov = new ParameterizedCovarianceFunction(); … … 128 140 return scale * Math.Pow(1 + 0.5 * d / shape, -shape); 129 141 }; 130 cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, shape, inverseLength );142 cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, shape, inverseLength, fixedInverseLength, fixedScale, fixedShape); 131 143 return cov; 132 144 } 133 145 134 private static IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices, double scale, double shape, double[] inverseLength) { 146 private static IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices, double scale, double shape, double[] inverseLength, 147 bool fixedInverseLength, bool fixedScale, bool fixedShape) { 135 148 double d = i == j 136 149 ? 0.0 … … 138 151 double b = 1 + 0.5 * d / shape; 139 152 int k = 0; 140 foreach (var columnIndex in columnIndices) { 141 yield return scale * Math.Pow(b, -shape - 1) * Util.SqrDist(x[i, columnIndex] * inverseLength[k], x[j, columnIndex] * inverseLength[k]); 142 k++; 153 if (!fixedInverseLength) { 154 foreach (var columnIndex in columnIndices) { 155 yield return 156 scale * Math.Pow(b, -shape - 1) * 157 Util.SqrDist(x[i, columnIndex] * inverseLength[k], x[j, columnIndex] * inverseLength[k]); 158 k++; 159 } 143 160 } 144 yield return 2 * scale * Math.Pow(b, -shape);145 yield return scale * Math.Pow(b, -shape) * (0.5 * d / b - shape * Math.Log(b));161 if (!fixedScale) yield return 2 * scale * Math.Pow(b, -shape); 162 if (!fixedShape) yield return scale * Math.Pow(b, -shape) * (0.5 * d / b - shape * Math.Log(b)); 146 163 } 147 164 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceRationalQuadraticIso.cs
r9456 r10489 44 44 get { return (IValueParameter<DoubleValue>)Parameters["Shape"]; } 45 45 } 46 47 private bool HasFixedScaleParameter { 48 get { return ScaleParameter.Value != null; } 49 } 50 private bool HasFixedInverseLengthParameter { 51 get { return InverseLengthParameter.Value != null; } 52 } 53 private bool HasFixedShapeParameter { 54 get { return ShapeParameter.Value != null; } 55 } 56 57 46 58 [StorableConstructor] 47 59 private CovarianceRationalQuadraticIso(bool deserializing) … … 68 80 69 81 public int GetNumberOfParameters(int numberOfVariables) { 70 return ( ScaleParameter.Value != null? 0 : 1) +71 ( ShapeParameter.Value != null? 0 : 1) +72 ( InverseLengthParameter.Value != null? 0 : 1);82 return (HasFixedScaleParameter ? 0 : 1) + 83 (HasFixedShapeParameter ? 0 : 1) + 84 (HasFixedInverseLengthParameter ? 0 : 1); 73 85 } 74 86 … … 84 96 int c = 0; 85 97 // gather parameter values 86 if ( InverseLengthParameter.Value != null) {98 if (HasFixedInverseLengthParameter) { 87 99 inverseLength = InverseLengthParameter.Value.Value; 88 100 } else { … … 90 102 c++; 91 103 } 92 if ( ScaleParameter.Value != null) {104 if (HasFixedScaleParameter) { 93 105 scale = ScaleParameter.Value.Value; 94 106 } else { … … 96 108 c++; 97 109 } 98 if ( ShapeParameter.Value != null) {110 if (HasFixedShapeParameter) { 99 111 shape = ShapeParameter.Value.Value; 100 112 } else { … … 108 120 double scale, shape, inverseLength; 109 121 GetParameterValues(p, out scale, out shape, out inverseLength); 122 var fixedInverseLength = HasFixedInverseLengthParameter; 123 var fixedScale = HasFixedScaleParameter; 124 var fixedShape = HasFixedShapeParameter; 110 125 // create functions 111 126 var cov = new ParameterizedCovarianceFunction(); … … 120 135 return scale * Math.Pow(1 + 0.5 * d / shape, -shape); 121 136 }; 122 cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, shape, inverseLength );137 cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, shape, inverseLength, fixedInverseLength, fixedScale, fixedShape); 123 138 return cov; 124 139 } 125 140 126 private static IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices, double scale, double shape, double inverseLength) { 141 private static IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices, double scale, double shape, double inverseLength, 142 bool fixedInverseLength, bool fixedScale, bool fixedShape) { 127 143 double d = i == j 128 144 ? 0.0 … … 130 146 131 147 double b = 1 + 0.5 * d / shape; 132 yield return scale * Math.Pow(b, -shape - 1) * d;133 yield return 2 * scale * Math.Pow(b, -shape);134 yield return scale * Math.Pow(b, -shape) * (0.5 * d / b - shape * Math.Log(b));148 if (!fixedInverseLength) yield return scale * Math.Pow(b, -shape - 1) * d; 149 if (!fixedScale) yield return 2 * scale * Math.Pow(b, -shape); 150 if (!fixedShape) yield return scale * Math.Pow(b, -shape) * (0.5 * d / b - shape * Math.Log(b)); 135 151 } 136 152 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceScale.cs
r9456 r10489 37 37 get { return (IValueParameter<DoubleValue>)Parameters["Scale"]; } 38 38 } 39 private bool HasFixedScaleParameter { 40 get { return ScaleParameter.Value != null; } 41 } 39 42 40 43 public IValueParameter<ICovarianceFunction> CovarianceFunctionParameter { … … 65 68 66 69 public int GetNumberOfParameters(int numberOfVariables) { 67 return ( ScaleParameter.Value != null? 0 : 1) + CovarianceFunctionParameter.Value.GetNumberOfParameters(numberOfVariables);70 return (HasFixedScaleParameter ? 0 : 1) + CovarianceFunctionParameter.Value.GetNumberOfParameters(numberOfVariables); 68 71 } 69 72 … … 77 80 private void GetParameterValues(double[] p, out double scale) { 78 81 // gather parameter values 79 if ( ScaleParameter.Value != null) {82 if (HasFixedScaleParameter) { 80 83 scale = ScaleParameter.Value.Value; 81 84 } else { … … 87 90 double scale; 88 91 GetParameterValues(p, out scale); 92 var fixedScale = HasFixedScaleParameter; 89 93 var subCov = CovarianceFunctionParameter.Value.GetParameterizedCovarianceFunction(p.Skip(1).ToArray(), columnIndices); 90 94 // create functions … … 92 96 cov.Covariance = (x, i, j) => scale * subCov.Covariance(x, i, j); 93 97 cov.CrossCovariance = (x, xt, i, j) => scale * subCov.CrossCovariance(x, xt, i, j); 94 cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, subCov );98 cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, subCov, fixedScale); 95 99 return cov; 96 100 } 97 101 98 private static IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices, double scale, ParameterizedCovarianceFunction cov) { 99 yield return 2 * scale * cov.Covariance(x, i, j); 102 private static IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices, double scale, ParameterizedCovarianceFunction cov, 103 bool fixedScale) { 104 if (!fixedScale) { 105 yield return 2 * scale * cov.Covariance(x, i, j); 106 } 100 107 foreach (var g in cov.CovarianceGradient(x, i, j)) 101 108 yield return scale * g; -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceSpectralMixture.cs
r10480 r10489 54 54 } 55 55 56 private bool HasFixedWeightParameter { 57 get { return WeightParameter.Value != null; } 58 } 59 private bool HasFixedFrequencyParameter { 60 get { return FrequencyParameter.Value != null; } 61 } 62 private bool HasFixedLengthScaleParameter { 63 get { return LengthScaleParameter.Value != null; } 64 } 65 56 66 [StorableConstructor] 57 67 private CovarianceSpectralMixture(bool deserializing) … … 80 90 var q = QParameter.Value.Value; 81 91 return 82 ( WeightParameter.Value != null? 0 : q) +83 ( FrequencyParameter.Value != null? 0 : q * numberOfVariables) +84 ( LengthScaleParameter.Value != null? 0 : q * numberOfVariables);92 (HasFixedWeightParameter ? 0 : q) + 93 (HasFixedFrequencyParameter ? 0 : q * numberOfVariables) + 94 (HasFixedLengthScaleParameter ? 0 : q * numberOfVariables); 85 95 } 86 96 … … 100 110 // guess number of elements for frequency and length (=q * numberOfVariables) 101 111 int n = WeightParameter.Value == null ? ((p.Length - q) / 2) : (p.Length / 2); 102 if ( WeightParameter.Value != null) {112 if (HasFixedWeightParameter) { 103 113 weight = WeightParameter.Value.ToArray(); 104 114 } else { … … 106 116 c += q; 107 117 } 108 if ( FrequencyParameter.Value != null) {118 if (HasFixedFrequencyParameter) { 109 119 frequency = FrequencyParameter.Value.ToArray(); 110 120 } else { … … 112 122 c += n; 113 123 } 114 if ( LengthScaleParameter.Value != null) {124 if (HasFixedLengthScaleParameter) { 115 125 lengthScale = LengthScaleParameter.Value.ToArray(); 116 126 } else { … … 124 134 double[] weight, frequency, lengthScale; 125 135 GetParameterValues(p, out weight, out frequency, out lengthScale); 136 var fixedWeight = HasFixedWeightParameter; 137 var fixedFrequency = HasFixedFrequencyParameter; 138 var fixedLengthScale = HasFixedLengthScaleParameter; 126 139 // create functions 127 140 var cov = new ParameterizedCovarianceFunction(); … … 135 148 }; 136 149 cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, QParameter.Value.Value, weight, frequency, 137 lengthScale, columnIndices );150 lengthScale, columnIndices, fixedWeight, fixedFrequency, fixedLengthScale); 138 151 return cov; 139 152 } … … 168 181 169 182 // order of returned gradients must match the order in GetParameterValues! 170 private static IEnumerable<double> GetGradient(double[,] x, int i, int j, int maxQ, double[] weight, double[] frequency, double[] lengthScale, IEnumerable<int> columnIndices) { 183 private static IEnumerable<double> GetGradient(double[,] x, int i, int j, int maxQ, double[] weight, double[] frequency, double[] lengthScale, IEnumerable<int> columnIndices, 184 bool fixedWeight, bool fixedFrequency, bool fixedLengthScale) { 171 185 double[] tau = Util.GetRow(x, i, columnIndices).Zip(Util.GetRow(x, j, columnIndices), (xi, xj) => xi - xj).ToArray(); 172 186 int numberOfVariables = lengthScale.Length / maxQ; 173 187 174 // weight 175 // for each component 176 for (int q = 0; q < maxQ; q++) { 177 double k = weight[q]; 178 int idx = 0; // helper index for tau 179 // for each selected variable 180 foreach (var c in columnIndices) { 181 k *= f1(tau[idx], lengthScale[q * numberOfVariables + c]) * f2(tau[idx], frequency[q * numberOfVariables + c]); 182 idx++; 183 } 184 yield return k; 185 } 186 187 // frequency 188 // for each component 189 for (int q = 0; q < maxQ; q++) { 190 int idx = 0; // helper index for tau 191 // for each selected variable 192 foreach (var c in columnIndices) { 193 double k = f1(tau[idx], lengthScale[q * numberOfVariables + c]) * 194 -2 * Math.PI * tau[idx] * frequency[q * numberOfVariables + c] * Math.Sin(2 * Math.PI * tau[idx] * frequency[q * numberOfVariables + c]); 195 idx++; 196 yield return weight[q] * k; 197 } 198 } 199 200 // length scale 201 // for each component 202 for (int q = 0; q < maxQ; q++) { 203 int idx = 0; // helper index for tau 204 // for each selected variable 205 foreach (var c in columnIndices) { 206 double k = -2 * Math.PI * Math.PI * tau[idx] * tau[idx] * lengthScale[q * numberOfVariables + c] * 207 f1(tau[idx], lengthScale[q * numberOfVariables + c]) * f2(tau[idx], frequency[q * numberOfVariables + c]); 208 idx++; 209 yield return weight[q] * k; 210 } 211 } 212 188 if (!fixedWeight) { 189 // weight 190 // for each component 191 for (int q = 0; q < maxQ; q++) { 192 double k = weight[q]; 193 int idx = 0; // helper index for tau 194 // for each selected variable 195 foreach (var c in columnIndices) { 196 k *= f1(tau[idx], lengthScale[q * numberOfVariables + c]) * f2(tau[idx], frequency[q * numberOfVariables + c]); 197 idx++; 198 } 199 yield return k; 200 } 201 } 202 203 if (!fixedFrequency) { 204 // frequency 205 // for each component 206 for (int q = 0; q < maxQ; q++) { 207 int idx = 0; // helper index for tau 208 // for each selected variable 209 foreach (var c in columnIndices) { 210 double k = f1(tau[idx], lengthScale[q * numberOfVariables + c]) * 211 -2 * Math.PI * tau[idx] * frequency[q * numberOfVariables + c] * 212 Math.Sin(2 * Math.PI * tau[idx] * frequency[q * numberOfVariables + c]); 213 idx++; 214 yield return weight[q] * k; 215 } 216 } 217 } 218 219 if (!fixedLengthScale) { 220 // length scale 221 // for each component 222 for (int q = 0; q < maxQ; q++) { 223 int idx = 0; // helper index for tau 224 // for each selected variable 225 foreach (var c in columnIndices) { 226 double k = -2 * Math.PI * Math.PI * tau[idx] * tau[idx] * lengthScale[q * numberOfVariables + c] * 227 f1(tau[idx], lengthScale[q * numberOfVariables + c]) * 228 f2(tau[idx], frequency[q * numberOfVariables + c]); 229 idx++; 230 yield return weight[q] * k; 231 } 232 } 233 } 213 234 } 214 235 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceSquaredExponentialArd.cs
r9456 r10489 40 40 get { return (IValueParameter<DoubleArray>)Parameters["InverseLength"]; } 41 41 } 42 private bool HasFixedInverseLengthParameter { 43 get { return InverseLengthParameter.Value != null; } 44 } 45 private bool HasFixedScaleParameter { 46 get { return ScaleParameter.Value != null; } 47 } 42 48 43 49 [StorableConstructor] … … 61 67 public int GetNumberOfParameters(int numberOfVariables) { 62 68 return 63 ( ScaleParameter.Value != null? 0 : 1) +64 ( InverseLengthParameter.Value != null? 0 : numberOfVariables);69 (HasFixedScaleParameter ? 0 : 1) + 70 (HasFixedInverseLengthParameter ? 0 : numberOfVariables); 65 71 } 66 72 … … 76 82 int c = 0; 77 83 // gather parameter values 78 if ( InverseLengthParameter.Value != null) {84 if (HasFixedInverseLengthParameter) { 79 85 inverseLength = InverseLengthParameter.Value.ToArray(); 80 86 } else { 81 87 int length = p.Length; 82 if ( ScaleParameter.Value == null) length--;88 if (HasFixedScaleParameter) length--; 83 89 inverseLength = p.Select(e => 1.0 / Math.Exp(e)).Take(length).ToArray(); 84 90 c += inverseLength.Length; 85 91 } 86 if ( ScaleParameter.Value != null) {92 if (HasFixedScaleParameter) { 87 93 scale = ScaleParameter.Value.Value; 88 94 } else { … … 97 103 double[] inverseLength; 98 104 GetParameterValues(p, out scale, out inverseLength); 105 var fixedInverseLength = HasFixedInverseLengthParameter; 106 var fixedScale = HasFixedScaleParameter; 99 107 // create functions 100 108 var cov = new ParameterizedCovarianceFunction(); … … 109 117 return scale * Math.Exp(-d / 2.0); 110 118 }; 111 cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, inverseLength );119 cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, inverseLength, fixedInverseLength, fixedScale); 112 120 return cov; 113 121 } 114 122 115 123 // order of returned gradients must match the order in GetParameterValues! 116 private static IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices, double scale, double[] inverseLength) { 124 private static IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices, double scale, double[] inverseLength, 125 bool fixedInverseLength, bool fixedScale) { 117 126 double d = i == j 118 127 ? 0.0 … … 120 129 121 130 int k = 0; 122 foreach (var columnIndex in columnIndices) { 123 double sqrDist = Util.SqrDist(x[i, columnIndex] * inverseLength[k], x[j, columnIndex] * inverseLength[k]); 124 yield return scale * Math.Exp(-d / 2.0) * sqrDist; 125 k++; 131 if (!fixedInverseLength) { 132 foreach (var columnIndex in columnIndices) { 133 double sqrDist = Util.SqrDist(x[i, columnIndex] * inverseLength[k], x[j, columnIndex] * inverseLength[k]); 134 yield return scale * Math.Exp(-d / 2.0) * sqrDist; 135 k++; 136 } 126 137 } 127 yield return 2.0 * scale * Math.Exp(-d / 2.0);138 if (!fixedScale) yield return 2.0 * scale * Math.Exp(-d / 2.0); 128 139 } 129 140 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceSquaredExponentialIso.cs
r9456 r10489 42 42 } 43 43 44 private bool HasFixedInverseLengthParameter { 45 get { return InverseLengthParameter.Value != null; } 46 } 47 private bool HasFixedScaleParameter { 48 get { return ScaleParameter.Value != null; } 49 } 50 44 51 [StorableConstructor] 45 52 private CovarianceSquaredExponentialIso(bool deserializing) … … 66 73 public int GetNumberOfParameters(int numberOfVariables) { 67 74 return 68 ( ScaleParameter.Value != null? 0 : 1) +69 ( InverseLengthParameter.Value != null? 0 : 1);75 (HasFixedScaleParameter ? 0 : 1) + 76 (HasFixedInverseLengthParameter ? 0 : 1); 70 77 } 71 78 … … 81 88 // gather parameter values 82 89 int c = 0; 83 if ( InverseLengthParameter.Value != null) {90 if (HasFixedInverseLengthParameter) { 84 91 inverseLength = InverseLengthParameter.Value.Value; 85 92 } else { … … 88 95 } 89 96 90 if ( ScaleParameter.Value != null) {97 if (HasFixedScaleParameter) { 91 98 scale = ScaleParameter.Value.Value; 92 99 } else { … … 100 107 double inverseLength, scale; 101 108 GetParameterValues(p, out scale, out inverseLength); 109 var fixedInverseLength = HasFixedInverseLengthParameter; 110 var fixedScale = HasFixedScaleParameter; 102 111 // create functions 103 112 var cov = new ParameterizedCovarianceFunction(); … … 112 121 return scale * Math.Exp(-d / 2.0); 113 122 }; 114 cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, scale, inverseLength, columnIndices); 123 cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, scale, inverseLength, columnIndices, 124 fixedInverseLength, fixedScale); 115 125 return cov; 116 126 } 117 127 118 128 // order of returned gradients must match the order in GetParameterValues! 119 private static IEnumerable<double> GetGradient(double[,] x, int i, int j, double sf2, double inverseLength, IEnumerable<int> columnIndices) { 129 private static IEnumerable<double> GetGradient(double[,] x, int i, int j, double sf2, double inverseLength, IEnumerable<int> columnIndices, 130 bool fixedInverseLength, bool fixedScale) { 120 131 double d = i == j 121 132 ? 0.0 122 133 : Util.SqrDist(x, i, j, inverseLength, columnIndices); 123 134 double g = Math.Exp(-d / 2.0); 124 yield return sf2 * g * d;125 yield return 2.0 * sf2 * g;135 if (!fixedInverseLength) yield return sf2 * g * d; 136 if (!fixedScale) yield return 2.0 * sf2 * g; 126 137 } 127 138 }
Note: See TracChangeset
for help on using the changeset viewer.