- Timestamp:
- 04/14/17 17:53:30 (8 years ago)
- Location:
- branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4
- Files:
-
- 7 deleted
- 12 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/HeuristicLab.Algorithms.DataAnalysis-3.4.csproj
r14870 r14872 369 369 <Compile Include="Plugin.cs" /> 370 370 <Compile Include="Properties\AssemblyInfo.cs" /> 371 <Compile Include="RadialBasisFunctions\Distances\AngularDistance.cs" />372 <Compile Include="RadialBasisFunctions\Distances\DistanceBase.cs" />373 <Compile Include="RadialBasisFunctions\Distances\EuclidianDistance.cs" />374 <Compile Include="RadialBasisFunctions\Distances\ManhattanDistance.cs" />375 <Compile Include="RadialBasisFunctions\Interfaces\IDistance.cs" />376 <Compile Include="RadialBasisFunctions\Interfaces\IKernelFunction.cs" />377 371 <Compile Include="RadialBasisFunctions\KernelFunctions\CicularKernel.cs" /> 378 372 <Compile Include="RadialBasisFunctions\KernelFunctions\GaussianKernel.cs" /> 379 <Compile Include="RadialBasisFunctions\KernelFunctions\InverseMultiquadraticKernel 373 <Compile Include="RadialBasisFunctions\KernelFunctions\InverseMultiquadraticKernel.cs" /> 380 374 <Compile Include="RadialBasisFunctions\KernelFunctions\KernelBase.cs" /> 381 375 <Compile Include="RadialBasisFunctions\KernelFunctions\LaplacianKernel.cs" /> … … 383 377 <Compile Include="RadialBasisFunctions\KernelFunctions\PolysplineKernel.cs" /> 384 378 <Compile Include="RadialBasisFunctions\KernelFunctions\ThinPlatePolysplineKernel.cs" /> 385 <Compile Include="RadialBasisFunctions\MatrixUtilities.cs" />386 379 <Compile Include="RadialBasisFunctions\RadialBasisFunctionModel.cs" /> 387 <Compile Include="RadialBasisFunctions\RadialBasisFunctionRegressionSolution.cs" />388 380 <Compile Include="RadialBasisFunctions\RadialBasisRegression.cs" /> 389 381 <Compile Include="RandomForest\RandomForestClassificationSolution.cs" /> … … 442 434 </BootstrapperPackage> 443 435 </ItemGroup> 436 <ItemGroup> 437 <Folder Include="RadialBasisFunctions\Distances\" /> 438 <Folder Include="RadialBasisFunctions\Interfaces\" /> 439 </ItemGroup> 444 440 <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> 445 441 <!-- To modify your build process, add your task inside one of the targets below and uncomment it. -
branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/RadialBasisFunctions/KernelFunctions/CicularKernel.cs
r14386 r14872 30 30 [StorableClass] 31 31 [Item("CircularKernel", "A circular kernel function")] 32 public class CircularKernel <T> : KernelBase<T>{32 public class CircularKernel : KernelBase { 33 33 34 34 #region HLConstructors & Boilerplate … … 37 37 [StorableHook(HookType.AfterDeserialization)] 38 38 private void AfterDeserialization() { } 39 protected CircularKernel(CircularKernel <T>original, Cloner cloner) : base(original, cloner) { }39 protected CircularKernel(CircularKernel original, Cloner cloner) : base(original, cloner) { } 40 40 public CircularKernel() { 41 Parameters.Add(new FixedValueParameter<DoubleValue>(BetaParameterName, "The beta in the kernel function 2*pi*(acos(-d)-d*(1-n²)^(0.5)) where n = ||x-c|| and d = n/beta", new DoubleValue(2)));41 Parameters.Add(new FixedValueParameter<DoubleValue>(BetaParameterName, "The beta in the kernel function 2*pi*(acos(-d)-d*(1-n²)^(0.5)) where n = ||x-c|| and d = n/beta", new DoubleValue(2))); 42 42 } 43 43 public override IDeepCloneable Clone(Cloner cloner) { 44 return new CircularKernel <T>(this, cloner);44 return new CircularKernel(this, cloner); 45 45 } 46 46 #endregion -
branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/RadialBasisFunctions/KernelFunctions/GaussianKernel.cs
r14386 r14872 30 30 [StorableClass] 31 31 [Item("GaussianKernel", "A kernel function that uses Gaussian function")] 32 public class GaussianKernel <T> : KernelBase<T>{32 public class GaussianKernel : KernelBase { 33 33 34 34 #region HLConstructors & Boilerplate … … 37 37 [StorableHook(HookType.AfterDeserialization)] 38 38 private void AfterDeserialization() { } 39 protected GaussianKernel(GaussianKernel <T>original, Cloner cloner) : base(original, cloner) { }39 protected GaussianKernel(GaussianKernel original, Cloner cloner) : base(original, cloner) { } 40 40 public GaussianKernel() { 41 41 Parameters.Add(new FixedValueParameter<DoubleValue>(BetaParameterName, "The beta in the kernelfunction exp(-||x-c||/beta²)", new DoubleValue(2))); 42 42 } 43 43 public override IDeepCloneable Clone(Cloner cloner) { 44 return new GaussianKernel <T>(this, cloner);44 return new GaussianKernel(this, cloner); 45 45 } 46 46 #endregion -
branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/RadialBasisFunctions/KernelFunctions/InverseMultiquadraticKernel.cs
r14871 r14872 30 30 [StorableClass] 31 31 [Item("InverseMultiquadraticKernel", "A kernel function that uses the inverse multiquadratic function")] 32 public class InverseMultiquadraticKernel <T> : KernelBase<T>{32 public class InverseMultiquadraticKernel : KernelBase { 33 33 #region HLConstructors & Boilerplate 34 34 [StorableConstructor] … … 36 36 [StorableHook(HookType.AfterDeserialization)] 37 37 private void AfterDeserialization() { } 38 protected InverseMultiquadraticKernel(InverseMultiquadraticKernel <T>original, Cloner cloner) : base(original, cloner) { }38 protected InverseMultiquadraticKernel(InverseMultiquadraticKernel original, Cloner cloner) : base(original, cloner) { } 39 39 public InverseMultiquadraticKernel() { 40 Parameters.Add(new FixedValueParameter<DoubleValue>(BetaParameterName, "The beta in the kernel functionsqrt(1+||x-c||^2/beta)", new DoubleValue(2)));40 Parameters.Add(new FixedValueParameter<DoubleValue>(BetaParameterName, "The beta in the kernel function 1 / sqrt(1+||x-c||^2/beta)", new DoubleValue(2))); 41 41 } 42 42 public override IDeepCloneable Clone(Cloner cloner) { 43 return new InverseMultiquadraticKernel <T>(this, cloner);43 return new InverseMultiquadraticKernel(this, cloner); 44 44 } 45 45 #endregion -
branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/RadialBasisFunctions/KernelFunctions/KernelBase.cs
r14386 r14872 31 31 namespace HeuristicLab.Algorithms.DataAnalysis { 32 32 [StorableClass] 33 public abstract class KernelBase <T> : ParameterizedNamedItem, IKernelFunction<T>{33 public abstract class KernelBase : ParameterizedNamedItem, ICovarianceFunction { 34 34 35 35 #region Parameternames … … 38 38 #endregion 39 39 #region Parameterproperties 40 public ValueParameter<IDistance<T>> DistanceParameter 41 { 42 get { return Parameters[DistanceParameterName] as ValueParameter<IDistance<T>>; } 40 public ValueParameter<IDistance> DistanceParameter { 41 get { return Parameters[DistanceParameterName] as ValueParameter<IDistance>; } 43 42 } 44 43 45 public IFixedValueParameter<DoubleValue> BetaParameter 46 { 44 public IFixedValueParameter<DoubleValue> BetaParameter { 47 45 get { return Parameters[BetaParameterName] as FixedValueParameter<DoubleValue>; } 48 46 } … … 50 48 #endregion 51 49 #region Properties 52 public IDistance<T> Distance 53 { 50 public IDistance Distance { 54 51 get { return DistanceParameter.Value; } 55 52 } 56 53 57 public double Beta 58 { 54 public double Beta { 59 55 get { return BetaParameter.Value.Value; } 60 56 } … … 62 58 #endregion 63 59 64 #region HLConstructors & Boilerplate65 60 [StorableConstructor] 66 61 protected KernelBase(bool deserializing) : base(deserializing) { } … … 68 63 private void AfterDeserialization() { } 69 64 70 protected KernelBase(KernelBase <T>original, Cloner cloner)65 protected KernelBase(KernelBase original, Cloner cloner) 71 66 : base(original, cloner) { } 72 67 73 68 protected KernelBase() { 74 Parameters.Add(new ValueParameter<IDistance <T>>(DistanceParameterName, "The distance function used for kernel calculation"));75 DistanceParameter.Value = new Euclid ianDistance() as IDistance<T>;69 Parameters.Add(new ValueParameter<IDistance>(DistanceParameterName, "The distance function used for kernel calculation")); 70 DistanceParameter.Value = new EuclideanDistance(); 76 71 } 77 #endregion78 72 79 public double Get( T a, Tb) {73 public double Get(object a, object b) { 80 74 return Get(Distance.Get(a, b)); 81 75 } … … 93 87 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) { 94 88 if (p == null || p.Length != 1) throw new ArgumentException("Illegal parametrization"); 95 var myClone = (KernelBase <T>)Clone(new Cloner());89 var myClone = (KernelBase)Clone(new Cloner()); 96 90 myClone.BetaParameter.Value.Value = p[0]; 97 91 var cov = new ParameterizedCovarianceFunction { … … 107 101 protected double GetNorm(double[,] x, double[,] xt, int i, int j, int[] columnIndices) { 108 102 var dist = Distance as IDistance<IEnumerable<double>>; 109 if (dist == null) throw new ArgumentException("The Distance needs to apply to double-Vectors");103 if (dist == null) throw new ArgumentException("The distance needs to apply to double vectors"); 110 104 var r1 = new IndexedEnumerable(x, i, columnIndices); 111 105 var r2 = new IndexedEnumerable(xt, j, columnIndices); … … 142 136 } 143 137 144 public double Current 145 { 138 public double Current { 146 139 get { return data[row, column.Current]; } 147 140 } 148 141 149 object IEnumerator.Current 150 { 151 get 152 { 142 object IEnumerator.Current { 143 get { 153 144 return data[row, column.Current]; 154 145 } -
branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/RadialBasisFunctions/KernelFunctions/LaplacianKernel.cs
r14386 r14872 29 29 namespace HeuristicLab.Algorithms.DataAnalysis { 30 30 [StorableClass] 31 [Item("LaplacianKernel", "A kernel function that uses an exponential function.\nIt is equvalent to the Gaussiankernel but less suseptible to improper values of beta")] 32 public class LaplacianKernel<T> : KernelBase<T> { 33 34 #region HLConstructors & Boilerplate 31 [Item("LaplacianKernel", "A kernel function that uses an exponential function.\nIt is equvalent to the Gaussian kernel but less susceptible to improper values of beta")] 32 public class LaplacianKernel : KernelBase { 35 33 [StorableConstructor] 36 34 protected LaplacianKernel(bool deserializing) : base(deserializing) { } 37 35 [StorableHook(HookType.AfterDeserialization)] 38 36 private void AfterDeserialization() { } 39 protected LaplacianKernel(LaplacianKernel <T>original, Cloner cloner) : base(original, cloner) { }37 protected LaplacianKernel(LaplacianKernel original, Cloner cloner) : base(original, cloner) { } 40 38 public LaplacianKernel() { 41 Parameters.Add(new FixedValueParameter<DoubleValue>(BetaParameterName, "The beta in the kernel function exp(-||x-c||/beta)", new DoubleValue(2)));39 Parameters.Add(new FixedValueParameter<DoubleValue>(BetaParameterName, "The beta in the kernel function exp(-||x-c||/beta)", new DoubleValue(2))); 42 40 } 43 41 public override IDeepCloneable Clone(Cloner cloner) { 44 return new LaplacianKernel <T>(this, cloner);42 return new LaplacianKernel(this, cloner); 45 43 } 46 #endregion47 44 48 45 protected override double Get(double norm) { -
branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/RadialBasisFunctions/KernelFunctions/MultiquadraticKernel.cs
r14386 r14872 30 30 [StorableClass] 31 31 [Item("MultiquadraticKernel", "A kernel function that uses the multiquadratic function")] 32 public class MultiquadraticKernel <T> : KernelBase<T>{32 public class MultiquadraticKernel : KernelBase { 33 33 34 34 #region HLConstructors & Boilerplate … … 37 37 [StorableHook(HookType.AfterDeserialization)] 38 38 private void AfterDeserialization() { } 39 protected MultiquadraticKernel(MultiquadraticKernel <T>original, Cloner cloner)39 protected MultiquadraticKernel(MultiquadraticKernel original, Cloner cloner) 40 40 : base(original, cloner) { } 41 41 42 42 public MultiquadraticKernel() { 43 Parameters.Add(new FixedValueParameter<DoubleValue>(BetaParameterName, "The beta in the kernel function sqrt(1+||x-c||²/beta)", new DoubleValue(2)));43 Parameters.Add(new FixedValueParameter<DoubleValue>(BetaParameterName, "The beta in the kernel function sqrt(1+||x-c||²/beta)", new DoubleValue(2))); 44 44 } 45 45 public override IDeepCloneable Clone(Cloner cloner) { 46 return new MultiquadraticKernel <T>(this, cloner);46 return new MultiquadraticKernel(this, cloner); 47 47 } 48 48 #endregion -
branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/RadialBasisFunctions/KernelFunctions/PolysplineKernel.cs
r14386 r14872 30 30 [StorableClass] 31 31 [Item("PolysplineKernel", "A kernel function that uses the multiquadratic function")] 32 public class PolysplineKernel <T> : KernelBase<T>{32 public class PolysplineKernel : KernelBase { 33 33 34 34 #region HLConstructors & Boilerplate … … 37 37 [StorableHook(HookType.AfterDeserialization)] 38 38 private void AfterDeserialization() { } 39 protected PolysplineKernel(PolysplineKernel <T>original, Cloner cloner)39 protected PolysplineKernel(PolysplineKernel original, Cloner cloner) 40 40 : base(original, cloner) { } 41 41 public PolysplineKernel() { … … 43 43 } 44 44 public override IDeepCloneable Clone(Cloner cloner) { 45 return new PolysplineKernel <T>(this, cloner);45 return new PolysplineKernel(this, cloner); 46 46 } 47 47 #endregion -
branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/RadialBasisFunctions/KernelFunctions/ThinPlatePolysplineKernel.cs
r14386 r14872 30 30 [StorableClass] 31 31 [Item("ThinPlatePolysplineKernel", "A kernel function that uses the ThinPlatePolyspline function")] 32 public class ThinPlatePolysplineKernel <T> : KernelBase<T>{32 public class ThinPlatePolysplineKernel : KernelBase { 33 33 #region HLConstructors & Boilerplate 34 34 [StorableConstructor] … … 36 36 [StorableHook(HookType.AfterDeserialization)] 37 37 private void AfterDeserialization() { } 38 protected ThinPlatePolysplineKernel(ThinPlatePolysplineKernel <T>original, Cloner cloner) : base(original, cloner) { }38 protected ThinPlatePolysplineKernel(ThinPlatePolysplineKernel original, Cloner cloner) : base(original, cloner) { } 39 39 public ThinPlatePolysplineKernel() { 40 40 Parameters.Add(new FixedValueParameter<DoubleValue>(BetaParameterName, "The Beta in the kernelfunction ||x-c||^(2*Beta)*log(||x-c||^Beta)", new DoubleValue(1))); 41 41 } 42 42 public override IDeepCloneable Clone(Cloner cloner) { 43 return new ThinPlatePolysplineKernel <T>(this, cloner);43 return new ThinPlatePolysplineKernel(this, cloner); 44 44 } 45 45 #endregion -
branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/RadialBasisFunctions/RadialBasisFunctionModel.cs
r14386 r14872 1 #region License Information1 #region License Information 2 2 /* HeuristicLab 3 3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL) … … 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Diagnostics; 24 25 using System.Linq; 25 26 using HeuristicLab.Common; 26 27 using HeuristicLab.Core; 27 using HeuristicLab.Data;28 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 29 29 using HeuristicLab.Problems.DataAnalysis; … … 31 31 namespace HeuristicLab.Algorithms.DataAnalysis { 32 32 /// <summary> 33 /// Represents a Radial Basis Functionregression model.33 /// Represents an RBF regression model. 34 34 /// </summary> 35 35 [StorableClass] 36 [Item("RBFModel", " Represents a Gaussian process posterior.")]36 [Item("RBFModel", "An RBF regression model")] 37 37 public sealed class RadialBasisFunctionModel : RegressionModel, IConfidenceRegressionModel { 38 public override IEnumerable<string> VariablesUsedForPrediction 39 { 38 public override IEnumerable<string> VariablesUsedForPrediction { 40 39 get { return allowedInputVariables; } 41 40 } 42 41 43 42 [Storable] 44 private string[] allowedInputVariables; 45 public string[] AllowedInputVariables 46 { 43 private readonly string[] allowedInputVariables; 44 public string[] AllowedInputVariables { 47 45 get { return allowedInputVariables; } 48 46 } 49 47 50 48 [Storable] 51 private double[] alpha; 52 [Storable] 53 private IDataset trainingDataset; // it is better to store the original training dataset completely because this is more efficient in persistence 54 [Storable] 55 private int[] trainingRows; 56 [Storable] 57 private IKernelFunction<double[]> kernel; 58 [Storable] 59 private DoubleMatrix gramInvert; 60 49 private readonly double[] alpha; 50 51 [Storable] 52 private readonly double[,] trainX; // it is better to store the original training dataset completely because this is more efficient in persistence 53 54 [Storable] 55 private readonly ITransformation<double>[] scaling; 56 57 [Storable] 58 private readonly ICovarianceFunction kernel; 59 60 private double[,] gramInvert; // not storable as it can be large (recreate after deserialization as required) 61 62 [Storable] 63 private readonly double meanOffset; // implementation works for zero-mean target variables 61 64 62 65 [StorableConstructor] … … 65 68 : base(original, cloner) { 66 69 // shallow copies of arrays because they cannot be modified 67 trainingRows = original.trainingRows;68 70 allowedInputVariables = original.allowedInputVariables; 69 71 alpha = original.alpha; 70 trainingDataset = original.trainingDataset; 71 kernel = original.kernel; 72 } 73 public RadialBasisFunctionModel(IDataset dataset, string targetVariable, IEnumerable<string> allowedInputVariables, IEnumerable<int> rows, IKernelFunction<double[]> kernel) 72 trainX = original.trainX; 73 gramInvert = original.gramInvert; 74 scaling = original.scaling; 75 76 meanOffset = original.meanOffset; 77 if (original.kernel != null) 78 kernel = cloner.Clone(original.kernel); 79 } 80 public RadialBasisFunctionModel(IDataset dataset, string targetVariable, IEnumerable<string> allowedInputVariables, IEnumerable<int> rows, 81 bool scaleInputs, ICovarianceFunction kernel) 74 82 : base(targetVariable) { 83 if (kernel.GetNumberOfParameters(allowedInputVariables.Count()) > 0) throw new ArgumentException("All parameters in the kernel function must be specified."); 75 84 name = ItemName; 76 85 description = ItemDescription; 77 86 this.allowedInputVariables = allowedInputVariables.ToArray(); 78 trainingRows = rows.ToArray(); 79 trainingDataset = dataset; 80 this.kernel = (IKernelFunction<double[]>)kernel.Clone(); 87 var trainingRows = rows.ToArray(); 88 this.kernel = (ICovarianceFunction)kernel.Clone(); 81 89 try { 82 var data = ExtractData(dataset, trainingRows); 83 var qualities = dataset.GetDoubleValues(targetVariable, trainingRows).ToArray(); 90 if (scaleInputs) 91 scaling = CreateScaling(dataset, trainingRows); 92 trainX = ExtractData(dataset, trainingRows, scaling); 93 var y = dataset.GetDoubleValues(targetVariable, trainingRows).ToArray(); 94 meanOffset = y.Average(); 95 for (int i = 0; i < y.Length; i++) y[i] -= meanOffset; 84 96 int info; 97 // TODO: improve efficiency by decomposing matrix once instead of solving the system and then inverting the matrix 85 98 alglib.densesolverlsreport denseSolveRep; 86 var gr = BuildGramMatrix(data); 87 alglib.rmatrixsolvels(gr, data.Length + 1, data.Length + 1, qualities.Concat(new[] { 0.0 }).ToArray(), 0.0, out info, out denseSolveRep, out alpha); 88 if (info != 1) throw new ArgumentException("Could not create Model."); 89 gramInvert = new DoubleMatrix(gr).Invert(); 90 } 91 catch (alglib.alglibexception ae) { 99 gramInvert = BuildGramMatrix(trainX); 100 int n = trainX.GetLength(0); 101 alglib.rmatrixsolvels(gramInvert, n, n, y, 0.0, out info, out denseSolveRep, out alpha); 102 if (info != 1) throw new ArgumentException("Could not create model."); 103 104 alglib.matinvreport report; 105 alglib.rmatrixinverse(ref gramInvert, out info, out report); 106 if (info != 1) throw new ArgumentException("Could not invert matrix. Is it quadratic symmetric positive definite?"); 107 108 } catch (alglib.alglibexception ae) { 92 109 // wrap exception so that calling code doesn't have to know about alglib implementation 93 throw new ArgumentException("There was a problem in the calculation of the RBF process model", ae); 94 } 95 } 96 private double[][] ExtractData(IDataset dataset, IEnumerable<int> rows) { 97 return rows.Select(r => allowedInputVariables.Select(v => dataset.GetDoubleValue(v, r)).ToArray()).ToArray(); 110 throw new ArgumentException("There was a problem in the calculation of the RBF model", ae); 111 } 112 } 113 114 private ITransformation<double>[] CreateScaling(IDataset dataset, int[] rows) { 115 var trans = new ITransformation<double>[allowedInputVariables.Length]; 116 int i = 0; 117 foreach (var variable in allowedInputVariables) { 118 var lin = new LinearTransformation(allowedInputVariables); 119 var max = dataset.GetDoubleValues(variable, rows).Max(); 120 var min = dataset.GetDoubleValues(variable, rows).Min(); 121 lin.Multiplier = 1.0 / (max - min); 122 lin.Addend = -min / (max - min); 123 trans[i] = lin; 124 i++; 125 } 126 return trans; 127 } 128 129 private double[,] ExtractData(IDataset dataset, IEnumerable<int> rows, ITransformation<double>[] scaling = null) { 130 double[][] variables; 131 if (scaling != null) { 132 variables = 133 allowedInputVariables.Select((var, i) => scaling[i].Apply(dataset.GetDoubleValues(var, rows)).ToArray()) 134 .ToArray(); 135 } else { 136 variables = 137 allowedInputVariables.Select(var => dataset.GetDoubleValues(var, rows).ToArray()).ToArray(); 138 } 139 int n = variables.First().Length; 140 var res = new double[n, variables.Length]; 141 for (int r = 0; r < n; r++) 142 for (int c = 0; c < variables.Length; c++) { 143 res[r, c] = variables[c][r]; 144 } 145 return res; 98 146 } 99 147 … … 104 152 #region IRegressionModel Members 105 153 public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) { 106 var solutions = ExtractData(dataset, rows); 107 var data = ExtractData(trainingDataset, trainingRows); 108 return solutions.Select(solution => alpha.Zip(data, (a, d) => a * kernel.Get(solution, d)).Sum() + 1 * alpha[alpha.Length - 1]).ToArray(); 154 var newX = ExtractData(dataset, rows, scaling); 155 var dim = newX.GetLength(1); 156 var cov = kernel.GetParameterizedCovarianceFunction(new double[0], Enumerable.Range(0, dim).ToArray()); 157 158 var pred = new double[newX.GetLength(0)]; 159 for (int i = 0; i < pred.Length; i++) { 160 double sum = meanOffset; 161 for (int j = 0; j < alpha.Length; j++) { 162 sum += alpha[j] * cov.CrossCovariance(trainX, newX, j, i); 163 } 164 pred[i] = sum; 165 } 166 return pred; 109 167 } 110 168 public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) { 111 return new RadialBasisFunctionRegressionSolution(this, new RegressionProblemData(problemData));169 return new ConfidenceRegressionSolution(this, new RegressionProblemData(problemData)); 112 170 } 113 171 #endregion 114 172 115 173 public IEnumerable<double> GetEstimatedVariances(IDataset dataset, IEnumerable<int> rows) { 116 var data = ExtractData(trainingDataset, trainingRows); 117 return ExtractData(dataset, rows).Select(x => GetVariance(x, data)); 174 if (gramInvert == null) CreateAndInvertGramMatrix(); 175 int n = gramInvert.GetLength(0); 176 var newData = ExtractData(dataset, rows, scaling); 177 var dim = newData.GetLength(1); 178 var cov = kernel.GetParameterizedCovarianceFunction(new double[0], Enumerable.Range(0, dim).ToArray()); 179 180 // TODO perf (matrix matrix multiplication) 181 for (int i = 0; i < newData.GetLength(0); i++) { 182 double[] p = new double[n]; 183 184 for (int j = 0; j < trainX.GetLength(0); j++) { 185 p[j] = cov.CrossCovariance(trainX, newData, j, i); 186 } 187 188 var Ap = new double[n]; 189 alglib.ablas.rmatrixmv(n, n, gramInvert, 0, 0, 0, p, 0, ref Ap, 0); 190 var res = 0.0; 191 // dot product 192 for (int j = 0; j < p.Length; j++) res += p[j] * Ap[j]; 193 yield return res > 0 ? res : 0; 194 } 118 195 } 119 196 public double LeaveOneOutCrossValidationRootMeanSquaredError() { 120 return Math.Sqrt(alpha.Select((t, i) => t / gramInvert[i, i]).Sum(d => d * d) / gramInvert.Rows); 121 } 122 197 if (gramInvert == null) CreateAndInvertGramMatrix(); 198 var n = gramInvert.GetLength(0); 199 var s = 1.0 / n; 200 201 var sum = 0.0; 202 for (int i = 0; i < alpha.Length; i++) { 203 var x = alpha[i] / gramInvert[i, i]; 204 sum += x * x; 205 } 206 sum *= s; 207 return Math.Sqrt(sum); 208 } 209 210 private void CreateAndInvertGramMatrix() { 211 try { 212 gramInvert = BuildGramMatrix(trainX); 213 int info = 0; 214 alglib.matinvreport report; 215 alglib.rmatrixinverse(ref gramInvert, out info, out report); 216 if (info != 1) 217 throw new ArgumentException("Could not invert matrix. Is it quadratic symmetric positive definite?"); 218 } catch (alglib.alglibexception) { 219 // wrap exception so that calling code doesn't have to know about alglib implementation 220 throw new ArgumentException("Could not invert matrix. Is it quadratic symmetric positive definite?"); 221 } 222 } 123 223 #region helpers 124 private double[,] BuildGramMatrix(double[ ][] data) {125 var size = data.Length + 1;126 var gram = new double[size, size];127 for (var i = 0; i < size; i++)128 for (var j = i; j < size; j++) {129 if (j == size - 1 && i == size - 1) gram[i, j] = 0;130 else if (j == size - 1 || i == size - 1) gram[j, i] = gram[i, j] = 1;131 else gram[j, i] = gram[i, j] = kernel.Get(data[i], data[j]); //symmteric Matrix --> half of the work224 private double[,] BuildGramMatrix(double[,] data) { 225 var n = data.GetLength(0); 226 var dim = data.GetLength(1); 227 var cov = kernel.GetParameterizedCovarianceFunction(new double[0], Enumerable.Range(0, dim).ToArray()); 228 var gram = new double[n, n]; 229 for (var i = 0; i < n; i++) 230 for (var j = i; j < n; j++) { 231 gram[j, i] = gram[i, j] = cov.Covariance(data, i, j); // symmetric matrix --> half of the work 132 232 } 133 233 return gram; 134 234 } 135 private double GetVariance(double[] solution, IEnumerable<double[]> data) { 136 var phiT = data.Select(x => kernel.Get(x, solution)).Concat(new[] { 1.0 }).ToColumnVector(); 137 var res = phiT.Transpose().Mul(gramInvert.Mul(phiT))[0, 0]; 138 return res > 0 ? res : 0; 139 } 235 140 236 #endregion 141 237 } -
branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/RadialBasisFunctions/RadialBasisRegression.cs
r14386 r14872 22 22 using System; 23 23 using System.Linq; 24 using System.Threading; 24 25 using HeuristicLab.Common; 25 26 using HeuristicLab.Core; … … 34 35 /// Linear regression data analysis algorithm. 35 36 /// </summary> 36 [Item("Radial Basis Function Regression (RBF-R)", "Radial basis function regression data analysis algorithm (uses for ALGLIB).")]37 [Item("Radial Basis Function Regression (RBF-R)", "Radial basis function regression data analysis algorithm.")] 37 38 [Creatable(CreatableAttribute.Categories.DataAnalysisRegression, Priority = 100)] 38 39 [StorableClass] 39 public sealed class RadialBasisRegression : FixedDataAnalysisAlgorithm<IRegressionProblem>{40 private const string R adialBasisRegressionModelResultName = "RBF regression solution";40 public sealed class RadialBasisRegression : BasicAlgorithm { 41 private const string RBFRegressionSolutionResultName = "RBF regression solution"; 41 42 43 public override bool SupportsPause { 44 get { return false; } 45 } 46 public override Type ProblemType { 47 get { return typeof(IRegressionProblem); } 48 } 49 public new IRegressionProblem Problem { 50 get { return (IRegressionProblem)base.Problem; } 51 set { base.Problem = value; } 52 } 42 53 43 #region Parameternames 44 private const string Kernelname = "Kernel"; 54 #region parameter names 55 private const string KernelParameterName = "Kernel"; 56 private const string ScaleInputVariablesParameterName = "ScaleInputVariables"; 45 57 #endregion 46 58 47 #region Paramterproperties 48 public ValueParameter<IKernelFunction<double[]>> KernelParameter 49 { 50 get { return Parameters[Kernelname] as ValueParameter<IKernelFunction<double[]>>; } 59 #region parameter properties 60 public ValueParameter<ICovarianceFunction> KernelParameter { 61 get { return (ValueParameter<ICovarianceFunction>)Parameters[KernelParameterName]; } 62 } 63 64 public IFixedValueParameter<BoolValue> ScaleInputVariablesParameter { 65 get { return (IFixedValueParameter<BoolValue>)Parameters[ScaleInputVariablesParameterName]; } 51 66 } 52 67 #endregion 53 68 54 #region Properties 55 public IKernelFunction<double[]> Kernel 56 { 69 #region properties 70 public ICovarianceFunction Kernel { 57 71 get { return KernelParameter.Value; } 72 } 73 74 public bool ScaleInputVariables { 75 get { return ScaleInputVariablesParameter.Value.Value; } 76 set { ScaleInputVariablesParameter.Value.Value = value; } 58 77 } 59 78 … … 67 86 public RadialBasisRegression() { 68 87 Problem = new RegressionProblem(); 69 Parameters.Add(new ValueParameter<IKernelFunction<double[]>>(Kernelname, "The radial basis function")); 70 var kernel = new PolysplineKernel<double[]>(); 88 Parameters.Add(new ValueParameter<ICovarianceFunction>(KernelParameterName, "The radial basis function")); 89 Parameters.Add(new FixedValueParameter<BoolValue>(ScaleInputVariablesParameterName, "Set to true if the input variables should be scaled to the interval [0..1]", new BoolValue(true))); 90 var kernel = new GaussianKernel(); 71 91 KernelParameter.Value = kernel; 72 kernel.BetaParameter.Value.Value = 1;73 92 } 74 93 [StorableHook(HookType.AfterDeserialization)] … … 79 98 } 80 99 81 #region regression 82 protected override void Run() { 83 double loocvrmse, cvRmsError; 84 var solution = CreateRadialBasisRegressionSolution(Problem.ProblemData, Kernel, out loocvrmse, out cvRmsError); 85 Results.Add(new Result(RadialBasisRegressionModelResultName, "The RBF regression solution.", solution)); 86 Results.Add(new Result("LOOCVRMSE", "The root of the mean of squared errors of a leave-one-out-cross-validation on the trainingsset (This is not the RSME on the trainingset)", new DoubleValue(loocvrmse))); 87 Results.Add(new Result("Estimated root mean square error (cross-validation)", "The estimated root of the mean of squared errors of the linear regression solution via cross validation.", new DoubleValue(cvRmsError))); 100 protected override void Run(CancellationToken cancellationToken) { 101 double loocvrmse, rmsError; 102 var solution = CreateRadialBasisRegressionSolution(Problem.ProblemData, Kernel, ScaleInputVariables, out loocvrmse, out rmsError); 103 Results.Add(new Result(RBFRegressionSolutionResultName, "The RBF regression solution.", solution)); 104 Results.Add(new Result("LOOCVRMSE", "The root mean squared error of a leave-one-out-cross-validation on the training set", new DoubleValue(loocvrmse))); 105 Results.Add(new Result("RMSE (test)", "The root mean squared error of the solution on the test set.", new DoubleValue(rmsError))); 88 106 } 89 107 90 public static I ConfidenceRegressionSolution CreateRadialBasisRegressionSolution(IRegressionProblemData problemData, IKernelFunction<double[]> kernel, out double loocvRmsError, out double cvRmsError) {91 var model = new RadialBasisFunctionModel(problemData.Dataset, problemData.TargetVariable, problemData.AllowedInputVariables, problemData.TrainingIndices, kernel);108 public static IRegressionSolution CreateRadialBasisRegressionSolution(IRegressionProblemData problemData, ICovarianceFunction kernel, bool scaleInputs, out double loocvRmsError, out double rmsError) { 109 var model = new RadialBasisFunctionModel(problemData.Dataset, problemData.TargetVariable, problemData.AllowedInputVariables, problemData.TrainingIndices, scaleInputs, kernel); 92 110 loocvRmsError = model.LeaveOneOutCrossValidationRootMeanSquaredError(); 93 cvRmsError = Math.Sqrt(model.GetEstimatedValues(problemData.Dataset, problemData.TestIndices)111 rmsError = Math.Sqrt(model.GetEstimatedValues(problemData.Dataset, problemData.TestIndices) 94 112 .Zip(problemData.TargetVariableTestValues, (a, b) => (a - b) * (a - b)) 95 . Sum());96 var solution = (RadialBasisFunctionRegressionSolution)model.CreateRegressionSolution((IRegressionProblemData)problemData.Clone());97 solution.Model.Name = "R adial BasisRegression Model";98 solution.Name = "R adial BasisRegression Solution";113 .Average()); 114 var solution = model.CreateRegressionSolution((IRegressionProblemData)problemData.Clone()); 115 solution.Model.Name = "RBF Regression Model"; 116 solution.Name = "RBF Regression Solution"; 99 117 return solution; 100 118 } 101 #endregion102 103 #region helpers104 #endregion105 119 } 106 120 } -
branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/TSNE/Distances/DistanceBase.cs
r14767 r14872 19 19 */ 20 20 #endregion 21 21 using System.Collections; 22 22 using System.Collections.Generic; 23 23 using HeuristicLab.Common; … … 42 42 } 43 43 44 private class DistanceComparer : IComparer<T> {44 private class DistanceComparer : IComparer<T>, IComparer { 45 45 private readonly T item; 46 46 private readonly IDistance<T> dist; … … 54 54 return dist.Get(x, item).CompareTo(dist.Get(y, item)); 55 55 } 56 57 public int Compare(object x, object y) { 58 return Compare((T)x, (T)y); 59 } 60 } 61 62 public double Get(object x, object y) { 63 return Get((T)x, (T)y); 64 } 65 66 public IComparer GetDistanceComparer(object item) { 67 return new DistanceComparer((T)item, this); 56 68 } 57 69 } -
branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/TSNE/Interfaces/IDistance.cs
r14767 r14872 19 19 */ 20 20 #endregion 21 21 using System.Collections; 22 22 using System.Collections.Generic; 23 23 using HeuristicLab.Core; 24 24 25 25 namespace HeuristicLab.Algorithms.DataAnalysis { 26 public interface IDistance<in T> : I Item{26 public interface IDistance<in T> : IDistance { 27 27 /// <summary> 28 28 /// Calculates a distance measure between two objects. … … 41 41 IComparer<T> GetDistanceComparer(T item); 42 42 } 43 44 public interface IDistance : IItem { 45 double Get(object x, object y); 46 IComparer GetDistanceComparer(object item); 47 } 43 48 }
Note: See TracChangeset
for help on using the changeset viewer.