Free cookie consent management tool by TermsFeed Policy Generator

Changeset 4555


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

Improved time series evaluators. #1142

Location:
branches/DataAnalysis
Files:
1 added
10 edited

Legend:

Unmodified
Added
Removed
  • branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis/3.3/Symbolic/Analyzer/ValidationBestScaledSymbolicTimeSeriesPrognosisSolutionAnalyzer.cs

    r4475 r4555  
    344344      int dimension = selectedTargetVariables.Count();
    345345
    346       IEnumerable<double[]> oneStepPredictions =
    347         SymbolicExpressionTreeInterpreter.GetSymbolicExpressionTreeValues(tree, ProblemData.Dataset, selectedTargetVariables, trainingRows, 1);
    348       IEnumerable<double[]> originalValues = from row in trainingRows
    349                                              select (from targetVariable in selectedTargetVariables
    350                                                      select ProblemData.Dataset[targetVariable, row]).ToArray();
     346      IEnumerable<IEnumerable<double>> oneStepPredictions =
     347        SymbolicExpressionTreeInterpreter.GetSymbolicExpressionTreeValues(tree, ProblemData.Dataset, selectedTargetVariables, trainingRows, 1)
     348        .Cast<IEnumerable<double>>();
     349      IEnumerable<IEnumerable<double>> originalValues = from row in trainingRows
     350                                                        select (from targetVariable in selectedTargetVariables
     351                                                                select ProblemData.Dataset[targetVariable, row]);
    351352      alpha = new double[dimension];
    352353      beta = new double[dimension];
  • branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis/3.3/Symbolic/Evaluators/SymbolicTimeSeriesPrognosisMahalanobisEvaluator.cs

    r4475 r4555  
    6060      double quality;
    6161
     62      Dataset dataset = problemData.Dataset;
    6263      // calculate scaling parameters based on one-step-predictions
    63       IEnumerable<string> selectedTargetVariables = from item in problemData.TargetVariables
    64                                                     where problemData.TargetVariables.ItemChecked(item)
    65                                                     select item.Value;
     64      IEnumerable<string> selectedTargetVariables = (from item in problemData.TargetVariables
     65                                                     where problemData.TargetVariables.ItemChecked(item)
     66                                                     select item.Value).ToArray();
    6667      int dimension = selectedTargetVariables.Count();
    6768
    68       IEnumerable<double[]> oneStepPredictions =
    69         interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, selectedTargetVariables, rows, 1);
    70       IEnumerable<double[]> originalValues = from row in rows
    71                                              select (from targetVariable in selectedTargetVariables
    72                                                      select problemData.Dataset[targetVariable, row]).ToArray();
     69      IEnumerable<int> selectedTargetVariableIndexes = (from targetVariable in selectedTargetVariables
     70                                                        select dataset.GetVariableIndex(targetVariable)).ToArray();
     71      IEnumerable<IEnumerable<double>> oneStepPredictions =
     72        interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, selectedTargetVariables, rows, 1)
     73        .Cast<IEnumerable<double>>();
     74      IEnumerable<IEnumerable<double>> originalValues = from row in rows
     75                                                        select (from targetVariableIndex in selectedTargetVariableIndexes
     76                                                                select dataset[row, targetVariableIndex]);
    7377      alpha = new double[dimension];
    7478      beta = new double[dimension];
     
    8993      IEnumerable<int> rows, int predictionHorizon,
    9094      DoubleArray lowerEstimationLimit, DoubleArray upperEstimationLimit,
    91       double[] alpha, double[] beta) {
     95      double[] beta, double[] alpha) {
    9296
     97      Dataset dataset = problemData.Dataset;
    9398
    94       IEnumerable<string> selectedTargetVariables = from targetVariable in problemData.TargetVariables
    95                                                     where problemData.TargetVariables.ItemChecked(targetVariable)
    96                                                     select targetVariable.Value;
     99      IEnumerable<string> selectedTargetVariables = (from targetVariable in problemData.TargetVariables
     100                                                     where problemData.TargetVariables.ItemChecked(targetVariable)
     101                                                     select targetVariable.Value).ToArray();
    97102
     103      IEnumerable<int> selectedTargetVariableIndexes = (from targetVariable in selectedTargetVariables
     104                                                        select dataset.GetVariableIndex(targetVariable)).ToArray();
    98105      IEnumerable<double[]> estimatedValues =
    99         interpreter.GetScaledSymbolicExpressionTreeValues(tree, problemData.Dataset, selectedTargetVariables,
     106        interpreter.GetScaledSymbolicExpressionTreeValues(tree, dataset, selectedTargetVariables,
    100107        rows, predictionHorizon, beta, alpha);
    101108
    102       IEnumerable<double[]> originalValues = from row in rows
    103                                              from step in Enumerable.Range(0, predictionHorizon)
    104                                              select (from targetVariable in selectedTargetVariables
    105                                                      select problemData.Dataset[targetVariable, row + step]).ToArray();
     109      IEnumerable<IEnumerable<double>> originalValues = from row in rows
     110                                                        from step in Enumerable.Range(0, predictionHorizon)
     111                                                        select (from targetVariableIndex in selectedTargetVariableIndexes
     112                                                                select dataset[row + step, targetVariableIndex]);
    106113
    107114      OnlineMeanMahalanobisDistanceEvaluator evaluator = new OnlineMeanMahalanobisDistanceEvaluator();
    108       IEnumerable<double>[] targetValues = (from targetVariable in problemData.TargetVariables.CheckedItems
    109                                             select problemData.Dataset.GetEnumeratedVariableValues(targetVariable.Value.Value, rows))
     115      // for covariance calculation: array of variables
     116      IEnumerable<double>[] targetValues = (from targetVariableIndex in selectedTargetVariableIndexes
     117                                            select dataset.GetEnumeratedVariableValues(targetVariableIndex, rows))
    110118                                           .ToArray();
    111119      evaluator.InitializeCovarianceMatrixFromSamples(targetValues);
     
    114122      var originalValuesEnumerator = originalValues.GetEnumerator();
    115123      while (originalValuesEnumerator.MoveNext() & estimatedValuesEnumerator.MoveNext()) {
    116         double[] currentOriginal = originalValuesEnumerator.Current;
     124        IEnumerable<double> currentOriginal = originalValuesEnumerator.Current;
    117125        double[] currentEstimated = estimatedValuesEnumerator.Current;
     126        // limit estimated values to bounds
    118127        for (int i = 0; i < currentEstimated.Length; i++) {
    119128          if (double.IsNaN(currentEstimated[i])) currentEstimated[i] = upperEstimationLimit[i];
  • branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis/3.3/Symbolic/Evaluators/SymbolicTimeSeriesPrognosisScaledNormalizedMseEvaluator.cs

    r4475 r4555  
    5656      double quality;
    5757
     58      Dataset dataset = problemData.Dataset;
    5859      // calculate scaling parameters based on one-step-predictions
    59       IEnumerable<string> selectedTargetVariables = from item in problemData.TargetVariables
    60                                                     where problemData.TargetVariables.ItemChecked(item)
    61                                                     select item.Value;
     60      IEnumerable<string> selectedTargetVariables = (from item in problemData.TargetVariables
     61                                                     where problemData.TargetVariables.ItemChecked(item)
     62                                                     select item.Value).ToArray();
    6263      int dimension = selectedTargetVariables.Count();
    6364
    64       IEnumerable<double[]> oneStepPredictions =
    65         interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, selectedTargetVariables, rows, 1);
    66       IEnumerable<double[]> originalValues = from row in rows
    67                                              select (from targetVariable in selectedTargetVariables
    68                                                      select problemData.Dataset[targetVariable, row]).ToArray();
     65      IEnumerable<int> selectedTargetVariableIndexes = (from targetVariable in selectedTargetVariables
     66                                                        select dataset.GetVariableIndex(targetVariable)).ToArray();
     67      IEnumerable<IEnumerable<double>> oneStepPredictions =
     68        interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, selectedTargetVariables, rows, 1).Cast<IEnumerable<double>>();
     69      IEnumerable<IEnumerable<double>> originalValues = from row in rows
     70                                                        select (from targetVariableIndex in selectedTargetVariableIndexes
     71                                                                select dataset[row, targetVariableIndex]);
    6972      alpha = new double[dimension];
    7073      beta = new double[dimension];
     
    8588      DoubleArray lowerEstimationLimit, DoubleArray upperEstimationLimit,
    8689      double[] beta, double[] alpha) {
    87       IEnumerable<string> selectedTargetVariables = from targetVariable in problemData.TargetVariables
    88                                                     where problemData.TargetVariables.ItemChecked(targetVariable)
    89                                                     select targetVariable.Value;
     90      Dataset dataset = problemData.Dataset;
     91      IEnumerable<string> selectedTargetVariables = (from targetVariable in problemData.TargetVariables
     92                                                     where problemData.TargetVariables.ItemChecked(targetVariable)
     93                                                     select targetVariable.Value).ToArray();
     94      IEnumerable<int> selectedTargetVariableIndexes = (from targetVariable in selectedTargetVariables
     95                                                        select dataset.GetVariableIndex(targetVariable)).ToArray();
    9096
    9197      IEnumerable<double[]> estimatedValues =
    92         interpreter.GetScaledSymbolicExpressionTreeValues(tree, problemData.Dataset, selectedTargetVariables,
     98        interpreter.GetScaledSymbolicExpressionTreeValues(tree, dataset, selectedTargetVariables,
    9399        rows, predictionHorizon, beta, alpha);
    94100
    95       IEnumerable<double[]> originalValues = from row in rows
    96                                              from step in Enumerable.Range(0, predictionHorizon)
    97                                              select (from targetVariable in selectedTargetVariables
    98                                                      select problemData.Dataset[targetVariable, row + step]).ToArray();
     101      IEnumerable<IEnumerable<double>> originalValues = from row in rows
     102                                                        from step in Enumerable.Range(0, predictionHorizon)
     103                                                        select (from targetVariableIndex in selectedTargetVariableIndexes
     104                                                                select dataset[row + step, targetVariableIndex]);
    99105
    100106      var evaluator = new OnlineMultiVariateEvaluator<OnlineNormalizedMeanSquaredErrorEvaluator>();
     
    103109      var originalValuesEnumerator = originalValues.GetEnumerator();
    104110      while (originalValuesEnumerator.MoveNext() & estimatedValuesEnumerator.MoveNext()) {
    105         double[] original = originalValuesEnumerator.Current;
     111        IEnumerable<double> original = originalValuesEnumerator.Current;
    106112        double[] estimated = estimatedValuesEnumerator.Current;
    107113        for (int i = 0; i < estimated.Length; i++) {
     
    116122    }
    117123
    118     public static void CalculateScalingParameters(IEnumerable<double[]> originalValues, IEnumerable<double[]> estimatedValues, ref double[] beta, ref double[] alpha) {
    119       List<OnlineMeanAndVarianceCalculator> estimatedVarianceEvaluators = new List<OnlineMeanAndVarianceCalculator>();
    120       List<OnlineCovarianceEvaluator> covarianceEvaluators = new List<OnlineCovarianceEvaluator>();
    121       List<OnlineMeanAndVarianceCalculator> originalMeanCalculators = new List<OnlineMeanAndVarianceCalculator>();
    122       int[] cnt = null;
     124    public static void CalculateScalingParameters(IEnumerable<IEnumerable<double>> originalValues, IEnumerable<IEnumerable<double>> estimatedValues, ref double[] beta, ref double[] alpha) {
     125      List<OnlineLinearScalingCalculator> linearScalingCalculators = new List<OnlineLinearScalingCalculator>();
     126      // initialize lists
     127      int dimension = originalValues.First().Count();
     128      for (int i = 0; i < dimension; i++) {
     129        linearScalingCalculators.Add(new OnlineLinearScalingCalculator());
     130      }
    123131
    124132      var estimatedEnumerator = estimatedValues.GetEnumerator();
    125133      var originalEnumerator = originalValues.GetEnumerator();
     134      // foreach row vector in both series
    126135      while (estimatedEnumerator.MoveNext() & originalEnumerator.MoveNext()) {
    127         double[] original = originalEnumerator.Current;
    128         double[] estimated = estimatedEnumerator.Current;
    129         int dimension = original.Length;
    130         // initialize
    131         if (cnt == null) {
    132           cnt = new int[dimension];
    133           for (int i = 0; i < dimension; i++) {
    134             estimatedVarianceEvaluators.Add(new OnlineMeanAndVarianceCalculator());
    135             covarianceEvaluators.Add(new OnlineCovarianceEvaluator());
    136             originalMeanCalculators.Add(new OnlineMeanAndVarianceCalculator());
     136        IEnumerable<double> original = originalEnumerator.Current;
     137        IEnumerable<double> estimated = estimatedEnumerator.Current;
     138        var originalComponentValuesEnumerator = original.GetEnumerator();
     139        var estimatedComponentValuesEnumerator = estimated.GetEnumerator();
     140
     141        int component = 0;
     142        // for each component in both row vectors
     143        while (originalComponentValuesEnumerator.MoveNext() & estimatedComponentValuesEnumerator.MoveNext() && component < dimension) {
     144          if (IsValidValue(originalComponentValuesEnumerator.Current) && IsValidValue(estimatedComponentValuesEnumerator.Current)) {
     145            linearScalingCalculators[component].Add(originalComponentValuesEnumerator.Current, estimatedComponentValuesEnumerator.Current);
    137146          }
    138         } else if (cnt.Length == dimension) {
    139           for (int component = 0; component < dimension; component++) {
    140             if (IsValidValue(original[component]) && IsValidValue(estimated[component])) {
    141               cnt[component]++;
    142               estimatedVarianceEvaluators[component].Add(estimated[component]);
    143               covarianceEvaluators[component].Add(original[component], estimated[component]);
    144               originalMeanCalculators[component].Add(original[component]);
    145             }
    146           }
    147         } else {
    148           throw new ArgumentException("Dimension of input array doesn't match");
     147          component++;
     148        }
     149        // check if both row vectors are finished
     150        if (originalComponentValuesEnumerator.MoveNext() | estimatedComponentValuesEnumerator.MoveNext() || component < dimension) {
     151          throw new ArgumentException("Number of elements in original and estimated row vectors does not match.");
    149152        }
    150153      }
    151154
     155      // check if both series are finished
    152156      if (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext())
    153157        throw new InvalidOperationException("Number of elements in estimated and original series doesn't match.");
    154       if (cnt == null) throw new ArgumentException("No elements in estimated and original.");
    155158
    156       for (int component = 0; component < cnt.Length; component++) {
    157         if (cnt[component] < 2) {
    158           alpha[component] = 0;
    159           beta[component] = 1;
    160         } else {
    161           if (estimatedVarianceEvaluators[component].PopulationVariance.IsAlmost(0.0))
    162             beta[component] = 1;
    163           else
    164             beta[component] = covarianceEvaluators[component].Covariance / estimatedVarianceEvaluators[component].PopulationVariance;
    165 
    166           alpha[component] = originalMeanCalculators[component].Mean - beta[component] * estimatedVarianceEvaluators[component].Mean;
    167         }
     159      // get alpha and beta for each component
     160      for (int component = 0; component < dimension; component++) {
     161        alpha[component] = linearScalingCalculators[component].Alpha;
     162        beta[component] = linearScalingCalculators[component].Beta;
    168163      }
    169164    }
  • 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}
  • branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Evaluators/SymbolicRegressionScaledMeanAndVarianceSquaredErrorEvaluator.cs

    r4460 r4555  
    126126      IEnumerable<double> originalValues = dataset.GetEnumeratedVariableValues(targetVariable, rows);
    127127      IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, dataset, rows);
    128       CalculateScalingParameters(originalValues, estimatedValues, out beta, out alpha);
     128      SymbolicRegressionScaledMeanSquaredErrorEvaluator.CalculateScalingParameters(originalValues, estimatedValues, out beta, out alpha);
    129129
    130130      return CalculateWithScaling(interpreter, solution, lowerEstimationLimit, upperEstimationLimit, dataset, targetVariable, rows, beta, alpha, out meanSE, out varianceSE, out count, out bias, out variance, out covariance);
     
    174174      }
    175175    }
    176 
    177     /// <summary>
    178     /// Calculates linear scaling parameters in one pass.
    179     /// The formulas to calculate the scaling parameters were taken from Scaled Symblic Regression by Maarten Keijzer.
    180     /// http://www.springerlink.com/content/x035121165125175/
    181     /// </summary>
    182     public static void CalculateScalingParameters(IEnumerable<double> original, IEnumerable<double> estimated, out double beta, out double alpha) {
    183       IEnumerator<double> originalEnumerator = original.GetEnumerator();
    184       IEnumerator<double> estimatedEnumerator = estimated.GetEnumerator();
    185       OnlineMeanAndVarianceCalculator yVarianceCalculator = new OnlineMeanAndVarianceCalculator();
    186       OnlineMeanAndVarianceCalculator tMeanCalculator = new OnlineMeanAndVarianceCalculator();
    187       OnlineCovarianceEvaluator ytCovarianceEvaluator = new OnlineCovarianceEvaluator();
    188       int cnt = 0;
    189 
    190       while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
    191         double y = estimatedEnumerator.Current;
    192         double t = originalEnumerator.Current;
    193         if (IsValidValue(t) && IsValidValue(y)) {
    194           tMeanCalculator.Add(t);
    195           yVarianceCalculator.Add(y);
    196           ytCovarianceEvaluator.Add(y, t);
    197 
    198           cnt++;
    199         }
    200       }
    201 
    202       if (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext())
    203         throw new ArgumentException("Number of elements in original and estimated enumeration doesn't match.");
    204       if (cnt < 2) {
    205         alpha = 0;
    206         beta = 1;
    207       } else {
    208         if (yVarianceCalculator.PopulationVariance.IsAlmost(0.0))
    209           beta = 1;
    210         else
    211           beta = ytCovarianceEvaluator.Covariance / yVarianceCalculator.PopulationVariance;
    212 
    213         alpha = tMeanCalculator.Mean - beta * yVarianceCalculator.Mean;
    214       }
    215     }
    216 
    217     private static bool IsValidValue(double d) {
    218       return !double.IsInfinity(d) && !double.IsNaN(d) && d > -1.0E07 && d < 1.0E07;  // don't consider very large or very small values for scaling
    219     }
    220176  }
    221177}
  • branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Evaluators/SymbolicRegressionScaledMeanSquaredErrorEvaluator.cs

    r4458 r4555  
    100100    }
    101101
    102     /// <summary>
    103     /// Calculates linear scaling parameters in one pass.
    104     /// The formulas to calculate the scaling parameters were taken from Scaled Symblic Regression by Maarten Keijzer.
    105     /// http://www.springerlink.com/content/x035121165125175/
    106     /// </summary>
    107102    public static void CalculateScalingParameters(IEnumerable<double> original, IEnumerable<double> estimated, out double beta, out double alpha) {
    108103      IEnumerator<double> originalEnumerator = original.GetEnumerator();
    109104      IEnumerator<double> estimatedEnumerator = estimated.GetEnumerator();
    110       OnlineMeanAndVarianceCalculator yVarianceCalculator = new OnlineMeanAndVarianceCalculator();
    111       OnlineMeanAndVarianceCalculator tMeanCalculator = new OnlineMeanAndVarianceCalculator();
    112       OnlineCovarianceEvaluator ytCovarianceEvaluator = new OnlineCovarianceEvaluator();
    113       int cnt = 0;
     105      OnlineLinearScalingCalculator linearScalingCalculator = new OnlineLinearScalingCalculator();
    114106
    115107      while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
     
    117109        double t = originalEnumerator.Current;
    118110        if (IsValidValue(t) && IsValidValue(y)) {
    119           tMeanCalculator.Add(t);
    120           yVarianceCalculator.Add(y);
    121           ytCovarianceEvaluator.Add(y, t);
    122 
    123           cnt++;
     111          linearScalingCalculator.Add(t, y);
    124112        }
    125113      }
     
    127115      if (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext())
    128116        throw new ArgumentException("Number of elements in original and estimated enumeration doesn't match.");
    129       if (cnt < 2) {
    130         alpha = 0;
    131         beta = 1;
    132       } else {
    133         if (yVarianceCalculator.PopulationVariance.IsAlmost(0.0))
    134           beta = 1;
    135         else
    136           beta = ytCovarianceEvaluator.Covariance / yVarianceCalculator.PopulationVariance;
    137 
    138         alpha = tMeanCalculator.Mean - beta * yVarianceCalculator.Mean;
     117      else {
     118        beta = linearScalingCalculator.Beta;
     119        alpha = linearScalingCalculator.Alpha;
    139120      }
    140121    }
  • branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis/3.3/HeuristicLab.Problems.DataAnalysis-3.3.csproj

    r4309 r4555  
    109109    <None Include="HeuristicLabProblemsDataAnalysisPlugin.cs.frame" />
    110110    <None Include="Properties\AssemblyInfo.frame" />
     111    <Compile Include="Evaluators\OnlineLinearScalingCalculator.cs" />
    111112    <Compile Include="Evaluators\OnlineCovarianceEvaluator.cs" />
    112113    <Compile Include="Evaluators\OnlineMeanAndVarianceCalculator.cs" />
Note: See TracChangeset for help on using the changeset viewer.