Browse Source

Statistics: single precision support on Covariance #235

netstandard
Christoph Ruegg 11 years ago
parent
commit
d3ec875011
  1. 72
      src/Numerics/Statistics/ArrayStatistics.cs
  2. 32
      src/Numerics/Statistics/Statistics.cs
  3. 24
      src/Numerics/Statistics/StreamingStatistics.cs

72
src/Numerics/Statistics/ArrayStatistics.cs

@ -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.

32
src/Numerics/Statistics/Statistics.cs

@ -508,6 +508,22 @@ namespace MathNet.Numerics.Statistics
: StreamingStatistics.Covariance(samples1, samples2);
}
/// <summary>
/// Estimates the unbiased population covariance from the provided samples.
/// 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">A subset of samples, sampled from the full population.</param>
/// <param name="samples2">A subset of samples, sampled from the full population.</param>
public static double Covariance(this IEnumerable<float> samples1, IEnumerable<float> samples2)
{
var array1 = samples1 as float[];
var array2 = samples2 as float[];
return array1 != null && array2 != null
? ArrayStatistics.Covariance(array1, array2)
: StreamingStatistics.Covariance(samples1, samples2);
}
/// <summary>
/// Estimates the unbiased population covariance from the provided samples.
/// On a dataset of size N will use an N-1 normalizer (Bessel's correction).
@ -537,6 +553,22 @@ namespace MathNet.Numerics.Statistics
: StreamingStatistics.PopulationCovariance(population1, population2);
}
/// <summary>
/// Evaluates the population covariance from the provided full populations.
/// 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">The full population data.</param>
/// <param name="population2">The full population data.</param>
public static double PopulationCovariance(this IEnumerable<float> population1, IEnumerable<float> population2)
{
var array1 = population1 as float[];
var array2 = population2 as float[];
return array1 != null && array2 != null
? ArrayStatistics.PopulationCovariance(array1, array2)
: StreamingStatistics.PopulationCovariance(population1, population2);
}
/// <summary>
/// Evaluates the population covariance from the provided full populations.
/// On a dataset of size N will use an N normalize and would thus be biased if applied to a subset.

24
src/Numerics/Statistics/StreamingStatistics.cs

@ -448,6 +448,18 @@ namespace MathNet.Numerics.Statistics
return n > 1 ? comoment/(n - 1) : double.NaN;
}
/// <summary>
/// Estimates the unbiased population covariance from the provided two sample enumerable sequences, in a single pass without memoization.
/// 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 stream.</param>
/// <param name="samples2">Second sample stream.</param>
public static double Covariance(IEnumerable<float> samples1, IEnumerable<float> samples2)
{
return Covariance(samples1.Select(x => (double)x), samples2.Select(x => (double)x));
}
/// <summary>
/// Evaluates the population covariance from the full population provided as two enumerable sequences, in a single pass without memoization.
/// On a dataset of size N will use an N normalizer and would thus be biased if applied to a subset.
@ -489,6 +501,18 @@ namespace MathNet.Numerics.Statistics
return comoment/n;
}
/// <summary>
/// Evaluates the population covariance from the full population provided as two enumerable sequences, in a single pass without memoization.
/// 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 stream.</param>
/// <param name="population2">Second population stream.</param>
public static double PopulationCovariance(IEnumerable<float> population1, IEnumerable<float> population2)
{
return PopulationCovariance(population1.Select(x => (double)x), population2.Select(x => (double)x));
}
/// <summary>
/// Estimates the root mean square (RMS) also known as quadratic mean from the enumerable, in a single pass without memoization.
/// Returns NaN if data is empty or any entry is NaN.

Loading…
Cancel
Save