Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/11/12 14:45:15 (12 years ago)
Author:
gkronber
Message:

#1902 fixed bug in calculation of variance in GPR model

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/GaussianProcessModel.cs

    r8473 r8475  
    130130      double[] m = meanFunction.GetMean(x);
    131131      for (int i = 0; i < n; i++) {
    132 
    133132        for (int j = i; j < n; j++) {
    134133          l[j, i] = covarianceFunction.GetCovariance(i, j) / sqrSigmaNoise;
     
    163162      int info;
    164163      alglib.matinvreport matInvRep;
    165 
    166       alglib.spdmatrixcholeskyinverse(ref l, n, false, out info, out matInvRep);
     164      double[,] lCopy = new double[l.GetLength(0), l.GetLength(1)];
     165      Array.Copy(l, lCopy, lCopy.Length);
     166
     167      alglib.spdmatrixcholeskyinverse(ref lCopy, n, false, out info, out matInvRep);
    167168      if (info != 1) throw new ArgumentException("Can't invert matrix to calculate gradients.");
    168169      for (int i = 0; i < n; i++) {
    169170        for (int j = 0; j <= i; j++)
    170           l[i, j] = l[i, j] / sqrSigmaNoise - alpha[i] * alpha[j];
    171       }
    172 
    173       double noiseGradient = sqrSigmaNoise * Enumerable.Range(0, n).Select(i => l[i, i]).Sum();
     171          lCopy[i, j] = lCopy[i, j] / sqrSigmaNoise - alpha[i] * alpha[j];
     172      }
     173
     174      double noiseGradient = sqrSigmaNoise * Enumerable.Range(0, n).Select(i => lCopy[i, i]).Sum();
    174175
    175176      double[] meanGradients = new double[meanFunction.GetNumberOfParameters(nAllowedVariables)];
     
    184185          for (int k = 0; k < covGradients.Length; k++) {
    185186            for (int j = 0; j < i; j++) {
    186               covGradients[k] += l[i, j] * covarianceFunction.GetGradient(i, j, k);
     187              covGradients[k] += lCopy[i, j] * covarianceFunction.GetGradient(i, j, k);
    187188            }
    188             covGradients[k] += 0.5 * l[i, i] * covarianceFunction.GetGradient(i, i, k);
     189            covGradients[k] += 0.5 * lCopy[i, i] * covarianceFunction.GetGradient(i, i, k);
    189190          }
    190191        }
     
    264265      double[,] sWKs = new double[n, newN];
    265266
    266 
    267267      // for stddev
    268268      covarianceFunction.SetData(newX);
     
    271271
    272272      covarianceFunction.SetData(x, newX);
    273       for (int i = 0; i < n; i++) {
    274         for (int j = 0; j < newN; j++) {
    275           sWKs[i, j] = covarianceFunction.GetCovariance(i, j) / Math.Sqrt(sqrSigmaNoise);
     273      for (int i = 0; i < newN; i++) {
     274        for (int j = 0; j < n; j++) {
     275          sWKs[j, i] = covarianceFunction.GetCovariance(j, i) / Math.Sqrt(sqrSigmaNoise);
    276276        }
    277277      }
     
    281281      alglib.densesolverreport denseSolveRep;
    282282      double[,] v;
    283       double[,] lTrans = new double[l.GetLength(1), l.GetLength(0)];
    284       for (int i = 0; i < lTrans.GetLength(0); i++)
    285         for (int j = 0; j < lTrans.GetLength(1); j++)
    286           lTrans[i, j] = l[j, i];
    287       alglib.rmatrixsolvem(lTrans, n, sWKs, newN, true, out info, out denseSolveRep, out v); // not working!
    288       // alglib.spdmatrixcholeskysolvem(lTrans, n, true, sWKs, newN, out info, out denseSolveRep, out v);
     283
     284      alglib.rmatrixsolvem(l, n, sWKs, newN, false, out info, out denseSolveRep, out v);
    289285
    290286      for (int i = 0; i < newN; i++) {
    291         var sumV2 = Util.ScalarProd(Util.GetCol(v, i), Util.GetCol(v, i));
    292         yield return kss[i] - sumV2;
    293       }
     287        var sumV = Util.ScalarProd(Util.GetCol(v, i), Util.GetCol(v, i));
     288        kss[i] -= sumV;
     289        if (kss[i] < 0) kss[i] = 0;
     290      }
     291      return kss;
    294292    }
    295293  }
Note: See TracChangeset for help on using the changeset viewer.