Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceNeuralNetwork.cs @ 13721

Last change on this file since 13721 was 13721, checked in by mkommend, 8 years ago

#2591: Changed all GP covariance and mean functions to use int[] for column indices instead of IEnumerable<int>. Changed GP utils, GPModel and StudentTProcessModell as well to use fewer iterators and adapted unit tests to new interface.

File size: 5.8 KB
RevLine 
[9359]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[9359]4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Algorithms.DataAnalysis {
31  [StorableClass]
32  [Item(Name = "CovarianceNeuralNetwork",
33    Description = "Neural network covariance function for Gaussian processes.")]
34  public sealed class CovarianceNeuralNetwork : ParameterizedNamedItem, ICovarianceFunction {
35    public IValueParameter<DoubleValue> ScaleParameter {
36      get { return (IValueParameter<DoubleValue>)Parameters["Scale"]; }
37    }
38
[9360]39    public IValueParameter<DoubleValue> LengthParameter {
40      get { return (IValueParameter<DoubleValue>)Parameters["Length"]; }
[9359]41    }
[10489]42    private bool HasFixedScaleParameter {
43      get { return ScaleParameter.Value != null; }
44    }
45    private bool HasFixedLengthParameter {
46      get { return LengthParameter.Value != null; }
47    }
[9359]48
49    [StorableConstructor]
50    private CovarianceNeuralNetwork(bool deserializing)
51      : base(deserializing) {
52    }
53
54    private CovarianceNeuralNetwork(CovarianceNeuralNetwork original, Cloner cloner)
55      : base(original, cloner) {
56    }
57
58    public CovarianceNeuralNetwork()
59      : base() {
60      Name = ItemName;
61      Description = ItemDescription;
62
63      Parameters.Add(new OptionalValueParameter<DoubleValue>("Scale", "The scale parameter."));
[9360]64      Parameters.Add(new OptionalValueParameter<DoubleValue>("Length", "The length parameter."));
[9359]65    }
66
67    public override IDeepCloneable Clone(Cloner cloner) {
68      return new CovarianceNeuralNetwork(this, cloner);
69    }
70
71    public int GetNumberOfParameters(int numberOfVariables) {
72      return
[10489]73        (HasFixedScaleParameter ? 0 : 1) +
74        (HasFixedLengthParameter ? 0 : 1);
[9359]75    }
76
77    public void SetParameter(double[] p) {
[9360]78      double scale, length;
79      GetParameterValues(p, out scale, out length);
[9359]80      ScaleParameter.Value = new DoubleValue(scale);
[9360]81      LengthParameter.Value = new DoubleValue(length);
[9359]82    }
83
84
[9360]85    private void GetParameterValues(double[] p, out double scale, out double length) {
[9359]86      // gather parameter values
87      int c = 0;
[10489]88      if (HasFixedLengthParameter) {
[9360]89        length = LengthParameter.Value.Value;
[9359]90      } else {
[9360]91        length = Math.Exp(2 * p[c]);
[9359]92        c++;
93      }
94
[10489]95      if (HasFixedScaleParameter) {
[9359]96        scale = ScaleParameter.Value.Value;
97      } else {
98        scale = Math.Exp(2 * p[c]);
99        c++;
100      }
101      if (p.Length != c) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceNeuralNetwork", "p");
102    }
103
[13721]104    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) {
[9360]105      double length, scale;
106      GetParameterValues(p, out scale, out length);
[10489]107      var fixedLength = HasFixedLengthParameter;
108      var fixedScale = HasFixedScaleParameter;
[9359]109
110      var cov = new ParameterizedCovarianceFunction();
111      cov.Covariance = (x, i, j) => {
[10489]112        double sx = 1.0;
113        double s1 = 1.0;
114        double s2 = 1.0;
[13721]115        for (int c = 0; c < columnIndices.Length; c++) {
116          var col = columnIndices[c];
[10489]117          sx += x[i, col] * x[j, col];
118          s1 += x[i, col] * x[i, col];
119          s2 += x[j, col] * x[j, col];
[9359]120        }
[10489]121
122        return (scale * Math.Asin(sx / (Math.Sqrt((length + s1) * (length + s2)))));
[9359]123      };
124      cov.CrossCovariance = (x, xt, i, j) => {
[10489]125        double sx = 1.0;
126        double s1 = 1.0;
127        double s2 = 1.0;
[13721]128        for (int c = 0; c < columnIndices.Length; c++) {
129          var col = columnIndices[c];
[10489]130          sx += x[i, col] * xt[j, col];
131          s1 += x[i, col] * x[i, col];
132          s2 += xt[j, col] * xt[j, col];
[9359]133        }
[10489]134
135        return (scale * Math.Asin(sx / (Math.Sqrt((length + s1) * (length + s2)))));
[9359]136      };
[10489]137      cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, length, scale, columnIndices, fixedLength, fixedScale);
138      return cov;
139    }
140
141    // order of returned gradients must match the order in GetParameterValues!
[13721]142    private static IEnumerable<double> GetGradient(double[,] x, int i, int j, double length, double scale, int[] columnIndices,
[10489]143      bool fixedLength, bool fixedScale) {
144      {
145        double sx = 1.0;
146        double s1 = 1.0;
147        double s2 = 1.0;
[13721]148        for (int c = 0; c < columnIndices.Length; c++) {
149          var col = columnIndices[c];
[10489]150          sx += x[i, col] * x[j, col];
151          s1 += x[i, col] * x[i, col];
152          s2 += x[j, col] * x[j, col];
[9359]153        }
[10489]154        var h = (length + s1) * (length + s2);
155        var f = sx / Math.Sqrt(h);
156        if (!fixedLength) {
157          yield return -scale / Math.Sqrt(1.0 - f * f) * ((length * sx * (2.0 * length + s1 + s2)) / Math.Pow(h, 3.0 / 2.0));
[9359]158        }
[10489]159        if (!fixedScale) {
160          yield return 2.0 * scale * Math.Asin(f);
161        }
[10490]162      }
[9359]163    }
164  }
165}
Note: See TracBrowser for help on using the repository browser.