- Timestamp:
- 07/02/16 14:38:40 (8 years ago)
- Location:
- stable
- Files:
-
- 30 edited
- 2 copied
Legend:
- Unmodified
- Added
- Removed
-
stable
- Property svn:mergeinfo changed
/trunk/sources merged: 13438,13721,13724,13784,13891
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Algorithms.DataAnalysis
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Algorithms.DataAnalysis merged: 13438,13721,13724,13784,13891
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceConst.cs
r12009 r13981 21 21 22 22 using System; 23 using System.Collections.Generic;24 using System.Linq;25 23 using HeuristicLab.Common; 26 24 using HeuristicLab.Core; … … 83 81 } 84 82 85 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int>columnIndices) {83 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) { 86 84 double scale; 87 85 GetParameterValues(p, out scale); … … 91 89 cov.CrossCovariance = (x, xt, i, j) => scale; 92 90 if (HasFixedScaleParameter) { 93 cov.CovarianceGradient = (x, i, j) => Enumerable.Empty<double>();91 cov.CovarianceGradient = (x, i, j) => new double[0]; 94 92 } else { 95 cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, scale, columnIndices);93 cov.CovarianceGradient = (x, i, j) => new[] { 2.0 * scale }; 96 94 } 97 95 return cov; 98 96 } 99 100 private static IEnumerable<double> GetGradient(double[,] x, int i, int j, double scale, IEnumerable<int> columnIndices) {101 yield return 2.0 * scale;102 }103 97 } 104 98 } -
stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceLinear.cs
r12009 r13981 21 21 22 22 using System; 23 using System.Collections.Generic;24 using System.Linq;25 23 using HeuristicLab.Common; 26 24 using HeuristicLab.Core; … … 52 50 } 53 51 54 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int>columnIndices) {52 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) { 55 53 if (p.Length > 0) throw new ArgumentException("No parameters are allowed for the linear covariance function."); 56 54 // create functions 57 55 var cov = new ParameterizedCovarianceFunction(); 58 cov.Covariance = (x, i, j) => Util.ScalarProd(x, i, j, 1, columnIndices);59 cov.CrossCovariance = (x, xt, i, j) => Util.ScalarProd(x, i, xt, j, 1.0 , columnIndices);60 cov.CovarianceGradient = (x, i, j) => Enumerable.Empty<double>();56 cov.Covariance = (x, i, j) => Util.ScalarProd(x, i, j, columnIndices, 1.0); 57 cov.CrossCovariance = (x, xt, i, j) => Util.ScalarProd(x, i, xt, j, columnIndices, 1.0); 58 cov.CovarianceGradient = (x, i, j) => new double[0]; 61 59 return cov; 62 60 } -
stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceLinearArd.cs
r12009 r13981 81 81 } 82 82 83 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int>columnIndices) {83 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) { 84 84 double[] inverseLength; 85 85 GetParameterValues(p, out inverseLength); … … 90 90 cov.CrossCovariance = (x, xt, i, j) => Util.ScalarProd(x, i, xt, j, inverseLength, columnIndices); 91 91 if (fixedInverseLength) 92 cov.CovarianceGradient = (x, i, j) => Enumerable.Empty<double>();92 cov.CovarianceGradient = (x, i, j) => new double[0]; 93 93 else 94 94 cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, inverseLength, columnIndices); … … 96 96 } 97 97 98 private static I Enumerable<double> GetGradient(double[,] x, int i, int j, double[] inverseLength, IEnumerable<int>columnIndices) {98 private static IList<double> GetGradient(double[,] x, int i, int j, double[] inverseLength, int[] columnIndices) { 99 99 int k = 0; 100 foreach (int columnIndex in columnIndices) { 101 yield return -2.0 * x[i, columnIndex] * x[j, columnIndex] * inverseLength[k] * inverseLength[k]; 100 var g = new List<double>(columnIndices.Length); 101 for (int c = 0; c < columnIndices.Length; c++) { 102 var columnIndex = columnIndices[c]; 103 g.Add(-2.0 * x[i, columnIndex] * x[j, columnIndex] * inverseLength[k] * inverseLength[k]); 102 104 k++; 103 105 } 106 return g; 104 107 } 105 108 } -
stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceMask.cs
r12009 r13981 20 20 #endregion 21 21 22 using System;23 using System.Collections.Generic;24 22 using System.Linq; 25 using System.Linq.Expressions;26 23 using HeuristicLab.Common; 27 24 using HeuristicLab.Core; … … 73 70 } 74 71 75 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int>columnIndices) {72 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) { 76 73 var cov = CovarianceFunctionParameter.Value; 77 74 var selectedDimensions = SelectedDimensionsParameter.Value; 78 75 79 return cov.GetParameterizedCovarianceFunction(p, selectedDimensions.Intersect(columnIndices) );76 return cov.GetParameterizedCovarianceFunction(p, selectedDimensions.Intersect(columnIndices).ToArray()); 80 77 } 81 78 } -
stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceMaternIso.cs
r12009 r13981 111 111 } 112 112 113 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int>columnIndices) {113 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) { 114 114 double inverseLength, scale; 115 115 int d = DParameter.Value.Value; … … 122 122 double dist = i == j 123 123 ? 0.0 124 : Math.Sqrt(Util.SqrDist(x, i, j, Math.Sqrt(d) * inverseLength, columnIndices));124 : Math.Sqrt(Util.SqrDist(x, i, j, columnIndices, Math.Sqrt(d) * inverseLength)); 125 125 return scale * m(d, dist); 126 126 }; 127 127 cov.CrossCovariance = (x, xt, i, j) => { 128 double dist = Math.Sqrt(Util.SqrDist(x, i, xt, j, Math.Sqrt(d) * inverseLength, columnIndices));128 double dist = Math.Sqrt(Util.SqrDist(x, i, xt, j, columnIndices, Math.Sqrt(d) * inverseLength)); 129 129 return scale * m(d, dist); 130 130 }; … … 155 155 } 156 156 157 158 private static IEnumerable<double> GetGradient(double[,] x, int i, int j, int d, double scale, double inverseLength, IEnumerable<int> columnIndices, 157 private static IList<double> GetGradient(double[,] x, int i, int j, int d, double scale, double inverseLength, int[] columnIndices, 159 158 bool fixedInverseLength, bool fixedScale) { 160 159 double dist = i == j 161 160 ? 0.0 162 : Math.Sqrt(Util.SqrDist(x, i, j, Math.Sqrt(d) * inverseLength, columnIndices));161 : Math.Sqrt(Util.SqrDist(x, i, j, columnIndices, Math.Sqrt(d) * inverseLength)); 163 162 164 if (!fixedInverseLength) yield return scale * dm(d, dist); 165 if (!fixedScale) yield return 2 * scale * m(d, dist); 163 var g = new List<double>(2); 164 if (!fixedInverseLength) g.Add(scale * dm(d, dist)); 165 if (!fixedScale) g.Add(2 * scale * m(d, dist)); 166 return g; 166 167 } 167 168 } -
stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceNeuralNetwork.cs
r12009 r13981 102 102 } 103 103 104 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int>columnIndices) {104 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) { 105 105 double length, scale; 106 106 GetParameterValues(p, out scale, out length); … … 113 113 double s1 = 1.0; 114 114 double s2 = 1.0; 115 foreach (var col in columnIndices) { 115 for (int c = 0; c < columnIndices.Length; c++) { 116 var col = columnIndices[c]; 116 117 sx += x[i, col] * x[j, col]; 117 118 s1 += x[i, col] * x[i, col]; … … 125 126 double s1 = 1.0; 126 127 double s2 = 1.0; 127 foreach (var col in columnIndices) { 128 for (int c = 0; c < columnIndices.Length; c++) { 129 var col = columnIndices[c]; 128 130 sx += x[i, col] * xt[j, col]; 129 131 s1 += x[i, col] * x[i, col]; … … 138 140 139 141 // order of returned gradients must match the order in GetParameterValues! 140 private static I Enumerable<double> GetGradient(double[,] x, int i, int j, double length, double scale, IEnumerable<int>columnIndices,142 private static IList<double> GetGradient(double[,] x, int i, int j, double length, double scale, int[] columnIndices, 141 143 bool fixedLength, bool fixedScale) { 142 { 143 double sx = 1.0; 144 double s1 = 1.0; 145 double s2 = 1.0; 146 foreach (var col in columnIndices) { 147 sx += x[i, col] * x[j, col]; 148 s1 += x[i, col] * x[i, col]; 149 s2 += x[j, col] * x[j, col]; 150 } 151 var h = (length + s1) * (length + s2); 152 var f = sx / Math.Sqrt(h); 153 if (!fixedLength) { 154 yield return -scale / Math.Sqrt(1.0 - f * f) * ((length * sx * (2.0 * length + s1 + s2)) / Math.Pow(h, 3.0 / 2.0)); 155 } 156 if (!fixedScale) { 157 yield return 2.0 * scale * Math.Asin(f); 158 } 144 double sx = 1.0; 145 double s1 = 1.0; 146 double s2 = 1.0; 147 for (int c = 0; c < columnIndices.Length; c++) { 148 var col = columnIndices[c]; 149 sx += x[i, col] * x[j, col]; 150 s1 += x[i, col] * x[i, col]; 151 s2 += x[j, col] * x[j, col]; 159 152 } 153 var h = (length + s1) * (length + s2); 154 var f = sx / Math.Sqrt(h); 155 156 var g = new List<double>(2); 157 if (!fixedLength) g.Add(-scale / Math.Sqrt(1.0 - f * f) * ((length * sx * (2.0 * length + s1 + s2)) / Math.Pow(h, 3.0 / 2.0))); 158 if (!fixedScale) g.Add(2.0 * scale * Math.Asin(f)); 159 return g; 160 160 } 161 161 } -
stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceNoise.cs
r12009 r13981 21 21 22 22 using System; 23 using System.Collections.Generic;24 using System.Linq;25 23 using HeuristicLab.Common; 26 24 using HeuristicLab.Core; … … 84 82 } 85 83 86 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int>columnIndices) {84 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) { 87 85 double scale; 88 86 GetParameterValues(p, out scale); … … 91 89 var cov = new ParameterizedCovarianceFunction(); 92 90 cov.Covariance = (x, i, j) => i == j ? scale : 0.0; 93 cov.CrossCovariance = (x, xt, i, j) => Util.SqrDist(x, i, xt, j, 1.0, columnIndices) < 1e-9 ? scale : 0.0;91 cov.CrossCovariance = (x, xt, i, j) => Util.SqrDist(x, i, xt, j, columnIndices, 1.0) < 1e-9 ? scale : 0.0; 94 92 if (fixedScale) 95 cov.CovarianceGradient = (x, i, j) => Enumerable.Empty<double>();93 cov.CovarianceGradient = (x, i, j) => new double[0]; 96 94 else 97 cov.CovarianceGradient = (x, i, j) => Enumerable.Repeat(i == j ? 2.0 * scale : 0.0, 1);95 cov.CovarianceGradient = (x, i, j) => new double[1] { i == j ? 2.0 * scale : 0.0 }; 98 96 return cov; 99 97 } -
stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovariancePeriodic.cs
r12009 r13981 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Linq;25 24 using HeuristicLab.Common; 26 25 using HeuristicLab.Core; … … 117 116 } 118 117 119 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int>columnIndices) {118 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) { 120 119 double inverseLength, period, scale; 121 120 GetParameterValues(p, out scale, out period, out inverseLength); … … 145 144 } 146 145 147 148 private static IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices, double scale, double period, double inverseLength, 146 private static IList<double> GetGradient(double[,] x, int i, int j, int[] columnIndices, double scale, double period, double inverseLength, 149 147 bool fixedInverseLength, bool fixedPeriod, bool fixedScale) { 150 148 double k = i == j ? 0.0 : Math.PI * GetDistance(x, x, i, j, columnIndices) / period; 151 149 double gradient = Math.Sin(k) * inverseLength; 152 150 gradient *= gradient; 153 if (!fixedInverseLength) yield return 4.0 * scale * Math.Exp(-2.0 * gradient) * gradient; 151 var g = new List<double>(3); 152 if (!fixedInverseLength) 153 g.Add(4.0 * scale * Math.Exp(-2.0 * gradient) * gradient); 154 154 if (!fixedPeriod) { 155 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;156 g.Add(2.0 * k * scale * Math.Exp(-2 * r * r) * Math.Sin(2 * k) * inverseLength * inverseLength); 157 157 } 158 158 if (!fixedScale) 159 yield return 2.0 * scale * Math.Exp(-2 * gradient);160 159 g.Add(2.0 * scale * Math.Exp(-2 * gradient)); 160 return g; 161 161 } 162 162 163 private static double GetDistance(double[,] x, double[,] xt, int i, int j, IEnumerable<int>columnIndices) {164 return Math.Sqrt(Util.SqrDist(x, i, xt, j, 1, columnIndices));163 private static double GetDistance(double[,] x, double[,] xt, int i, int j, int[] columnIndices) { 164 return Math.Sqrt(Util.SqrDist(x, i, xt, j, columnIndices, 1)); 165 165 } 166 166 } -
stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovariancePiecewisePolynomial.cs
r12009 r13981 69 69 Parameters.Add(new OptionalValueParameter<DoubleValue>("Scale", "The scale parameter of the piecewise polynomial covariance function.")); 70 70 71 var validValues = new ItemSet<IntValue>(new IntValue[] { 72 (IntValue)(new IntValue().AsReadOnly()), 73 (IntValue)(new IntValue(1).AsReadOnly()), 74 (IntValue)(new IntValue(2).AsReadOnly()), 71 var validValues = new ItemSet<IntValue>(new IntValue[] { 72 (IntValue)(new IntValue().AsReadOnly()), 73 (IntValue)(new IntValue(1).AsReadOnly()), 74 (IntValue)(new IntValue(2).AsReadOnly()), 75 75 (IntValue)(new IntValue(3).AsReadOnly()) }); 76 76 Parameters.Add(new ConstrainedValueParameter<IntValue>("V", "The v parameter of the piecewise polynomial function (allowed values 0, 1, 2, 3).", validValues, validValues.First())); … … 113 113 } 114 114 115 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int>columnIndices) {115 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) { 116 116 double length, scale; 117 117 int v = VParameter.Value.Value; … … 148 148 var cov = new ParameterizedCovarianceFunction(); 149 149 cov.Covariance = (x, i, j) => { 150 double k = Math.Sqrt(Util.SqrDist(x, i, x, j, 1.0 / length, columnIndices));150 double k = Math.Sqrt(Util.SqrDist(x, i, x, j, columnIndices, 1.0 / length)); 151 151 return scale * Math.Pow(Math.Max(1 - k, 0), exp + v) * f(k); 152 152 }; 153 153 cov.CrossCovariance = (x, xt, i, j) => { 154 double k = Math.Sqrt(Util.SqrDist(x, i, xt, j, 1.0 / length, columnIndices));154 double k = Math.Sqrt(Util.SqrDist(x, i, xt, j, columnIndices, 1.0 / length)); 155 155 return scale * Math.Pow(Math.Max(1 - k, 0), exp + v) * f(k); 156 156 }; … … 159 159 } 160 160 161 private static I Enumerable<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 IList<double> GetGradient(double[,] x, int i, int j, double length, double scale, int v, double exp, Func<double, double> f, Func<double, double> df, int[] columnIndices, 162 162 bool fixedLength, bool fixedScale) { 163 double k = Math.Sqrt(Util.SqrDist(x, i, x, j, 1.0 / length, columnIndices)); 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); 163 double k = Math.Sqrt(Util.SqrDist(x, i, x, j, columnIndices, 1.0 / length)); 164 var g = new List<double>(2); 165 if (!fixedLength) g.Add(scale * Math.Pow(Math.Max(1.0 - k, 0), exp + v - 1) * k * ((exp + v) * f(k) - Math.Max(1 - k, 0) * df(k))); 166 if (!fixedScale) g.Add(2.0 * scale * Math.Pow(Math.Max(1 - k, 0), exp + v) * f(k)); 167 return g; 166 168 } 167 169 } -
stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovariancePolynomial.cs
r12009 r13981 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Linq;25 24 using HeuristicLab.Common; 26 25 using HeuristicLab.Core; … … 107 106 } 108 107 109 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int>columnIndices) {108 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) { 110 109 double @const, scale; 111 110 int degree = DegreeParameter.Value.Value; … … 116 115 // create functions 117 116 var cov = new ParameterizedCovarianceFunction(); 118 cov.Covariance = (x, i, j) => scale * Math.Pow(@const + Util.ScalarProd(x, i, j, 1.0, columnIndices), degree);119 cov.CrossCovariance = (x, xt, i, j) => scale * Math.Pow(@const + Util.ScalarProd(x, i, xt, j, 1.0, columnIndices), degree);117 cov.Covariance = (x, i, j) => scale * Math.Pow(@const + Util.ScalarProd(x, i, j, columnIndices, 1.0), degree); 118 cov.CrossCovariance = (x, xt, i, j) => scale * Math.Pow(@const + Util.ScalarProd(x, i, xt, j, columnIndices, 1.0), degree); 120 119 cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, @const, scale, degree, columnIndices, fixedConst, fixedScale); 121 120 return cov; 122 121 } 123 122 124 private static I Enumerable<double> GetGradient(double[,] x, int i, int j, double c, double scale, int degree, IEnumerable<int>columnIndices,123 private static IList<double> GetGradient(double[,] x, int i, int j, double c, double scale, int degree, int[] columnIndices, 125 124 bool fixedConst, bool fixedScale) { 126 double s = Util.ScalarProd(x, i, j, 1.0, columnIndices); 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); 125 double s = Util.ScalarProd(x, i, j, columnIndices, 1.0); 126 var g = new List<double>(2); 127 if (!fixedConst) g.Add(c * degree * scale * Math.Pow(c + s, degree - 1)); 128 if (!fixedScale) g.Add(2 * scale * Math.Pow(c + s, degree)); 129 return g; 129 130 } 130 131 } -
stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceProduct.cs
r12009 r13981 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using System.Linq.Expressions;26 25 using HeuristicLab.Common; 27 26 using HeuristicLab.Core; … … 76 75 } 77 76 78 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int>columnIndices) {77 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) { 79 78 if (factors.Count == 0) throw new ArgumentException("at least one factor is necessary for the product covariance function."); 80 79 var functions = new List<ParameterizedCovarianceFunction>(); … … 93 92 } 94 93 95 public static I Enumerable<double> GetGradient(double[,] x, int i, int j, List<ParameterizedCovarianceFunction> factorFunctions) {94 public static IList<double> GetGradient(double[,] x, int i, int j, List<ParameterizedCovarianceFunction> factorFunctions) { 96 95 var covariances = factorFunctions.Select(f => f.Covariance(x, i, j)).ToArray(); 96 var gr = new List<double>(); 97 97 for (int ii = 0; ii < factorFunctions.Count; ii++) { 98 98 foreach (var g in factorFunctions[ii].CovarianceGradient(x, i, j)) { … … 100 100 for (int jj = 0; jj < covariances.Length; jj++) 101 101 if (ii != jj) res *= covariances[jj]; 102 yield return res;102 gr.Add(res); 103 103 } 104 104 } 105 return gr; 105 106 } 106 107 } -
stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceRationalQuadraticArd.cs
r12009 r13981 121 121 } 122 122 123 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int>columnIndices) {123 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) { 124 124 double scale, shape; 125 125 double[] inverseLength; … … 144 144 } 145 145 146 private static I Enumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int>columnIndices, double scale, double shape, double[] inverseLength,146 private static IList<double> GetGradient(double[,] x, int i, int j, int[] columnIndices, double scale, double shape, double[] inverseLength, 147 147 bool fixedInverseLength, bool fixedScale, bool fixedShape) { 148 148 double d = i == j … … 151 151 double b = 1 + 0.5 * d / shape; 152 152 int k = 0; 153 var g = new List<double>(columnIndices.Length + 2); 153 154 if (!fixedInverseLength) { 154 155 foreach (var columnIndex in columnIndices) { 155 yield return156 g.Add( 156 157 scale * Math.Pow(b, -shape - 1) * 157 Util.SqrDist(x[i, columnIndex] * inverseLength[k], x[j, columnIndex] * inverseLength[k]) ;158 Util.SqrDist(x[i, columnIndex] * inverseLength[k], x[j, columnIndex] * inverseLength[k])); 158 159 k++; 159 160 } 160 161 } 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)); 162 if (!fixedScale) g.Add(2 * scale * Math.Pow(b, -shape)); 163 if (!fixedShape) g.Add(scale * Math.Pow(b, -shape) * (0.5 * d / b - shape * Math.Log(b))); 164 return g; 163 165 } 164 166 } -
stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceRationalQuadraticIso.cs
r12009 r13981 117 117 } 118 118 119 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int>columnIndices) {119 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) { 120 120 double scale, shape, inverseLength; 121 121 GetParameterValues(p, out scale, out shape, out inverseLength); … … 128 128 double d = i == j 129 129 ? 0.0 130 : Util.SqrDist(x, i, j, inverseLength, columnIndices);130 : Util.SqrDist(x, i, j, columnIndices, inverseLength); 131 131 return scale * Math.Pow(1 + 0.5 * d / shape, -shape); 132 132 }; 133 133 cov.CrossCovariance = (x, xt, i, j) => { 134 double d = Util.SqrDist(x, i, xt, j, inverseLength, columnIndices);134 double d = Util.SqrDist(x, i, xt, j, columnIndices, inverseLength); 135 135 return scale * Math.Pow(1 + 0.5 * d / shape, -shape); 136 136 }; … … 139 139 } 140 140 141 private static I Enumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int>columnIndices, double scale, double shape, double inverseLength,141 private static IList<double> GetGradient(double[,] x, int i, int j, int[] columnIndices, double scale, double shape, double inverseLength, 142 142 bool fixedInverseLength, bool fixedScale, bool fixedShape) { 143 143 double d = i == j 144 144 ? 0.0 145 : Util.SqrDist(x, i, j, inverseLength, columnIndices);145 : Util.SqrDist(x, i, j, columnIndices, inverseLength); 146 146 147 147 double b = 1 + 0.5 * d / shape; 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)); 148 var g = new List<double>(3); 149 if (!fixedInverseLength) g.Add(scale * Math.Pow(b, -shape - 1) * d); 150 if (!fixedScale) g.Add(2 * scale * Math.Pow(b, -shape)); 151 if (!fixedShape) g.Add(scale * Math.Pow(b, -shape) * (0.5 * d / b - shape * Math.Log(b))); 152 return g; 151 153 } 152 154 } -
stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceScale.cs
r12009 r13981 87 87 } 88 88 89 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int>columnIndices) {89 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) { 90 90 double scale; 91 91 GetParameterValues(p, out scale); … … 100 100 } 101 101 102 private static I Enumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int>columnIndices, double scale, ParameterizedCovarianceFunction cov,102 private static IList<double> GetGradient(double[,] x, int i, int j, int[] columnIndices, double scale, ParameterizedCovarianceFunction cov, 103 103 bool fixedScale) { 104 var gr = new List<double>((!fixedScale ? 1 : 0) + cov.CovarianceGradient(x, i, j).Count); 104 105 if (!fixedScale) { 105 yield return 2 * scale * cov.Covariance(x, i, j);106 gr.Add(2 * scale * cov.Covariance(x, i, j)); 106 107 } 107 108 foreach (var g in cov.CovarianceGradient(x, i, j)) 108 yield return scale * g; 109 gr.Add(scale * g); 110 return gr; 109 111 } 110 112 } -
stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceSpectralMixture.cs
r12009 r13981 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using System.Linq.Expressions;26 25 using HeuristicLab.Common; 27 26 using HeuristicLab.Core; … … 131 130 } 132 131 133 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int>columnIndices) {132 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) { 134 133 double[] weight, frequency, lengthScale; 135 134 GetParameterValues(p, out weight, out frequency, out lengthScale); … … 152 151 } 153 152 154 private static double GetCovariance(double[,] x, double[,] xt, int i, int j, int maxQ, double[] weight, double[] frequency, double[] lengthScale, IEnumerable<int>columnIndices) {153 private static double GetCovariance(double[,] x, double[,] xt, int i, int j, int maxQ, double[] weight, double[] frequency, double[] lengthScale, int[] columnIndices) { 155 154 // tau = x - x' (only for selected variables) 156 155 double[] tau = … … 164 163 int idx = 0; // helper index for tau 165 164 // for each selected variable 166 foreach (var c in columnIndices) { 167 kc *= f1(tau[idx], lengthScale[q * numberOfVariables + c]) * f2(tau[idx], frequency[q * numberOfVariables + c]); 165 for (int c = 0; c < columnIndices.Length; c++) { 166 var col = columnIndices[c]; 167 kc *= f1(tau[idx], lengthScale[q * numberOfVariables + col]) * f2(tau[idx], frequency[q * numberOfVariables + col]); 168 168 idx++; 169 169 } … … 181 181 182 182 // order of returned gradients must match the order in GetParameterValues! 183 private static I Enumerable<double> GetGradient(double[,] x, int i, int j, int maxQ, double[] weight, double[] frequency, double[] lengthScale, IEnumerable<int>columnIndices,183 private static IList<double> GetGradient(double[,] x, int i, int j, int maxQ, double[] weight, double[] frequency, double[] lengthScale, int[] columnIndices, 184 184 bool fixedWeight, bool fixedFrequency, bool fixedLengthScale) { 185 185 double[] tau = Util.GetRow(x, i, columnIndices).Zip(Util.GetRow(x, j, columnIndices), (xi, xj) => xi - xj).ToArray(); 186 186 int numberOfVariables = lengthScale.Length / maxQ; 187 187 188 var g = new List<double>((!fixedWeight ? maxQ : 0) + (!fixedFrequency ? maxQ * columnIndices.Length : 0) + (!fixedLengthScale ? maxQ * columnIndices.Length : 0)); 188 189 if (!fixedWeight) { 189 190 // weight … … 193 194 int idx = 0; // helper index for tau 194 195 // 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]); 196 for (int c = 0; c < columnIndices.Length; c++) { 197 var col = columnIndices[c]; 198 k *= f1(tau[idx], lengthScale[q * numberOfVariables + col]) * f2(tau[idx], frequency[q * numberOfVariables + col]); 197 199 idx++; 198 200 } 199 yield return k;201 g.Add(k); 200 202 } 201 203 } … … 212 214 Math.Sin(2 * Math.PI * tau[idx] * frequency[q * numberOfVariables + c]); 213 215 idx++; 214 yield return weight[q] * k;216 g.Add(weight[q] * k); 215 217 } 216 218 } … … 228 230 f2(tau[idx], frequency[q * numberOfVariables + c]); 229 231 idx++; 230 yield return weight[q] * k;232 g.Add(weight[q] * k); 231 233 } 232 234 } 233 235 } 236 237 return g; 234 238 } 235 239 } -
stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceSquaredExponentialArd.cs
r12009 r13981 99 99 } 100 100 101 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int>columnIndices) {101 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) { 102 102 double scale; 103 103 double[] inverseLength; … … 122 122 123 123 // order of returned gradients must match the order in GetParameterValues! 124 private static I Enumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int>columnIndices, double scale, double[] inverseLength,124 private static IList<double> GetGradient(double[,] x, int i, int j, int[] columnIndices, double scale, double[] inverseLength, 125 125 bool fixedInverseLength, bool fixedScale) { 126 126 double d = i == j … … 129 129 130 130 int k = 0; 131 var g = new List<double>((!fixedInverseLength ? columnIndices.Length : 0) + (!fixedScale ? 1 : 0)); 131 132 if (!fixedInverseLength) { 132 foreach (var columnIndex in columnIndices) { 133 for (int c = 0; c < columnIndices.Length; c++) { 134 var columnIndex = columnIndices[c]; 133 135 double sqrDist = Util.SqrDist(x[i, columnIndex] * inverseLength[k], x[j, columnIndex] * inverseLength[k]); 134 yield return scale * Math.Exp(-d / 2.0) * sqrDist;136 g.Add(scale * Math.Exp(-d / 2.0) * sqrDist); 135 137 k++; 136 138 } 137 139 } 138 if (!fixedScale) yield return 2.0 * scale * Math.Exp(-d / 2.0); 140 if (!fixedScale) g.Add(2.0 * scale * Math.Exp(-d / 2.0)); 141 return g; 139 142 } 140 143 } -
stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceSquaredExponentialIso.cs
r12009 r13981 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Linq.Expressions;25 24 using HeuristicLab.Common; 26 25 using HeuristicLab.Core; … … 104 103 } 105 104 106 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int>columnIndices) {105 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) { 107 106 double inverseLength, scale; 108 107 GetParameterValues(p, out scale, out inverseLength); … … 114 113 double d = i == j 115 114 ? 0.0 116 : Util.SqrDist(x, i, j, inverseLength, columnIndices);115 : Util.SqrDist(x, i, j, columnIndices, inverseLength); 117 116 return scale * Math.Exp(-d / 2.0); 118 117 }; 119 118 cov.CrossCovariance = (x, xt, i, j) => { 120 double d = Util.SqrDist(x, i, xt, j, inverseLength, columnIndices);119 double d = Util.SqrDist(x, i, xt, j, columnIndices, inverseLength); 121 120 return scale * Math.Exp(-d / 2.0); 122 121 }; … … 127 126 128 127 // order of returned gradients must match the order in GetParameterValues! 129 private static I Enumerable<double> GetGradient(double[,] x, int i, int j, double sf2, double inverseLength, IEnumerable<int> columnIndices,128 private static IList<double> GetGradient(double[,] x, int i, int j, double sf2, double inverseLength, int[] columnIndices, 130 129 bool fixedInverseLength, bool fixedScale) { 131 130 double d = i == j 132 131 ? 0.0 133 : Util.SqrDist(x, i, j, inverseLength, columnIndices);132 : Util.SqrDist(x, i, j, columnIndices, inverseLength); 134 133 double g = Math.Exp(-d / 2.0); 135 if (!fixedInverseLength) yield return sf2 * g * d; 136 if (!fixedScale) yield return 2.0 * sf2 * g; 134 var gr = new List<double>(2); 135 if (!fixedInverseLength) gr.Add(sf2 * g * d); 136 if (!fixedScale) gr.Add(2.0 * sf2 * g); 137 return gr; 137 138 } 138 139 } -
stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceSum.cs
r12009 r13981 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using System.Linq.Expressions;26 25 using HeuristicLab.Common; 27 26 using HeuristicLab.Core; … … 76 75 } 77 76 78 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int>columnIndices) {77 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) { 79 78 if (terms.Count == 0) throw new ArgumentException("at least one term is necessary for the product covariance function."); 80 79 var functions = new List<ParameterizedCovarianceFunction>(); … … 88 87 sum.Covariance = (x, i, j) => functions.Select(e => e.Covariance(x, i, j)).Sum(); 89 88 sum.CrossCovariance = (x, xt, i, j) => functions.Select(e => e.CrossCovariance(x, xt, i, j)).Sum(); 90 sum.CovarianceGradient = (x, i, j) => functions.Select(e => e.CovarianceGradient(x, i, j)).Aggregate(Enumerable.Concat); 89 sum.CovarianceGradient = (x, i, j) => { 90 var g = new List<double>(); 91 foreach (var e in functions) 92 g.AddRange(e.CovarianceGradient(x, i, j)); 93 return g; 94 }; 91 95 return sum; 92 96 } -
stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/GaussianProcessModel.cs
r13287 r13981 167 167 try { 168 168 CalculateModel(ds, rows, scaleInputs); 169 } catch (alglib.alglibexception ae) { 169 } 170 catch (alglib.alglibexception ae) { 170 171 // wrap exception so that calling code doesn't have to know about alglib implementation 171 172 throw new ArgumentException("There was a problem in the calculation of the Gaussian process model", ae); … … 185 186 int n = x.GetLength(0); 186 187 188 var columns = Enumerable.Range(0, x.GetLength(1)).ToArray(); 187 189 // calculate cholesky decomposed (lower triangular) covariance matrix 188 var cov = covarianceFunction.GetParameterizedCovarianceFunction(covarianceParameter, Enumerable.Range(0, x.GetLength(1)));190 var cov = covarianceFunction.GetParameterizedCovarianceFunction(covarianceParameter, columns); 189 191 this.l = CalculateL(x, cov, sqrSigmaNoise); 190 192 191 193 // calculate mean 192 var mean = meanFunction.GetParameterizedMeanFunction(meanParameter, Enumerable.Range(0, x.GetLength(1)));194 var mean = meanFunction.GetParameterizedMeanFunction(meanParameter, columns); 193 195 double[] m = Enumerable.Range(0, x.GetLength(0)) 194 196 .Select(r => mean.Mean(x, r)) … … 227 229 double[] meanGradients = new double[meanFunction.GetNumberOfParameters(nAllowedVariables)]; 228 230 for (int k = 0; k < meanGradients.Length; k++) { 229 var meanGrad = Enumerable.Range(0, alpha.Length) 230 .Select(r => mean.Gradient(x, r, k)); 231 var meanGrad = new double[alpha.Length]; 232 for (int g = 0; g < meanGrad.Length; g++) 233 meanGrad[g] = mean.Gradient(x, g, k); 231 234 meanGradients[k] = -Util.ScalarProd(meanGrad, alpha); 232 235 } … … 236 239 for (int i = 0; i < n; i++) { 237 240 for (int j = 0; j < i; j++) { 238 var g = cov.CovarianceGradient(x, i, j) .ToArray();241 var g = cov.CovarianceGradient(x, i, j); 239 242 for (int k = 0; k < covGradients.Length; k++) { 240 243 covGradients[k] += lCopy[i, j] * g[k]; … … 242 245 } 243 246 244 var gDiag = cov.CovarianceGradient(x, i, i) .ToArray();247 var gDiag = cov.CovarianceGradient(x, i, i); 245 248 for (int k = 0; k < covGradients.Length; k++) { 246 249 // diag … … 320 323 int newN = newX.GetLength(0); 321 324 322 var Ks = new double[newN, n]; 323 var mean = meanFunction.GetParameterizedMeanFunction(meanParameter, Enumerable.Range(0, newX.GetLength(1))); 325 var Ks = new double[newN][]; 326 var columns = Enumerable.Range(0, newX.GetLength(1)).ToArray(); 327 var mean = meanFunction.GetParameterizedMeanFunction(meanParameter, columns); 324 328 var ms = Enumerable.Range(0, newX.GetLength(0)) 325 329 .Select(r => mean.Mean(newX, r)) 326 330 .ToArray(); 327 var cov = covarianceFunction.GetParameterizedCovarianceFunction(covarianceParameter, Enumerable.Range(0, newX.GetLength(1)));331 var cov = covarianceFunction.GetParameterizedCovarianceFunction(covarianceParameter, columns); 328 332 for (int i = 0; i < newN; i++) { 333 Ks[i] = new double[n]; 329 334 for (int j = 0; j < n; j++) { 330 Ks[i ,j] = cov.CrossCovariance(x, newX, j, i);335 Ks[i][j] = cov.CrossCovariance(x, newX, j, i); 331 336 } 332 337 } 333 338 334 339 return Enumerable.Range(0, newN) 335 .Select(i => ms[i] + Util.ScalarProd(Util.GetRow(Ks, i), alpha)); 336 } catch (alglib.alglibexception ae) { 340 .Select(i => ms[i] + Util.ScalarProd(Ks[i], alpha)); 341 } 342 catch (alglib.alglibexception ae) { 337 343 // wrap exception so that calling code doesn't have to know about alglib implementation 338 344 throw new ArgumentException("There was a problem in the calculation of the Gaussian process model", ae); … … 352 358 var kss = new double[newN]; 353 359 double[,] sWKs = new double[n, newN]; 354 var cov = covarianceFunction.GetParameterizedCovarianceFunction(covarianceParameter, Enumerable.Range(0, x.GetLength(1))); 360 var columns = Enumerable.Range(0, newX.GetLength(1)).ToArray(); 361 var cov = covarianceFunction.GetParameterizedCovarianceFunction(covarianceParameter, columns); 355 362 356 363 if (l == null) { … … 372 379 373 380 for (int i = 0; i < newN; i++) { 374 var sumV = Util.ScalarProd(Util.GetCol(sWKs, i), Util.GetCol(sWKs, i)); 381 var col = Util.GetCol(sWKs, i).ToArray(); 382 var sumV = Util.ScalarProd(col, col); 375 383 kss[i] += sqrSigmaNoise; // kss is V(f), add noise variance of predictive distibution to get V(y) 376 384 kss[i] -= sumV; … … 378 386 } 379 387 return kss; 380 } catch (alglib.alglibexception ae) { 388 } 389 catch (alglib.alglibexception ae) { 381 390 // wrap exception so that calling code doesn't have to know about alglib implementation 382 391 throw new ArgumentException("There was a problem in the calculation of the Gaussian process model", ae); -
stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/ICovarianceFunction.cs
r12009 r13981 20 20 #endregion 21 21 22 using System;23 22 using System.Collections.Generic; 24 using System.Linq.Expressions;25 23 using HeuristicLab.Core; 26 24 … … 29 27 public delegate double CovarianceFunctionDelegate(double[,] x, int i, int j); 30 28 public delegate double CrossCovarianceFunctionDelegate(double[,] x, double[,] xt, int i, int j); 31 public delegate I Enumerable<double> CovarianceGradientFunctionDelegate(double[,] x, int i, int j);29 public delegate IList<double> CovarianceGradientFunctionDelegate(double[,] x, int i, int j); 32 30 33 31 public class ParameterizedCovarianceFunction { … … 40 38 int GetNumberOfParameters(int numberOfVariables); 41 39 void SetParameter(double[] p); 42 ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int>columnIndices);40 ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices); 43 41 } 44 42 } -
stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/IMeanFunction.cs
r12009 r13981 20 20 #endregion 21 21 22 using System;23 using System.Collections.Generic;24 22 using HeuristicLab.Core; 25 23 … … 36 34 int GetNumberOfParameters(int numberOfVariables); 37 35 void SetParameter(double[] p); 38 ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, IEnumerable<int>columnIndices);36 ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, int[] columnIndices); 39 37 } 40 38 } -
stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanFunctions/MeanConst.cs
r12009 r13981 76 76 } 77 77 78 public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, IEnumerable<int>columnIndices) {78 public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, int[] columnIndices) { 79 79 double c; 80 80 GetParameters(p, out c); -
stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanFunctions/MeanLinear.cs
r12009 r13981 21 21 22 22 using System; 23 using System.Collections.Generic;24 23 using System.Linq; 25 24 using HeuristicLab.Common; … … 70 69 } 71 70 72 public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, IEnumerable<int>columnIndices) {71 public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, int[] columnIndices) { 73 72 double[] weights; 74 int[] columns = columnIndices .ToArray();73 int[] columns = columnIndices; 75 74 GetParameter(p, out weights); 76 75 var mf = new ParameterizedMeanFunction(); … … 78 77 // sanity check 79 78 if (weights.Length != columns.Length) throw new ArgumentException("The number of rparameters must match the number of variables for the linear mean function."); 80 return Util.ScalarProd(weights, Util.GetRow(x, i, columns) );79 return Util.ScalarProd(weights, Util.GetRow(x, i, columns).ToArray()); 81 80 }; 82 81 mf.Gradient = (x, i, k) => { -
stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanFunctions/MeanModel.cs
r13146 r13981 73 73 } 74 74 75 public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, IEnumerable<int>columnIndices) {75 public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, int[] columnIndices) { 76 76 if (p.Length > 0) throw new ArgumentException("No parameters allowed for model-based mean function.", "p"); 77 77 var solution = RegressionSolution; 78 78 var variableNames = solution.ProblemData.AllowedInputVariables.ToArray(); 79 if (variableNames.Length != columnIndices. Count())79 if (variableNames.Length != columnIndices.Length) 80 80 throw new ArgumentException("The number of input variables does not match in MeanModel"); 81 81 var variableValues = variableNames.Select(_ => new List<double>() { 0.0 }).ToArray(); // or of zeros -
stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanFunctions/MeanProduct.cs
r12009 r13981 73 73 74 74 75 public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, IEnumerable<int>columnIndices) {75 public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, int[] columnIndices) { 76 76 var factorMf = new List<ParameterizedMeanFunction>(); 77 77 int totalNumberOfParameters = GetNumberOfParameters(numberOfVariables); -
stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanFunctions/MeanSum.cs
r12009 r13981 68 68 } 69 69 70 public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, IEnumerable<int>columnIndices) {70 public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, int[] columnIndices) { 71 71 var termMf = new List<ParameterizedMeanFunction>(); 72 72 int totalNumberOfParameters = GetNumberOfParameters(numberOfVariables); -
stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanFunctions/MeanZero.cs
r12009 r13981 20 20 #endregion 21 21 using System; 22 using System.Collections.Generic;23 using System.Linq;24 22 using HeuristicLab.Common; 25 23 using HeuristicLab.Core; … … 50 48 } 51 49 52 public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, IEnumerable<int>columnIndices) {50 public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, int[] columnIndices) { 53 51 if (p.Length > 0) throw new ArgumentException("No parameters allowed for zero mean function.", "p"); 54 52 var mf = new ParameterizedMeanFunction(); -
stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/StudentTProcessModel.cs
r13438 r13981 171 171 try { 172 172 CalculateModel(ds, rows, scaleInputs); 173 } catch (alglib.alglibexception ae) { 173 } 174 catch (alglib.alglibexception ae) { 174 175 // wrap exception so that calling code doesn't have to know about alglib implementation 175 176 throw new ArgumentException("There was a problem in the calculation of the Gaussian process model", ae); … … 188 189 189 190 int n = x.GetLength(0); 191 var columns = Enumerable.Range(0, x.GetLength(1)).ToArray(); 190 192 191 193 // calculate cholesky decomposed (lower triangular) covariance matrix 192 var cov = covarianceFunction.GetParameterizedCovarianceFunction(covarianceParameter, Enumerable.Range(0, x.GetLength(1)));194 var cov = covarianceFunction.GetParameterizedCovarianceFunction(covarianceParameter, columns); 193 195 this.l = CalculateL(x, cov); 194 196 195 197 // calculate mean 196 var mean = meanFunction.GetParameterizedMeanFunction(meanParameter, Enumerable.Range(0, x.GetLength(1)));198 var mean = meanFunction.GetParameterizedMeanFunction(meanParameter, columns); 197 199 double[] m = Enumerable.Range(0, x.GetLength(0)) 198 200 .Select(r => mean.Mean(x, r)) … … 240 242 double[] meanGradients = new double[meanFunction.GetNumberOfParameters(nAllowedVariables)]; 241 243 for (int k = 0; k < meanGradients.Length; k++) { 242 var meanGrad = Enumerable.Range(0, alpha.Length) 243 .Select(r => mean.Gradient(x, r, k)); 244 meanGradients[k] = -Util.ScalarProd(meanGrad, alpha); //TODO not working yet, try to fix with gradient check 244 var meanGrad = new double[alpha.Length]; 245 for (int g = 0; g < meanGrad.Length; g++) 246 meanGrad[g] = mean.Gradient(x, g, k); 247 meanGradients[k] = -Util.ScalarProd(meanGrad, alpha);//TODO not working yet, try to fix with gradient check 245 248 } 246 249 … … 249 252 for (int i = 0; i < n; i++) { 250 253 for (int j = 0; j < i; j++) { 251 var g = cov.CovarianceGradient(x, i, j) .ToArray();254 var g = cov.CovarianceGradient(x, i, j); 252 255 for (int k = 0; k < covGradients.Length; k++) { 253 256 covGradients[k] += lCopy[i, j] * g[k]; … … 255 258 } 256 259 257 var gDiag = cov.CovarianceGradient(x, i, i) .ToArray();260 var gDiag = cov.CovarianceGradient(x, i, i); 258 261 for (int k = 0; k < covGradients.Length; k++) { 259 262 // diag … … 336 339 double[,] newX = GetData(dataset, allowedInputVariables, rows, inputScaling); 337 340 int newN = newX.GetLength(0); 338 339 var Ks = new double[newN, n]; 340 var mean = meanFunction.GetParameterizedMeanFunction(meanParameter, Enumerable.Range(0, newX.GetLength(1))); 341 var columns = Enumerable.Range(0, newX.GetLength(1)).ToArray(); 342 343 var Ks = new double[newN][]; 344 var mean = meanFunction.GetParameterizedMeanFunction(meanParameter, columns); 341 345 var ms = Enumerable.Range(0, newX.GetLength(0)) 342 346 .Select(r => mean.Mean(newX, r)) 343 347 .ToArray(); 344 var cov = covarianceFunction.GetParameterizedCovarianceFunction(covarianceParameter, Enumerable.Range(0, newX.GetLength(1)));348 var cov = covarianceFunction.GetParameterizedCovarianceFunction(covarianceParameter, columns); 345 349 for (int i = 0; i < newN; i++) { 350 Ks[i] = new double[n]; 346 351 for (int j = 0; j < n; j++) { 347 Ks[i ,j] = cov.CrossCovariance(x, newX, j, i);352 Ks[i][j] = cov.CrossCovariance(x, newX, j, i); 348 353 } 349 354 } 350 355 351 356 return Enumerable.Range(0, newN) 352 .Select(i => ms[i] + Util.ScalarProd(Util.GetRow(Ks, i), alpha)); 353 } catch (alglib.alglibexception ae) { 357 .Select(i => ms[i] + Util.ScalarProd(Ks[i], alpha)); 358 } 359 catch (alglib.alglibexception ae) { 354 360 // wrap exception so that calling code doesn't have to know about alglib implementation 355 361 throw new ArgumentException("There was a problem in the calculation of the Gaussian process model", ae); … … 369 375 var kss = new double[newN]; 370 376 double[,] sWKs = new double[n, newN]; 371 var cov = covarianceFunction.GetParameterizedCovarianceFunction(covarianceParameter, Enumerable.Range(0, x.GetLength(1)) );372 377 var cov = covarianceFunction.GetParameterizedCovarianceFunction(covarianceParameter, Enumerable.Range(0, x.GetLength(1)).ToArray()); 378 373 379 if (l == null) { 374 380 l = CalculateL(x, cov); 375 381 } 376 382 377 383 // for stddev 378 384 for (int i = 0; i < newN; i++) 379 385 kss[i] = cov.Covariance(newX, i, i); 380 386 381 387 for (int i = 0; i < newN; i++) { 382 388 for (int j = 0; j < n; j++) { 383 sWKs[j, i] = cov.CrossCovariance(x, newX, j, i) 389 sWKs[j, i] = cov.CrossCovariance(x, newX, j, i); 384 390 } 385 391 } 386 392 387 393 // for stddev 388 394 alglib.ablas.rmatrixlefttrsm(n, newN, l, 0, 0, false, false, 0, ref sWKs, 0, 0); 389 395 390 396 for (int i = 0; i < newN; i++) { 391 var sumV = Util.ScalarProd(Util.GetCol(sWKs, i), Util.GetCol(sWKs, i)); 397 var col = Util.GetCol(sWKs, i).ToArray(); 398 var sumV = Util.ScalarProd(col, col); 392 399 kss[i] -= sumV; 393 kss[i] *= (nu + beta - 2) / (nu + n - 2);400 kss[i] *= (nu + beta - 2) / (nu + n - 2); 394 401 if (kss[i] < 0) kss[i] = 0; 395 402 } 396 403 return kss; 397 } catch (alglib.alglibexception ae) { 404 } 405 catch (alglib.alglibexception ae) { 398 406 // wrap exception so that calling code doesn't have to know about alglib implementation 399 407 throw new ArgumentException("There was a problem in the calculation of the Gaussian process model", ae); -
stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/Util.cs
r12009 r13981 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using HeuristicLab.Core;26 using HeuristicLab.Data;27 25 28 26 namespace HeuristicLab.Algorithms.DataAnalysis { 29 27 internal static class Util { 30 public static double ScalarProd(IEnumerable<double> v, IEnumerable<double> u) { 31 return v.Zip(u, (vi, ui) => vi * ui).Sum(); 28 public static double ScalarProd(double[] v, double[] u) { 29 if (v.Length != u.Length) throw new InvalidOperationException(); 30 double prod = 0.0; 31 for (int i = 0; i < v.Length; i++) 32 prod += v[i] * u[i]; 33 return prod; 32 34 } 33 35 … … 41 43 } 42 44 43 public static double SqrDist(double[,] x, int i, int j, double scale = 1.0, IEnumerable<int> columnIndices = null) {44 return SqrDist(x, i, x, j, scale, columnIndices);45 public static double SqrDist(double[,] x, int i, int j, int[] columnIndices, double scale = 1.0) { 46 return SqrDist(x, i, x, j, columnIndices, scale); 45 47 } 46 48 47 public static double SqrDist(double[,] x, int i, double[,] xt, int j, double scale = 1.0, IEnumerable<int> columnIndices = null) {49 public static double SqrDist(double[,] x, int i, double[,] xt, int j, int[] columnIndices, double scale = 1.0) { 48 50 double ss = 0.0; 49 if (columnIndices == null) columnIndices = Enumerable.Range(0, x.GetLength(1)); 50 foreach (int columnIndex in columnIndices) { 51 if (columnIndices == null) columnIndices = Enumerable.Range(0, x.GetLength(1)).ToArray(); 52 for (int c = 0; c < columnIndices.Length; c++) { 53 var columnIndex = columnIndices[c]; 51 54 double d = x[i, columnIndex] - xt[j, columnIndex]; 52 55 ss += d * d; … … 55 58 } 56 59 57 public static double SqrDist(double[,] x, int i, int j, double[] scale, IEnumerable<int> columnIndices = null) {60 public static double SqrDist(double[,] x, int i, int j, double[] scale, int[] columnIndices) { 58 61 return SqrDist(x, i, x, j, scale, columnIndices); 59 62 } 60 63 61 public static double SqrDist(double[,] x, int i, double[,] xt, int j, double[] scale, IEnumerable<int> columnIndices = null) {64 public static double SqrDist(double[,] x, int i, double[,] xt, int j, double[] scale, int[] columnIndices) { 62 65 double ss = 0.0; 63 if (columnIndices == null) columnIndices = Enumerable.Range(0, x.GetLength(1));64 66 int scaleIndex = 0; 65 foreach (int columnIndex in columnIndices) { 67 for (int c = 0; c < columnIndices.Length; c++) { 68 var columnIndex = columnIndices[c]; 66 69 double d = x[i, columnIndex] - xt[j, columnIndex]; 67 70 ss += d * d * scale[scaleIndex] * scale[scaleIndex]; … … 73 76 return ss; 74 77 } 75 public static double ScalarProd(double[,] x, int i, int j, double scale = 1.0, IEnumerable<int> columnIndices = null) {76 return ScalarProd(x, i, x, j, scale, columnIndices);78 public static double ScalarProd(double[,] x, int i, int j, int[] columnIndices, double scale = 1.0) { 79 return ScalarProd(x, i, x, j, columnIndices, scale); 77 80 } 78 81 79 public static double ScalarProd(double[,] x, int i, double[,] xt, int j, double scale = 1.0, IEnumerable<int> columnIndices = null) {82 public static double ScalarProd(double[,] x, int i, double[,] xt, int j, int[] columnIndices, double scale = 1.0) { 80 83 double sum = 0.0; 81 if (columnIndices == null) columnIndices = Enumerable.Range(0, x.GetLength(1));82 foreach (int columnIndex in columnIndices) {84 for (int c = 0; c < columnIndices.Length; c++) { 85 var columnIndex = columnIndices[c]; 83 86 sum += x[i, columnIndex] * xt[j, columnIndex]; 84 87 } 85 88 return scale * scale * sum; 86 89 } 87 public static double ScalarProd(double[,] x, int i, int j, double[] scale, IEnumerable<int> columnIndices = null) {90 public static double ScalarProd(double[,] x, int i, int j, double[] scale, int[] columnIndices) { 88 91 return ScalarProd(x, i, x, j, scale, columnIndices); 89 92 } 90 93 91 public static double ScalarProd(double[,] x, int i, double[,] xt, int j, double[] scale, IEnumerable<int> columnIndices = null) {94 public static double ScalarProd(double[,] x, int i, double[,] xt, int j, double[] scale, int[] columnIndices) { 92 95 double sum = 0.0; 93 if (columnIndices == null) columnIndices = Enumerable.Range(0, x.GetLength(1));94 96 int scaleIndex = 0; 95 foreach (int columnIndex in columnIndices) { 97 for (int c = 0; c < columnIndices.Length; c++, scaleIndex++) { 98 var columnIndex = columnIndices[c]; 96 99 sum += x[i, columnIndex] * scale[scaleIndex] * xt[j, columnIndex] * scale[scaleIndex]; 97 scaleIndex++;98 100 } 99 101 // must be at the end of scale after iterating over columnIndices
Note: See TracChangeset
for help on using the changeset viewer.