Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/25/08 13:58:01 (16 years ago)
Author:
gkronber
Message:

fixed #448 (Calculation of mean throws an exception if the input data contains NaN values) in CEDMA refactoring branch (#419).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/CEDMA-Refactoring-Ticket419/HeuristicLab.DataAnalysis/Statistics.cs

    r290 r1070  
    158158      if(values.Length == 0) throw new InvalidOperationException();
    159159      double sum = 0.0;
    160       int n = end - start;
     160      int n = 0;
    161161      for(int i = start; i < end; i++) {
    162         if(double.IsNaN(values[i])) {
    163           throw new NotFiniteNumberException();
    164         } else {
     162        if(!double.IsNaN(values[i])) {
    165163          sum += values[i];
    166         }
    167       }
     164          n++;
     165        }
     166      }
     167      if (n == 0) throw new InvalidOperationException();
    168168      return sum / n;
    169169    }
     
    221221    /// <returns></returns>
    222222    public static double Variance(double[] values, int start, int end) {
    223       int n = end - start;
    224       if(n == 0) {
    225         throw new InvalidOperationException();
    226       }
    227       if(n == 1)
     223      if (end - start == 1)
    228224        return 0.0;
    229225
     
    231227      double squaredErrorsSum = 0.0;
    232228
    233       for(int i = start; i < end; i++) {
    234         if(double.IsNaN(values[i])) {
    235           throw new NotFiniteNumberException();
    236         }
    237         double d = values[i] - mean;
    238         squaredErrorsSum += d * d;
    239       }
    240       return squaredErrorsSum / (n - 1);     
     229      int n = 0;
     230      for (int i = start; i < end; i++) {
     231        if (!double.IsNaN(values[i])) {
     232          double d = values[i] - mean;
     233          squaredErrorsSum += d * d;
     234          n++;
     235        }
     236      }
     237      if (n < 2) {
     238        throw new InvalidOperationException();
     239      }
     240      return squaredErrorsSum / (n - 1);
    241241    }
    242242  }
Note: See TracChangeset for help on using the changeset viewer.