Changeset 8473 for trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4
- Timestamp:
- 08/10/12 20:42:30 (12 years ago)
- Location:
- trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4
- Files:
-
- 1 added
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceConst.cs
r8464 r8473 32 32 [Storable] 33 33 private double sf2; 34 public double Scale { get { return sf2; } } 34 35 35 36 [StorableConstructor] … … 56 57 57 58 public void SetParameter(double[] hyp) { 58 this.sf2 = Math. Min(1E6, Math.Exp(2 * hyp[0])); // upper limit for scale59 this.sf2 = Math.Exp(2 * hyp[0]); 59 60 } 60 61 public void SetData(double[,] x) { -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceNoise.cs
r8464 r8473 32 32 [Storable] 33 33 private double sf2; 34 public double Scale { get { return sf2; } } 34 35 35 36 [StorableConstructor] … … 56 57 57 58 public void SetParameter(double[] hyp) { 58 this.sf2 = Math. Min(1E6, Math.Exp(2 * hyp[0])); // upper limit for scale59 this.sf2 = Math.Exp(2 * hyp[0]); 59 60 } 60 61 public void SetData(double[,] x) { -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovariancePeriodic.cs
r8463 r8473 35 35 [Storable] 36 36 private double sf2; 37 public double Scale { get { return sf2; } } 37 38 [Storable] 38 39 private double l; 40 public double Length { get { return l; } } 39 41 [Storable] 40 42 private double p; 43 public double Period { get { return p; } } 41 44 42 45 private bool symmetric; … … 74 77 this.p = Math.Exp(hyp[1]); 75 78 this.sf2 = Math.Exp(2 * hyp[2]); 76 77 sf2 = Math.Min(10E6, sf2); // upper limit for the scale 79 // sf2 = Math.Min(10E6, sf2); // upper limit for the scale 78 80 79 81 sd = null; -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceSEard.cs
r8455 r8473 36 36 [Storable] 37 37 private double sf2; 38 public double Scale { get { return sf2; } } 39 38 40 [Storable] 39 41 private double[] l; 42 public double[] Length { 43 get { 44 if (l == null) return new double[0]; 45 var copy = new double[l.Length]; 46 Array.Copy(l, copy, copy.Length); 47 return copy; 48 } 49 } 40 50 41 51 private double[,] sd; … … 76 86 this.l = hyp.Take(hyp.Length - 1).Select(Math.Exp).ToArray(); 77 87 this.sf2 = Math.Exp(2 * hyp[hyp.Length - 1]); 78 sf2 = Math.Min(10E6, sf2); // upper limit for the scale88 // sf2 = Math.Min(10E6, sf2); // upper limit for the scale 79 89 80 90 sd = null; -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceSEiso.cs
r8463 r8473 37 37 [Storable] 38 38 private double sf2; 39 public double Scale { get { return sf2; } } 39 40 [Storable] 40 41 private double l; 42 public double Length { get { return l; } } 41 43 [Storable] 42 44 private bool symmetric; … … 80 82 public void SetParameter(double[] hyp) { 81 83 this.l = Math.Exp(hyp[0]); 82 this.sf2 = Math. Min(1E6, Math.Exp(2 * hyp[1])); // upper limit for scale84 this.sf2 = Math.Exp(2 * hyp[1]); 83 85 sd = null; 84 86 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/GaussianProcessModel.cs
r8463 r8473 74 74 [Storable] 75 75 private Scaling inputScaling; 76 [Storable]77 private Scaling targetScaling;78 76 79 77 … … 85 83 this.covarianceFunction = cloner.Clone(original.covarianceFunction); 86 84 this.inputScaling = cloner.Clone(original.inputScaling); 87 this.targetScaling = cloner.Clone(original.targetScaling);88 85 this.negativeLogLikelihood = original.negativeLogLikelihood; 89 86 this.targetVariable = original.targetVariable; … … 106 103 this.allowedInputVariables = allowedInputVariables.ToArray(); 107 104 108 sqrSigmaNoise = Math.Exp(2.0 * hyp.First());109 sqrSigmaNoise = Math.Max(10E-6, sqrSigmaNoise); // lower limit for the noise level110 105 111 106 int nVariables = this.allowedInputVariables.Length; 112 this.meanFunction.SetParameter(hyp .Skip(1)107 this.meanFunction.SetParameter(hyp 113 108 .Take(this.meanFunction.GetNumberOfParameters(nVariables)) 114 109 .ToArray()); 115 this.covarianceFunction.SetParameter(hyp.Skip( 1 +this.meanFunction.GetNumberOfParameters(nVariables))110 this.covarianceFunction.SetParameter(hyp.Skip(this.meanFunction.GetNumberOfParameters(nVariables)) 116 111 .Take(this.covarianceFunction.GetNumberOfParameters(nVariables)) 117 112 .ToArray()); 113 sqrSigmaNoise = Math.Exp(2.0 * hyp.Last()); 118 114 119 115 CalculateModel(ds, rows); … … 123 119 inputScaling = new Scaling(ds, allowedInputVariables, rows); 124 120 x = AlglibUtil.PrepareAndScaleInputMatrix(ds, allowedInputVariables, rows, inputScaling); 125 126 127 targetScaling = new Scaling(ds, new string[] { targetVariable }, rows); 128 var y = targetScaling.GetScaledValues(ds, targetVariable, rows); 121 var y = ds.GetDoubleValues(targetVariable, rows); 129 122 130 123 int n = x.GetLength(0); … … 149 142 150 143 var res = alglib.trfac.spdmatrixcholesky(ref l, n, false); 151 if (!res) throw new InvalidOperationException("Matrix is not positive semidefinite");144 if (!res) throw new ArgumentException("Matrix is not positive semidefinite"); 152 145 153 146 // calculate sum of diagonal elements for likelihood … … 198 191 } 199 192 200 return new double[] { noiseGradient } 201 .Concat(meanGradients) 202 .Concat(covGradients).ToArray(); 193 return 194 meanGradients 195 .Concat(covGradients) 196 .Concat(new double[] { noiseGradient }).ToArray(); 203 197 } 204 198 … … 231 225 // var kss = new double[newN]; 232 226 var Ks = new double[newN, n]; 233 double[,] sWKs = new double[n, newN];227 //double[,] sWKs = new double[n, newN]; 234 228 // double[,] v; 235 229 … … 243 237 var ms = meanFunction.GetMean(newX); 244 238 for (int i = 0; i < newN; i++) { 245 246 239 for (int j = 0; j < n; j++) { 247 240 Ks[i, j] = covarianceFunction.GetCovariance(j, i); 248 sWKs[j, i] = Ks[i, j] / Math.Sqrt(sqrSigmaNoise);241 //sWKs[j, i] = Ks[i, j] / Math.Sqrt(sqrSigmaNoise); 249 242 } 250 243 } … … 253 246 // alglib.rmatrixsolvem(l, n, sWKs, newN, true, out info, out denseSolveRep, out v); 254 247 255 double targetScaleMin, targetScaleMax;256 targetScaling.GetScalingParameters(targetVariable, out targetScaleMin, out targetScaleMax);257 248 return Enumerable.Range(0, newN) 258 .Select(i => ms[i] + Util.ScalarProd(Util.GetRow(Ks, i), alpha)) 259 .Select(m => m * (targetScaleMax - targetScaleMin) + targetScaleMin); 249 .Select(i => ms[i] + Util.ScalarProd(Util.GetRow(Ks, i), alpha)); 260 250 //for (int i = 0; i < newN; i++) { 261 251 // // predMean[i] = ms[i] + prod(GetRow(Ks, i), alpha); … … 265 255 266 256 } 257 258 public IEnumerable<double> GetEstimatedVariance(Dataset dataset, IEnumerable<int> rows) { 259 var newX = AlglibUtil.PrepareAndScaleInputMatrix(dataset, allowedInputVariables, rows, inputScaling); 260 int newN = newX.GetLength(0); 261 int n = x.GetLength(0); 262 263 var kss = new double[newN]; 264 double[,] sWKs = new double[n, newN]; 265 266 267 // for stddev 268 covarianceFunction.SetData(newX); 269 for (int i = 0; i < newN; i++) 270 kss[i] = covarianceFunction.GetCovariance(i, i); 271 272 covarianceFunction.SetData(x, newX); 273 for (int i = 0; i < n; i++) { 274 for (int j = 0; j < newN; j++) { 275 sWKs[i, j] = covarianceFunction.GetCovariance(i, j) / Math.Sqrt(sqrSigmaNoise); 276 } 277 } 278 279 // for stddev 280 int info; 281 alglib.densesolverreport denseSolveRep; 282 double[,] v; 283 double[,] lTrans = new double[l.GetLength(1), l.GetLength(0)]; 284 for (int i = 0; i < lTrans.GetLength(0); i++) 285 for (int j = 0; j < lTrans.GetLength(1); j++) 286 lTrans[i, j] = l[j, i]; 287 alglib.rmatrixsolvem(lTrans, n, sWKs, newN, true, out info, out denseSolveRep, out v); // not working! 288 // alglib.spdmatrixcholeskysolvem(lTrans, n, true, sWKs, newN, out info, out denseSolveRep, out v); 289 290 for (int i = 0; i < newN; i++) { 291 var sumV2 = Util.ScalarProd(Util.GetCol(v, i), Util.GetCol(v, i)); 292 yield return kss[i] - sumV2; 293 } 294 } 267 295 } 268 296 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/GaussianProcessRegressionModelCreator.cs
r8401 r8473 20 20 #endregion 21 21 22 using System; 22 23 using System.Linq; 23 24 using HeuristicLab.Common; … … 60 61 61 62 public override IOperation Apply() { 62 var model = Create(ProblemData, Hyperparameter.ToArray(), MeanFunction, CovarianceFunction); 63 ModelParameter.ActualValue = model; 64 NegativeLogLikelihoodParameter.ActualValue = new DoubleValue(model.NegativeLogLikelihood); 65 HyperparameterGradientsParameter.ActualValue = new RealVector(model.GetHyperparameterGradients()); 63 try { 64 var model = Create(ProblemData, Hyperparameter.ToArray(), MeanFunction, CovarianceFunction); 65 ModelParameter.ActualValue = model; 66 NegativeLogLikelihoodParameter.ActualValue = new DoubleValue(model.NegativeLogLikelihood); 67 HyperparameterGradientsParameter.ActualValue = new RealVector(model.GetHyperparameterGradients()); 68 return base.Apply(); 69 } 70 catch (ArgumentException) { } 71 catch (alglib.alglibexception) { } 72 NegativeLogLikelihoodParameter.ActualValue = new DoubleValue(1E300); 73 HyperparameterGradientsParameter.ActualValue = new RealVector(Hyperparameter.Count()); 66 74 return base.Apply(); 67 75 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/GaussianProcessRegressionSolution.cs
r8371 r8473 20 20 #endregion 21 21 22 using System.Collections.Generic; 23 using System.Linq; 22 24 using HeuristicLab.Common; 23 25 using HeuristicLab.Core; … … 51 53 return new GaussianProcessRegressionSolution(this, cloner); 52 54 } 55 56 public IEnumerable<double> EstimatedVariance { 57 get { return GetEstimatedVariance(Enumerable.Range(0, ProblemData.Dataset.Rows)); } 58 } 59 public IEnumerable<double> EstimatedTrainingVariance { 60 get { return GetEstimatedVariance(ProblemData.TrainingIndices); } 61 } 62 public IEnumerable<double> EstimatedTestVariance { 63 get { return GetEstimatedVariance(ProblemData.TestIndices); } 64 } 65 66 public IEnumerable<double> GetEstimatedVariance(IEnumerable<int> rows) { 67 return Model.GetEstimatedVariance(ProblemData.Dataset, rows); 68 } 53 69 } 54 70 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanConst.cs
r8416 r8473 32 32 [Storable] 33 33 private double c; 34 public double Value { get { return c; } } 35 34 36 public int GetNumberOfParameters(int numberOfVariables) { 35 37 return 1; -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanLinear.cs
r8416 r8473 31 31 [Storable] 32 32 private double[] alpha; 33 public double[] Weights { 34 get { 35 if (alpha == null) return new double[0]; 36 var copy = new double[alpha.Length]; 37 Array.Copy(alpha, copy, copy.Length); 38 return copy; 39 } 40 } 33 41 public int GetNumberOfParameters(int numberOfVariables) { 34 42 return numberOfVariables; -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/HeuristicLab.Algorithms.DataAnalysis-3.4.csproj
r8471 r8473 120 120 </Compile> 121 121 <Compile Include="FixedDataAnalysisAlgorithm.cs" /> 122 <Compile Include="GaussianProcess\CovarianceRQiso.cs" /> 122 123 <Compile Include="GaussianProcess\CovarianceNoise.cs" /> 123 124 <Compile Include="GaussianProcess\CovarianceConst.cs" /> -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/Interfaces/IGaussianProcessModel.cs
r8416 r8473 20 20 #endregion 21 21 22 using System.Collections.Generic; 22 23 using HeuristicLab.Problems.DataAnalysis; 23 24 … … 31 32 ICovarianceFunction CovarianceFunction { get; } 32 33 double[] GetHyperparameterGradients(); 34 35 IEnumerable<double> GetEstimatedVariance(Dataset ds, IEnumerable<int> rows); 33 36 } 34 37 }
Note: See TracChangeset
for help on using the changeset viewer.