Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/05/14 17:30:38 (11 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/CovarianceRationalQuadraticArd.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.
     
    4545      get { return (IValueParameter<DoubleValue>)Parameters["Shape"]; }
    4646    }
     47    private bool HasFixedScaleParameter {
     48      get { return ScaleParameter.Value != null; }
     49    }
     50    private bool HasFixedInverseLengthParameter {
     51      get { return InverseLengthParameter.Value != null; }
     52    }
     53    private bool HasFixedShapeParameter {
     54      get { return ShapeParameter.Value != null; }
     55    }
    4756
    4857    [StorableConstructor]
     
    7180    public int GetNumberOfParameters(int numberOfVariables) {
    7281      return
    73         (ScaleParameter.Value != null ? 0 : 1) +
    74         (ShapeParameter.Value != null ? 0 : 1) +
    75         (InverseLengthParameter.Value != null ? 0 : numberOfVariables);
     82        (HasFixedScaleParameter ? 0 : 1) +
     83        (HasFixedShapeParameter ? 0 : 1) +
     84        (HasFixedInverseLengthParameter ? 0 : numberOfVariables);
    7685    }
    7786
     
    8897      int c = 0;
    8998      // gather parameter values
    90       if (ScaleParameter.Value != null) {
     99      if (HasFixedInverseLengthParameter) {
     100        inverseLength = InverseLengthParameter.Value.ToArray();
     101      } else {
     102        int length = p.Length;
     103        if (!HasFixedScaleParameter) length--;
     104        if (!HasFixedShapeParameter) length--;
     105        inverseLength = p.Select(e => 1.0 / Math.Exp(e)).Take(length).ToArray();
     106        c += inverseLength.Length;
     107      }
     108      if (HasFixedScaleParameter) {
    91109        scale = ScaleParameter.Value.Value;
    92110      } else {
     
    94112        c++;
    95113      }
    96       if (ShapeParameter.Value != null) {
     114      if (HasFixedShapeParameter) {
    97115        shape = ShapeParameter.Value.Value;
    98116      } else {
    99117        shape = Math.Exp(p[c]);
    100118        c++;
    101       }
    102       if (InverseLengthParameter.Value != null) {
    103         inverseLength = InverseLengthParameter.Value.ToArray();
    104       } else {
    105         inverseLength = p.Skip(2).Select(e => 1.0 / Math.Exp(e)).ToArray();
    106         c += inverseLength.Length;
    107119      }
    108120      if (p.Length != c) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceRationalQuadraticArd", "p");
     
    113125      double[] inverseLength;
    114126      GetParameterValues(p, out scale, out shape, out inverseLength);
     127      var fixedInverseLength = HasFixedInverseLengthParameter;
     128      var fixedScale = HasFixedScaleParameter;
     129      var fixedShape = HasFixedShapeParameter;
    115130      // create functions
    116131      var cov = new ParameterizedCovarianceFunction();
     
    125140        return scale * Math.Pow(1 + 0.5 * d / shape, -shape);
    126141      };
    127       cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, shape, inverseLength);
     142      cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, shape, inverseLength, fixedInverseLength, fixedScale, fixedShape);
    128143      return cov;
    129144    }
    130145
    131     private static IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices, double scale, double shape, double[] inverseLength) {
    132       if (columnIndices == null) columnIndices = Enumerable.Range(0, x.GetLength(1));
     146    private static IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices, double scale, double shape, double[] inverseLength,
     147      bool fixedInverseLength, bool fixedScale, bool fixedShape) {
    133148      double d = i == j
    134149                   ? 0.0
     
    136151      double b = 1 + 0.5 * d / shape;
    137152      int k = 0;
    138       foreach (var columnIndex in columnIndices) {
    139         yield return scale * Math.Pow(b, -shape - 1) * Util.SqrDist(x[i, columnIndex] * inverseLength[k], x[j, columnIndex] * inverseLength[k]);
    140         k++;
     153      if (!fixedInverseLength) {
     154        foreach (var columnIndex in columnIndices) {
     155          yield return
     156            scale * Math.Pow(b, -shape - 1) *
     157            Util.SqrDist(x[i, columnIndex] * inverseLength[k], x[j, columnIndex] * inverseLength[k]);
     158          k++;
     159        }
    141160      }
    142       yield return 2 * scale * Math.Pow(b, -shape);
    143       yield return scale * Math.Pow(b, -shape) * (0.5 * d / b - shape * Math.Log(b));
     161      if (!fixedScale) yield return 2 * scale * Math.Pow(b, -shape);
     162      if (!fixedShape) yield return scale * Math.Pow(b, -shape) * (0.5 * d / b - shape * Math.Log(b));
    144163    }
    145164  }
Note: See TracChangeset for help on using the changeset viewer.