|
|
|
@ -434,9 +434,9 @@ namespace MathNet.Numerics.Statistics |
|
|
|
return double.NaN; |
|
|
|
} |
|
|
|
|
|
|
|
var mean1 = Mean(samples1); |
|
|
|
var mean2 = Mean(samples2); |
|
|
|
var covariance = 0.0; |
|
|
|
double mean1 = Mean(samples1); |
|
|
|
double mean2 = Mean(samples2); |
|
|
|
double covariance = 0.0; |
|
|
|
for (int i = 0; i < samples1.Length; i++) |
|
|
|
{ |
|
|
|
covariance += (samples1[i] - mean1)*(samples2[i] - mean2); |
|
|
|
@ -445,6 +445,36 @@ namespace MathNet.Numerics.Statistics |
|
|
|
return covariance/(samples1.Length - 1); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Estimates the unbiased population covariance from the provided two sample arrays.
|
|
|
|
/// On a dataset of size N will use an N-1 normalizer (Bessel's correction).
|
|
|
|
/// Returns NaN if data has less than two entries or if any entry is NaN.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="samples1">First sample array.</param>
|
|
|
|
/// <param name="samples2">Second sample array.</param>
|
|
|
|
public static double Covariance(float[] samples1, float[] samples2) |
|
|
|
{ |
|
|
|
if (samples1.Length != samples2.Length) |
|
|
|
{ |
|
|
|
throw new ArgumentException(Resources.ArgumentVectorsSameLength); |
|
|
|
} |
|
|
|
|
|
|
|
if (samples1.Length <= 1) |
|
|
|
{ |
|
|
|
return double.NaN; |
|
|
|
} |
|
|
|
|
|
|
|
double mean1 = Mean(samples1); |
|
|
|
double mean2 = Mean(samples2); |
|
|
|
double covariance = 0.0; |
|
|
|
for (int i = 0; i < samples1.Length; i++) |
|
|
|
{ |
|
|
|
covariance += (samples1[i] - mean1) * (samples2[i] - mean2); |
|
|
|
} |
|
|
|
|
|
|
|
return covariance / (samples1.Length - 1); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Evaluates the population covariance from the full population provided as two arrays.
|
|
|
|
/// On a dataset of size N will use an N normalizer and would thus be biased if applied to a subset.
|
|
|
|
@ -464,9 +494,9 @@ namespace MathNet.Numerics.Statistics |
|
|
|
return double.NaN; |
|
|
|
} |
|
|
|
|
|
|
|
var mean1 = Mean(population1); |
|
|
|
var mean2 = Mean(population2); |
|
|
|
var covariance = 0.0; |
|
|
|
double mean1 = Mean(population1); |
|
|
|
double mean2 = Mean(population2); |
|
|
|
double covariance = 0.0; |
|
|
|
for (int i = 0; i < population1.Length; i++) |
|
|
|
{ |
|
|
|
covariance += (population1[i] - mean1)*(population2[i] - mean2); |
|
|
|
@ -475,6 +505,36 @@ namespace MathNet.Numerics.Statistics |
|
|
|
return covariance/population1.Length; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Evaluates the population covariance from the full population provided as two arrays.
|
|
|
|
/// On a dataset of size N will use an N normalizer and would thus be biased if applied to a subset.
|
|
|
|
/// Returns NaN if data is empty or if any entry is NaN.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="population1">First population array.</param>
|
|
|
|
/// <param name="population2">Second population array.</param>
|
|
|
|
public static double PopulationCovariance(float[] population1, float[] population2) |
|
|
|
{ |
|
|
|
if (population1.Length != population2.Length) |
|
|
|
{ |
|
|
|
throw new ArgumentException(Resources.ArgumentVectorsSameLength); |
|
|
|
} |
|
|
|
|
|
|
|
if (population1.Length == 0) |
|
|
|
{ |
|
|
|
return double.NaN; |
|
|
|
} |
|
|
|
|
|
|
|
double mean1 = Mean(population1); |
|
|
|
double mean2 = Mean(population2); |
|
|
|
double covariance = 0.0; |
|
|
|
for (int i = 0; i < population1.Length; i++) |
|
|
|
{ |
|
|
|
covariance += (population1[i] - mean1) * (population2[i] - mean2); |
|
|
|
} |
|
|
|
|
|
|
|
return covariance / population1.Length; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Estimates the root mean square (RMS) also known as quadratic mean from the unsorted data array.
|
|
|
|
/// Returns NaN if data is empty or any entry is NaN.
|
|
|
|
|