Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/20/14 17:45:06 (11 years ago)
Author:
gkronber
Message:

#2125 fixed the bug that covariance functions returned the full gradient vector even when parameters are partially fixed.
changed the calculation of NN covariance and gradient to direct calculation (instead of AutoDiff)

File:
1 edited

Legend:

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

    r9456 r10489  
    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 (InverseLengthParameter.Value != null) {
     84      if (HasFixedInverseLengthParameter) {
    7985        inverseLength = InverseLengthParameter.Value.ToArray();
    8086      } else {
    8187        int length = p.Length;
    82         if (ScaleParameter.Value == null) length--;
     88        if (HasFixedScaleParameter) length--;
    8389        inverseLength = p.Select(e => 1.0 / Math.Exp(e)).Take(length).ToArray();
    8490        c += inverseLength.Length;
    8591      }
    86       if (ScaleParameter.Value != null) {
     92      if (HasFixedScaleParameter) {
    8793        scale = ScaleParameter.Value.Value;
    8894      } else {
     
    97103      double[] inverseLength;
    98104      GetParameterValues(p, out scale, out inverseLength);
     105      var fixedInverseLength = HasFixedInverseLengthParameter;
     106      var fixedScale = HasFixedScaleParameter;
    99107      // create functions
    100108      var cov = new ParameterizedCovarianceFunction();
     
    109117        return scale * Math.Exp(-d / 2.0);
    110118      };
    111       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);
    112120      return cov;
    113121    }
    114122
    115123    // order of returned gradients must match the order in GetParameterValues!
    116     private static IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices, double scale, double[] inverseLength) {
     124    private static IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices, double scale, double[] inverseLength,
     125      bool fixedInverseLength, bool fixedScale) {
    117126      double d = i == j
    118127                   ? 0.0
     
    120129
    121130      int k = 0;
    122       foreach (var columnIndex in columnIndices) {
    123         double sqrDist = Util.SqrDist(x[i, columnIndex] * inverseLength[k], x[j, columnIndex] * inverseLength[k]);
    124         yield return scale * Math.Exp(-d / 2.0) * sqrDist;
    125         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        }
    126137      }
    127       yield return 2.0 * scale * Math.Exp(-d / 2.0);
     138      if (!fixedScale) yield return 2.0 * scale * Math.Exp(-d / 2.0);
    128139    }
    129140  }
Note: See TracChangeset for help on using the changeset viewer.