Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/14/12 13:25:17 (12 years ago)
Author:
gkronber
Message:

#1902 changed interface for covariance functions to improve readability, fixed several bugs in the covariance functions and in the line chart for Gaussian process models.

File:
1 edited

Legend:

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

    r8473 r8484  
    2121
    2222using System;
     23using System.Collections.Generic;
    2324using HeuristicLab.Common;
    2425using HeuristicLab.Core;
     
    2930  [Item(Name = "CovariancePeriodic", Description = "Periodic covariance function for Gaussian processes.")]
    3031  public class CovariancePeriodic : Item, ICovarianceFunction {
    31     [Storable]
    32     private double[,] x;
    33     [Storable]
    34     private double[,] xt;
    3532    [Storable]
    3633    private double sf2;
     
    4340    public double Period { get { return p; } }
    4441
    45     private bool symmetric;
    46 
    47     private double[,] sd;
    4842    public int GetNumberOfParameters(int numberOfVariables) {
    4943      return 3;
     
    5347    protected CovariancePeriodic(CovariancePeriodic original, Cloner cloner)
    5448      : base(original, cloner) {
    55       if (original.x != null) {
    56         x = new double[original.x.GetLength(0), original.x.GetLength(1)];
    57         Array.Copy(original.x, x, x.Length);
    58         xt = new double[original.xt.GetLength(0), original.xt.GetLength(1)];
    59         Array.Copy(original.xt, xt, xt.Length);
    60       }
    6149      sf2 = original.sf2;
    6250      l = original.l;
    6351      p = original.p;
    64       symmetric = original.symmetric;
    6552    }
    6653    public CovariancePeriodic()
     
    7764      this.p = Math.Exp(hyp[1]);
    7865      this.sf2 = Math.Exp(2 * hyp[2]);
    79       // sf2 = Math.Min(10E6, sf2); // upper limit for the scale
    80 
    81       sd = null;
    82     }
    83     public void SetData(double[,] x) {
    84       SetData(x, x);
    85       this.symmetric = true;
    8666    }
    8767
    88     public void SetData(double[,] x, double[,] xt) {
    89       this.x = x;
    90       this.xt = xt;
    91       this.symmetric = false;
    92 
    93       sd = null;
    94     }
    95 
    96     public double GetCovariance(int i, int j) {
    97       if (sd == null) CalculateSquaredDistances();
    98       double k = sd[i, j];
     68    public double GetCovariance(double[,] x, int i, int j) {
     69      double k = i == j ? 0.0 : GetDistance(x, x, i, j);
    9970      k = Math.PI * k / p;
    10071      k = Math.Sin(k) / l;
     
    10475    }
    10576
    106     public double GetGradient(int i, int j, int k) {
    107       double v = Math.PI * sd[i, j] / p;
    108       switch (k) {
    109         case 0: {
    110             double newK = Math.Sin(v) / l;
    111             newK = newK * newK;
    112             return 4 * sf2 * Math.Exp(-2 * newK) * newK;
    113           }
    114         case 1: {
    115             double r = Math.Sin(v) / l;
    116             return 4 * sf2 / l * Math.Exp(-2 * r * r) * r * Math.Cos(v) * v;
    117           }
    118         case 2: {
    119             double newK = Math.Sin(v) / l;
    120             newK = newK * newK;
    121             return 2 * sf2 * Math.Exp(-2 * newK);
    122 
    123           }
    124         default: {
    125             throw new ArgumentException("CovariancePeriodic only has three hyperparameters.", "k");
    126           }
    127       }
     77    public IEnumerable<double> GetGradient(double[,] x, int i, int j) {
     78      double v = i == j ? 0.0 : Math.PI * GetDistance(x, x, i, j) / p;
     79      double gradient = Math.Sin(v) / l;
     80      gradient *= gradient;
     81      yield return 4.0 * sf2 * Math.Exp(-2.0 * gradient) * gradient;
     82      double r = Math.Sin(v) / l;
     83      yield return 4.0 * sf2 / l * Math.Exp(-2 * r * r) * r * Math.Cos(v) * v;
     84      yield return 2.0 * sf2 * Math.Exp(-2 * gradient);
    12885    }
    12986
    130     private void CalculateSquaredDistances() {
    131       if (x.GetLength(1) != xt.GetLength(1)) throw new InvalidOperationException();
    132       int rows = x.GetLength(0);
    133       int cols = xt.GetLength(0);
    134       sd = new double[rows, cols];
     87    public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {
     88      double k = GetDistance(x, xt, i, j);
     89      k = Math.PI * k / p;
     90      k = Math.Sin(k) / l;
     91      k = k * k;
    13592
    136       if (symmetric) {
    137         for (int i = 0; i < rows; i++) {
    138           for (int j = i; j < cols; j++) {
    139             sd[i, j] = Math.Sqrt(Util.SqrDist(Util.GetRow(x, i), Util.GetRow(x, j)));
    140             sd[j, i] = sd[i, j];
    141           }
    142         }
    143       } else {
    144         for (int i = 0; i < rows; i++) {
    145           for (int j = 0; j < cols; j++) {
    146             sd[i, j] = Math.Sqrt(Util.SqrDist(Util.GetRow(x, i), Util.GetRow(xt, j)));
    147           }
    148         }
    149       }
     93      return sf2 * Math.Exp(-2.0 * k);
     94    }
     95
     96    private double GetDistance(double[,] x, double[,] xt, int i, int j) {
     97      return Math.Sqrt(Util.SqrDist(Util.GetRow(x, i), Util.GetRow(xt, j)));
    15098    }
    15199  }
Note: See TracChangeset for help on using the changeset viewer.