private GaussianProcessModel(GaussianProcessModel original, Cloner cloner)
            : base(original, cloner)
        {
            this.meanFunction       = cloner.Clone(original.meanFunction);
            this.covarianceFunction = cloner.Clone(original.covarianceFunction);
            if (original.inputScaling != null)
            {
                this.inputScaling = cloner.Clone(original.inputScaling);
            }
            this.trainingDataset       = cloner.Clone(original.trainingDataset);
            this.negativeLogLikelihood = original.negativeLogLikelihood;
            this.targetVariable        = original.targetVariable;
            this.sqrSigmaNoise         = original.sqrSigmaNoise;
            if (original.meanParameter != null)
            {
                this.meanParameter = (double[])original.meanParameter.Clone();
            }
            if (original.covarianceParameter != null)
            {
                this.covarianceParameter = (double[])original.covarianceParameter.Clone();
            }

            // shallow copies of arrays because they cannot be modified
            this.trainingRows          = original.trainingRows;
            this.allowedInputVariables = original.allowedInputVariables;
            this.alpha = original.alpha;
            this.l     = original.l;
            this.x     = original.x;
        }
        public GaussianProcessModel(IDataset ds, string targetVariable, IEnumerable <string> allowedInputVariables, IEnumerable <int> rows,
                                    IEnumerable <double> hyp, IMeanFunction meanFunction, ICovarianceFunction covarianceFunction,
                                    bool scaleInputs = true)
            : base()
        {
            this.name                  = ItemName;
            this.description           = ItemDescription;
            this.meanFunction          = (IMeanFunction)meanFunction.Clone();
            this.covarianceFunction    = (ICovarianceFunction)covarianceFunction.Clone();
            this.targetVariable        = targetVariable;
            this.allowedInputVariables = allowedInputVariables.ToArray();


            int nVariables = this.allowedInputVariables.Length;

            meanParameter = hyp
                            .Take(this.meanFunction.GetNumberOfParameters(nVariables))
                            .ToArray();

            covarianceParameter = hyp.Skip(this.meanFunction.GetNumberOfParameters(nVariables))
                                  .Take(this.covarianceFunction.GetNumberOfParameters(nVariables))
                                  .ToArray();
            sqrSigmaNoise = Math.Exp(2.0 * hyp.Last());
            try {
                CalculateModel(ds, rows, scaleInputs);
            } catch (alglib.alglibexception ae) {
                // wrap exception so that calling code doesn't have to know about alglib implementation
                throw new ArgumentException("There was a problem in the calculation of the Gaussian process model", ae);
            }
        }
 public void ClearState()
 {
     meanFunc            = null;
     covFunc             = null;
     bestQ               = double.NegativeInfinity;
     bestHyperParameters = null;
 }
 // cloning
 private GaussianProcessCovarianceOptimizationProblem(GaussianProcessCovarianceOptimizationProblem original, Cloner cloner)
     : base(original, cloner)
 {
     bestQ    = original.bestQ;
     meanFunc = cloner.Clone(original.meanFunc);
     covFunc  = cloner.Clone(original.covFunc);
     if (bestHyperParameters != null)
     {
         bestHyperParameters = new double[original.bestHyperParameters.Length];
         Array.Copy(original.bestHyperParameters, bestHyperParameters, bestHyperParameters.Length);
     }
 }
 // updates the overall best quality and overall best model for Analyze()
 private void UpdateBestSoFar(double bestQ, double[] bestHyperParameters, IMeanFunction meanFunc, ICovarianceFunction covFunc)
 {
     lock (problemStateLocker) {
         if (bestQ > this.bestQ)
         {
             this.bestQ = bestQ;
             this.bestHyperParameters = new double[bestHyperParameters.Length];
             Array.Copy(bestHyperParameters, this.bestHyperParameters, this.bestHyperParameters.Length);
             this.meanFunc = meanFunc;
             this.covFunc  = covFunc;
         }
     }
 }
    private void TestMeanFunction(IMeanFunction mf, double hypValue, double[] expectedMean, double[][] expectedGradients) {
      var x = GetData();
      var xt = GetDataTest();

      int nHyp = mf.GetNumberOfParameters(x.GetLength(1));
      var hyp = Enumerable.Repeat(hypValue, nHyp).ToArray();
      var meanFunction = mf.GetParameterizedMeanFunction(hyp, Enumerable.Range(0, x.GetLength(1)).ToArray());

      var m = Enumerable.Range(0, xt.GetLength(0)).Select(i => meanFunction.Mean(xt, i)).ToArray();

      AssertEqual(expectedMean, m);

      for (int k = 0; k < nHyp; k++) {
        var g = Enumerable.Range(0, xt.GetLength(0)).Select(i => meanFunction.Gradient(x, i, k)).ToArray();
        AssertEqual(expectedGradients[k], g);
      }
    }
 public static IGaussianProcessModel Create(IRegressionProblemData problemData, double[] hyperparameter, IMeanFunction meanFunction, ICovarianceFunction covarianceFunction, bool scaleInputs = true) {
   return new GaussianProcessModel(problemData.Dataset, problemData.TargetVariable, problemData.AllowedInputVariables, problemData.TrainingIndices, hyperparameter, meanFunction, covarianceFunction, scaleInputs);
 }
    public GaussianProcessModel(IDataset ds, string targetVariable, IEnumerable<string> allowedInputVariables, IEnumerable<int> rows,
      IEnumerable<double> hyp, IMeanFunction meanFunction, ICovarianceFunction covarianceFunction,
      bool scaleInputs = true)
      : base(targetVariable) {
      this.name = ItemName;
      this.description = ItemDescription;
      this.meanFunction = (IMeanFunction)meanFunction.Clone();
      this.covarianceFunction = (ICovarianceFunction)covarianceFunction.Clone();
      this.allowedInputVariables = allowedInputVariables.ToArray();


      int nVariables = this.allowedInputVariables.Length;
      meanParameter = hyp
        .Take(this.meanFunction.GetNumberOfParameters(nVariables))
        .ToArray();

      covarianceParameter = hyp.Skip(this.meanFunction.GetNumberOfParameters(nVariables))
                                             .Take(this.covarianceFunction.GetNumberOfParameters(nVariables))
                                             .ToArray();
      sqrSigmaNoise = Math.Exp(2.0 * hyp.Last());
      try {
        CalculateModel(ds, rows, scaleInputs);
      }
      catch (alglib.alglibexception ae) {
        // wrap exception so that calling code doesn't have to know about alglib implementation
        throw new ArgumentException("There was a problem in the calculation of the Gaussian process model", ae);
      }
    }
    private GaussianProcessModel(GaussianProcessModel original, Cloner cloner)
      : base(original, cloner) {
      this.meanFunction = cloner.Clone(original.meanFunction);
      this.covarianceFunction = cloner.Clone(original.covarianceFunction);
      if (original.inputScaling != null)
        this.inputScaling = cloner.Clone(original.inputScaling);
      this.trainingDataset = cloner.Clone(original.trainingDataset);
      this.negativeLogLikelihood = original.negativeLogLikelihood;
      this.sqrSigmaNoise = original.sqrSigmaNoise;
      if (original.meanParameter != null) {
        this.meanParameter = (double[])original.meanParameter.Clone();
      }
      if (original.covarianceParameter != null) {
        this.covarianceParameter = (double[])original.covarianceParameter.Clone();
      }

      // shallow copies of arrays because they cannot be modified
      this.trainingRows = original.trainingRows;
      this.allowedInputVariables = original.allowedInputVariables;
      this.alpha = original.alpha;
      this.l = original.l;
      this.x = original.x;
    }
Exemple #10
0
 public static IGaussianProcessModel Create(IClassificationProblemData problemData, double[] hyperparameter, IMeanFunction meanFunction, ICovarianceFunction covarianceFunction, bool scaleInputs = true)
 {
     return(new GaussianProcessModel(problemData.Dataset, problemData.TargetVariable, problemData.AllowedInputVariables, problemData.TrainingIndices, hyperparameter, meanFunction, covarianceFunction, scaleInputs));
 }