Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceSum.cs @ 8323

Last change on this file since 8323 was 8323, checked in by gkronber, 12 years ago

#1902 initial import of Gaussian process regression algorithm

File size: 1.6 KB
Line 
1using System.Collections.Generic;
2using System.Linq;
3
4namespace HeuristicLab.Algorithms.DataAnalysis.GaussianProcess {
5  public class CovarianceSum : ICovarianceFunction {
6    private IList<ICovarianceFunction> covariances;
7
8    public int NumberOfParameters {
9      get { return covariances.Sum(c => c.NumberOfParameters); }
10    }
11
12    public CovarianceSum(IEnumerable<ICovarianceFunction> covariances) {
13      this.covariances = covariances.ToList();
14    }
15
16    public void SetMatrix(double[,] x) {
17      foreach (var covariance in covariances) {
18        covariance.SetMatrix(x, x);
19      }
20    }
21
22    public void SetMatrix(double[,] x, double[,] xt) {
23      foreach (var covariance in covariances) {
24        covariance.SetMatrix(x, xt);
25      }
26    }
27
28    public void SetHyperparamter(double[] hyp) {
29      int i = 0;
30      foreach (var covariance in covariances) {
31        int n = covariance.NumberOfParameters;
32        covariance.SetHyperparamter(hyp.Skip(i).Take(n).ToArray());
33        i += n;
34      }
35    }
36
37    public double GetCovariance(int i, int j) {
38      return covariances.Select(c => c.GetCovariance(i, j)).Sum();
39    }
40
41
42    public double[] GetDiagonalCovariances() {
43      return covariances
44        .Select(c => c.GetDiagonalCovariances())
45        .Aggregate((s, d) => s.Zip(d, (a, b) => a + b).ToArray())
46        .ToArray();
47    }
48
49    public double[] GetDerivatives(int i, int j) {
50      return covariances
51        .Select(c => c.GetDerivatives(i, j))
52        .Aggregate(Enumerable.Empty<double>(), (h0, h1) => h0.Concat(h1))
53        .ToArray();
54    }
55  }
56}
Note: See TracBrowser for help on using the repository browser.