Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/04/12 09:50:52 (12 years ago)
Author:
gkronber
Message:

#1902 implemented LinearARD and MaternIso covariance functions.

File:
1 edited

Legend:

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

    r8484 r8562  
    2121
    2222using System;
     23using System.Collections.Generic;
    2324using System.Linq;
    2425using HeuristicLab.Common;
     
    3233  public class CovarianceLinearArd : Item, ICovarianceFunction {
    3334    [Storable]
    34     private double[,] x;
    35     [Storable]
    36     private double[,] xt;
    37 
    38     [Storable]
    39     private double[] l;
    40     public double[] Length {
     35    private double[] inverseLength;
     36    public double[] InverseLength {
    4137      get {
    42         double[] res = new double[l.Length];
    43         Array.Copy(l, res, res.Length);
     38        if (inverseLength == null) return null;
     39        double[] res = new double[inverseLength.Length];
     40        Array.Copy(inverseLength, res, res.Length);
    4441        return res;
    4542      }
    4643    }
    4744
    48     private double[,] k;
    49     private bool symmetric;
    50 
    51 
    5245    public int GetNumberOfParameters(int numberOfVariables) {
    5346      return numberOfVariables;
    5447    }
     48
    5549    [StorableConstructor]
    5650    protected CovarianceLinearArd(bool deserializing) : base(deserializing) { }
    5751    protected CovarianceLinearArd(CovarianceLinearArd original, Cloner cloner)
    5852      : base(original, cloner) {
    59       if (original.x != null) {
    60         this.x = new double[original.x.GetLength(0), original.x.GetLength(1)];
    61         Array.Copy(original.x, this.x, x.Length);
    62 
    63         this.xt = new double[original.xt.GetLength(0), original.xt.GetLength(1)];
    64         Array.Copy(original.xt, this.xt, xt.Length);
    65 
    66         this.k = new double[original.k.GetLength(0), original.k.GetLength(1)];
    67         Array.Copy(original.k, this.k, k.Length);
    68         this.l = new double[original.l.GetLength(0)];
    69         Array.Copy(original.l, this.l, l.Length);
    70       }
    71       this.symmetric = original.symmetric;
     53      this.inverseLength = original.InverseLength;  // array is copied in the getter
    7254    }
    7355    public CovarianceLinearArd()
     
    8062
    8163    public void SetParameter(double[] hyp) {
    82       l = hyp.Select(Math.Exp).ToArray();
     64      inverseLength = hyp.Select(e => 1.0 / Math.Exp(e)).ToArray();
    8365    }
    8466
    85     public void SetData(double[,] x) {
    86       SetData(x, x);
    87       this.symmetric = true;
     67    public double GetCovariance(double[,] x, int i, int j) {
     68      return Util.ScalarProd(x, i, j, inverseLength);
    8869    }
    8970
    90     public void SetData(double[,] x, double[,] xt) {
    91       this.x = x;
    92       this.xt = xt;
    93       this.symmetric = false;
    94 
    95       k = null;
     71    public IEnumerable<double> GetGradient(double[,] x, int i, int j) {
     72      for (int k = 0; k < inverseLength.Length; k++) {
     73        yield return -2.0 * x[i, k] * x[j, k] * inverseLength[k] * inverseLength[k];
     74      }
    9675    }
    9776
    98     public double GetCovariance(int i, int j) {
    99       if (k == null) CalculateInnerProduct();
    100       return k[i, j];
    101     }
    102 
    103     public double GetGradient(int i, int j, int k) {
    104 
    105     }
    106 
    107 
    108     private void CalculateInnerProduct() {
    109       if (x.GetLength(1) != xt.GetLength(1)) throw new InvalidOperationException();
    110       int rows = x.GetLength(0);
    111       int cols = xt.GetLength(0);
    112       k = new double[rows, cols];
    113       if (symmetric) {
    114         for (int i = 0; i < rows; i++) {
    115           for (int j = i; j < cols; j++) {
    116 
    117             k[i, j] = Util.ScalarProd(Util.GetRow(x, i).Select((e, k) => e / l[k]),
    118                                       Util.GetRow(x, j).Select((e, k) => e / l[k]));
    119             k[j, i] = k[i, j];
    120           }
    121         }
    122       } else {
    123         for (int i = 0; i < rows; i++) {
    124           for (int j = 0; j < cols; j++) {
    125             k[i, j] = Util.ScalarProd(Util.GetRow(x, i).Select((e, k) => e / l[k]),
    126                                       Util.GetRow(xt, j).Select((e, k) => e / l[k]));
    127           }
    128         }
    129       }
     77    public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {
     78      return Util.ScalarProd(x, i, xt, j, inverseLength);
    13079    }
    13180  }
Note: See TracChangeset for help on using the changeset viewer.