Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/05/10 16:30:57 (14 years ago)
Author:
gkronber
Message:

Improved time series evaluators. #1142

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  
    1414    private double distance;
    1515    private double[,] covMatrix;
     16    private double[] diff;
     17    private double[] target;
    1618    public double MeanMahalanobisDistance {
    1719      get {
     
    3941    }
    4042
    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.");
    4445
    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        }
    4857      }
    49       double[] target = new double[original.Length];
    5058
    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);
    5263
    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++;
    5671      }
    57       distance += sum;
    58       n++;
    5972    }
    6073
     
    6275      n = 0;
    6376      distance = 0.0;
     77      covMatrix = null;
     78      diff = null;
     79      target = null;
    6480    }
    6581
     
    88104      alglib.matinv.rmatrixinverse(ref covMatrix, covMatrix.GetLength(0), ref info, ref report);
    89105      if (info != 1) throw new InvalidOperationException("Can't invert covariance matrix.");
     106      diff = new double[samples.Length];
     107      target = new double[samples.Length];
    90108    }
    91109  }
  • branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate/3.3/Evaluators/OnlineMultiVariateEvaluator.cs

    r4475 r4555  
    2323    }
    2424
    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        }
    3145      }
    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.");
    3848      }
    3949    }
  • branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate/3.3/Interfaces/IMultiVariateDataAnalysisSolution.cs

    r4401 r4555  
    3535    IEnumerable<double[]> EstimatedTrainingValues { get; }
    3636    IEnumerable<double[]> EstimatedTestValues { get; }
    37    
    3837  }
    3938}
  • branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate/3.3/Interfaces/IMultiVariateOnlineEvaluator.cs

    r4475 r4555  
    2222using HeuristicLab.Core;
    2323using HeuristicLab.Optimization;
     24using System.Collections.Generic;
    2425
    2526namespace HeuristicLab.Problems.DataAnalysis.MultiVariate {
     
    2728    double Value { get; }
    2829    void Reset();
    29     void Add(double[] original, double[] estimated);
     30    void Add(IEnumerable<double> original, IEnumerable<double> estimated);
    3031  }
    3132}
Note: See TracChangeset for help on using the changeset viewer.