Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/06/12 15:02:34 (12 years ago)
Author:
gkronber
Message:

#1902 worked on sum and product covariance functions and fixed a few bugs.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceLinear.cs

    r8401 r8416  
    2929  [Item(Name = "CovarianceLinear", Description = "Linear covariance function with for Gaussian processes.")]
    3030  public class CovarianceLinear : Item, ICovarianceFunction {
     31    private static readonly double[] emptyArray = new double[0];
     32
    3133    [Storable]
    3234    private double[,] x;
    3335    [Storable]
    3436    private double[,] xt;
    35 
    3637
    3738    private double[,] k;
     
    4546    protected CovarianceLinear(CovarianceLinear original, Cloner cloner)
    4647      : base(original, cloner) {
    47       // note: using shallow copies here!
    48       this.x = original.x;
    49       this.xt = original.xt;
     48      if (original.x != null) {
     49        this.x = new double[original.x.GetLength(0), original.x.GetLength(1)];
     50        Array.Copy(original.x, this.x, x.Length);
    5051
     52        this.xt = new double[original.xt.GetLength(0), original.xt.GetLength(1)];
     53        Array.Copy(original.xt, this.xt, xt.Length);
     54
     55        this.k = new double[original.k.GetLength(0), original.k.GetLength(1)];
     56        Array.Copy(original.k, this.k, k.Length);
     57      }
     58      this.symmetric = original.symmetric;
    5159    }
    5260    public CovarianceLinear()
     
    5866    }
    5967
    60     public void SetParameter(double[] hyp, double[,] x) {
    61       if (hyp.Length > 0) throw new ArgumentException();
    62       SetParameter(hyp, x, x);
     68    public void SetParameter(double[] hyp) {
     69      if (hyp.Length > 0) throw new ArgumentException("No hyperparameters are allowed for the linear covariance function.");
     70      k = null;
     71    }
     72
     73    public void SetData(double[,] x) {
     74      SetData(x, x);
    6375      this.symmetric = true;
    6476    }
    6577
    66     public void SetParameter(double[] hyp, double[,] x, double[,] xt) {
     78    public void SetData(double[,] x, double[,] xt) {
    6779      this.x = x;
    6880      this.xt = xt;
     
    7789    }
    7890
    79 
    80     public double[] GetDiagonalCovariances() {
    81       if (x != xt) throw new InvalidOperationException();
    82       int rows = x.GetLength(0);
    83       int cols = x.GetLength(1);
    84       var k = new double[rows];
    85       for (int i = 0; i < rows; i++) {
    86         k[i] = 0;
    87         for (int j = 0; j < cols; j++) {
    88           k[i] += x[i, j] * x[i, j];
    89         }
    90       }
    91       return k;
    92     }
    93 
    9491    public double[] GetGradient(int i, int j) {
    95       throw new NotSupportedException();
     92      return emptyArray;
    9693    }
    9794
Note: See TracChangeset for help on using the changeset viewer.