- Timestamp:
- 10/05/10 16:30:57 (14 years ago)
- Location:
- branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate/3.3
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate/3.3/Evaluators/OnlineMeanMahalanobisDistanceEvaluator.cs
r4475 r4555 14 14 private double distance; 15 15 private double[,] covMatrix; 16 private double[] diff; 17 private double[] target; 16 18 public double MeanMahalanobisDistance { 17 19 get { … … 39 41 } 40 42 41 public void Add(double[] original, double[] estimated) { 42 if (original.Length != estimated.Length) throw new ArgumentException("Number of elements of original and estimated doesn't match."); 43 if (original.Length != covMatrix.GetLength(0)) throw new ArgumentException("Original and estimated is not compatible with covariance matrix."); 43 public void Add(IEnumerable<double> original, IEnumerable<double> estimated) { 44 if (covMatrix == null) throw new InvalidOperationException("Covariance matrix must be initialized before values can be added."); 44 45 45 double[] diff = new double[original.Length]; 46 for (int i = 0; i < diff.Length; i++) { 47 diff[i] = original[i] - estimated[i]; 46 { 47 // calculate difference vector 48 var originalEnumerator = original.GetEnumerator(); 49 var estimatedEnumerator = estimated.GetEnumerator(); 50 int i = 0; 51 while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext() && i < diff.Length) { 52 diff[i++] = originalEnumerator.Current - estimatedEnumerator.Current; 53 } 54 if (originalEnumerator.MoveNext() | estimatedEnumerator.MoveNext() || i < diff.Length) { 55 throw new ArgumentException("Number of elements of original and estimated doesn't match or is not compatible with covariance matrix."); 56 } 48 57 } 49 double[] target = new double[original.Length];50 58 51 alglib.ablas.rmatrixmv(covMatrix.GetLength(0), covMatrix.GetLength(1), ref covMatrix, 0, 0, 0, ref diff, 0, ref target, 0); 59 { 60 // calculate mahalanobis distance using covariance matrix 61 // covMatrix^(-1) * diff => target 62 alglib.ablas.rmatrixmv(covMatrix.GetLength(0), covMatrix.GetLength(1), ref covMatrix, 0, 0, 0, ref diff, 0, ref target, 0); 52 63 53 double sum = 0.0; 54 for (int i = 0; i < original.Length; i++) { 55 sum += diff[i] * target[i]; 64 // diff^T * (covMatrix^(-1) * diff) => sum 65 double sum = 0.0; 66 for (int i = 0; i < diff.Length; i++) { 67 sum += diff[i] * target[i]; 68 } 69 distance += sum; 70 n++; 56 71 } 57 distance += sum;58 n++;59 72 } 60 73 … … 62 75 n = 0; 63 76 distance = 0.0; 77 covMatrix = null; 78 diff = null; 79 target = null; 64 80 } 65 81 … … 88 104 alglib.matinv.rmatrixinverse(ref covMatrix, covMatrix.GetLength(0), ref info, ref report); 89 105 if (info != 1) throw new InvalidOperationException("Can't invert covariance matrix."); 106 diff = new double[samples.Length]; 107 target = new double[samples.Length]; 90 108 } 91 109 } -
branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate/3.3/Evaluators/OnlineMultiVariateEvaluator.cs
r4475 r4555 23 23 } 24 24 25 public void Add(double[] original, double[] estimated) { 26 if (original.Length != estimated.Length) throw new ArgumentException("Number of elements of original and estimated doesn't match."); 27 if (evaluators == null) InitializeEvaluators(original.Length); 28 if (original.Length != evaluators.Count) throw new ArgumentException("Dimension doesn't match previous array dimensions."); 29 for (int i = 0; i < original.Length; i++) { 30 evaluators[i].Add(original[i], estimated[i]); 25 public void Add(IEnumerable<double> original, IEnumerable<double> estimated) { 26 var originalEnumerator = original.GetEnumerator(); 27 var estimatedEnumerator = estimated.GetEnumerator(); 28 // first call of Add() => initialize evaluators list 29 if (evaluators == null) { 30 evaluators = new List<T>(); 31 while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) { 32 T evaluator = new T(); 33 evaluator.Add(originalEnumerator.Current, estimatedEnumerator.Current); 34 evaluators.Add(evaluator); 35 } 36 } else { 37 // subsequent call of Add(): for each evaluator a value must be added 38 int i = 0; 39 while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext() && i < evaluators.Count) { 40 evaluators[i++].Add(originalEnumerator.Current, estimatedEnumerator.Current); 41 } 42 if (i < evaluators.Count) { 43 throw new ArgumentException("Number of elements in original and estimated does not match the number of elements in previously added vectors."); 44 } 31 45 } 32 } 33 34 private void InitializeEvaluators(int count) { 35 evaluators = new List<T>(); 36 for (int i = 0; i < count; i++) { 37 evaluators.Add(new T()); 46 if (originalEnumerator.MoveNext() | estimatedEnumerator.MoveNext()) { 47 throw new ArgumentException("Number of elements in original and estimated does not match."); 38 48 } 39 49 } -
branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate/3.3/Interfaces/IMultiVariateDataAnalysisSolution.cs
r4401 r4555 35 35 IEnumerable<double[]> EstimatedTrainingValues { get; } 36 36 IEnumerable<double[]> EstimatedTestValues { get; } 37 38 37 } 39 38 } -
branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate/3.3/Interfaces/IMultiVariateOnlineEvaluator.cs
r4475 r4555 22 22 using HeuristicLab.Core; 23 23 using HeuristicLab.Optimization; 24 using System.Collections.Generic; 24 25 25 26 namespace HeuristicLab.Problems.DataAnalysis.MultiVariate { … … 27 28 double Value { get; } 28 29 void Reset(); 29 void Add( double[] original, double[]estimated);30 void Add(IEnumerable<double> original, IEnumerable<double> estimated); 30 31 } 31 32 }
Note: See TracChangeset
for help on using the changeset viewer.