Browse Source

Statistics: clarify inline xml doc and parameter names

v2
Christoph Ruegg 14 years ago
parent
commit
93689ef2b3
  1. 72
      src/Numerics/Statistics/ArrayStatistics.cs
  2. 125
      src/Numerics/Statistics/Statistics.cs
  3. 52
      src/Numerics/Statistics/StreamingStatistics.cs

72
src/Numerics/Statistics/ArrayStatistics.cs

@ -106,69 +106,69 @@ namespace MathNet.Numerics.Statistics
} }
/// <summary> /// <summary>
/// Estimates the unbiased population or sample variance from the unsorted data array. /// Estimates the unbiased population variance from the provided samples as unsorted array.
/// On a dataset of size N will use an N-1 normalizer /// On a dataset of size N will use an N-1 normalizer.
/// Returns NaN if data is empty or any entry is NaN. /// Returns NaN if data has less than two entries or if any entry is NaN.
/// </summary> /// </summary>
/// <param name="data">Sample array, no sorting is assumed.</param> /// <param name="samples">Sample array, no sorting is assumed.</param>
public static double Variance(double[] data) public static double Variance(double[] samples)
{ {
if (data == null) throw new ArgumentNullException("data"); if (samples == null) throw new ArgumentNullException("samples");
if (data.Length <= 1) return double.NaN; if (samples.Length <= 1) return double.NaN;
double variance = 0; double variance = 0;
double t = data[0]; double t = samples[0];
for (int i = 1; i < data.Length; i++) for (int i = 1; i < samples.Length; i++)
{ {
t += data[i]; t += samples[i];
double diff = ((i + 1)*data[i]) - t; double diff = ((i + 1)*samples[i]) - t;
variance += (diff*diff)/((i + 1)*i); variance += (diff*diff)/((i + 1)*i);
} }
return variance/(data.Length - 1); return variance/(samples.Length - 1);
} }
/// <summary> /// <summary>
/// Estimates the unbiased population or sample standard deviation from the unsorted data array. /// Estimates the unbiased population standard deviation from the provided samples as unsorted array.
/// On a dataset of size N will use an N-1 normalizer /// On a dataset of size N will use an N-1 normalizer.
/// Returns NaN if data is empty or any entry is NaN. /// Returns NaN if data has less than two entries or if any entry is NaN.
/// </summary> /// </summary>
/// <param name="data">Sample array, no sorting is assumed.</param> /// <param name="samples">Sample array, no sorting is assumed.</param>
public static double StandardDeviation(double[] data) public static double StandardDeviation(double[] samples)
{ {
return Math.Sqrt(Variance(data)); return Math.Sqrt(Variance(samples));
} }
/// <summary> /// <summary>
/// Estimates the biased population variance from the unsorted data array. /// Evaluates the biased population variance from the provided full population as unsorted array.
/// On a dataset of size N will use an N normalizer /// On a dataset of size N will use an N normalizer.
/// Returns NaN if data is empty or any entry is NaN. /// Returns NaN if data is empty or if any entry is NaN.
/// </summary> /// </summary>
/// <param name="data">Sample array, no sorting is assumed.</param> /// <param name="population">Sample array, no sorting is assumed.</param>
public static double PopulationVariance(double[] data) public static double PopulationVariance(double[] population)
{ {
if (data == null) throw new ArgumentNullException("data"); if (population == null) throw new ArgumentNullException("population");
if (data.Length == 0) return double.NaN; if (population.Length == 0) return double.NaN;
double variance = 0; double variance = 0;
double t = data[0]; double t = population[0];
for (int i = 1; i < data.Length; i++) for (int i = 1; i < population.Length; i++)
{ {
t += data[i]; t += population[i];
double diff = ((i + 1)*data[i]) - t; double diff = ((i + 1)*population[i]) - t;
variance += (diff*diff)/((i + 1)*i); variance += (diff*diff)/((i + 1)*i);
} }
return variance/data.Length; return variance/population.Length;
} }
/// <summary> /// <summary>
/// Estimates the biased population standard deviation from the unsorted data array. /// Evaluates the biased population standard deviation from the provided full population as unsorted array.
/// On a dataset of size N will use an N normalizer /// On a dataset of size N will use an N normalizer.
/// Returns NaN if data is empty or any entry is NaN. /// Returns NaN if data is empty or if any entry is NaN.
/// </summary> /// </summary>
/// <param name="data">Sample array, no sorting is assumed.</param> /// <param name="population">Sample array, no sorting is assumed.</param>
public static double PopulationStandardDeviation(double[] data) public static double PopulationStandardDeviation(double[] population)
{ {
return Math.Sqrt(PopulationVariance(data)); return Math.Sqrt(PopulationVariance(population));
} }
/// <summary> /// <summary>

