/// <summary> /// Calculates the variance of the values /// </summary> /// <param name="values"></param> /// <returns></returns> public static double Variance(this int[] values) { var oneByN = 1.0 / values.Length; var mu = values.Sum() * oneByN; return(values.Sum(value => Mathh.Pow2(value - mu)) * oneByN); }
/// <summary> /// Calculates the variance and the mean of the values /// </summary> /// <param name="values"></param> /// <param name="mean"></param> /// <returns></returns> public static double Variance(this double[] values, out double mean) { var oneByN = 1.0 / values.Length; var mu = mean = values.Sum() * oneByN; return(values.Sum(value => Mathh.Pow2(value - mu)) * oneByN); }
public static int Sse(this int[] x1, int[] x2) { var n = x1.Length; //if (n != x2.Length) // throw new ArgumentException("x1 and x2 are not in the same length."); var result = 0; for (var i = 0; i < n; i++) { result += Mathh.Pow2(x2[i] - x1[i]); } return(result); }
/// <summary> /// Calculates the mean squared error. /// </summary> /// <param name="x1"></param> /// <param name="x2"></param> /// <returns></returns> public static double Mse(this double[] x1, double[] x2) { var n = x1.Length; if (n != x2.Length) { throw new ArgumentException("x1 and x2 are not in the same length."); } var result = .0; for (var i = 0; i < n; i++) { result += Mathh.Pow2(x2[i] - x1[i]); } return(result / n); }