/// <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; } }
/// <summary> /// Calculates the a term of the quadratic regression function (y = a*x^2 + b*x + c) /// given the Y and X values. /// </summary> /// <param name="y">Y values</param> /// <param name="x">X values</param> /// <returns>The a term of y = a*x^2 + b*x + c</returns> public static double ATerm3(Statistics y, Statistics x) { if (x.Length < 3) throw new InvalidOperationException("Insufficient pairs of co-ordinates"); // Not L10N //notation sjk to mean the sum of x_i^j*y_i^k. double s40 = x._list.Sum(v => v*v*v*v); //sum of x^4 double s30 = x._list.Sum(v => v*v*v); //sum of x^3 double s20 = x._list.Sum(v => v*v); //sum of x^2 double s10 = x.Sum(); //sum of x double s00 = x.Length; //sum of x^0 * y^0 ie 1 * number of entries double s21 = x._list.Select((v, i) => v*v*y._list[i]).Sum(); //sum of x^2*y double s11 = x._list.Select((v, i) => v*y._list[i]).Sum(); //sum of x*y double s01 = y.Sum(); //sum of y //a = Da/D return (s21 * (s20 * s00 - s10 * s10) - s11 * (s30 * s00 - s10 * s20) + s01 * (s30 * s10 - s20 * s20)) / (s40 * (s20 * s00 - s10 * s10) - s30 * (s30 * s00 - s10 * s20) + s20 * (s30 * s10 - s20 * s20)); }
public static RatioValue Calculate(IList<double> numerators, IList<double> denominators) { if (numerators.Count != denominators.Count) { throw new ArgumentException(); } if (numerators.Count == 0) { return null; } if (numerators.Count == 1) { return new RatioValue(numerators.First()/denominators.First()); } var statsNumerators = new Statistics(numerators); var statsDenominators = new Statistics(denominators); var ratios = new Statistics(numerators.Select((value, index) => value/denominators[index])); // The mean ratio is the average of "ratios" weighted by "statsDenominators". // It's also equal to the sum of the numerators divided by the sum of the denominators. var meanRatio = statsNumerators.Sum()/statsDenominators.Sum(); // Helpers.Assume(Math.Abs(mean - stats.Mean(statsW)) < 0.0001); // Make sure the value does not exceed the bounds of a float. float meanRatioFloat = (float)Math.Min(float.MaxValue, Math.Max(float.MinValue, meanRatio)); return new RatioValue { Ratio = meanRatioFloat, StdDev = (float) ratios.StdDev(statsDenominators), DotProduct = (float) statsNumerators.Angle(statsDenominators), }; }