Changeset 8982 for trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanFunctions
- Timestamp:
- 12/01/12 19:02:47 (12 years ago)
- Location:
- trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanFunctions
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanFunctions/MeanConst.cs
r8929 r8982 21 21 22 22 using System; 23 using System.Collections.Generic; 23 24 using System.Linq; 24 25 using HeuristicLab.Common; 25 26 using HeuristicLab.Core; 26 27 using HeuristicLab.Data; 28 using HeuristicLab.Parameters; 27 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 30 … … 31 33 [Item(Name = "MeanConst", Description = "Constant mean function for Gaussian processes.")] 32 34 public sealed class MeanConst : ParameterizedNamedItem, IMeanFunction { 33 [Storable] 34 private double c; 35 [Storable] 36 private readonly HyperParameter<DoubleValue> valueParameter; 37 public IValueParameter<DoubleValue> ValueParameter { get { return valueParameter; } } 35 public IValueParameter<DoubleValue> ValueParameter { 36 get { return (IValueParameter<DoubleValue>)Parameters["Value"]; } 37 } 38 38 39 39 [StorableConstructor] … … 41 41 private MeanConst(MeanConst original, Cloner cloner) 42 42 : base(original, cloner) { 43 this.c = original.c;44 this.valueParameter = cloner.Clone(original.valueParameter);45 RegisterEvents();46 43 } 47 44 public MeanConst() … … 50 47 this.description = ItemDescription; 51 48 52 this.valueParameter = new HyperParameter<DoubleValue>("Value", "The constant value for the constant mean function."); 53 Parameters.Add(valueParameter); 54 RegisterEvents(); 49 Parameters.Add(new OptionalValueParameter<DoubleValue>("Value", "The constant value for the constant mean function.")); 55 50 } 56 51 … … 59 54 } 60 55 61 [StorableHook(HookType.AfterDeserialization)] 62 private void AfterDeserialization() { 63 RegisterEvents(); 56 public int GetNumberOfParameters(int numberOfVariables) { 57 return ValueParameter.Value != null ? 0 : 1; 64 58 } 65 59 66 private void RegisterEvents() { 67 Util.AttachValueChangeHandler<DoubleValue, double>(valueParameter, () => { c = valueParameter.Value.Value; }); 60 public void SetParameter(double[] p) { 61 double c; 62 GetParameters(p, out c); 63 ValueParameter.Value = new DoubleValue(c); 68 64 } 69 65 70 public int GetNumberOfParameters(int numberOfVariables) { 71 return valueParameter.Fixed ? 0 : 1; 66 private void GetParameters(double[] p, out double c) { 67 if (ValueParameter.Value == null) { 68 c = p[0]; 69 } else { 70 if (p.Length > 0) 71 throw new ArgumentException( 72 "The length of the parameter vector does not match the number of free parameters for the constant mean function.", 73 "p"); 74 c = ValueParameter.Value.Value; 75 } 72 76 } 73 77 74 public void SetParameter(double[] hyp) { 75 if (!valueParameter.Fixed) { 76 valueParameter.SetValue(new DoubleValue(hyp[0])); 77 } else if (hyp.Length > 0) 78 throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for the constant mean function.", "hyp"); 79 } 80 81 public double[] GetMean(double[,] x) { 82 return Enumerable.Repeat(c, x.GetLength(0)).ToArray(); 83 } 84 85 public double[] GetGradients(int k, double[,] x) { 86 if (k > 0) throw new ArgumentException(); 87 return Enumerable.Repeat(1.0, x.GetLength(0)).ToArray(); 78 public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, IEnumerable<int> columnIndices) { 79 double c; 80 GetParameters(p, out c); 81 var mf = new ParameterizedMeanFunction(); 82 mf.Mean = (x, i) => c; 83 mf.Gradient = (x, i, k) => { 84 if (k > 0) throw new ArgumentException(); 85 return 1.0; 86 }; 87 return mf; 88 88 } 89 89 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanFunctions/MeanLinear.cs
r8929 r8982 21 21 22 22 using System; 23 using System.Collections.Generic; 23 24 using System.Linq; 24 25 using HeuristicLab.Common; 25 26 using HeuristicLab.Core; 26 27 using HeuristicLab.Data; 28 using HeuristicLab.Parameters; 27 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 30 … … 31 33 [Item(Name = "MeanLinear", Description = "Linear mean function for Gaussian processes.")] 32 34 public sealed class MeanLinear : ParameterizedNamedItem, IMeanFunction { 33 [Storable] 34 private double[] weights; 35 [Storable] 36 private readonly HyperParameter<DoubleArray> weightsParameter; 37 public IValueParameter<DoubleArray> WeightsParameter { get { return weightsParameter; } } 35 public IValueParameter<DoubleArray> WeightsParameter { 36 get { return (IValueParameter<DoubleArray>)Parameters["Weights"]; } 37 } 38 38 39 39 [StorableConstructor] … … 41 41 private MeanLinear(MeanLinear original, Cloner cloner) 42 42 : base(original, cloner) { 43 if (original.weights != null) {44 this.weights = new double[original.weights.Length];45 Array.Copy(original.weights, weights, original.weights.Length);46 }47 weightsParameter = cloner.Clone(original.weightsParameter);48 RegisterEvents();49 43 } 50 44 public MeanLinear() 51 45 : base() { 52 this.weightsParameter = new HyperParameter<DoubleArray>("Weights", "The weights parameter for the linear mean function."); 53 Parameters.Add(weightsParameter); 54 RegisterEvents(); 46 Parameters.Add(new OptionalValueParameter<DoubleArray>("Weights", "The weights parameter for the linear mean function.")); 55 47 } 56 48 … … 59 51 } 60 52 61 [StorableHook(HookType.AfterDeserialization)] 62 private void AfterDeserialization() { 63 RegisterEvents(); 53 public int GetNumberOfParameters(int numberOfVariables) { 54 return WeightsParameter.Value != null ? 0 : numberOfVariables; 64 55 } 65 56 66 p rivate void RegisterEvents() {67 Util.AttachArrayChangeHandler<DoubleArray, double>(weightsParameter, () => {68 weights = weightsParameter.Value.ToArray();69 });57 public void SetParameter(double[] p) { 58 double[] weights; 59 GetParameter(p, out weights); 60 WeightsParameter.Value = new DoubleArray(weights); 70 61 } 71 62 72 public int GetNumberOfParameters(int numberOfVariables) { 73 return weightsParameter.Fixed ? 0 : numberOfVariables; 63 public void GetParameter(double[] p, out double[] weights) { 64 if (WeightsParameter.Value == null) { 65 weights = p; 66 } else { 67 if (p.Length != 0) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for the linear mean function.", "p"); 68 weights = WeightsParameter.Value.ToArray(); 69 } 74 70 } 75 71 76 public void SetParameter(double[] hyp) { 77 if (!weightsParameter.Fixed) { 78 weightsParameter.SetValue(new DoubleArray(hyp)); 79 } else if (hyp.Length != 0) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for the linear mean function.", "hyp"); 80 } 81 82 public double[] GetMean(double[,] x) { 83 // sanity check 84 if (weights.Length != x.GetLength(1)) throw new ArgumentException("The number of hyperparameters must match the number of variables for the linear mean function."); 85 int cols = x.GetLength(1); 86 int n = x.GetLength(0); 87 return (from i in Enumerable.Range(0, n) 88 let rowVector = Enumerable.Range(0, cols).Select(j => x[i, j]) 89 select Util.ScalarProd(weights, rowVector)) 90 .ToArray(); 91 } 92 93 public double[] GetGradients(int k, double[,] x) { 94 int cols = x.GetLength(1); 95 int n = x.GetLength(0); 96 if (k > cols) throw new ArgumentException(); 97 return (Enumerable.Range(0, n).Select(r => x[r, k])).ToArray(); 72 public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, IEnumerable<int> columnIndices) { 73 double[] weights; 74 int[] columns = columnIndices.ToArray(); 75 GetParameter(p, out weights); 76 var mf = new ParameterizedMeanFunction(); 77 mf.Mean = (x, i) => { 78 // sanity check 79 if (weights.Length != columns.Length) throw new ArgumentException("The number of rparameters must match the number of variables for the linear mean function."); 80 return Util.ScalarProd(weights, Util.GetRow(x, i, columns)); 81 }; 82 mf.Gradient = (x, i, k) => { 83 if (k > columns.Length) throw new ArgumentException(); 84 return x[i, columns[k]]; 85 }; 86 return mf; 98 87 } 99 88 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanFunctions/MeanProduct.cs
r8929 r8982 19 19 */ 20 20 #endregion 21 22 using System.Collections.Generic; 21 23 using System.Linq; 22 24 using HeuristicLab.Common; … … 61 63 } 62 64 63 public void SetParameter(double[] hyp) {65 public void SetParameter(double[] p) { 64 66 int offset = 0; 65 67 foreach (var t in factors) { 66 68 var numberOfParameters = t.GetNumberOfParameters(numberOfVariables); 67 t.SetParameter( hyp.Skip(offset).Take(numberOfParameters).ToArray());69 t.SetParameter(p.Skip(offset).Take(numberOfParameters).ToArray()); 68 70 offset += numberOfParameters; 69 71 } 70 72 } 71 73 72 public double[] GetMean(double[,] x) {73 var res = factors.First().GetMean(x);74 foreach (var t in factors.Skip(1)) {75 var a = t.GetMean(x);76 for (int i = 0; i < res.Length; i++) res[i] *= a[i];77 }78 return res;79 }80 74 81 public double[] GetGradients(int k, double[,] x) { 82 double[] res = Enumerable.Repeat(1.0, x.GetLength(0)).ToArray(); 83 // find index of factor for the given k 84 int j = 0; 85 while (k >= factors[j].GetNumberOfParameters(numberOfVariables)) { 86 k -= factors[j].GetNumberOfParameters(numberOfVariables); 87 j++; 88 } 89 for (int i = 0; i < factors.Count; i++) { 90 var f = factors[i]; 91 if (i == j) { 92 // multiply gradient 93 var g = f.GetGradients(k, x); 94 for (int ii = 0; ii < res.Length; ii++) res[ii] *= g[ii]; 95 } else { 96 // multiply mean 97 var m = f.GetMean(x); 98 for (int ii = 0; ii < res.Length; ii++) res[ii] *= m[ii]; 75 public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, IEnumerable<int> columnIndices) { 76 var factorMf = new List<ParameterizedMeanFunction>(); 77 int totalNumberOfParameters = GetNumberOfParameters(numberOfVariables); 78 int[] factorIndexMap = new int[totalNumberOfParameters]; // maps k-th hyperparameter to the correct mean-term 79 int[] hyperParameterIndexMap = new int[totalNumberOfParameters]; // maps k-th hyperparameter to the l-th hyperparameter of the correct mean-term 80 int c = 0; 81 // get the parameterized mean function for each term 82 for (int factorIndex = 0; factorIndex < factors.Count; factorIndex++) { 83 var numberOfParameters = factors[factorIndex].GetNumberOfParameters(numberOfVariables); 84 factorMf.Add(factors[factorIndex].GetParameterizedMeanFunction(p.Take(numberOfParameters).ToArray(), columnIndices)); 85 p = p.Skip(numberOfParameters).ToArray(); 86 87 for (int hyperParameterIndex = 0; hyperParameterIndex < numberOfParameters; hyperParameterIndex++) { 88 factorIndexMap[c] = factorIndex; 89 hyperParameterIndexMap[c] = hyperParameterIndex; 90 c++; 99 91 } 100 92 } 101 return res; 93 94 var mf = new ParameterizedMeanFunction(); 95 mf.Mean = (x, i) => factorMf.Select(t => t.Mean(x, i)).Aggregate((a, b) => a * b); 96 mf.Gradient = (x, i, k) => { 97 double result = 1.0; 98 int hyperParameterFactorIndex = factorIndexMap[k]; 99 for (int factorIndex = 0; factorIndex < factors.Count; factorIndex++) { 100 if (factorIndex == hyperParameterFactorIndex) { 101 // multiply gradient 102 result *= factorMf[factorIndex].Gradient(x, i, hyperParameterIndexMap[k]); 103 } else { 104 // multiply mean 105 result *= factorMf[factorIndex].Mean(x, i); 106 } 107 } 108 return result; 109 }; 110 return mf; 102 111 } 103 112 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanFunctions/MeanSum.cs
r8929 r8982 19 19 */ 20 20 #endregion 21 22 using System.Collections.Generic; 21 23 using System.Linq; 22 24 using HeuristicLab.Common; … … 57 59 } 58 60 59 public void SetParameter(double[] hyp) {61 public void SetParameter(double[] p) { 60 62 int offset = 0; 61 63 foreach (var t in terms) { 62 64 var numberOfParameters = t.GetNumberOfParameters(numberOfVariables); 63 t.SetParameter( hyp.Skip(offset).Take(numberOfParameters).ToArray());65 t.SetParameter(p.Skip(offset).Take(numberOfParameters).ToArray()); 64 66 offset += numberOfParameters; 65 67 } 66 68 } 67 69 68 public double[] GetMean(double[,] x) { 69 var res = terms.First().GetMean(x); 70 foreach (var t in terms.Skip(1)) { 71 var a = t.GetMean(x); 72 for (int i = 0; i < res.Length; i++) res[i] += a[i]; 70 public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, IEnumerable<int> columnIndices) { 71 var termMf = new List<ParameterizedMeanFunction>(); 72 int totalNumberOfParameters = GetNumberOfParameters(numberOfVariables); 73 int[] termIndexMap = new int[totalNumberOfParameters]; // maps k-th parameter to the correct mean-term 74 int[] hyperParameterIndexMap = new int[totalNumberOfParameters]; // maps k-th parameter to the l-th parameter of the correct mean-term 75 int c = 0; 76 // get the parameterized mean function for each term 77 for (int termIndex = 0; termIndex < terms.Count; termIndex++) { 78 var numberOfParameters = terms[termIndex].GetNumberOfParameters(numberOfVariables); 79 termMf.Add(terms[termIndex].GetParameterizedMeanFunction(p.Take(numberOfParameters).ToArray(), columnIndices)); 80 p = p.Skip(numberOfParameters).ToArray(); 81 82 for (int hyperParameterIndex = 0; hyperParameterIndex < numberOfParameters; hyperParameterIndex++) { 83 termIndexMap[c] = termIndex; 84 hyperParameterIndexMap[c] = hyperParameterIndex; 85 c++; 86 } 73 87 } 74 return res;75 }76 88 77 public double[] GetGradients(int k, double[,] x) { 78 int i = 0; 79 while (k >= terms[i].GetNumberOfParameters(numberOfVariables)) { 80 k -= terms[i].GetNumberOfParameters(numberOfVariables); 81 i++; 82 } 83 return terms[i].GetGradients(k, x); 89 var mf = new ParameterizedMeanFunction(); 90 mf.Mean = (x, i) => termMf.Select(t => t.Mean(x, i)).Sum(); 91 mf.Gradient = (x, i, k) => { 92 return termMf[termIndexMap[k]].Gradient(x, i, hyperParameterIndexMap[k]); 93 }; 94 return mf; 84 95 } 85 96 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanFunctions/MeanZero.cs
r8929 r8982 20 20 #endregion 21 21 using System; 22 using System.Collections.Generic; 22 23 using System.Linq; 23 24 using HeuristicLab.Common; … … 45 46 } 46 47 47 public void SetParameter(double[] hyp) {48 if ( hyp.Length > 0) throw new ArgumentException("No hyper-parameters allowed for zero mean function.", "hyp");48 public void SetParameter(double[] p) { 49 if (p.Length > 0) throw new ArgumentException("No parameters allowed for zero mean function.", "p"); 49 50 } 50 51 51 public double[] GetMean(double[,] x) { 52 return Enumerable.Repeat(0.0, x.GetLength(0)).ToArray(); 53 } 54 55 public double[] GetGradients(int k, double[,] x) { 56 if (k > 0) throw new ArgumentException(); 57 return Enumerable.Repeat(0.0, x.GetLength(0)).ToArray(); 52 public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, IEnumerable<int> columnIndices) { 53 if (p.Length > 0) throw new ArgumentException("No parameters allowed for zero mean function.", "p"); 54 var mf = new ParameterizedMeanFunction(); 55 mf.Mean = (x, i) => 0.0; 56 mf.Gradient = (x, i, k) => { 57 if (k > 0) 58 throw new ArgumentException(); 59 return 0.0; 60 }; 61 return mf; 58 62 } 59 63 }
Note: See TracChangeset
for help on using the changeset viewer.