#region License Information
/* HeuristicLab
* Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using HeuristicLab.Algorithms.DataAnalysis;
using HeuristicLab.Core;
using HeuristicLab.Random;
namespace HeuristicLab.Problems.Instances.DataAnalysis {
public class Util {
public static List SampleGaussianProcess(IRandom random, ParameterizedCovarianceFunction covFunction, List> data) {
int n = data[0].Count;
var normalRand = new NormalDistributedRandom(random, 0, 1);
var alpha = (from i in Enumerable.Range(0, n)
select normalRand.NextDouble()).ToArray();
return SampleGaussianProcess(random, covFunction, data, alpha);
}
public static List SampleGaussianProcess(IRandom random, ParameterizedCovarianceFunction covFunction, List> data, double[] alpha) {
if (alpha.Length != data[0].Count) throw new ArgumentException();
double[,] x = new double[data[0].Count, data.Count];
for (int i = 0; i < x.GetLength(0); i++)
for (int j = 0; j < x.GetLength(1); j++)
x[i, j] = data[j][i];
double[,] K = new double[x.GetLength(0), x.GetLength(0)];
for (int i = 0; i < K.GetLength(0); i++)
for (int j = i; j < K.GetLength(1); j++)
K[i, j] = covFunction.Covariance(x, i, j);
if (!alglib.spdmatrixcholesky(ref K, K.GetLength(0), true)) throw new ArgumentException();
List target = new List(K.GetLength(0));
for (int i = 0; i < K.GetLength(0); i++) {
double s = 0.0;
for (int j = K.GetLength(0) - 1; j >= 0; j--) {
s += K[j, i] * alpha[j];
}
target.Add(s);
}
return target;
}
}
}