Browse Source

Statistics: do not use term 'samples' in population statistics

v2
Christoph Ruegg 13 years ago
parent
commit
9104c3b789
  1. 24
      src/Numerics/Statistics/ArrayStatistics.cs
  2. 26
      src/Numerics/Statistics/StreamingStatistics.cs

24
src/Numerics/Statistics/ArrayStatistics.cs

@ -202,24 +202,24 @@ namespace MathNet.Numerics.Statistics
/// On a dataset of size N will use an N normalizer and would thus be biased if applied to a subset. /// 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. /// Returns NaN if data is empty or if any entry is NaN.
/// </summary> /// </summary>
/// <param name="samples1">First sample array.</param> /// <param name="population1">First population array.</param>
/// <param name="samples2">Second sample array.</param> /// <param name="population2">Second population array.</param>
public static double PopulationCovariance(double[] samples1, double[] samples2) public static double PopulationCovariance(double[] population1, double[] population2)
{ {
if (samples1 == null) throw new ArgumentNullException("samples1"); if (population1 == null) throw new ArgumentNullException("population1");
if (samples2 == null) throw new ArgumentNullException("samples2"); if (population2 == null) throw new ArgumentNullException("population2");
if (samples1.Length != samples2.Length) throw new ArgumentException(Resources.ArgumentVectorsSameLength); if (population1.Length != population2.Length) throw new ArgumentException(Resources.ArgumentVectorsSameLength);
if (samples1.Length == 0) return double.NaN; if (population1.Length == 0) return double.NaN;
var mean1 = Mean(samples1); var mean1 = Mean(population1);
var mean2 = Mean(samples2); var mean2 = Mean(population2);
var covariance = 0.0; var covariance = 0.0;
for (int i = 0; i < samples1.Length; i++) for (int i = 0; i < population1.Length; i++)
{ {
covariance += (samples1[i] - mean1) * (samples2[i] - mean2); covariance += (population1[i] - mean1) * (population2[i] - mean2);
} }
return covariance/samples1.Length; return covariance/population1.Length;
} }
/// <summary> /// <summary>

26
src/Numerics/Statistics/StreamingStatistics.cs

@ -245,12 +245,12 @@ namespace MathNet.Numerics.Statistics
/// On a dataset of size N will use an N normalizer and would thus be biased if applied to a subset. /// 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. /// Returns NaN if data is empty or if any entry is NaN.
/// </summary> /// </summary>
/// <param name="samples1">First sample stream.</param> /// <param name="population1">First population stream.</param>
/// <param name="samples2">Second sample stream.</param> /// <param name="population2">Second population stream.</param>
public static double PopulationCovariance(IEnumerable<double> samples1, IEnumerable<double> samples2) public static double PopulationCovariance(IEnumerable<double> population1, IEnumerable<double> population2)
{ {
if (samples1 == null) throw new ArgumentNullException("samples1"); if (population1 == null) throw new ArgumentNullException("population1");
if (samples2 == null) throw new ArgumentNullException("samples2"); if (population2 == null) throw new ArgumentNullException("population2");
// https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance // https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
@ -259,24 +259,24 @@ namespace MathNet.Numerics.Statistics
var mean2 = 0.0; var mean2 = 0.0;
var comoment = 0.0; var comoment = 0.0;
using (var s1 = samples1.GetEnumerator()) using (var p1 = population1.GetEnumerator())
using (var s2 = samples2.GetEnumerator()) using (var p2 = population2.GetEnumerator())
{ {
while (s1.MoveNext()) while (p1.MoveNext())
{ {
if (!s2.MoveNext()) if (!p2.MoveNext())
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength); throw new ArgumentException(Resources.ArgumentVectorsSameLength);
} }
var mean2Prev = mean2; var mean2Prev = mean2;
n++; n++;
mean1 += (s1.Current - mean1) / n; mean1 += (p1.Current - mean1) / n;
mean2 += (s2.Current - mean2) / n; mean2 += (p2.Current - mean2) / n;
comoment += (s1.Current - mean1) * (s2.Current - mean2Prev); comoment += (p1.Current - mean1) * (p2.Current - mean2Prev);
} }
if (s2.MoveNext()) if (p2.MoveNext())
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength); throw new ArgumentException(Resources.ArgumentVectorsSameLength);
} }

Loading…
Cancel
Save