Changeset 10553 for branches/ClassificationModelComparison/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceRationalQuadraticArd.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/CovarianceRationalQuadraticArd.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. … … 45 45 get { return (IValueParameter<DoubleValue>)Parameters["Shape"]; } 46 46 } 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 } 47 56 48 57 [StorableConstructor] … … 71 80 public int GetNumberOfParameters(int numberOfVariables) { 72 81 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); 76 85 } 77 86 … … 88 97 int c = 0; 89 98 // 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) { 91 109 scale = ScaleParameter.Value.Value; 92 110 } else { … … 94 112 c++; 95 113 } 96 if ( ShapeParameter.Value != null) {114 if (HasFixedShapeParameter) { 97 115 shape = ShapeParameter.Value.Value; 98 116 } else { 99 117 shape = Math.Exp(p[c]); 100 118 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;107 119 } 108 120 if (p.Length != c) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceRationalQuadraticArd", "p"); … … 113 125 double[] inverseLength; 114 126 GetParameterValues(p, out scale, out shape, out inverseLength); 127 var fixedInverseLength = HasFixedInverseLengthParameter; 128 var fixedScale = HasFixedScaleParameter; 129 var fixedShape = HasFixedShapeParameter; 115 130 // create functions 116 131 var cov = new ParameterizedCovarianceFunction(); … … 125 140 return scale * Math.Pow(1 + 0.5 * d / shape, -shape); 126 141 }; 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); 128 143 return cov; 129 144 } 130 145 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) { 133 148 double d = i == j 134 149 ? 0.0 … … 136 151 double b = 1 + 0.5 * d / shape; 137 152 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 } 141 160 } 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)); 144 163 } 145 164 }
Note: See TracChangeset
for help on using the changeset viewer.