Changeset 8742 for branches/HeuristicLab.TimeSeries/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/GaussianProcessModel.cs
- Timestamp:
- 10/05/12 11:58:17 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.TimeSeries/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/GaussianProcessModel.cs
r8477 r8742 42 42 43 43 [Storable] 44 private double[] hyperparameterGradients; 45 public double[] HyperparameterGradients { 46 get { 47 var copy = new double[hyperparameterGradients.Length]; 48 Array.Copy(hyperparameterGradients, copy, copy.Length); 49 return copy; 50 } 51 } 52 53 [Storable] 44 54 private ICovarianceFunction covarianceFunction; 45 55 public ICovarianceFunction CovarianceFunction { … … 66 76 [Storable] 67 77 private double sqrSigmaNoise; 78 public double SigmaNoise { 79 get { return Math.Sqrt(sqrSigmaNoise); } 80 } 68 81 69 82 [Storable] … … 124 137 l = new double[n, n]; 125 138 126 meanFunction.SetData(x);127 covarianceFunction.SetData(x);128 129 139 // calculate means and covariances 130 140 double[] m = meanFunction.GetMean(x); 131 141 for (int i = 0; i < n; i++) { 132 142 for (int j = i; j < n; j++) { 133 l[j, i] = covarianceFunction.GetCovariance( i, j) / sqrSigmaNoise;143 l[j, i] = covarianceFunction.GetCovariance(x, i, j) / sqrSigmaNoise; 134 144 if (j == i) l[j, i] += 1.0; 135 145 } … … 153 163 alpha[i] = alpha[i] / sqrSigmaNoise; 154 164 negativeLogLikelihood = 0.5 * Util.ScalarProd(ym, alpha) + diagSum + (n / 2.0) * Math.Log(2.0 * Math.PI * sqrSigmaNoise); 155 } 156 157 public double[] GetHyperparameterGradients() { 165 158 166 // derivatives 159 int n = x.GetLength(0);160 167 int nAllowedVariables = x.GetLength(1); 161 168 162 int info;163 169 alglib.matinvreport matInvRep; 164 170 double[,] lCopy = new double[l.GetLength(0), l.GetLength(1)]; … … 183 189 if (covGradients.Length > 0) { 184 190 for (int i = 0; i < n; i++) { 191 for (int j = 0; j < i; j++) { 192 var g = covarianceFunction.GetGradient(x, i, j).ToArray(); 193 for (int k = 0; k < covGradients.Length; k++) { 194 covGradients[k] += lCopy[i, j] * g[k]; 195 } 196 } 197 198 var gDiag = covarianceFunction.GetGradient(x, i, i).ToArray(); 185 199 for (int k = 0; k < covGradients.Length; k++) { 186 for (int j = 0; j < i; j++) { 187 covGradients[k] += lCopy[i, j] * covarianceFunction.GetGradient(i, j, k); 188 } 189 covGradients[k] += 0.5 * lCopy[i, i] * covarianceFunction.GetGradient(i, i, k); 200 // diag 201 covGradients[k] += 0.5 * lCopy[i, i] * gDiag[k]; 190 202 } 191 203 } 192 204 } 193 205 194 return206 hyperparameterGradients = 195 207 meanGradients 196 208 .Concat(covGradients) 197 209 .Concat(new double[] { noiseGradient }).ToArray(); 210 198 211 } 199 212 … … 208 221 } 209 222 public GaussianProcessRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) { 210 return new GaussianProcessRegressionSolution(this, problemData);223 return new GaussianProcessRegressionSolution(this, new RegressionProblemData(problemData)); 211 224 } 212 225 IRegressionSolution IRegressionModel.CreateRegressionSolution(IRegressionProblemData problemData) { … … 214 227 } 215 228 #endregion 229 216 230 217 231 private IEnumerable<double> GetEstimatedValuesHelper(Dataset dataset, IEnumerable<int> rows) { … … 219 233 int newN = newX.GetLength(0); 220 234 int n = x.GetLength(0); 221 // var predMean = new double[newN];222 // predVar = new double[newN];223 224 225 226 // var kss = new double[newN];227 235 var Ks = new double[newN, n]; 228 //double[,] sWKs = new double[n, newN];229 // double[,] v;230 231 232 // for stddev233 //covarianceFunction.SetParameter(covHyp, newX);234 //kss = covarianceFunction.GetDiagonalCovariances();235 236 covarianceFunction.SetData(x, newX);237 meanFunction.SetData(newX);238 236 var ms = meanFunction.GetMean(newX); 239 237 for (int i = 0; i < newN; i++) { 240 238 for (int j = 0; j < n; j++) { 241 Ks[i, j] = covarianceFunction.GetCovariance(j, i); 242 //sWKs[j, i] = Ks[i, j] / Math.Sqrt(sqrSigmaNoise); 243 } 244 } 245 246 // for stddev 247 // alglib.rmatrixsolvem(l, n, sWKs, newN, true, out info, out denseSolveRep, out v); 239 Ks[i, j] = covarianceFunction.GetCrossCovariance(x, newX, j, i); 240 } 241 } 248 242 249 243 return Enumerable.Range(0, newN) 250 244 .Select(i => ms[i] + Util.ScalarProd(Util.GetRow(Ks, i), alpha)); 251 //for (int i = 0; i < newN; i++) {252 // // predMean[i] = ms[i] + prod(GetRow(Ks, i), alpha);253 // // var sumV2 = prod(GetCol(v, i), GetCol(v, i));254 // // predVar[i] = kss[i] - sumV2;255 //}256 257 245 } 258 246 … … 266 254 267 255 // for stddev 268 covarianceFunction.SetData(newX);269 256 for (int i = 0; i < newN; i++) 270 kss[i] = covarianceFunction.GetCovariance(i, i); 271 272 covarianceFunction.SetData(x, newX); 257 kss[i] = covarianceFunction.GetCovariance(newX, i, i); 258 273 259 for (int i = 0; i < newN; i++) { 274 260 for (int j = 0; j < n; j++) { 275 sWKs[j, i] = covarianceFunction.GetC ovariance(j, i) / Math.Sqrt(sqrSigmaNoise);261 sWKs[j, i] = covarianceFunction.GetCrossCovariance(x, newX, j, i) / Math.Sqrt(sqrSigmaNoise); 276 262 } 277 263 } 278 264 279 265 // for stddev 280 int info; 281 alglib.densesolverreport denseSolveRep; 282 double[,] v; 283 284 alglib.rmatrixsolvem(l, n, sWKs, newN, false, out info, out denseSolveRep, out v); 266 alglib.ablas.rmatrixlefttrsm(n, newN, l, 0, 0, false, false, 0, ref sWKs, 0, 0); 285 267 286 268 for (int i = 0; i < newN; i++) { 287 var sumV = Util.ScalarProd(Util.GetCol( v, i), Util.GetCol(v, i));269 var sumV = Util.ScalarProd(Util.GetCol(sWKs, i), Util.GetCol(sWKs, i)); 288 270 kss[i] -= sumV; 289 271 if (kss[i] < 0) kss[i] = 0;
Note: See TracChangeset
for help on using the changeset viewer.