Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/05/12 11:58:17 (12 years ago)
Author:
mkommend
Message:

#1081: Merged trunk changes and fixed compilation errors due to the merge.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/GaussianProcessModel.cs

    r8477 r8742  
    4242
    4343    [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]
    4454    private ICovarianceFunction covarianceFunction;
    4555    public ICovarianceFunction CovarianceFunction {
     
    6676    [Storable]
    6777    private double sqrSigmaNoise;
     78    public double SigmaNoise {
     79      get { return Math.Sqrt(sqrSigmaNoise); }
     80    }
    6881
    6982    [Storable]
     
    124137      l = new double[n, n];
    125138
    126       meanFunction.SetData(x);
    127       covarianceFunction.SetData(x);
    128 
    129139      // calculate means and covariances
    130140      double[] m = meanFunction.GetMean(x);
    131141      for (int i = 0; i < n; i++) {
    132142        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;
    134144          if (j == i) l[j, i] += 1.0;
    135145        }
     
    153163        alpha[i] = alpha[i] / sqrSigmaNoise;
    154164      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
    158166      // derivatives
    159       int n = x.GetLength(0);
    160167      int nAllowedVariables = x.GetLength(1);
    161168
    162       int info;
    163169      alglib.matinvreport matInvRep;
    164170      double[,] lCopy = new double[l.GetLength(0), l.GetLength(1)];
     
    183189      if (covGradients.Length > 0) {
    184190        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();
    185199          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];
    190202          }
    191203        }
    192204      }
    193205
    194       return
     206      hyperparameterGradients =
    195207        meanGradients
    196208        .Concat(covGradients)
    197209        .Concat(new double[] { noiseGradient }).ToArray();
     210
    198211    }
    199212
     
    208221    }
    209222    public GaussianProcessRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
    210       return new GaussianProcessRegressionSolution(this, problemData);
     223      return new GaussianProcessRegressionSolution(this, new RegressionProblemData(problemData));
    211224    }
    212225    IRegressionSolution IRegressionModel.CreateRegressionSolution(IRegressionProblemData problemData) {
     
    214227    }
    215228    #endregion
     229
    216230
    217231    private IEnumerable<double> GetEstimatedValuesHelper(Dataset dataset, IEnumerable<int> rows) {
     
    219233      int newN = newX.GetLength(0);
    220234      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];
    227235      var Ks = new double[newN, n];
    228       //double[,] sWKs = new double[n, newN];
    229       // double[,] v;
    230 
    231 
    232       // for stddev
    233       //covarianceFunction.SetParameter(covHyp, newX);
    234       //kss = covarianceFunction.GetDiagonalCovariances();
    235 
    236       covarianceFunction.SetData(x, newX);
    237       meanFunction.SetData(newX);
    238236      var ms = meanFunction.GetMean(newX);
    239237      for (int i = 0; i < newN; i++) {
    240238        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      }
    248242
    249243      return Enumerable.Range(0, newN)
    250244        .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 
    257245    }
    258246
     
    266254
    267255      // for stddev
    268       covarianceFunction.SetData(newX);
    269256      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
    273259      for (int i = 0; i < newN; i++) {
    274260        for (int j = 0; j < n; j++) {
    275           sWKs[j, i] = covarianceFunction.GetCovariance(j, i) / Math.Sqrt(sqrSigmaNoise);
     261          sWKs[j, i] = covarianceFunction.GetCrossCovariance(x, newX, j, i) / Math.Sqrt(sqrSigmaNoise);
    276262        }
    277263      }
    278264
    279265      // 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);
    285267
    286268      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));
    288270        kss[i] -= sumV;
    289271        if (kss[i] < 0) kss[i] = 0;
Note: See TracChangeset for help on using the changeset viewer.