Пример #1
0
 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);
     }
 }
Пример #2
0
 /// <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);
     }
 }
Пример #3
0
 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;
     }
 }
Пример #4
0
 /// <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;
     }
 }