[8826] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Algorithms.DataAnalysis;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Random;
|
---|
| 28 |
|
---|
| 29 | namespace HeuristicLab.Problems.Instances.DataAnalysis {
|
---|
| 30 | public class Util {
|
---|
| 31 |
|
---|
| 32 |
|
---|
[9099] | 33 | public static List<double> SampleGaussianProcess(IRandom random, ParameterizedCovarianceFunction covFunction, List<List<double>> data) {
|
---|
[9124] | 34 | int n = data[0].Count;
|
---|
[8826] | 35 |
|
---|
[9124] | 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 |
|
---|
[8873] | 45 | double[,] x = new double[data[0].Count, data.Count];
|
---|
[8826] | 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++)
|
---|
[9099] | 52 | K[i, j] = covFunction.Covariance(x, i, j);
|
---|
[8826] | 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 |
|
---|
[9124] | 61 | s += K[j, i] * alpha[j];
|
---|
[8826] | 62 | }
|
---|
| 63 |
|
---|
| 64 | target.Add(s);
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | return target;
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 | }
|
---|