Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/05/14 17:30:38 (10 years ago)
Author:
mkommend
Message:

#1998: Updated classification model comparision branch with trunk changes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/ClassificationModelComparison/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceSquaredExponentialArd.cs

    r8982 r10553  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    4040      get { return (IValueParameter<DoubleArray>)Parameters["InverseLength"]; }
    4141    }
     42    private bool HasFixedInverseLengthParameter {
     43      get { return InverseLengthParameter.Value != null; }
     44    }
     45    private bool HasFixedScaleParameter {
     46      get { return ScaleParameter.Value != null; }
     47    }
    4248
    4349    [StorableConstructor]
     
    6167    public int GetNumberOfParameters(int numberOfVariables) {
    6268      return
    63         (ScaleParameter.Value != null ? 0 : 1) +
    64         (InverseLengthParameter.Value != null ? 0 : numberOfVariables);
     69        (HasFixedScaleParameter ? 0 : 1) +
     70        (HasFixedInverseLengthParameter ? 0 : numberOfVariables);
    6571    }
    6672
     
    7682      int c = 0;
    7783      // gather parameter values
    78       if (ScaleParameter.Value != null) {
     84      if (HasFixedInverseLengthParameter) {
     85        inverseLength = InverseLengthParameter.Value.ToArray();
     86      } else {
     87        int length = p.Length;
     88        if (!HasFixedScaleParameter) length--;
     89        inverseLength = p.Select(e => 1.0 / Math.Exp(e)).Take(length).ToArray();
     90        c += inverseLength.Length;
     91      }
     92      if (HasFixedScaleParameter) {
    7993        scale = ScaleParameter.Value.Value;
    8094      } else {
    8195        scale = Math.Exp(2 * p[c]);
    8296        c++;
    83       }
    84       if (InverseLengthParameter.Value != null) {
    85         inverseLength = InverseLengthParameter.Value.ToArray();
    86       } else {
    87         inverseLength = p.Skip(1).Select(e => 1.0 / Math.Exp(e)).ToArray();
    88         c += inverseLength.Length;
    8997      }
    9098      if (p.Length != c) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceSquaredExponentialArd", "p");
     
    95103      double[] inverseLength;
    96104      GetParameterValues(p, out scale, out inverseLength);
     105      var fixedInverseLength = HasFixedInverseLengthParameter;
     106      var fixedScale = HasFixedScaleParameter;
    97107      // create functions
    98108      var cov = new ParameterizedCovarianceFunction();
     
    107117        return scale * Math.Exp(-d / 2.0);
    108118      };
    109       cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, inverseLength);
     119      cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, inverseLength, fixedInverseLength, fixedScale);
    110120      return cov;
    111121    }
    112122
    113 
    114     private static IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices, double scale, double[] inverseLength) {
    115       if (columnIndices == null) columnIndices = Enumerable.Range(0, x.GetLength(1));
     123    // order of returned gradients must match the order in GetParameterValues!
     124    private static IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices, double scale, double[] inverseLength,
     125      bool fixedInverseLength, bool fixedScale) {
    116126      double d = i == j
    117127                   ? 0.0
    118128                   : Util.SqrDist(x, i, j, inverseLength, columnIndices);
     129
    119130      int k = 0;
    120       foreach (var columnIndex in columnIndices) {
    121         double sqrDist = Util.SqrDist(x[i, columnIndex] * inverseLength[k], x[j, columnIndex] * inverseLength[k]);
    122         yield return scale * Math.Exp(-d / 2.0) * sqrDist;
    123         k++;
     131      if (!fixedInverseLength) {
     132        foreach (var columnIndex in columnIndices) {
     133          double sqrDist = Util.SqrDist(x[i, columnIndex] * inverseLength[k], x[j, columnIndex] * inverseLength[k]);
     134          yield return scale * Math.Exp(-d / 2.0) * sqrDist;
     135          k++;
     136        }
    124137      }
    125 
    126       yield return 2.0 * scale * Math.Exp(-d / 2.0);
     138      if (!fixedScale) yield return 2.0 * scale * Math.Exp(-d / 2.0);
    127139    }
    128140  }
Note: See TracChangeset for help on using the changeset viewer.