Changeset 8612


Ignore:
Timestamp:
09/10/12 13:28:55 (7 years ago)
Author:
gkronber
Message:

#1902 implemented all mean and covariance functions with parameters as ParameterizedNamedItems

Location:
trunk/sources
Files:
1 deleted
23 edited

Legend:

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

    r8582 r8612  
    3131  [Item(Name = "CovarianceConst",
    3232    Description = "Constant covariance function for Gaussian processes.")]
    33   public class CovarianceConst : CovarianceFunction {
     33  public sealed class CovarianceConst : ParameterizedNamedItem, ICovarianceFunction {
    3434
     35    [Storable]
     36    private double scale;
     37    [Storable]
     38    private readonly HyperParameter<DoubleValue> scaleParameter;
    3539    public IValueParameter<DoubleValue> ScaleParameter {
    3640      get { return scaleParameter; }
    3741    }
    3842
    39     [Storable]
    40     private readonly HyperParameter<DoubleValue> scaleParameter;
    41 
    42     [Storable]
    43     private double scale;
    44 
    4543    [StorableConstructor]
    46     protected CovarianceConst(bool deserializing)
     44    private CovarianceConst(bool deserializing)
    4745      : base(deserializing) {
    4846    }
    4947
    50     protected CovarianceConst(CovarianceConst original, Cloner cloner)
     48    private CovarianceConst(CovarianceConst original, Cloner cloner)
    5149      : base(original, cloner) {
    5250      this.scaleParameter = cloner.Clone(original.scaleParameter);
     
    5856    public CovarianceConst()
    5957      : base() {
     58      Name = ItemName;
     59      Description = ItemDescription;
     60
    6061      scaleParameter = new HyperParameter<DoubleValue>("Scale", "The scale of the constant covariance function.");
    6162      Parameters.Add(scaleParameter);
     
    7071    // caching
    7172    private void RegisterEvents() {
    72       AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { scale = scaleParameter.Value.Value; });
     73      Util.AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { scale = scaleParameter.Value.Value; });
    7374    }
    7475
     
    7879    }
    7980
    80     public override int GetNumberOfParameters(int numberOfVariables) {
     81    public int GetNumberOfParameters(int numberOfVariables) {
    8182      return scaleParameter.Fixed ? 0 : 1;
    8283    }
    8384
    84     public override void SetParameter(double[] hyp) {
     85    public void SetParameter(double[] hyp) {
    8586      if (!scaleParameter.Fixed && hyp.Length == 1) {
    8687        scaleParameter.SetValue(new DoubleValue(Math.Exp(2 * hyp[0])));
     
    9091    }
    9192
    92     public override double GetCovariance(double[,] x, int i, int j) {
     93    public double GetCovariance(double[,] x, int i, int j) {
    9394      return scale;
    9495    }
    9596
    96     public override IEnumerable<double> GetGradient(double[,] x, int i, int j) {
     97    public IEnumerable<double> GetGradient(double[,] x, int i, int j) {
    9798      yield return 2.0 * scale;
    9899    }
    99100
    100     public override double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {
     101    public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {
    101102      return scale;
    102103    }
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceLinear.cs

    r8582 r8612  
    2929  [StorableClass]
    3030  [Item(Name = "CovarianceLinear", Description = "Linear covariance function for Gaussian processes.")]
    31   public class CovarianceLinear : CovarianceFunction {
    32     public override int GetNumberOfParameters(int numberOfVariables) {
    33       return 0;
    34     }
     31  public sealed class CovarianceLinear : Item, ICovarianceFunction {
    3532    [StorableConstructor]
    36     protected CovarianceLinear(bool deserializing) : base(deserializing) { }
    37     protected CovarianceLinear(CovarianceLinear original, Cloner cloner)
     33    private CovarianceLinear(bool deserializing) : base(deserializing) { }
     34    private CovarianceLinear(CovarianceLinear original, Cloner cloner)
    3835      : base(original, cloner) {
    3936    }
     
    4643    }
    4744
    48     public override void SetParameter(double[] hyp) {
     45    public int GetNumberOfParameters(int numberOfVariables) {
     46      return 0;
     47    }
     48
     49    public void SetParameter(double[] hyp) {
    4950      if (hyp.Length > 0) throw new ArgumentException("No hyperparameters are allowed for the linear covariance function.");
    5051    }
    5152
    52     public override double GetCovariance(double[,] x, int i, int j) {
     53    public double GetCovariance(double[,] x, int i, int j) {
    5354      return Util.ScalarProd(x, i, j);
    5455    }
    5556
    56     public override IEnumerable<double> GetGradient(double[,] x, int i, int j) {
     57    public IEnumerable<double> GetGradient(double[,] x, int i, int j) {
    5758      yield break;
    5859    }
    5960
    60     public override double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {
     61    public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {
    6162      return Util.ScalarProd(x, i, xt, j);
    6263    }
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceLinearArd.cs

    r8582 r8612  
    3232  [Item(Name = "CovarianceLinearArd",
    3333    Description = "Linear covariance function with automatic relevance determination for Gaussian processes.")]
    34   public class CovarianceLinearArd : CovarianceFunction {
     34  public sealed class CovarianceLinearArd : ParameterizedNamedItem, ICovarianceFunction {
     35    [Storable]
     36    private double[] inverseLength;
     37    [Storable]
     38    private readonly HyperParameter<DoubleArray> inverseLengthParameter;
    3539    public IValueParameter<DoubleArray> InverseLengthParameter {
    3640      get { return inverseLengthParameter; }
    3741    }
    3842
    39     [Storable]
    40     private HyperParameter<DoubleArray> inverseLengthParameter;
    41 
    42     [Storable]
    43     private double[] inverseLength;
    44 
    4543    [StorableConstructor]
    46     protected CovarianceLinearArd(bool deserializing) : base(deserializing) { }
    47     protected CovarianceLinearArd(CovarianceLinearArd original, Cloner cloner)
     44    private CovarianceLinearArd(bool deserializing) : base(deserializing) { }
     45    private CovarianceLinearArd(CovarianceLinearArd original, Cloner cloner)
    4846      : base(original, cloner) {
    4947      inverseLengthParameter = cloner.Clone(original.inverseLengthParameter);
     
    5755    public CovarianceLinearArd()
    5856      : base() {
     57      Name = ItemName;
     58      Description = ItemDescription;
     59
    5960      inverseLengthParameter = new HyperParameter<DoubleArray>("InverseLength",
    6061                                                               "The inverse length parameter for ARD.");
     
    7475    // caching
    7576    private void RegisterEvents() {
    76       AttachArrayChangeHandler<DoubleArray, double>(inverseLengthParameter, () => { inverseLength = inverseLengthParameter.Value.ToArray(); });
     77      Util.AttachArrayChangeHandler<DoubleArray, double>(inverseLengthParameter, () => { inverseLength = inverseLengthParameter.Value.ToArray(); });
    7778    }
    7879
    7980
    80     public override int GetNumberOfParameters(int numberOfVariables) {
     81    public int GetNumberOfParameters(int numberOfVariables) {
    8182      if (!inverseLengthParameter.Fixed)
    8283        return numberOfVariables;
     
    8586    }
    8687
    87     public override void SetParameter(double[] hyp) {
     88    public void SetParameter(double[] hyp) {
    8889      if (!inverseLengthParameter.Fixed && hyp.Length > 0) {
    8990        inverseLengthParameter.SetValue(new DoubleArray(hyp.Select(e => 1.0 / Math.Exp(e)).ToArray()));
     
    9192    }
    9293
    93     public override double GetCovariance(double[,] x, int i, int j) {
     94    public double GetCovariance(double[,] x, int i, int j) {
    9495      return Util.ScalarProd(x, i, j, inverseLength);
    9596    }
    9697
    97     public override IEnumerable<double> GetGradient(double[,] x, int i, int j) {
     98    public IEnumerable<double> GetGradient(double[,] x, int i, int j) {
    9899      for (int k = 0; k < inverseLength.Length; k++) {
    99100        yield return -2.0 * x[i, k] * x[j, k] * inverseLength[k] * inverseLength[k];
     
    101102    }
    102103
    103     public override double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {
     104    public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {
    104105      return Util.ScalarProd(x, i, xt, j, inverseLength);
    105106    }
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceMaternIso.cs

    r8583 r8612  
    3333  [Item(Name = "CovarianceMaternIso",
    3434    Description = "Matern covariance function for Gaussian processes.")]
    35   public class CovarianceMaternIso : CovarianceFunction {
     35  public sealed class CovarianceMaternIso : ParameterizedNamedItem, ICovarianceFunction {
     36    [Storable]
     37    private double inverseLength;
     38    [Storable]
     39    private readonly HyperParameter<DoubleValue> inverseLengthParameter;
     40    public IValueParameter<DoubleValue> InverseLengthParameter {
     41      get { return inverseLengthParameter; }
     42    }
     43
     44    [Storable]
     45    private double sf2;
     46    [Storable]
     47    private readonly HyperParameter<DoubleValue> scaleParameter;
    3648    public IValueParameter<DoubleValue> ScaleParameter {
    3749      get { return scaleParameter; }
    3850    }
    39     public IValueParameter<DoubleValue> InverseLengthParameter {
    40       get { return inverseLengthParameter; }
    41     }
     51
     52    [Storable]
     53    private int d;
     54    [Storable]
     55    private readonly ConstrainedValueParameter<IntValue> dParameter;
    4256    public IConstrainedValueParameter<IntValue> DParameter {
    4357      get { return dParameter; }
    4458    }
    4559
    46     [Storable]
    47     private readonly HyperParameter<DoubleValue> inverseLengthParameter;
    48     [Storable]
    49     private readonly HyperParameter<DoubleValue> scaleParameter;
    50     [Storable]
    51     private readonly ConstrainedValueParameter<IntValue> dParameter;
    52 
    53     [Storable]
    54     private double inverseLength;
    55     [Storable]
    56     private double sf2;
    57     [Storable]
    58     private int d;
    5960
    6061    [StorableConstructor]
    61     protected CovarianceMaternIso(bool deserializing)
     62    private CovarianceMaternIso(bool deserializing)
    6263      : base(deserializing) {
    6364    }
    6465
    65     protected CovarianceMaternIso(CovarianceMaternIso original, Cloner cloner)
     66    private CovarianceMaternIso(CovarianceMaternIso original, Cloner cloner)
    6667      : base(original, cloner) {
    6768      this.scaleParameter = cloner.Clone(original.scaleParameter);
     
    7677    public CovarianceMaternIso()
    7778      : base() {
     79      Name = ItemName;
     80      Description = ItemDescription;
     81
    7882      inverseLengthParameter = new HyperParameter<DoubleValue>("InverseLength", "The inverse length parameter of the isometric Matern covariance function.");
    7983      scaleParameter = new HyperParameter<DoubleValue>("Scale", "The scale parameter of the isometric Matern covariance function.");
     
    103107    // caching
    104108    private void RegisterEvents() {
    105       AttachValueChangeHandler<DoubleValue, double>(inverseLengthParameter, () => { inverseLength = inverseLengthParameter.Value.Value; });
    106       AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { sf2 = scaleParameter.Value.Value; });
    107       AttachValueChangeHandler<IntValue, int>(dParameter, () => { d = dParameter.Value.Value; });
     109      Util.AttachValueChangeHandler<DoubleValue, double>(inverseLengthParameter, () => { inverseLength = inverseLengthParameter.Value.Value; });
     110      Util.AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { sf2 = scaleParameter.Value.Value; });
     111      Util.AttachValueChangeHandler<IntValue, int>(dParameter, () => { d = dParameter.Value.Value; });
    108112    }
    109113
    110     public override int GetNumberOfParameters(int numberOfVariables) {
     114    public int GetNumberOfParameters(int numberOfVariables) {
    111115      return
    112116        (inverseLengthParameter.Fixed ? 0 : 1) +
     
    114118    }
    115119
    116     public override void SetParameter(double[] hyp) {
     120    public void SetParameter(double[] hyp) {
    117121      int i = 0;
    118122      if (!inverseLengthParameter.Fixed) {
     
    150154    }
    151155
    152     public override double GetCovariance(double[,] x, int i, int j) {
     156    public double GetCovariance(double[,] x, int i, int j) {
    153157      double dist = i == j
    154158                   ? 0.0
     
    157161    }
    158162
    159     public override IEnumerable<double> GetGradient(double[,] x, int i, int j) {
     163    public IEnumerable<double> GetGradient(double[,] x, int i, int j) {
    160164      double dist = i == j
    161165                   ? 0.0
     
    166170    }
    167171
    168     public override double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {
     172    public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {
    169173      double dist = Math.Sqrt(Util.SqrDist(x, i, xt, j, Math.Sqrt(d) * inverseLength));
    170174      return sf2 * m(dist);
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceNoise.cs

    r8484 r8612  
    2424using HeuristicLab.Common;
    2525using HeuristicLab.Core;
     26using HeuristicLab.Data;
    2627using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2728
     
    3031  [Item(Name = "CovarianceNoise",
    3132    Description = "Noise covariance function for Gaussian processes.")]
    32   public class CovarianceNoise : Item, ICovarianceFunction {
     33  public sealed class CovarianceNoise : ParameterizedNamedItem, ICovarianceFunction {
     34
     35
    3336    [Storable]
    3437    private double sf2;
    35     public double Scale { get { return sf2; } }
     38    [Storable]
     39    private readonly HyperParameter<DoubleValue> scaleParameter;
     40    public IValueParameter<DoubleValue> ScaleParameter {
     41      get { return scaleParameter; }
     42    }
    3643
    3744    [StorableConstructor]
    38     protected CovarianceNoise(bool deserializing)
     45    private CovarianceNoise(bool deserializing)
    3946      : base(deserializing) {
    4047    }
    4148
    42     protected CovarianceNoise(CovarianceNoise original, Cloner cloner)
     49    private CovarianceNoise(CovarianceNoise original, Cloner cloner)
    4350      : base(original, cloner) {
     51      this.scaleParameter = cloner.Clone(original.scaleParameter);
    4452      this.sf2 = original.sf2;
     53      RegisterEvents();
    4554    }
    4655
    4756    public CovarianceNoise()
    4857      : base() {
     58      Name = ItemName;
     59      Description = ItemDescription;
     60
     61      this.scaleParameter = new HyperParameter<DoubleValue>("Scale", "The scale of noise.");
     62      Parameters.Add(this.scaleParameter);
     63
     64      RegisterEvents();
    4965    }
    5066
     
    5369    }
    5470
     71    [StorableHook(HookType.AfterDeserialization)]
     72    private void AfterDeserialization() {
     73      RegisterEvents();
     74    }
     75
     76    private void RegisterEvents() {
     77      Util.AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { sf2 = scaleParameter.Value.Value; });
     78    }
     79
    5580    public int GetNumberOfParameters(int numberOfVariables) {
    56       return 1;
     81      return scaleParameter.Fixed ? 0 : 1;
    5782    }
    5883
    5984    public void SetParameter(double[] hyp) {
    60       this.sf2 = Math.Exp(2 * hyp[0]);
     85      if (!scaleParameter.Fixed) {
     86        scaleParameter.SetValue(new DoubleValue(Math.Exp(2 * hyp[0])));
     87      } else {
     88        if (hyp.Length > 0) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceNoise", "hyp");
     89      }
    6190    }
    6291
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovariancePeriodic.cs

    r8582 r8612  
    3131  [StorableClass]
    3232  [Item(Name = "CovariancePeriodic", Description = "Periodic covariance function for Gaussian processes.")]
    33   public class CovariancePeriodic : CovarianceFunction {
     33  public sealed class CovariancePeriodic : ParameterizedNamedItem, ICovarianceFunction {
     34
     35    [Storable]
     36    private double scale;
     37    [Storable]
     38    private readonly HyperParameter<DoubleValue> scaleParameter;
    3439    public IValueParameter<DoubleValue> ScaleParameter {
    3540      get { return scaleParameter; }
    3641    }
     42
     43    [Storable]
     44    private double inverseLength;
     45    [Storable]
     46    private readonly HyperParameter<DoubleValue> inverseLengthParameter;
    3747    public IValueParameter<DoubleValue> InverseLengthParameter {
    3848      get { return inverseLengthParameter; }
    3949    }
     50
     51    [Storable]
     52    private double period;
     53    [Storable]
     54    private readonly HyperParameter<DoubleValue> periodParameter;
    4055    public IValueParameter<DoubleValue> PeriodParameter {
    4156      get { return periodParameter; }
    4257    }
    4358
    44     [Storable]
    45     private HyperParameter<DoubleValue> scaleParameter;
    46     [Storable]
    47     private HyperParameter<DoubleValue> inverseLengthParameter;
    48     [Storable]
    49     private HyperParameter<DoubleValue> periodParameter;
    50 
    51     [Storable]
    52     private double scale;
    53     [Storable]
    54     private double inverseLength;
    55     [Storable]
    56     private double period;
    57 
    5859
    5960    [StorableConstructor]
    60     protected CovariancePeriodic(bool deserializing) : base(deserializing) { }
    61     protected CovariancePeriodic(CovariancePeriodic original, Cloner cloner)
     61    private CovariancePeriodic(bool deserializing) : base(deserializing) { }
     62    private CovariancePeriodic(CovariancePeriodic original, Cloner cloner)
    6263      : base(original, cloner) {
    6364      this.scaleParameter = cloner.Clone(original.scaleParameter);
     
    7374    public CovariancePeriodic()
    7475      : base() {
     76      Name = ItemName;
     77      Description = ItemDescription;
     78     
    7579      scaleParameter = new HyperParameter<DoubleValue>("Scale", "The scale of the periodic covariance function.");
    7680      inverseLengthParameter = new HyperParameter<DoubleValue>("InverseLength", "The inverse length parameter for the periodic covariance function.");
     
    9498    // caching
    9599    private void RegisterEvents() {
    96       AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { scale = scaleParameter.Value.Value; });
    97       AttachValueChangeHandler<DoubleValue, double>(inverseLengthParameter, () => { inverseLength = inverseLengthParameter.Value.Value; });
    98       AttachValueChangeHandler<DoubleValue, double>(periodParameter, () => { period = periodParameter.Value.Value; });
     100      Util.AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { scale = scaleParameter.Value.Value; });
     101      Util.AttachValueChangeHandler<DoubleValue, double>(inverseLengthParameter, () => { inverseLength = inverseLengthParameter.Value.Value; });
     102      Util.AttachValueChangeHandler<DoubleValue, double>(periodParameter, () => { period = periodParameter.Value.Value; });
    99103    }
    100104
    101     public override int GetNumberOfParameters(int numberOfVariables) {
     105    public int GetNumberOfParameters(int numberOfVariables) {
    102106      return
    103107        (new[] { scaleParameter, inverseLengthParameter, periodParameter }).Count(p => !p.Fixed);
    104108    }
    105109
    106     public override void SetParameter(double[] hyp) {
     110    public void SetParameter(double[] hyp) {
    107111      int i = 0;
    108112      if (!inverseLengthParameter.Fixed) {
     
    121125    }
    122126
    123     public override double GetCovariance(double[,] x, int i, int j) {
     127    public double GetCovariance(double[,] x, int i, int j) {
    124128      double k = i == j ? 0.0 : GetDistance(x, x, i, j);
    125129      k = Math.PI * k / period;
     
    130134    }
    131135
    132     public override IEnumerable<double> GetGradient(double[,] x, int i, int j) {
     136    public IEnumerable<double> GetGradient(double[,] x, int i, int j) {
    133137      double v = i == j ? 0.0 : Math.PI * GetDistance(x, x, i, j) / period;
    134138      double gradient = Math.Sin(v) * inverseLength;
     
    140144    }
    141145
    142     public override double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {
     146    public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {
    143147      double k = GetDistance(x, xt, i, j);
    144148      k = Math.PI * k / period;
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceProd.cs

    r8489 r8612  
    3131  [Item(Name = "CovarianceProd",
    3232    Description = "Product covariance function for Gaussian processes.")]
    33   public class CovarianceProd : Item, ICovarianceFunction {
     33  public sealed class CovarianceProd : Item, ICovarianceFunction {
    3434    [Storable]
    3535    private ItemList<ICovarianceFunction> factors;
     
    4242
    4343    [StorableConstructor]
    44     protected CovarianceProd(bool deserializing)
     44    private CovarianceProd(bool deserializing)
    4545      : base(deserializing) {
    4646    }
    4747
    48     protected CovarianceProd(CovarianceProd original, Cloner cloner)
     48    private CovarianceProd(CovarianceProd original, Cloner cloner)
    4949      : base(original, cloner) {
    5050      this.factors = cloner.Clone(original.factors);
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceRQArd.cs

    r8565 r8612  
    2525using HeuristicLab.Common;
    2626using HeuristicLab.Core;
     27using HeuristicLab.Data;
    2728using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2829
     
    3132  [Item(Name = "CovarianceRQArd",
    3233    Description = "Rational quadratic covariance function with automatic relevance determination for Gaussian processes.")]
    33   public class CovarianceRQArd : Item, ICovarianceFunction {
     34  public sealed class CovarianceRQArd : ParameterizedNamedItem, ICovarianceFunction {
    3435    [Storable]
    3536    private double sf2;
    36     public double Scale { get { return sf2; } }
     37    [Storable]
     38    private readonly HyperParameter<DoubleValue> scaleParameter;
     39    public IValueParameter<DoubleValue> ScaleParameter {
     40      get { return scaleParameter; }
     41    }
     42
    3743    [Storable]
    3844    private double[] inverseLength;
    39     public double[] InverseLength {
    40       get {
    41         if (inverseLength == null) return null;
    42         double[] res = new double[inverseLength.Length];
    43         Array.Copy(inverseLength, res, res.Length);
    44         return res;
    45       }
     45    [Storable]
     46    private readonly HyperParameter<DoubleArray> inverseLengthParameter;
     47    public IValueParameter<DoubleArray> InverseLengthParameter {
     48      get { return inverseLengthParameter; }
    4649    }
     50
    4751    [Storable]
    48     private double alpha;
    49     public double Shape { get { return alpha; } }
     52    private double shape;
     53    [Storable]
     54    private readonly HyperParameter<DoubleValue> shapeParameter;
     55    public IValueParameter<DoubleValue> ShapeParameter {
     56      get { return shapeParameter; }
     57    }
    5058
    5159    [StorableConstructor]
    52     protected CovarianceRQArd(bool deserializing)
     60    private CovarianceRQArd(bool deserializing)
    5361      : base(deserializing) {
    5462    }
    5563
    56     protected CovarianceRQArd(CovarianceRQArd original, Cloner cloner)
     64    private CovarianceRQArd(CovarianceRQArd original, Cloner cloner)
    5765      : base(original, cloner) {
     66      this.scaleParameter = cloner.Clone(original.scaleParameter);
    5867      this.sf2 = original.sf2;
    59       this.inverseLength = original.InverseLength; // array is cloned in the getter
    60       this.alpha = original.alpha;
     68
     69      this.inverseLengthParameter = cloner.Clone(original.inverseLengthParameter);
     70      if (original.inverseLength != null) {
     71        this.inverseLength = new double[original.inverseLength.Length];
     72        Array.Copy(original.inverseLength, inverseLength, inverseLength.Length);
     73      }
     74
     75      this.shapeParameter = cloner.Clone(original.shapeParameter);
     76      this.shape = original.shape;
     77
     78      RegisterEvents();
    6179    }
    6280
    6381    public CovarianceRQArd()
    6482      : base() {
     83      Name = ItemName;
     84      Description = ItemDescription;
     85
     86      this.scaleParameter = new HyperParameter<DoubleValue>("Scale", "The scale parameter of the rational quadratic covariance function with ARD.");
     87      this.inverseLengthParameter = new HyperParameter<DoubleArray>("InverseLength", "The inverse length parameter for automatic relevance determination.");
     88      this.shapeParameter = new HyperParameter<DoubleValue>("Shape", "The shape parameter (alpha) of the rational quadratic covariance function with ARD.");
     89
     90      Parameters.Add(scaleParameter);
     91      Parameters.Add(inverseLengthParameter);
     92      Parameters.Add(shapeParameter);
     93
     94      RegisterEvents();
    6595    }
    6696
     
    6999    }
    70100
     101    [StorableHook(HookType.AfterDeserialization)]
     102    private void AfterDeserialization() {
     103      RegisterEvents();
     104    }
     105
     106    private void RegisterEvents() {
     107      Util.AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { sf2 = scaleParameter.Value.Value; });
     108      Util.AttachValueChangeHandler<DoubleValue, double>(shapeParameter, () => { shape = shapeParameter.Value.Value; });
     109      Util.AttachArrayChangeHandler<DoubleArray, double>(inverseLengthParameter, () => { inverseLength = inverseLengthParameter.Value.ToArray(); });
     110    }
     111
    71112    public int GetNumberOfParameters(int numberOfVariables) {
    72       return numberOfVariables + 2;
     113      return
     114        (scaleParameter.Fixed ? 0 : 1) +
     115        (shapeParameter.Fixed ? 0 : 1) +
     116        (inverseLengthParameter.Fixed ? 0 : numberOfVariables);
    73117    }
    74118
    75119    public void SetParameter(double[] hyp) {
    76       this.inverseLength = hyp.Take(hyp.Length - 2).Select(e => 1.0 / Math.Exp(e)).ToArray();
    77       this.sf2 = Math.Exp(2 * hyp[hyp.Length - 2]);
    78       this.alpha = Math.Exp(hyp[hyp.Length - 1]);
     120      int i = 0;
     121      if (!scaleParameter.Fixed) {
     122        scaleParameter.SetValue(new DoubleValue(Math.Exp(2 * hyp[i])));
     123        i++;
     124      }
     125      if (!shapeParameter.Fixed) {
     126        shapeParameter.SetValue(new DoubleValue(Math.Exp(hyp[i])));
     127        i++;
     128      }
     129      if (!inverseLengthParameter.Fixed) {
     130        inverseLengthParameter.SetValue(new DoubleArray(hyp.Skip(i).Select(e => 1.0 / Math.Exp(e)).ToArray()));
     131        i += hyp.Skip(i).Count();
     132      }
     133      if (hyp.Length != i) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceRQArd", "hyp");
    79134    }
    80135
     
    84139                   ? 0.0
    85140                   : Util.SqrDist(x, i, j, inverseLength);
    86       return sf2 * Math.Pow(1 + 0.5 * d / alpha, -alpha);
     141      return sf2 * Math.Pow(1 + 0.5 * d / shape, -shape);
    87142    }
    88143
     
    91146                   ? 0.0
    92147                   : Util.SqrDist(x, i, j, inverseLength);
    93       double b = 1 + 0.5 * d / alpha;
     148      double b = 1 + 0.5 * d / shape;
    94149      for (int k = 0; k < inverseLength.Length; k++) {
    95         yield return sf2 * Math.Pow(b, -alpha - 1) * Util.SqrDist(x[i, k] * inverseLength[k], x[j, k] * inverseLength[k]);
     150        yield return sf2 * Math.Pow(b, -shape - 1) * Util.SqrDist(x[i, k] * inverseLength[k], x[j, k] * inverseLength[k]);
    96151      }
    97       yield return 2 * sf2 * Math.Pow(b, -alpha);
    98       yield return sf2 * Math.Pow(b, -alpha) * (0.5 * d / b - alpha * Math.Log(b));
     152      yield return 2 * sf2 * Math.Pow(b, -shape);
     153      yield return sf2 * Math.Pow(b, -shape) * (0.5 * d / b - shape * Math.Log(b));
    99154    }
    100155
    101156    public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {
    102157      double d = Util.SqrDist(x, i, xt, j, inverseLength);
    103       return sf2 * Math.Pow(1 + 0.5 * d / alpha, -alpha);
     158      return sf2 * Math.Pow(1 + 0.5 * d / shape, -shape);
    104159    }
    105160  }
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceRQiso.cs

    r8491 r8612  
    2424using HeuristicLab.Common;
    2525using HeuristicLab.Core;
     26using HeuristicLab.Data;
    2627using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2728
     
    3031  [Item(Name = "CovarianceRQiso",
    3132    Description = "Isotropic rational quadratic covariance function for Gaussian processes.")]
    32   public class CovarianceRQiso : Item, ICovarianceFunction {
     33  public sealed class CovarianceRQiso : ParameterizedNamedItem, ICovarianceFunction {
    3334    [Storable]
    3435    private double sf2;
    35     public double Scale { get { return sf2; } }
     36    [Storable]
     37    private readonly HyperParameter<DoubleValue> scaleParameter;
     38    public IValueParameter<DoubleValue> ScaleParameter { get { return scaleParameter; } }
     39
    3640    [Storable]
    3741    private double inverseLength;
    38     public double InverseLength { get { return inverseLength; } }
    3942    [Storable]
    40     private double alpha;
    41     public double Shape { get { return alpha; } }
     43    private readonly HyperParameter<DoubleValue> inverseLengthParameter;
     44    public IValueParameter<DoubleValue> InverseLengthParameter { get { return inverseLengthParameter; } }
     45
     46    [Storable]
     47    private double shape;
     48    [Storable]
     49    private readonly HyperParameter<DoubleValue> shapeParameter;
     50    public IValueParameter<DoubleValue> ShapeParameter { get { return shapeParameter; } }
    4251
    4352    [StorableConstructor]
    44     protected CovarianceRQiso(bool deserializing)
     53    private CovarianceRQiso(bool deserializing)
    4554      : base(deserializing) {
    4655    }
    4756
    48     protected CovarianceRQiso(CovarianceRQiso original, Cloner cloner)
     57    private CovarianceRQiso(CovarianceRQiso original, Cloner cloner)
    4958      : base(original, cloner) {
    5059      this.sf2 = original.sf2;
     60      this.scaleParameter = cloner.Clone(original.scaleParameter);
     61
    5162      this.inverseLength = original.inverseLength;
    52       this.alpha = original.alpha;
     63      this.inverseLengthParameter = cloner.Clone(original.inverseLengthParameter);
     64
     65      this.shape = original.shape;
     66      this.shapeParameter = cloner.Clone(original.shapeParameter);
     67
     68      RegisterEvents();
    5369    }
    5470
    5571    public CovarianceRQiso()
    5672      : base() {
     73      Name = ItemName;
     74      Description = ItemDescription;
     75
     76      this.scaleParameter = new HyperParameter<DoubleValue>("Scale", "The scale parameter of the isometric rational quadratic covariance function.");
     77      this.inverseLengthParameter = new HyperParameter<DoubleValue>("InverseLength", "The inverse length parameter of the isometric rational quadratic covariance function.");
     78      this.shapeParameter = new HyperParameter<DoubleValue>("Shape", "The shape parameter (alpha) of the isometric rational quadratic covariance function.");
     79
     80      Parameters.Add(scaleParameter);
     81      Parameters.Add(inverseLengthParameter);
     82      Parameters.Add(shapeParameter);
     83
     84      RegisterEvents();
    5785    }
    5886
     
    6189    }
    6290
     91    [StorableHook(HookType.AfterDeserialization)]
     92    private void AfterDeserialization() {
     93      RegisterEvents();
     94    }
     95
     96    private void RegisterEvents() {
     97      Util.AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { sf2 = scaleParameter.Value.Value; });
     98      Util.AttachValueChangeHandler<DoubleValue, double>(inverseLengthParameter, () => { inverseLength = inverseLengthParameter.Value.Value; });
     99      Util.AttachValueChangeHandler<DoubleValue, double>(shapeParameter, () => { shape = shapeParameter.Value.Value; });
     100    }
     101
    63102    public int GetNumberOfParameters(int numberOfVariables) {
    64       return 3;
     103      return
     104        (scaleParameter.Fixed ? 0 : 1) +
     105        (inverseLengthParameter.Fixed ? 0 : 1) +
     106        (shapeParameter.Fixed ? 0 : 1);
    65107    }
    66108
    67109    public void SetParameter(double[] hyp) {
    68       if (hyp.Length != 3) throw new ArgumentException("CovarianceRQiso has three hyperparameters", "k");
    69       this.inverseLength = 1.0 / Math.Exp(hyp[0]);
    70       this.sf2 = Math.Exp(2 * hyp[1]);
    71       this.alpha = Math.Exp(hyp[2]);
     110      int i = 0;
     111      if (!scaleParameter.Fixed) {
     112        scaleParameter.SetValue(new DoubleValue(Math.Exp(2 * hyp[i])));
     113        i++;
     114      }
     115      if (!shapeParameter.Fixed) {
     116        shapeParameter.SetValue(new DoubleValue(Math.Exp(hyp[i])));
     117        i++;
     118      }
     119      if (!inverseLengthParameter.Fixed) {
     120        inverseLengthParameter.SetValue(new DoubleValue(1.0 / Math.Exp(hyp[i])));
     121        i++;
     122      }
     123      if (hyp.Length != i) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceRQiso", "hyp");
    72124    }
    73125
     
    77129                   ? 0.0
    78130                   : Util.SqrDist(x, i, j, inverseLength);
    79       return sf2 * Math.Pow(1 + 0.5 * d / alpha, -alpha);
     131      return sf2 * Math.Pow(1 + 0.5 * d / shape, -shape);
    80132    }
    81133
     
    85137                   : Util.SqrDist(x, i, j, inverseLength);
    86138
    87       double b = 1 + 0.5 * d / alpha;
    88       yield return sf2 * Math.Pow(b, -alpha - 1) * d;
    89       yield return 2 * sf2 * Math.Pow(b, -alpha);
    90       yield return sf2 * Math.Pow(b, -alpha) * (0.5 * d / b - alpha * Math.Log(b));
     139      double b = 1 + 0.5 * d / shape;
     140      yield return sf2 * Math.Pow(b, -shape - 1) * d;
     141      yield return 2 * sf2 * Math.Pow(b, -shape);
     142      yield return sf2 * Math.Pow(b, -shape) * (0.5 * d / b - shape * Math.Log(b));
    91143    }
    92144
    93145    public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {
    94146      double d = Util.SqrDist(x, i, xt, j, inverseLength);
    95       return sf2 * Math.Pow(1 + 0.5 * d / alpha, -alpha);
     147      return sf2 * Math.Pow(1 + 0.5 * d / shape, -shape);
    96148    }
    97149  }
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceSEard.cs

    r8565 r8612  
    2525using HeuristicLab.Common;
    2626using HeuristicLab.Core;
     27using HeuristicLab.Data;
    2728using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2829
     
    3031  [StorableClass]
    3132  [Item(Name = "CovarianceSEard", Description = "Squared exponential covariance function with automatic relevance determination for Gaussian processes.")]
    32   public class CovarianceSEard : Item, ICovarianceFunction {
     33  public sealed class CovarianceSEard : ParameterizedNamedItem, ICovarianceFunction {
    3334    [Storable]
    3435    private double sf2;
    35     public double Scale { get { return sf2; } }
     36    [Storable]
     37    private readonly HyperParameter<DoubleValue> scaleParameter;
     38    public IValueParameter<DoubleValue> ScaleParameter { get { return scaleParameter; } }
    3639
    3740    [Storable]
    3841    private double[] inverseLength;
    39     public double[] InverseLength {
    40       get {
    41         if (inverseLength == null) return new double[0];
    42         var copy = new double[inverseLength.Length];
    43         Array.Copy(inverseLength, copy, copy.Length);
    44         return copy;
     42    [Storable]
     43    private readonly HyperParameter<DoubleArray> inverseLengthParameter;
     44    public IValueParameter<DoubleArray> InverseLengthParameter { get { return inverseLengthParameter; } }
     45
     46    [StorableConstructor]
     47    private CovarianceSEard(bool deserializing) : base(deserializing) { }
     48    private CovarianceSEard(CovarianceSEard original, Cloner cloner)
     49      : base(original, cloner) {
     50      this.sf2 = original.sf2;
     51      this.scaleParameter = cloner.Clone(original.scaleParameter);
     52
     53      if (original.inverseLength != null) {
     54        this.inverseLength = new double[original.inverseLength.Length];
     55        Array.Copy(original.inverseLength, this.inverseLength, this.inverseLength.Length);
    4556      }
    46     }
     57      this.inverseLengthParameter = cloner.Clone(original.inverseLengthParameter);
    4758
    48     public int GetNumberOfParameters(int numberOfVariables) {
    49       return numberOfVariables + 1;
    50     }
    51     [StorableConstructor]
    52     protected CovarianceSEard(bool deserializing) : base(deserializing) { }
    53     protected CovarianceSEard(CovarianceSEard original, Cloner cloner)
    54       : base(original, cloner) {
    55       this.inverseLength = original.InverseLength; // array is cloned in the getter
    56       this.sf2 = original.sf2;
     59      RegisterEvents();
    5760    }
    5861    public CovarianceSEard()
    5962      : base() {
     63      Name = ItemName;
     64      Description = ItemDescription;
     65
     66      this.scaleParameter = new HyperParameter<DoubleValue>("Scale", "The scale parameter of the squared exponential covariance function with ARD.");
     67      this.inverseLengthParameter = new HyperParameter<DoubleArray>("InverseLength", "The inverse length parameter for automatic relevance determination.");
     68
     69      Parameters.Add(scaleParameter);
     70      Parameters.Add(inverseLengthParameter);
     71
     72      RegisterEvents();
    6073    }
    6174
     
    6477    }
    6578
     79    [StorableHook(HookType.AfterDeserialization)]
     80    private void AfterDeserialization() {
     81      RegisterEvents();
     82    }
     83
     84    private void RegisterEvents() {
     85      Util.AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { sf2 = scaleParameter.Value.Value; });
     86      Util.AttachArrayChangeHandler<DoubleArray, double>(inverseLengthParameter, () => {
     87        inverseLength =
     88          inverseLengthParameter.Value.ToArray();
     89      });
     90    }
     91
     92    public int GetNumberOfParameters(int numberOfVariables) {
     93      return
     94        (scaleParameter.Fixed ? 0 : 1) +
     95        (inverseLengthParameter.Fixed ? 0 : numberOfVariables);
     96    }
     97
     98
    6699    public void SetParameter(double[] hyp) {
    67       this.inverseLength = hyp.Take(hyp.Length - 1).Select(p => 1.0 / Math.Exp(p)).ToArray();
    68       this.sf2 = Math.Exp(2 * hyp[hyp.Length - 1]);
     100      int i = 0;
     101      if (!scaleParameter.Fixed) {
     102        scaleParameter.SetValue(new DoubleValue(Math.Exp(2 * hyp[i])));
     103        i++;
     104      }
     105      if (!inverseLengthParameter.Fixed) {
     106        inverseLengthParameter.SetValue(new DoubleArray(hyp.Skip(i).Select(e => 1.0 / Math.Exp(e)).ToArray()));
     107        i += hyp.Skip(i).Count();
     108      }
     109      if (hyp.Length != i) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovariancSEard", "hyp");
    69110    }
    70111
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceSEiso.cs

    r8491 r8612  
    2424using HeuristicLab.Common;
    2525using HeuristicLab.Core;
     26using HeuristicLab.Data;
    2627using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2728
     
    3031  [Item(Name = "CovarianceSEiso",
    3132    Description = "Isotropic squared exponential covariance function for Gaussian processes.")]
    32   public class CovarianceSEiso : Item, ICovarianceFunction {
     33  public sealed class CovarianceSEiso : ParameterizedNamedItem, ICovarianceFunction {
    3334    [Storable]
    3435    private double sf2;
    35     public double Scale { get { return sf2; } }
     36    [Storable]
     37    private readonly HyperParameter<DoubleValue> scaleParameter;
     38    public IValueParameter<DoubleValue> ScaleParameter { get { return scaleParameter; } }
     39
    3640    [Storable]
    3741    private double inverseLength;
    38     public double InverseLength { get { return inverseLength; } }
     42    [Storable]
     43    private readonly HyperParameter<DoubleValue> inverseLengthParameter;
     44    public IValueParameter<DoubleValue> InverseLengthParameter { get { return inverseLengthParameter; } }
    3945
    4046    [StorableConstructor]
    41     protected CovarianceSEiso(bool deserializing)
     47    private CovarianceSEiso(bool deserializing)
    4248      : base(deserializing) {
    4349    }
    4450
    45     protected CovarianceSEiso(CovarianceSEiso original, Cloner cloner)
     51    private CovarianceSEiso(CovarianceSEiso original, Cloner cloner)
    4652      : base(original, cloner) {
    4753      this.sf2 = original.sf2;
     54      this.scaleParameter = cloner.Clone(original.scaleParameter);
     55
    4856      this.inverseLength = original.inverseLength;
     57      this.inverseLengthParameter = cloner.Clone(original.inverseLengthParameter);
     58
     59      RegisterEvents();
    4960    }
    5061
    5162    public CovarianceSEiso()
    5263      : base() {
     64      Name = ItemName;
     65      Description = ItemDescription;
     66
     67      this.scaleParameter = new HyperParameter<DoubleValue>("Scale", "The scale parameter of the isometric squared exponential covariance function.");
     68      this.inverseLengthParameter = new HyperParameter<DoubleValue>("InverseLength", "The inverse length parameter of the isometric squared exponential covariance function.");
     69
     70      Parameters.Add(scaleParameter);
     71      Parameters.Add(inverseLengthParameter);
     72
     73      RegisterEvents();
    5374    }
    5475
     
    5778    }
    5879
     80    [StorableHook(HookType.AfterDeserialization)]
     81    private void AfterDeserialization() {
     82      RegisterEvents();
     83    }
     84
     85    private void RegisterEvents() {
     86      Util.AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { sf2 = scaleParameter.Value.Value; });
     87      Util.AttachValueChangeHandler<DoubleValue, double>(inverseLengthParameter, () => { inverseLength = inverseLengthParameter.Value.Value; });
     88    }
     89
    5990    public int GetNumberOfParameters(int numberOfVariables) {
    60       return 2;
     91      return
     92        (scaleParameter.Fixed ? 0 : 1) +
     93        (inverseLengthParameter.Fixed ? 0 : 1);
    6194    }
    6295
    6396    public void SetParameter(double[] hyp) {
    64       if (hyp.Length != 2) throw new ArgumentException("CovarianceSEiso has two hyperparameters", "k");
    65       this.inverseLength = 1.0 / Math.Exp(hyp[0]);
    66       this.sf2 = Math.Exp(2 * hyp[1]);
     97      int i = 0;
     98      if (!inverseLengthParameter.Fixed) {
     99        inverseLengthParameter.SetValue(new DoubleValue(1.0 / Math.Exp(hyp[i])));
     100        i++;
     101      }
     102      if (!scaleParameter.Fixed) {
     103        scaleParameter.SetValue(new DoubleValue(Math.Exp(2 * hyp[i])));
     104        i++;
     105      }
     106      if (hyp.Length != i) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceSEiso", "hyp");
    67107    }
    68108
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceSum.cs

    r8489 r8612  
    3131  [Item(Name = "CovarianceSum",
    3232    Description = "Sum covariance function for Gaussian processes.")]
    33   public class CovarianceSum : Item, ICovarianceFunction {
     33  public sealed class CovarianceSum : Item, ICovarianceFunction {
    3434    [Storable]
    3535    private ItemList<ICovarianceFunction> terms;
     
    4242
    4343    [StorableConstructor]
    44     protected CovarianceSum(bool deserializing)
     44    private CovarianceSum(bool deserializing)
    4545      : base(deserializing) {
    4646    }
    4747
    48     protected CovarianceSum(CovarianceSum original, Cloner cloner)
     48    private CovarianceSum(CovarianceSum original, Cloner cloner)
    4949      : base(original, cloner) {
    5050      this.terms = cloner.Clone(original.terms);
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/GaussianProcessModel.cs

    r8582 r8612  
    137137      l = new double[n, n];
    138138
    139       meanFunction.SetData(x);
    140 
    141139      // calculate means and covariances
    142140      double[] m = meanFunction.GetMean(x);
     
    235233      int n = x.GetLength(0);
    236234      var Ks = new double[newN, n];
    237       meanFunction.SetData(newX);
    238235      var ms = meanFunction.GetMean(newX);
    239236      for (int i = 0; i < newN; i++) {
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/HyperParameter.cs

    r8592 r8612  
    2929  [StorableClass]
    3030  [Item(Name = "HyperParameter", Description = "Represents a hyperparameter for Gaussian processes.")]
    31   public class HyperParameter<T> : OptionalValueParameter<T>, IValueParameter<T> where T : class, IItem {
     31  internal class HyperParameter<T> : OptionalValueParameter<T>, IValueParameter<T> where T : class, IItem {
    3232
    3333    [Storable]
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/IMeanFunction.cs

    r8416 r8612  
    2525    int GetNumberOfParameters(int numberOfVariables);
    2626    void SetParameter(double[] hyp);
    27     void SetData(double[,] x);
    2827    double[] GetMean(double[,] x);
    2928    double[] GetGradients(int k, double[,] x);
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanConst.cs

    r8473 r8612  
    2424using HeuristicLab.Common;
    2525using HeuristicLab.Core;
     26using HeuristicLab.Data;
    2627using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2728
     
    2930  [StorableClass]
    3031  [Item(Name = "MeanConst", Description = "Constant mean function for Gaussian processes.")]
    31   public class MeanConst : Item, IMeanFunction {
     32  public sealed class MeanConst : ParameterizedNamedItem, IMeanFunction {
    3233    [Storable]
    3334    private double c;
    34     public double Value { get { return c; } }
     35    [Storable]
     36    private readonly HyperParameter<DoubleValue> valueParameter;
     37    public IValueParameter<DoubleValue> ValueParameter { get { return valueParameter; } }
    3538
    36     public int GetNumberOfParameters(int numberOfVariables) {
    37       return 1;
    38     }
    3939    [StorableConstructor]
    40     protected MeanConst(bool deserializing) : base(deserializing) { }
    41     protected MeanConst(MeanConst original, Cloner cloner)
     40    private MeanConst(bool deserializing) : base(deserializing) { }
     41    private MeanConst(MeanConst original, Cloner cloner)
    4242      : base(original, cloner) {
    4343      this.c = original.c;
     44      this.valueParameter = cloner.Clone(original.valueParameter);
     45      RegisterEvents();
    4446    }
    4547    public MeanConst()
    4648      : base() {
     49      this.name = ItemName;
     50      this.description = ItemDescription;
     51
     52      this.valueParameter = new HyperParameter<DoubleValue>("Value", "The constant value for the constant mean function.");
     53      Parameters.Add(valueParameter);
     54      RegisterEvents();
     55    }
     56
     57    public override IDeepCloneable Clone(Cloner cloner) {
     58      return new MeanConst(this, cloner);
     59    }
     60
     61    [StorableHook(HookType.AfterDeserialization)]
     62    private void AfterDeserialization() {
     63      RegisterEvents();
     64    }
     65
     66    private void RegisterEvents() {
     67      Util.AttachValueChangeHandler<DoubleValue, double>(valueParameter, () => { c = valueParameter.Value.Value; });
     68    }
     69
     70    public int GetNumberOfParameters(int numberOfVariables) {
     71      return valueParameter.Fixed ? 0 : 1;
    4772    }
    4873
    4974    public void SetParameter(double[] hyp) {
    50       if (hyp.Length != 1) throw new ArgumentException("Only one hyper-parameter allowed for constant mean function.", "hyp");
    51       this.c = hyp[0];
    52     }
    53     public void SetData(double[,] x) {
    54       // nothing to do
     75      if (!valueParameter.Fixed) {
     76        valueParameter.SetValue(new DoubleValue(hyp[0]));
     77      } else if (hyp.Length > 0)
     78        throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for the constant mean function.", "hyp");
    5579    }
    5680
     
    6387      return Enumerable.Repeat(1.0, x.GetLength(0)).ToArray();
    6488    }
    65 
    66     public override IDeepCloneable Clone(Cloner cloner) {
    67       return new MeanConst(this, cloner);
    68     }
    6989  }
    7090}
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanLinear.cs

    r8473 r8612  
    1919 */
    2020#endregion
     21
    2122using System;
    2223using System.Linq;
    2324using HeuristicLab.Common;
    2425using HeuristicLab.Core;
     26using HeuristicLab.Data;
    2527using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2628
     
    2830  [StorableClass]
    2931  [Item(Name = "MeanLinear", Description = "Linear mean function for Gaussian processes.")]
    30   public class MeanLinear : Item, IMeanFunction {
     32  public sealed class MeanLinear : ParameterizedNamedItem, IMeanFunction {
    3133    [Storable]
    32     private double[] alpha;
    33     public double[] Weights {
    34       get {
    35         if (alpha == null) return new double[0];
    36         var copy = new double[alpha.Length];
    37         Array.Copy(alpha, copy, copy.Length);
    38         return copy;
     34    private double[] weights;
     35    [Storable]
     36    private readonly HyperParameter<DoubleArray> weightsParameter;
     37    public IValueParameter<DoubleArray> WeightsParameter { get { return weightsParameter; } }
     38
     39    [StorableConstructor]
     40    private MeanLinear(bool deserializing) : base(deserializing) { }
     41    private MeanLinear(MeanLinear original, Cloner cloner)
     42      : base(original, cloner) {
     43      if (original.weights != null) {
     44        this.weights = new double[original.weights.Length];
     45        Array.Copy(original.weights, weights, original.weights.Length);
    3946      }
    40     }
    41     public int GetNumberOfParameters(int numberOfVariables) {
    42       return numberOfVariables;
    43     }
    44     [StorableConstructor]
    45     protected MeanLinear(bool deserializing) : base(deserializing) { }
    46     protected MeanLinear(MeanLinear original, Cloner cloner)
    47       : base(original, cloner) {
    48       if (original.alpha != null) {
    49         this.alpha = new double[original.alpha.Length];
    50         Array.Copy(original.alpha, alpha, original.alpha.Length);
    51       }
     47      weightsParameter = cloner.Clone(original.weightsParameter);
     48      RegisterEvents();
    5249    }
    5350    public MeanLinear()
    5451      : base() {
     52      this.weightsParameter = new HyperParameter<DoubleArray>("Weights", "The weights parameter for the linear mean function.");
     53      Parameters.Add(weightsParameter);
     54      RegisterEvents();
     55    }
     56
     57    public override IDeepCloneable Clone(Cloner cloner) {
     58      return new MeanLinear(this, cloner);
     59    }
     60
     61    [StorableHook(HookType.AfterDeserialization)]
     62    private void AfterDeserialization() {
     63      RegisterEvents();
     64    }
     65
     66    private void RegisterEvents() {
     67      Util.AttachArrayChangeHandler<DoubleArray, double>(weightsParameter, () => {
     68        weights = weightsParameter.Value.ToArray();
     69      });
     70    }
     71
     72    public int GetNumberOfParameters(int numberOfVariables) {
     73      return weightsParameter.Fixed ? 0 : numberOfVariables;
    5574    }
    5675
    5776    public void SetParameter(double[] hyp) {
    58       this.alpha = new double[hyp.Length];
    59       Array.Copy(hyp, alpha, hyp.Length);
    60     }
    61     public void SetData(double[,] x) {
    62       // nothing to do
     77      if (!weightsParameter.Fixed) {
     78        weightsParameter.SetValue(new DoubleArray(hyp));
     79      } else if (hyp.Length != 0) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for the linear mean function.", "hyp");
    6380    }
    6481
    6582    public double[] GetMean(double[,] x) {
    6683      // sanity check
    67       if (alpha.Length != x.GetLength(1)) throw new ArgumentException("The number of hyperparameters must match the number of variables for the linear mean function.");
     84      if (weights.Length != x.GetLength(1)) throw new ArgumentException("The number of hyperparameters must match the number of variables for the linear mean function.");
    6885      int cols = x.GetLength(1);
    6986      int n = x.GetLength(0);
    7087      return (from i in Enumerable.Range(0, n)
    71               let rowVector = from j in Enumerable.Range(0, cols)
    72                               select x[i, j]
    73               select Util.ScalarProd(alpha, rowVector))
     88              let rowVector = Enumerable.Range(0, cols).Select(j => x[i, j])
     89              select Util.ScalarProd(weights, rowVector))
    7490        .ToArray();
    7591    }
     
    7995      int n = x.GetLength(0);
    8096      if (k > cols) throw new ArgumentException();
    81       return (from r in Enumerable.Range(0, n)
    82               select x[r, k]).ToArray();
    83     }
    84 
    85     public override IDeepCloneable Clone(Cloner cloner) {
    86       return new MeanLinear(this, cloner);
     97      return (Enumerable.Range(0, n).Select(r => x[r, k])).ToArray();
    8798    }
    8899  }
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanProd.cs

    r8463 r8612  
    2727  [StorableClass]
    2828  [Item(Name = "MeanProd", Description = "Product of mean functions for Gaussian processes.")]
    29   public class MeanProd : Item, IMeanFunction {
     29  public sealed class MeanProd : Item, IMeanFunction {
    3030    [Storable]
    3131    private ItemList<IMeanFunction> factors;
     
    3838    }
    3939
    40     public int GetNumberOfParameters(int numberOfVariables) {
    41       this.numberOfVariables = numberOfVariables;
    42       return factors.Select(t => t.GetNumberOfParameters(numberOfVariables)).Sum();
    43     }
    44 
    4540    [StorableConstructor]
    46     protected MeanProd(bool deserializing)
     41    private MeanProd(bool deserializing)
    4742      : base(deserializing) {
    4843    }
    4944
    50     protected MeanProd(MeanProd original, Cloner cloner)
     45    private MeanProd(MeanProd original, Cloner cloner)
    5146      : base(original, cloner) {
    5247      this.factors = cloner.Clone(original.factors);
     
    5651    public MeanProd() {
    5752      this.factors = new ItemList<IMeanFunction>();
     53    }
     54    public override IDeepCloneable Clone(Cloner cloner) {
     55      return new MeanProd(this, cloner);
     56    }
     57
     58    public int GetNumberOfParameters(int numberOfVariables) {
     59      this.numberOfVariables = numberOfVariables;
     60      return factors.Select(t => t.GetNumberOfParameters(numberOfVariables)).Sum();
    5861    }
    5962
     
    6568        offset += numberOfParameters;
    6669      }
    67     }
    68 
    69     public void SetData(double[,] x) {
    70       foreach (var t in factors) t.SetData(x);
    7170    }
    7271
     
    102101      return res;
    103102    }
    104 
    105     public override IDeepCloneable Clone(Cloner cloner) {
    106       return new MeanProd(this, cloner);
    107     }
    108103  }
    109104}
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanSum.cs

    r8439 r8612  
    2727  [StorableClass]
    2828  [Item(Name = "MeanSum", Description = "Sum of mean functions for Gaussian processes.")]
    29   public class MeanSum : Item, IMeanFunction {
     29  public sealed class MeanSum : Item, IMeanFunction {
    3030    [Storable]
    3131    private ItemList<IMeanFunction> terms;
     
    3737    }
    3838
    39     public int GetNumberOfParameters(int numberOfVariables) {
    40       this.numberOfVariables = numberOfVariables;
    41       return terms.Select(t => t.GetNumberOfParameters(numberOfVariables)).Sum();
    42     }
    4339    [StorableConstructor]
    44     protected MeanSum(bool deserializing) : base(deserializing) { }
    45     protected MeanSum(MeanSum original, Cloner cloner)
     40    private MeanSum(bool deserializing) : base(deserializing) { }
     41    private MeanSum(MeanSum original, Cloner cloner)
    4642      : base(original, cloner) {
    4743      this.terms = cloner.Clone(original.terms);
     
    5046    public MeanSum() {
    5147      this.terms = new ItemList<IMeanFunction>();
     48    }
     49
     50    public override IDeepCloneable Clone(Cloner cloner) {
     51      return new MeanSum(this, cloner);
     52    }
     53
     54    public int GetNumberOfParameters(int numberOfVariables) {
     55      this.numberOfVariables = numberOfVariables;
     56      return terms.Select(t => t.GetNumberOfParameters(numberOfVariables)).Sum();
    5257    }
    5358
     
    5964        offset += numberOfParameters;
    6065      }
    61     }
    62 
    63     public void SetData(double[,] x) {
    64       foreach (var t in terms) t.SetData(x);
    6566    }
    6667
     
    8283      return terms[i].GetGradients(k, x);
    8384    }
    84 
    85     public override IDeepCloneable Clone(Cloner cloner) {
    86       return new MeanSum(this, cloner);
    87     }
    8885  }
    8986}
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanZero.cs

    r8416 r8612  
    2828  [StorableClass]
    2929  [Item(Name = "MeanZero", Description = "Constant zero mean function for Gaussian processes.")]
    30   public class MeanZero : Item, IMeanFunction {
    31     public int GetNumberOfParameters(int numberOfVariables) {
    32       return 0;
    33     }
     30  public sealed class MeanZero : Item, IMeanFunction {
    3431    [StorableConstructor]
    35     protected MeanZero(bool deserializing) : base(deserializing) { }
    36     protected MeanZero(MeanZero original, Cloner cloner)
     32    private MeanZero(bool deserializing) : base(deserializing) { }
     33    private MeanZero(MeanZero original, Cloner cloner)
    3734      : base(original, cloner) {
    3835    }
     
    4037    }
    4138
     39    public override IDeepCloneable Clone(Cloner cloner) {
     40      return new MeanZero(this, cloner);
     41    }
     42
     43    public int GetNumberOfParameters(int numberOfVariables) {
     44      return 0;
     45    }
     46
    4247    public void SetParameter(double[] hyp) {
    4348      if (hyp.Length > 0) throw new ArgumentException("No hyper-parameters allowed for zero mean function.", "hyp");
    44     }
    45 
    46     public void SetData(double[,] x) {
    47       // do nothing
    4849    }
    4950
     
    5657      return Enumerable.Repeat(0.0, x.GetLength(0)).ToArray();
    5758    }
    58     public override IDeepCloneable Clone(Cloner cloner) {
    59       return new MeanZero(this, cloner);
    60     }
    6159  }
    6260}
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/Util.cs

    r8562 r8612  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    2324using System.Linq;
     25using HeuristicLab.Core;
     26using HeuristicLab.Data;
    2427
    2528namespace HeuristicLab.Algorithms.DataAnalysis {
    26   public static class Util {
     29  internal static class Util {
    2730    public static double ScalarProd(IEnumerable<double> v, IEnumerable<double> u) {
    2831      return v.Zip(u, (vi, ui) => vi * ui).Sum();
     
    9497      return Enumerable.Range(0, rows).Select(r => x[r, c]);
    9598    }
     99
     100
     101    public static void AttachValueChangeHandler<T, U>(IValueParameter<T> parameter, Action action)
     102      where T : ValueTypeValue<U>
     103      where U : struct {
     104      parameter.ValueChanged += (sender, args) => {
     105        if (parameter.Value != null) {
     106          parameter.Value.ValueChanged += (s, a) => action();
     107          action();
     108        }
     109      };
     110      if (parameter.Value != null) {
     111        parameter.Value.ValueChanged += (s, a) => action();
     112      }
     113    }
     114
     115    public static void AttachArrayChangeHandler<T, U>(IValueParameter<T> parameter, Action action)
     116      where T : ValueTypeArray<U>
     117      where U : struct {
     118      parameter.ValueChanged += (sender, args) => {
     119        if (parameter.Value != null) {
     120          parameter.Value.ItemChanged += (s, a) => action();
     121          parameter.Value.Reset += (s, a) => action();
     122          action();
     123        }
     124      };
     125      if (parameter.Value != null) {
     126        parameter.Value.ItemChanged += (s, a) => action();
     127        parameter.Value.Reset += (s, a) => action();
     128      }
     129    }
    96130  }
    97131}
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/HeuristicLab.Algorithms.DataAnalysis-3.4.csproj

    r8609 r8612  
    121121    <Compile Include="FixedDataAnalysisAlgorithm.cs" />
    122122    <Compile Include="GaussianProcess\HyperParameter.cs" />
    123     <Compile Include="GaussianProcess\CovarianceFunction.cs" />
    124123    <Compile Include="GaussianProcess\CovarianceRQArd.cs" />
    125124    <Compile Include="GaussianProcess\CovarianceMaternIso.cs" />
  • trunk/sources/HeuristicLab.Tests/HeuristicLab.Algorithms.DataAnalysis-3.4/GaussianProcessFunctionsTest.cs

    r8583 r8612  
    15091509      var hyp = Enumerable.Repeat(hypValue, nHyp).ToArray();
    15101510      mf.SetParameter(hyp);
    1511       mf.SetData(x);
    15121511
    15131512      var m = mf.GetMean(xt);
Note: See TracChangeset for help on using the changeset viewer.