Changeset 2136 for trunk/sources/HeuristicLab.DataAnalysis
- Timestamp:
- 07/06/09 17:12:37 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.DataAnalysis/3.2/Statistics.cs
r1788 r2136 119 119 120 120 /// <summary> 121 /// calculates the sum of all values.122 /// </summary>123 /// <param name="values"></param>124 /// <returns></returns>125 public static double Sum(double[] values) {126 int n = values.Length;127 double sum = 0.0;128 for (int i = 0; i < n; i++) {129 if (double.IsNaN(values[i])) {130 throw new NotFiniteNumberException();131 } else {132 sum += values[i];133 }134 }135 return sum;136 }137 138 /// <summary>139 121 /// Calculates the mean of all values. 140 122 /// </summary> … … 158 140 /// <returns></returns> 159 141 public static double Mean(double[] values, int start, int end) { 160 if (values.Length == 0) throw new InvalidOperationException(); 142 if (values.Length == 0) throw new ArgumentException("Values is empty."); 143 if(end <=start) throw new ArgumentException("End is smaller or equal start"); 161 144 double sum = 0.0; 162 145 int n = 0; … … 167 150 } 168 151 } 169 if (n == 0) throw new InvalidOperationException(); 170 return sum / n; 152 if (n > 0) 153 return sum / n; 154 else throw new ArgumentException("Only NaN elements in values"); 171 155 } 172 156 … … 223 207 /// <returns></returns> 224 208 public static double Variance(double[] values, int start, int end) { 209 if (values.Length == 0) throw new ArgumentException("Values is empty."); 210 if (end <= start) throw new ArgumentException("End is smaller or equal start"); 225 211 if (end - start == 1) 226 212 return 0.0; … … 238 224 } 239 225 if (n < 2) { 240 throw new InvalidOperationException();226 throw new ArgumentException("Only one non-NaN element in values"); 241 227 } 242 228 return squaredErrorsSum / (n - 1);
Note: See TracChangeset
for help on using the changeset viewer.