/// <summary>A Vector extension method that variances the given x coordinate.</summary> /// <param name="x">The x to act on.</param> /// <returns>A double.</returns> public static double Variance(this Vector x) { var mean = x.Mean(); var sum = 0d; for (int i = 0; i < x.Length; i++) { sum += System.Math.Pow(x[i] - mean, 2); } return(sum / (x.Length - 1)); }
/// <summary> /// A Vector extension method that computes variances the given x coordinate /// </summary> /// <param name="x">The source to act on.</param> /// <param name="isSamplePop">is sample population?</param> /// <returns>A double.</returns> public static double Variance(this Vector x, bool isSamplePop = false) { var mean = x.Mean(); var sum = 0d; for (int i = 0; i < x.Length; i++) { sum += System.Math.Pow(x[i] - mean, 2); } return(sum / (isSamplePop ? x.Length - 1 : x.Length)); }
/// <summary>A Vector extension method that covariances.</summary> /// <exception cref="InvalidOperationException">Thrown when the requested operation is invalid.</exception> /// <param name="x">The x to act on.</param> /// <param name="y">The Vector to process.</param> /// <returns>A double.</returns> public static double Covariance(this Vector x, Vector y) { if (x.Length != y.Length) { throw new InvalidOperationException("Vectors must be the same length."); } var xmean = x.Mean(); var ymean = y.Mean(); var sum = 0d; for (int i = 0; i < x.Length; i++) { sum += (x[i] - xmean) * (y[i] - ymean); } return(sum / (x.Length - 1)); }
/// <summary> /// Computes the Coefficient of Variation of the Root Mean Squared Error for the given inputs. /// </summary> /// <param name="y1">Predicted values.</param> /// <param name="y2">Actual values.</param> /// <returns>Double.</returns> public static double ComputeCoefRMSE(Vector y1, Vector y2) { return numl.Supervised.Score.ComputeRMSE(y1, y2) / y1.Mean(); }