Changeset 8484 for trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/GaussianProcessModel.cs
- Timestamp:
- 08/14/12 13:25:17 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/GaussianProcessModel.cs
r8475 r8484 39 39 public double NegativeLogLikelihood { 40 40 get { return negativeLogLikelihood; } 41 } 42 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 } 41 51 } 42 52 … … 125 135 126 136 meanFunction.SetData(x); 127 covarianceFunction.SetData(x);128 137 129 138 // calculate means and covariances … … 131 140 for (int i = 0; i < n; i++) { 132 141 for (int j = i; j < n; j++) { 133 l[j, i] = covarianceFunction.GetCovariance( i, j) / sqrSigmaNoise;142 l[j, i] = covarianceFunction.GetCovariance(x, i, j) / sqrSigmaNoise; 134 143 if (j == i) l[j, i] += 1.0; 135 144 } … … 153 162 alpha[i] = alpha[i] / sqrSigmaNoise; 154 163 negativeLogLikelihood = 0.5 * Util.ScalarProd(ym, alpha) + diagSum + (n / 2.0) * Math.Log(2.0 * Math.PI * sqrSigmaNoise); 155 } 156 157 public double[] GetHyperparameterGradients() { 164 158 165 // derivatives 159 int n = x.GetLength(0);160 166 int nAllowedVariables = x.GetLength(1); 161 167 162 int info;163 168 alglib.matinvreport matInvRep; 164 169 double[,] lCopy = new double[l.GetLength(0), l.GetLength(1)]; … … 183 188 if (covGradients.Length > 0) { 184 189 for (int i = 0; i < n; i++) { 190 for (int j = 0; j < i; j++) { 191 var g = covarianceFunction.GetGradient(x, i, j).ToArray(); 192 for (int k = 0; k < covGradients.Length; k++) { 193 covGradients[k] += lCopy[i, j] * g[k]; 194 } 195 } 196 197 var gDiag = covarianceFunction.GetGradient(x, i, i).ToArray(); 185 198 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); 199 // diag 200 covGradients[k] += 0.5 * lCopy[i, i] * gDiag[k]; 190 201 } 191 202 } 192 203 } 193 204 194 return205 hyperparameterGradients = 195 206 meanGradients 196 207 .Concat(covGradients) 197 208 .Concat(new double[] { noiseGradient }).ToArray(); 209 198 210 } 199 211 … … 219 231 int newN = newX.GetLength(0); 220 232 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 233 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 234 meanFunction.SetData(newX); 238 235 var ms = meanFunction.GetMean(newX); 239 236 for (int i = 0; i < newN; i++) { 240 237 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); 238 Ks[i, j] = covarianceFunction.GetCrossCovariance(x, newX, j, i); 239 } 240 } 248 241 249 242 return Enumerable.Range(0, newN) 250 243 .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 244 } 258 245 … … 266 253 267 254 // for stddev 268 covarianceFunction.SetData(newX);269 255 for (int i = 0; i < newN; i++) 270 kss[i] = covarianceFunction.GetCovariance(i, i); 271 272 covarianceFunction.SetData(x, newX); 256 kss[i] = covarianceFunction.GetCovariance(newX, i, i); 257 273 258 for (int i = 0; i < newN; i++) { 274 259 for (int j = 0; j < n; j++) { 275 sWKs[j, i] = covarianceFunction.GetC ovariance(j, i) / Math.Sqrt(sqrSigmaNoise);260 sWKs[j, i] = covarianceFunction.GetCrossCovariance(x, newX, j, i) / Math.Sqrt(sqrSigmaNoise); 276 261 } 277 262 } 278 263 279 264 // 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); 265 alglib.ablas.rmatrixlefttrsm(n, newN, l, 0, 0, false, false, 0, ref sWKs, 0, 0); 285 266 286 267 for (int i = 0; i < newN; i++) { 287 var sumV = Util.ScalarProd(Util.GetCol( v, i), Util.GetCol(v, i));268 var sumV = Util.ScalarProd(Util.GetCol(sWKs, i), Util.GetCol(sWKs, i)); 288 269 kss[i] -= sumV; 289 270 if (kss[i] < 0) kss[i] = 0;
Note: See TracChangeset
for help on using the changeset viewer.