/// <summary> /// Calculates the a term (slope) of the linear regression function (y = a*x + b) /// 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 + b</returns> public static double ATerm2(Statistics y, Statistics x) { try { return Covariance(y, x) / (Math.Pow(x.StdDev(), 2)); } catch (Exception) { return double.NaN; } }
/// <summary> /// Calculates the covariance between two sets of numbers. /// </summary> /// <param name="s1">First set of numbers</param> /// <param name="s2">Second set of numbers</param> /// <returns></returns> public static double Covariance(Statistics s1, Statistics s2) { try { if (s1.Length != s2.Length) return double.NaN; int len = s1.Length; double sumMul = 0; for (int i = 0; i < len; i++) sumMul += (s1._list[i]*s2._list[i]); return (sumMul - len*s1.Mean()*s2.Mean())/(len - 1); } catch (Exception) { return double.NaN; } }
/// <summary> /// Calculates the slope (a term) of the linear regression function (y = a*x + b) /// given the Y and X values. /// </summary> /// <param name="y">Y values</param> /// <param name="x">X values</param> /// <returns>The slope</returns> public static double Slope(Statistics y, Statistics x) { return ATerm2(y, x); }
/// <summary> /// Calculates the slope (a term) of the linear regression function (y = a*x + b) /// using the current set of numbers as Y values and another set /// as X values. /// </summary> /// <param name="x">X values</param> /// <returns>The slope</returns> public double Slope(Statistics x) { return ATerm2(x); }
/// <summary> /// Calculates the a term (slope) of the linear regression function (y = a*x + b) /// using the current set of numbers as Y values and another set /// as X values. /// </summary> /// <param name="x">X values</param> /// <returns>The a term of y = a*x + b</returns> public double ATerm2(Statistics x) { return ATerm2(this, x); }