125
src/Numerics/Statistics/Statistics.cs

@ -41,6 +41,7 @@ namespace MathNet.Numerics.Statistics
{ {
/// <summary> /// <summary>
/// Returns the minimum value in the sample data. /// Returns the minimum value in the sample data.
/// Returns NaN if data is empty or if any entry is NaN.
/// </summary> /// </summary>
/// <param name="data">The sample data.</param> /// <param name="data">The sample data.</param>
/// <returns>The minimum value in the sample data.</returns> /// <returns>The minimum value in the sample data.</returns>
@ -53,6 +54,8 @@ namespace MathNet.Numerics.Statistics
} }
/// <summary> /// <summary>
/// Returns the minimum value in the sample data. /// Returns the minimum value in the sample data.
/// Returns NaN if data is empty or if any entry is NaN.
/// Null-entries are ignored.
/// </summary> /// </summary>
/// <param name="data">The sample data.</param> /// <param name="data">The sample data.</param>
/// <returns>The minimum value in the sample data.</returns> /// <returns>The minimum value in the sample data.</returns>
@ -64,6 +67,7 @@ namespace MathNet.Numerics.Statistics
/// <summary> /// <summary>
/// Returns the maximum value in the sample data. /// Returns the maximum value in the sample data.
/// Returns NaN if data is empty or if any entry is NaN.
/// </summary> /// </summary>
/// <param name="data">The sample data.</param> /// <param name="data">The sample data.</param>
/// <returns>The maximum value in the sample data.</returns> /// <returns>The maximum value in the sample data.</returns>
@ -77,6 +81,8 @@ namespace MathNet.Numerics.Statistics
/// <summary> /// <summary>
/// Returns the maximum value in the sample data. /// Returns the maximum value in the sample data.
/// Returns NaN if data is empty or if any entry is NaN.
/// Null-entries are ignored.
/// </summary> /// </summary>
/// <param name="data">The sample data.</param> /// <param name="data">The sample data.</param>
/// <returns>The maximum value in the sample data.</returns> /// <returns>The maximum value in the sample data.</returns>
@ -88,6 +94,7 @@ namespace MathNet.Numerics.Statistics
/// <summary> /// <summary>
/// Estimates the sample mean. /// Estimates the sample mean.
/// Returns NaN if data is empty or if any entry is NaN.
/// </summary> /// </summary>
/// <param name="data">The data to calculate the mean of.</param> /// <param name="data">The data to calculate the mean of.</param>
/// <returns>The mean of the sample.</returns> /// <returns>The mean of the sample.</returns>
@ -101,6 +108,8 @@ namespace MathNet.Numerics.Statistics
/// <summary> /// <summary>
/// Estimates the sample mean. /// Estimates the sample mean.
/// Returns NaN if data is empty or if any entry is NaN.
/// Null-entries are ignored.
/// </summary> /// </summary>
/// <param name="data">The data to calculate the mean of.</param> /// <param name="data">The data to calculate the mean of.</param>
/// <returns>The mean of the sample.</returns> /// <returns>The mean of the sample.</returns>
@ -111,99 +120,111 @@ namespace MathNet.Numerics.Statistics
} }
/// <summary> /// <summary>
/// Estimates the unbiased population (sample) variance estimator (on a dataset of size N will use an N-1 normalizer). /// Estimates the unbiased population variance from the provided samples.
/// On a dataset of size N will use an N-1 normalizer.
/// Returns NaN if data has less than two entries or if any entry is NaN.
/// </summary> /// </summary>
/// <param name="data">The data to calculate the variance of.</param> /// <param name="samples">A subset of samples, sampled from the full population.</param>
/// <returns>The unbiased population variance of the sample.</returns> public static double Variance(this IEnumerable<double> samples)
public static double Variance(this IEnumerable<double> data)
{ {
var array = data as double[]; var array = samples as double[];
return array != null return array != null
? ArrayStatistics.Variance(array) ? ArrayStatistics.Variance(array)
: StreamingStatistics.Variance(data); : StreamingStatistics.Variance(samples);
} }
/// <summary> /// <summary>
/// Estimates the unbiased population (sample) variance estimator (on a dataset of size N will use an N-1 normalizer) for nullable data. /// Estimates the unbiased population variance from the provided samples.
/// On a dataset of size N will use an N-1 normalizer.
/// Returns NaN if data has less than two entries or if any entry is NaN.
/// Null-entries are ignored.
/// </summary> /// </summary>
/// <param name="data">The data to calculate the variance of.</param> /// <param name="samples">A subset of samples, sampled from the full population.</param>
/// <returns>The population variance of the sample.</returns> public static double Variance(this IEnumerable<double?> samples)
public static double Variance(this IEnumerable<double?> data)
{ {
if (data == null) throw new ArgumentNullException("data"); if (samples == null) throw new ArgumentNullException("samples");
return StreamingStatistics.Variance(data.Where(d => d.HasValue).Select(d => d.Value)); return StreamingStatistics.Variance(samples.Where(d => d.HasValue).Select(d => d.Value));
} }
/// <summary> /// <summary>
/// Estimates the biased population variance estimator (on a dataset of size N will use an N normalizer). /// Evaluates the biased population variance from the provided full population.
/// On a dataset of size N will use an N normalizer.
/// Returns NaN if data is empty or if any entry is NaN.
/// </summary> /// </summary>
/// <param name="data">The data to calculate the variance of.</param> /// <param name="population">The full population data.</param>
/// <returns>The biased population variance of the sample.</returns> public static double PopulationVariance(this IEnumerable<double> population)
public static double PopulationVariance(this IEnumerable<double> data)
{ {
var array = data as double[]; var array = population as double[];
return array != null return array != null
? ArrayStatistics.PopulationVariance(array) ? ArrayStatistics.PopulationVariance(array)
: StreamingStatistics.PopulationVariance(data); : StreamingStatistics.PopulationVariance(population);
} }
/// <summary> /// <summary>
/// Estimates the biased population variance estimator (on a dataset of size N will use an N normalizer) for nullable data. /// Evaluates the biased population variance from the provided full population.
/// On a dataset of size N will use an N normalizer.
/// Returns NaN if data is empty or if any entry is NaN.
/// Null-entries are ignored.
/// </summary> /// </summary>
/// <param name="data">The data to calculate the variance of.</param> /// <param name="population">The full population data.</param>
/// <returns>The population variance of the sample.</returns> public static double PopulationVariance(this IEnumerable<double?> population)
public static double PopulationVariance(this IEnumerable<double?> data)
{ {
if (data == null) throw new ArgumentNullException("data"); if (population == null) throw new ArgumentNullException("population");
return StreamingStatistics.PopulationVariance(data.Where(d => d.HasValue).Select(d => d.Value)); return StreamingStatistics.PopulationVariance(population.Where(d => d.HasValue).Select(d => d.Value));
} }
/// <summary> /// <summary>
/// Estimates the unbiased sample standard deviation (on a dataset of size N will use an N-1 normalizer). /// Estimates the unbiased population standard deviation from the provided samples.
/// On a dataset of size N will use an N-1 normalizer.
/// Returns NaN if data has less than two entries or if any entry is NaN.
/// </summary> /// </summary>
/// <param name="data">The data to calculate the standard deviation of.</param> /// <param name="samples">A subset of samples, sampled from the full population.</param>
/// <returns>The standard deviation of the sample.</returns> public static double StandardDeviation(this IEnumerable<double> samples)
public static double StandardDeviation(this IEnumerable<double> data)
{ {
var array = data as double[]; var array = samples as double[];
return array != null return array != null
? ArrayStatistics.StandardDeviation(array) ? ArrayStatistics.StandardDeviation(array)
: StreamingStatistics.StandardDeviation(data); : StreamingStatistics.StandardDeviation(samples);
} }
/// <summary> /// <summary>
/// Estimates the unbiased sample standard deviation (on a dataset of size N will use an N-1 normalizer). /// Estimates the unbiased population standard deviation from the provided samples.
/// On a dataset of size N will use an N-1 normalizer.
/// Returns NaN if data has less than two entries or if any entry is NaN.
/// Null-entries are ignored.
/// </summary> /// </summary>
/// <param name="data">The data to calculate the standard deviation of.</param> /// <param name="samples">A subset of samples, sampled from the full population.</param>
/// <returns>The standard deviation of the sample.</returns> public static double StandardDeviation(this IEnumerable<double?> samples)
public static double StandardDeviation(this IEnumerable<double?> data)
{ {
if (data == null) throw new ArgumentNullException("data"); if (samples == null) throw new ArgumentNullException("samples");
return StreamingStatistics.StandardDeviation(data.Where(d => d.HasValue).Select(d => d.Value)); return StreamingStatistics.StandardDeviation(samples.Where(d => d.HasValue).Select(d => d.Value));
} }
/// <summary> /// <summary>
/// Estimates the biased sample standard deviation (on a dataset of size N will use an N normalizer). /// Evaluates the biased population standard deviation from the provided full population.
/// On a dataset of size N will use an N normalizer.
/// Returns NaN if data is empty or if any entry is NaN.
/// </summary> /// </summary>
/// <param name="data">The data to calculate the standard deviation of.</param> /// <param name="population">The full population data.</param>
/// <returns>The standard deviation of the sample.</returns> public static double PopulationStandardDeviation(this IEnumerable<double> population)
public static double PopulationStandardDeviation(this IEnumerable<double> data)
{ {
var array = data as double[]; var array = population as double[];
return array != null return array != null
? ArrayStatistics.PopulationStandardDeviation(array) ? ArrayStatistics.PopulationStandardDeviation(array)
: StreamingStatistics.PopulationStandardDeviation(data); : StreamingStatistics.PopulationStandardDeviation(population);
} }
/// <summary> /// <summary>
/// Estimates the biased sample standard deviation (on a dataset of size N will use an N normalizer). /// Evaluates the biased population standard deviation from the provided full population.
/// On a dataset of size N will use an N normalizer.
/// Returns NaN if data is empty or if any entry is NaN.
/// Null-entries are ignored.
/// </summary> /// </summary>
/// <param name="data">The data to calculate the standard deviation of.</param> /// <param name="population">The full population data.</param>
/// <returns>The standard deviation of the sample.</returns> public static double PopulationStandardDeviation(this IEnumerable<double?> population)
public static double PopulationStandardDeviation(this IEnumerable<double?> data)
{ {
if (data == null) throw new ArgumentNullException("data"); if (population == null) throw new ArgumentNullException("population");
return StreamingStatistics.PopulationStandardDeviation(data.Where(d => d.HasValue).Select(d => d.Value)); return StreamingStatistics.PopulationStandardDeviation(population.Where(d => d.HasValue).Select(d => d.Value));
} }
/// <summary> /// <summary>
@ -290,7 +311,7 @@ namespace MathNet.Numerics.Statistics
} }
/// <summary> /// <summary>
/// Estimates the empiric inverse CDF at tau from the provided samples. /// Estimates the empirical inverse CDF at tau from the provided samples.
/// </summary> /// </summary>
/// <param name="data">The data sample sequence.</param> /// <param name="data">The data sample sequence.</param>
/// <param name="tau">Quantile selector, between 0.0 and 1.0 (inclusive).</param> /// <param name="tau">Quantile selector, between 0.0 and 1.0 (inclusive).</param>
@ -302,7 +323,7 @@ namespace MathNet.Numerics.Statistics
} }
/// <summary> /// <summary>
/// Estimates the empiric inverse CDF at tau from the provided samples. /// Estimates the empirical inverse CDF at tau from the provided samples.
/// </summary> /// </summary>
/// <param name="data">The data sample sequence.</param> /// <param name="data">The data sample sequence.</param>
/// <param name="tau">Quantile selector, between 0.0 and 1.0 (inclusive).</param> /// <param name="tau">Quantile selector, between 0.0 and 1.0 (inclusive).</param>
@ -314,7 +335,7 @@ namespace MathNet.Numerics.Statistics
} }
/// <summary> /// <summary>
/// Estimates the empiric inverse CDF at tau from the provided samples. /// Estimates the empirical inverse CDF at tau from the provided samples.
/// </summary> /// </summary>
/// <param name="data">The data sample sequence.</param> /// <param name="data">The data sample sequence.</param>
public static Func<double, double> InverseCDFFunc(this IEnumerable<double> data) public static Func<double, double> InverseCDFFunc(this IEnumerable<double> data)
@ -326,7 +347,7 @@ namespace MathNet.Numerics.Statistics
} }
/// <summary> /// <summary>
/// Estimates the empiric inverse CDF at tau from the provided samples. /// Estimates the empirical inverse CDF at tau from the provided samples.
/// </summary> /// </summary>
/// <param name="data">The data sample sequence.</param> /// <param name="data">The data sample sequence.</param>
public static Func<double, double> InverseCDFFunc(this IEnumerable<double?> data) public static Func<double, double> InverseCDFFunc(this IEnumerable<double?> data)

