Changeset 10553 for branches/ClassificationModelComparison/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceSquaredExponentialArd.cs
- Timestamp:
- 03/05/14 17:30:38 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ClassificationModelComparison/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceSquaredExponentialArd.cs
r8982 r10553 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-201 2Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 40 40 get { return (IValueParameter<DoubleArray>)Parameters["InverseLength"]; } 41 41 } 42 private bool HasFixedInverseLengthParameter { 43 get { return InverseLengthParameter.Value != null; } 44 } 45 private bool HasFixedScaleParameter { 46 get { return ScaleParameter.Value != null; } 47 } 42 48 43 49 [StorableConstructor] … … 61 67 public int GetNumberOfParameters(int numberOfVariables) { 62 68 return 63 ( ScaleParameter.Value != null? 0 : 1) +64 ( InverseLengthParameter.Value != null? 0 : numberOfVariables);69 (HasFixedScaleParameter ? 0 : 1) + 70 (HasFixedInverseLengthParameter ? 0 : numberOfVariables); 65 71 } 66 72 … … 76 82 int c = 0; 77 83 // 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) { 79 93 scale = ScaleParameter.Value.Value; 80 94 } else { 81 95 scale = Math.Exp(2 * p[c]); 82 96 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;89 97 } 90 98 if (p.Length != c) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceSquaredExponentialArd", "p"); … … 95 103 double[] inverseLength; 96 104 GetParameterValues(p, out scale, out inverseLength); 105 var fixedInverseLength = HasFixedInverseLengthParameter; 106 var fixedScale = HasFixedScaleParameter; 97 107 // create functions 98 108 var cov = new ParameterizedCovarianceFunction(); … … 107 117 return scale * Math.Exp(-d / 2.0); 108 118 }; 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); 110 120 return cov; 111 121 } 112 122 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) { 116 126 double d = i == j 117 127 ? 0.0 118 128 : Util.SqrDist(x, i, j, inverseLength, columnIndices); 129 119 130 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 } 124 137 } 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); 127 139 } 128 140 }
Note: See TracChangeset
for help on using the changeset viewer.