public double Median(Statistics weights) { try { var dict = new Dictionary <double, double>(); for (int i = 0; i < _list.Length; i++) { double value = _list[i]; double weight; dict.TryGetValue(value, out weight); weight += weights._list[i]; dict[value] = weight; } var keys = dict.Keys.ToArray(); Array.Sort(keys); double total = weights.Sum(); double sum = 0; for (int i = 0; i < keys.Length; i++) { sum += dict[keys[i]]; if (sum >= total / 2) { return(keys[i]); } } return(double.NaN); } catch { return(double.NaN); } }
/// <summary> /// Calculates a weighted mean average of the set of numbers. /// See: /// http://en.wikipedia.org/wiki/Weighted_mean /// </summary> /// <param name="weights">The weights</param> /// <returns>Weighted mean</returns> public double Mean(Statistics weights) { try { double sum = 0; for (int i = 0; i < _list.Length; i++) { sum += _list[i] * weights._list[i]; } return(sum / weights.Sum()); } catch (Exception) { return(double.NaN); } }
public double Median(Statistics weights) { try { var dict = new Dictionary<double, double>(); for (int i = 0; i < _list.Length; i++) { double value = _list[i]; double weight; dict.TryGetValue(value, out weight); weight += weights._list[i]; dict[value] = weight; } var keys = dict.Keys.ToArray(); Array.Sort(keys); double total = weights.Sum(); double sum = 0; for (int i = 0; i < keys.Length; i++) { sum += dict[keys[i]]; if (sum >= total / 2) { return keys[i]; } } return double.NaN; } catch { return double.NaN; } }
/// <summary> /// Calculates a weighted mean average of the set of numbers. /// See: /// http://en.wikipedia.org/wiki/Weighted_mean /// </summary> /// <param name="weights">The weights</param> /// <returns>Weighted mean</returns> public double Mean(Statistics weights) { try { double sum = 0; for (int i = 0; i < _list.Length; i++) sum += _list[i] * weights._list[i]; return sum / weights.Sum(); } catch (Exception) { return double.NaN; } }