Free cookie consent management tool by TermsFeed Policy Generator

Changeset 1783 for trunk/sources


Ignore:
Timestamp:
05/12/09 17:27:51 (16 years ago)
Author:
abeham
Message:

simplified code (#573)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.StatisticalAnalysis/3.2/SimpleStatisticsCalculator.cs

    r1745 r1783  
    8282      #endregion
    8383
     84      int len = samples.Length;
     85      if (len < 1) throw new ArgumentException("ERROR in SimpleStatisticsCalculator: Sample size is less than 1");
     86
    8487      Array.Sort<double>(samples);
    85       int len = samples.Length;
    86       if (len <= 1) throw new ArgumentException("ERROR in SimpleStatisticsCalculator: Sample size is less or equal than 1");
    8788      double mean = 0.0;
    8889      double median = ((len % 2 == 0) ? ((samples[len / 2 - 1] + samples[len / 2]) / 2.0) : (samples[len / 2]));
     
    9596      }
    9697      mean = sum / (double)len;
    97       for (int i = 0; i < samples.Length; i++) {
    98         stdDev = Math.Pow(mean - samples[i], 2);
     98      if (len > 1) {
     99        for (int i = 0; i < samples.Length; i++) {
     100          stdDev = Math.Pow(mean - samples[i], 2);
     101        }
     102        stdDev = Math.Sqrt(stdDev / (double)(len - 1));
    99103      }
    100       stdDev = Math.Sqrt(stdDev / (double)(len - 1));
    101104
    102105      #region output variables
    103       if (GetVariableInfo("Mean").Local) {
    104         IVariable var = GetVariable(GetVariableInfo("Mean").ActualName);
    105         if (var == null) AddVariable(new Variable(GetVariableInfo("Mean").ActualName, new DoubleData(mean)));
    106         else (var.Value as DoubleData).Data = mean;
    107       } else {
    108         string name = scope.TranslateName("Mean");
    109         IVariable var = scope.GetVariable(name);
    110         if (var == null) scope.AddVariable(new Variable(name, new DoubleData(mean)));
    111         else (var.Value as DoubleData).Data = mean;
    112       }
    113       if (GetVariableInfo("Median").Local) {
    114         IVariable var = GetVariable(GetVariableInfo("Median").ActualName);
    115         if (var == null) AddVariable(new Variable(GetVariableInfo("Median").ActualName, new DoubleData(median)));
    116         else (var.Value as DoubleData).Data = median;
    117       } else {
    118         string name = scope.TranslateName("Median");
    119         IVariable var = scope.GetVariable(name);
    120         if (var == null) scope.AddVariable(new Variable(name, new DoubleData(median)));
    121         else (var.Value as DoubleData).Data = median;
    122       }
    123       if (GetVariableInfo("StdDev").Local) {
    124         IVariable var = GetVariable(GetVariableInfo("StdDev").ActualName);
    125         if (var == null) AddVariable(new Variable(GetVariableInfo("StdDev").ActualName, new DoubleData(stdDev)));
    126         else (var.Value as DoubleData).Data = stdDev;
    127       } else {
    128         string name = scope.TranslateName("StdDev");
    129         IVariable var = scope.GetVariable(name);
    130         if (var == null) scope.AddVariable(new Variable(name, new DoubleData(stdDev)));
    131         else (var.Value as DoubleData).Data = stdDev;
    132       }
    133       if (GetVariableInfo("Sum").Local) {
    134         IVariable var = GetVariable(GetVariableInfo("Sum").ActualName);
    135         if (var == null) AddVariable(new Variable(GetVariableInfo("Sum").ActualName, new DoubleData(sum)));
    136         else (var.Value as DoubleData).Data = sum;
    137       } else {
    138         string name = scope.TranslateName("Sum");
    139         IVariable var = scope.GetVariable(name);
    140         if (var == null) scope.AddVariable(new Variable(name, new DoubleData(sum)));
    141         else (var.Value as DoubleData).Data = sum;
    142       }
    143       if (GetVariableInfo("Minimum").Local) {
    144         IVariable var = GetVariable(GetVariableInfo("Minimum").ActualName);
    145         if (var == null) AddVariable(new Variable(GetVariableInfo("Minimum").ActualName, new DoubleData(min)));
    146         else (var.Value as DoubleData).Data = min;
    147       } else {
    148         string name = scope.TranslateName("Minimum");
    149         IVariable var = scope.GetVariable(name);
    150         if (var == null) scope.AddVariable(new Variable(name, new DoubleData(min)));
    151         else (var.Value as DoubleData).Data = min;
    152       }
    153       if (GetVariableInfo("Maximum").Local) {
    154         IVariable var = GetVariable(GetVariableInfo("Maximum").ActualName);
    155         if (var == null) AddVariable(new Variable(GetVariableInfo("Maximum").ActualName, new DoubleData(max)));
    156         else (var.Value as DoubleData).Data = max;
    157       } else {
    158         string name = scope.TranslateName("Maximum");
    159         IVariable var = scope.GetVariable(name);
    160         if (var == null) scope.AddVariable(new Variable(name, new DoubleData(max)));
    161         else (var.Value as DoubleData).Data = max;
    162       }
     106      WriteVariable(GetVariableInfo("Mean"), mean, scope);
     107      WriteVariable(GetVariableInfo("Median"), median, scope);
     108      WriteVariable(GetVariableInfo("StdDev"), stdDev, scope);
     109      WriteVariable(GetVariableInfo("Sum"), sum, scope);
     110      WriteVariable(GetVariableInfo("Minimum"), min, scope);
     111      WriteVariable(GetVariableInfo("Maximum"), max, scope);
    163112      #endregion
    164113      return null;
    165114    }
     115
     116    private void WriteVariable(IVariableInfo info, double value, IScope scope) {
     117      if (info.Local) {
     118        IVariable var = GetVariable(info.ActualName);
     119        if (var == null) AddVariable(new Variable(info.ActualName, new DoubleData(value)));
     120        else (var.Value as DoubleData).Data = value;
     121      } else {
     122        string name = scope.TranslateName(info.FormalName);
     123        IVariable var = scope.GetVariable(name);
     124        if (var == null) scope.AddVariable(new Variable(name, new DoubleData(value)));
     125        else (var.Value as DoubleData).Data = value;
     126      }
     127    }
    166128  }
    167129}
Note: See TracChangeset for help on using the changeset viewer.