Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1967 implemented utility app to draw random samples using a GP prior

File size: 2.5 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    public static List<double> SampleGaussianProcess(IRandom random, ParameterizedCovarianceFunction covFunction, List<List<double>> data) {
34      int n = data[0].Count;
35
36      var normalRand = new NormalDistributedRandom(random, 0, 1);
37      var alpha = (from i in Enumerable.Range(0, n)
38                   select normalRand.NextDouble()).ToArray();
39      return SampleGaussianProcess(random, covFunction, data, alpha);
40    }
41
42    public static List<double> SampleGaussianProcess(IRandom random, ParameterizedCovarianceFunction covFunction, List<List<double>> data, double[] alpha) {
43      if (alpha.Length != data[0].Count) throw new ArgumentException();
44
45      double[,] x = new double[data[0].Count, data.Count];
46      for (int i = 0; i < x.GetLength(0); i++)
47        for (int j = 0; j < x.GetLength(1); j++)
48          x[i, j] = data[j][i];
49      double[,] K = new double[x.GetLength(0), x.GetLength(0)];
50      for (int i = 0; i < K.GetLength(0); i++)
51        for (int j = i; j < K.GetLength(1); j++)
52          K[i, j] = covFunction.Covariance(x, i, j);
53
54      if (!alglib.spdmatrixcholesky(ref K, K.GetLength(0), true)) throw new ArgumentException();
55
56      List<double> target = new List<double>(K.GetLength(0));
57      for (int i = 0; i < K.GetLength(0); i++) {
58        double s = 0.0;
59        for (int j = K.GetLength(0) - 1; j >= 0; j--) {
60
61          s += K[j, i] * alpha[j];
62        }
63
64        target.Add(s);
65      }
66
67      return target;
68    }
69  }
70}
Note: See TracBrowser for help on using the repository browser.