Changeset 1071 for trunk/sources
- Timestamp:
- 12/25/08 13:58:56 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.DataAnalysis/Statistics.cs
r290 r1071 158 158 if(values.Length == 0) throw new InvalidOperationException(); 159 159 double sum = 0.0; 160 int n = end - start;160 int n = 0; 161 161 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])) { 165 163 sum += values[i]; 166 } 167 } 164 n++; 165 } 166 } 167 if (n == 0) throw new InvalidOperationException(); 168 168 return sum / n; 169 169 } … … 221 221 /// <returns></returns> 222 222 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) 228 224 return 0.0; 229 225 … … 231 227 double squaredErrorsSum = 0.0; 232 228 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); 241 241 } 242 242 }
Note: See TracChangeset
for help on using the changeset viewer.