Changeset 8416 for trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceProd.cs
- Timestamp:
- 08/06/12 15:02:34 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceProd.cs
r8323 r8416 1 using System.Collections.Generic; 1 #region License Information 2 /* HeuristicLab 3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 * 5 * This file is part of HeuristicLab. 6 * 7 * HeuristicLab is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * 12 * HeuristicLab is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>. 19 */ 20 #endregion 21 2 22 using System.Linq; 23 using HeuristicLab.Common; 24 using HeuristicLab.Core; 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 3 26 4 namespace HeuristicLab.Algorithms.DataAnalysis.GaussianProcess { 5 public class CovarianceProd : ICovarianceFunction { 6 private IList<ICovarianceFunction> covariances; 27 namespace HeuristicLab.Algorithms.DataAnalysis { 28 [StorableClass] 29 [Item(Name = "CovarianceProd", 30 Description = "Product covariance function for Gaussian processes.")] 31 public class CovarianceProd : Item, ICovarianceFunction { 32 [Storable] 33 private ItemList<ICovarianceFunction> factors; 7 34 8 public int NumberOfParameters { 9 get { return covariances.Sum(c => c.NumberOfParameters); } 35 [Storable] 36 private int numberOfVariables; 37 public ItemList<ICovarianceFunction> Factors { 38 get { return factors; } 10 39 } 11 40 12 public CovarianceProd(IEnumerable<ICovarianceFunction> covariances) { 13 this.covariances = covariances.ToList(); 41 [StorableConstructor] 42 protected CovarianceProd(bool deserializing) 43 : base(deserializing) { 14 44 } 15 45 16 public void SetMatrix(double[,] x) { 17 foreach (var covariance in covariances) { 18 covariance.SetMatrix(x, x); 46 protected CovarianceProd(CovarianceProd original, Cloner cloner) 47 : base(original, cloner) { 48 this.factors = cloner.Clone(original.factors); 49 this.numberOfVariables = original.numberOfVariables; 50 } 51 52 public CovarianceProd() 53 : base() { 54 this.factors = new ItemList<ICovarianceFunction>(); 55 } 56 57 public override IDeepCloneable Clone(Cloner cloner) { 58 return new CovarianceProd(this, cloner); 59 } 60 61 public int GetNumberOfParameters(int numberOfVariables) { 62 this.numberOfVariables = numberOfVariables; 63 return factors.Select(t => t.GetNumberOfParameters(numberOfVariables)).Sum(); 64 } 65 66 public void SetParameter(double[] hyp) { 67 int offset = 0; 68 foreach (var t in factors) { 69 var numberOfParameters = t.GetNumberOfParameters(numberOfVariables); 70 t.SetParameter(hyp.Skip(offset).Take(numberOfParameters).ToArray()); 71 offset += numberOfParameters; 19 72 } 20 73 } 21 22 public void SetMatrix(double[,] x, double[,] xt) { 23 foreach (var covariance in covariances) { 24 covariance.SetMatrix(x, xt); 25 } 74 public void SetData(double[,] x) { 75 SetData(x, x); 26 76 } 27 77 28 public void SetHyperparamter(double[] hyp) { 29 int i = 0; 30 foreach (var covariance in covariances) { 31 int n = covariance.NumberOfParameters; 32 covariance.SetHyperparamter(hyp.Skip(i).Take(n).ToArray()); 33 i += n; 78 public void SetData(double[,] x, double[,] xt) { 79 foreach (var t in factors) { 80 t.SetData(x, xt); 34 81 } 35 82 } 36 83 37 84 public double GetCovariance(int i, int j) { 38 return covariances.Select(c => c.GetCovariance(i, j)) 39 .Aggregate((a, b) => a * b); 85 return factors.Select(t => t.GetCovariance(i, j)).Aggregate((a, b) => a * b); 40 86 } 41 87 42 43 public double[] GetDiagonalCovariances() { 44 return covariances 45 .Select(c => c.GetDiagonalCovariances()) 46 .Aggregate((s, d) => s.Zip(d, (a, b) => a * b).ToArray()) 47 .ToArray(); 48 } 49 50 public double[] GetDerivatives(int i, int j) { 51 return covariances 52 .Select(c => c.GetDerivatives(i, j)) 53 .Aggregate(Enumerable.Empty<double>(), (h0, h1) => h0.Concat(h1)) 54 .ToArray(); 88 public double[] GetGradient(int i, int j) { 89 return factors.Select(t => t.GetGradient(i, j)).SelectMany(seq => seq).ToArray(); 55 90 } 56 91 }
Note: See TracChangeset
for help on using the changeset viewer.