52
src/Numerics/Statistics/StreamingStatistics.cs

@ -107,19 +107,19 @@ namespace MathNet.Numerics.Statistics
} }
/// <summary> /// <summary>
/// Estimates the unbiased population or sample variance from the enumerable, in a single pass without memoization. /// Estimates the unbiased population variance from the provided samples as enumerable sequence, in a single pass without memoization.
/// On a dataset of size N will use an N-1 normalizer /// On a dataset of size N will use an N-1 normalizer.
/// Returns NaN if data is empty or any entry is NaN. /// Returns NaN if data has less than two entries or if any entry is NaN.
/// </summary> /// </summary>
/// <param name="stream">Sample stream, no sorting is assumed.</param> /// <param name="samples">Sample stream, no sorting is assumed.</param>
public static double Variance(IEnumerable<double> stream) public static double Variance(IEnumerable<double> samples)
{ {
if (stream == null) throw new ArgumentNullException("stream"); if (samples == null) throw new ArgumentNullException("samples");
double variance = 0; double variance = 0;
double t = 0; double t = 0;
ulong j = 0; ulong j = 0;
using (var iterator = stream.GetEnumerator()) using (var iterator = samples.GetEnumerator())
{ {
if (iterator.MoveNext()) if (iterator.MoveNext())
{ {
@ -140,30 +140,30 @@ namespace MathNet.Numerics.Statistics
} }
/// <summary> /// <summary>
/// Estimates the unbiased population or sample standard deviation from the enumerable, in a single pass without memoization. /// Estimates the unbiased population standard deviation from the provided samples as enumerable sequence, in a single pass without memoization.
/// On a dataset of size N will use an N-1 normalizer /// On a dataset of size N will use an N-1 normalizer.
/// Returns NaN if data is empty or any entry is NaN. /// Returns NaN if data has less than two entries or if any entry is NaN.
/// </summary> /// </summary>
/// <param name="stream">Sample stream, no sorting is assumed.</param> /// <param name="samples">Sample stream, no sorting is assumed.</param>
public static double StandardDeviation(IEnumerable<double> stream) public static double StandardDeviation(IEnumerable<double> samples)
{ {
return Math.Sqrt(Variance(stream)); return Math.Sqrt(Variance(samples));
} }
/// <summary> /// <summary>
/// Estimates the biased population variance from the enumerable, in a single pass without memoization. /// Evaluates the biased population variance from the provided full population as enumerable sequence, in a single pass without memoization.
/// On a dataset of size N will use an N normalizer /// On a dataset of size N will use an N normalizer.
/// Returns NaN if data is empty or any entry is NaN. /// Returns NaN if data is empty or if any entry is NaN.
/// </summary> /// </summary>
/// <param name="stream">Sample stream, no sorting is assumed.</param> /// <param name="population">Sample stream, no sorting is assumed.</param>
public static double PopulationVariance(IEnumerable<double> stream) public static double PopulationVariance(IEnumerable<double> population)
{ {
if (stream == null) throw new ArgumentNullException("stream"); if (population == null) throw new ArgumentNullException("population");
double variance = 0; double variance = 0;
double t = 0; double t = 0;
ulong j = 0; ulong j = 0;
using (var iterator = stream.GetEnumerator()) using (var iterator = population.GetEnumerator())
{ {
if (iterator.MoveNext()) if (iterator.MoveNext())
{ {
@ -184,14 +184,14 @@ namespace MathNet.Numerics.Statistics
} }
/// <summary> /// <summary>
/// Estimates the biased population standard deviation from the enumerable, in a single pass without memoization. /// Evaluates the biased population standard deviation from the provided full population as enumerable sequence, in a single pass without memoization.
/// On a dataset of size N will use an N normalizer /// On a dataset of size N will use an N normalizer.
/// Returns NaN if data is empty or any entry is NaN. /// Returns NaN if data is empty or if any entry is NaN.
/// </summary> /// </summary>
/// <param name="stream">Sample stream, no sorting is assumed.</param> /// <param name="population">Sample stream, no sorting is assumed.</param>
public static double PopulationStandardDeviation(IEnumerable<double> stream) public static double PopulationStandardDeviation(IEnumerable<double> population)
{ {
return Math.Sqrt(PopulationVariance(stream)); return Math.Sqrt(PopulationVariance(population));
} }
} }
} }

Loading…
Cancel
Save