Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GaussianProcessTuning/HeuristicLab.Problems.Instances.DataAnalysis.GaussianProcessRegression/Util.cs @ 9122

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

#1967: adapted GP tuning branch to work with most recent trunk

File size: 2.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Algorithms.DataAnalysis;
26using HeuristicLab.Core;
27using HeuristicLab.Random;
28
29namespace HeuristicLab.Problems.Instances.DataAnalysis {
30  public class Util {
31
32
33
34    public static List<double> SampleGaussianProcess(IRandom random, ParameterizedCovarianceFunction covFunction, List<List<double>> data) {
35
36      double[,] x = new double[data[0].Count, data.Count];
37      for (int i = 0; i < x.GetLength(0); i++)
38        for (int j = 0; j < x.GetLength(1); j++)
39          x[i, j] = data[j][i];
40      double[,] K = new double[x.GetLength(0), x.GetLength(0)];
41      for (int i = 0; i < K.GetLength(0); i++)
42        for (int j = i; j < K.GetLength(1); j++)
43          K[i, j] = covFunction.Covariance(x, i, j);
44
45      var normalRand = new NormalDistributedRandom(random, 0, 1);
46
47      if (!alglib.spdmatrixcholesky(ref K, K.GetLength(0), true)) throw new ArgumentException();
48      var r = (from i in Enumerable.Range(0, K.GetLength(0))
49               select normalRand.NextDouble()).ToArray();
50
51      List<double> target = new List<double>(K.GetLength(0));
52      for (int i = 0; i < K.GetLength(0); i++) {
53        double s = 0.0;
54        for (int j = K.GetLength(0) - 1; j >= 0; j--) {
55
56          s += K[j, i] * r[j];
57        }
58
59        target.Add(s);
60      }
61
62      return target;
63    }
64  }
65}
Note: See TracBrowser for help on using the repository browser.