Index: branches/HeuristicLab.Mono/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/GaussianProcessModel.cs
===================================================================
--- branches/HeuristicLab.Mono/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/GaussianProcessModel.cs	(revision 8365)
+++ branches/HeuristicLab.Mono/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/GaussianProcessModel.cs	(revision 8451)
@@ -28,5 +28,5 @@
 using HeuristicLab.Problems.DataAnalysis;
 
-namespace HeuristicLab.Algorithms.DataAnalysis.GaussianProcess {
+namespace HeuristicLab.Algorithms.DataAnalysis {
   /// <summary>
   /// Represents a Gaussian process model.
@@ -35,8 +35,6 @@
   [Item("GaussianProcessModel", "Represents a Gaussian process posterior.")]
   public sealed class GaussianProcessModel : NamedItem, IGaussianProcessModel {
-
     [Storable]
     private double negativeLogLikelihood;
-
     public double NegativeLogLikelihood {
       get { return negativeLogLikelihood; }
@@ -54,9 +52,4 @@
     }
     [Storable]
-    private double[] hyp;
-    public IEnumerable<double> Hyperparameters {
-      get { return hyp; }
-    }
-    [Storable]
     private string targetVariable;
     public string TargetVariable {
@@ -73,8 +66,4 @@
     [Storable]
     private double sqrSigmaNoise;
-    [Storable]
-    private double[] meanHyp;
-    [Storable]
-    private double[] covHyp;
 
     [Storable]
@@ -91,15 +80,14 @@
     private GaussianProcessModel(GaussianProcessModel original, Cloner cloner)
       : base(original, cloner) {
-      this.hyp = original.hyp;
       this.meanFunction = cloner.Clone(original.meanFunction);
       this.covarianceFunction = cloner.Clone(original.covarianceFunction);
+      this.scaling = cloner.Clone(original.scaling);
       this.negativeLogLikelihood = original.negativeLogLikelihood;
       this.targetVariable = original.targetVariable;
+      this.sqrSigmaNoise = original.sqrSigmaNoise;
+
+      // shallow copies of arrays because they cannot be modified
       this.allowedInputVariables = original.allowedInputVariables;
       this.alpha = original.alpha;
-      this.sqrSigmaNoise = original.sqrSigmaNoise;
-      this.scaling = cloner.Clone(original.scaling);
-      this.meanHyp = original.meanHyp;
-      this.covHyp = original.covHyp;
       this.l = original.l;
       this.x = original.x;
@@ -110,20 +98,24 @@
       this.name = ItemName;
       this.description = ItemDescription;
-      this.hyp = hyp.ToArray();
-      this.meanFunction = meanFunction;
-      this.covarianceFunction = covarianceFunction;
+      this.meanFunction = (IMeanFunction)meanFunction.Clone();
+      this.covarianceFunction = (ICovarianceFunction)covarianceFunction.Clone();
       this.targetVariable = targetVariable;
       this.allowedInputVariables = allowedInputVariables.ToArray();
-      int nAllowedVariables = allowedInputVariables.Count();
 
       sqrSigmaNoise = Math.Exp(2.0 * hyp.First());
       sqrSigmaNoise = Math.Max(10E-6, sqrSigmaNoise); // lower limit for the noise level
-      meanHyp = hyp.Skip(1).Take(meanFunction.GetNumberOfParameters(nAllowedVariables)).ToArray();
-      covHyp = hyp.Skip(1 + meanFunction.GetNumberOfParameters(nAllowedVariables)).Take(covarianceFunction.GetNumberOfParameters(nAllowedVariables)).ToArray();
-
-      CalculateModel(ds, targetVariable, allowedInputVariables, rows);
-    }
-
-    private void CalculateModel(Dataset ds, string targetVariable, IEnumerable<string> allowedInputVariables, IEnumerable<int> rows) {
+
+      int nVariables = this.allowedInputVariables.Length;
+      this.meanFunction.SetParameter(hyp.Skip(1)
+        .Take(this.meanFunction.GetNumberOfParameters(nVariables))
+        .ToArray());
+      this.covarianceFunction.SetParameter(hyp.Skip(1 + this.meanFunction.GetNumberOfParameters(nVariables))
+        .Take(this.covarianceFunction.GetNumberOfParameters(nVariables))
+        .ToArray());
+
+      CalculateModel(ds, rows);
+    }
+
+    private void CalculateModel(Dataset ds, IEnumerable<int> rows) {
       scaling = new Scaling(ds, allowedInputVariables, rows);
       x = AlglibUtil.PrepareAndScaleInputMatrix(ds, allowedInputVariables, rows, scaling);
@@ -134,9 +126,9 @@
       l = new double[n, n];
 
-      meanFunction.SetParameter(meanHyp, x);
-      covarianceFunction.SetParameter(covHyp, x);
+      meanFunction.SetData(x);
+      covarianceFunction.SetData(x);
 
       // calculate means and covariances
-      double[] m = meanFunction.GetMean();
+      double[] m = meanFunction.GetMean(x);
       for (int i = 0; i < n; i++) {
 
@@ -188,18 +180,20 @@
       double[] meanGradients = new double[meanFunction.GetNumberOfParameters(nAllowedVariables)];
       for (int i = 0; i < meanGradients.Length; i++) {
-        var meanGrad = meanFunction.GetGradients(i);
+        var meanGrad = meanFunction.GetGradients(i, x);
         meanGradients[i] = -Util.ScalarProd(meanGrad, alpha);
       }
 
       double[] covGradients = new double[covarianceFunction.GetNumberOfParameters(nAllowedVariables)];
-      for (int i = 0; i < n; i++) {
-        for (int j = 0; j < n; j++) {
-          var covDeriv = covarianceFunction.GetGradient(i, j);
-          for (int k = 0; k < covGradients.Length; k++) {
-            covGradients[k] += q[i, j] * covDeriv[k];
+      if (covGradients.Length > 0) {
+        for (int i = 0; i < n; i++) {
+          for (int j = 0; j < n; j++) {
+            var covDeriv = covarianceFunction.GetGradient(i, j);
+            for (int k = 0; k < covGradients.Length; k++) {
+              covGradients[k] += q[i, j] * covDeriv[k];
+            }
           }
         }
-      }
-      covGradients = covGradients.Select(g => g / 2.0).ToArray();
+        covGradients = covGradients.Select(g => g / 2.0).ToArray();
+      }
 
       return new double[] { noiseGradient }
@@ -244,7 +238,7 @@
       //kss = covarianceFunction.GetDiagonalCovariances();
 
-      covarianceFunction.SetParameter(covHyp, x, newX);
-      meanFunction.SetParameter(meanHyp, newX);
-      var ms = meanFunction.GetMean();
+      covarianceFunction.SetData(x, newX);
+      meanFunction.SetData(newX);
+      var ms = meanFunction.GetMean(newX);
       for (int i = 0; i < newN; i++) {
 
@@ -267,13 +261,4 @@
 
     }
-
-    #region events
-    public event EventHandler Changed;
-    private void OnChanged(EventArgs e) {
-      var handlers = Changed;
-      if (handlers != null)
-        handlers(this, e);
-    }
-    #endregion
   }
 }
