diff --git a/src/Numerics/Statistics/ArrayStatistics.cs b/src/Numerics/Statistics/ArrayStatistics.cs
index 8b0bca39..b3827bd2 100644
--- a/src/Numerics/Statistics/ArrayStatistics.cs
+++ b/src/Numerics/Statistics/ArrayStatistics.cs
@@ -163,6 +163,28 @@ namespace MathNet.Numerics.Statistics
return mean;
}
+ ///
+ /// Estimates the arithmetic sample mean from the unsorted data array.
+ /// Returns NaN if data is empty or any entry is NaN.
+ ///
+ /// Sample array, no sorting is assumed.
+ public static double Mean(float[] data)
+ {
+ if (data.Length == 0)
+ {
+ return float.NaN;
+ }
+
+ double mean = 0;
+ ulong m = 0;
+ for (int i = 0; i < data.Length; i++)
+ {
+ mean += (data[i] - mean) / ++m;
+ }
+
+ return mean;
+ }
+
///
/// Evaluates the geometric mean of the unsorted data array.
/// Returns NaN if data is empty or any entry is NaN.
@@ -230,6 +252,31 @@ namespace MathNet.Numerics.Statistics
return variance/(samples.Length - 1);
}
+ ///
+ /// Estimates the unbiased population variance from the provided samples as unsorted array.
+ /// 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.
+ ///
+ /// Sample array, no sorting is assumed.
+ public static double Variance(float[] samples)
+ {
+ if (samples.Length <= 1)
+ {
+ return float.NaN;
+ }
+
+ double variance = 0;
+ double t = samples[0];
+ for (int i = 1; i < samples.Length; i++)
+ {
+ t += samples[i];
+ double diff = ((i + 1) * samples[i]) - t;
+ variance += (diff * diff) / ((i + 1.0) * i);
+ }
+
+ return variance / (samples.Length - 1);
+ }
+
///
/// Evaluates the population variance from the full population provided as unsorted array.
/// On a dataset of size N will use an N normalizer and would thus be biased if applied to a subset.
@@ -255,6 +302,31 @@ namespace MathNet.Numerics.Statistics
return variance/population.Length;
}
+ ///
+ /// Evaluates the population variance from the full population provided as unsorted array.
+ /// 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.
+ ///
+ /// Sample array, no sorting is assumed.
+ public static double PopulationVariance(float[] population)
+ {
+ if (population.Length == 0)
+ {
+ return float.NaN;
+ }
+
+ double variance = 0;
+ double t = population[0];
+ for (int i = 1; i < population.Length; i++)
+ {
+ t += population[i];
+ double diff = ((i + 1) * population[i]) - t;
+ variance += (diff * diff) / ((i + 1.0) * i);
+ }
+
+ return variance / population.Length;
+ }
+
///
/// 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 (Bessel's correction).
@@ -266,6 +338,17 @@ namespace MathNet.Numerics.Statistics
return Math.Sqrt(Variance(samples));
}
+ ///
+ /// 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 (Bessel's correction).
+ /// Returns NaN if data has less than two entries or if any entry is NaN.
+ ///
+ /// Sample array, no sorting is assumed.
+ public static double StandardDeviation(float[] samples)
+ {
+ return Math.Sqrt(Variance(samples));
+ }
+
///
/// Evaluates the population standard deviation from the full population provided as unsorted array.
/// On a dataset of size N will use an N normalizer and would thus be biased if applied to a subset.
@@ -277,6 +360,17 @@ namespace MathNet.Numerics.Statistics
return Math.Sqrt(PopulationVariance(population));
}
+ ///
+ /// Evaluates the population standard deviation from the full population provided as unsorted array.
+ /// 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.
+ ///
+ /// Sample array, no sorting is assumed.
+ public static double PopulationStandardDeviation(float[] population)
+ {
+ return Math.Sqrt(PopulationVariance(population));
+ }
+
///
/// Estimates the arithmetic sample mean and the unbiased population variance from the provided samples as unsorted array.
/// On a dataset of size N will use an N-1 normalizer (Bessel's correction).
@@ -288,6 +382,17 @@ namespace MathNet.Numerics.Statistics
return new Tuple(Mean(samples), Variance(samples));
}
+ ///
+ /// Estimates the arithmetic sample mean and the unbiased population variance from the provided samples as unsorted array.
+ /// On a dataset of size N will use an N-1 normalizer (Bessel's correction).
+ /// Returns NaN for mean if data is empty or any entry is NaN and NaN for variance if data has less than two entries or if any entry is NaN.
+ ///
+ /// Sample array, no sorting is assumed.
+ public static Tuple MeanVariance(float[] samples)
+ {
+ return new Tuple(Mean(samples), Variance(samples));
+ }
+
///
/// Estimates the arithmetic sample mean and the unbiased population standard deviation from the provided samples as unsorted array.
/// On a dataset of size N will use an N-1 normalizer (Bessel's correction).
@@ -299,6 +404,17 @@ namespace MathNet.Numerics.Statistics
return new Tuple(Mean(samples), StandardDeviation(samples));
}
+ ///
+ /// Estimates the arithmetic sample mean and the unbiased population standard deviation from the provided samples as unsorted array.
+ /// On a dataset of size N will use an N-1 normalizer (Bessel's correction).
+ /// Returns NaN for mean if data is empty or any entry is NaN and NaN for standard deviation if data has less than two entries or if any entry is NaN.
+ ///
+ /// Sample array, no sorting is assumed.
+ public static Tuple MeanStandardDeviation(float[] samples)
+ {
+ return new Tuple(Mean(samples), StandardDeviation(samples));
+ }
+
///
/// 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).
@@ -381,6 +497,28 @@ namespace MathNet.Numerics.Statistics
return Math.Sqrt(mean);
}
+ ///
+ /// 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.
+ ///
+ /// Sample array, no sorting is assumed.
+ public static double RootMeanSquare(float[] data)
+ {
+ if (data.Length == 0)
+ {
+ return double.NaN;
+ }
+
+ double mean = 0;
+ ulong m = 0;
+ for (int i = 0; i < data.Length; i++)
+ {
+ mean += (data[i] * data[i] - mean) / ++m;
+ }
+
+ return Math.Sqrt(mean);
+ }
+
///
/// Returns the order statistic (order 1..N) from the unsorted data array.
/// WARNING: Works inplace and can thus causes the data array to be reordered.
diff --git a/src/Numerics/Statistics/Statistics.cs b/src/Numerics/Statistics/Statistics.cs
index d22ede28..ae6fb487 100644
--- a/src/Numerics/Statistics/Statistics.cs
+++ b/src/Numerics/Statistics/Statistics.cs
@@ -134,6 +134,20 @@ namespace MathNet.Numerics.Statistics
: StreamingStatistics.Mean(data);
}
+ ///
+ /// Evaluates the sample mean, an estimate of the population mean.
+ /// Returns NaN if data is empty or if any entry is NaN.
+ ///
+ /// The data to calculate the mean of.
+ /// The mean of the sample.
+ public static double Mean(this IEnumerable data)
+ {
+ var array = data as float[];
+ return array != null
+ ? ArrayStatistics.Mean(array)
+ : StreamingStatistics.Mean(data);
+ }
+
///
/// Evaluates the sample mean, an estimate of the population mean.
/// Returns NaN if data is empty or if any entry is NaN.
@@ -188,6 +202,20 @@ namespace MathNet.Numerics.Statistics
: StreamingStatistics.Variance(samples);
}
+ ///
+ /// Estimates the unbiased population variance 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.
+ ///
+ /// A subset of samples, sampled from the full population.
+ public static double Variance(this IEnumerable samples)
+ {
+ var array = samples as float[];
+ return array != null
+ ? ArrayStatistics.Variance(array)
+ : StreamingStatistics.Variance(samples);
+ }
+
///
/// Estimates the unbiased population variance from the provided samples.
/// On a dataset of size N will use an N-1 normalizer (Bessel's correction).
@@ -214,6 +242,20 @@ namespace MathNet.Numerics.Statistics
: StreamingStatistics.PopulationVariance(population);
}
+ ///
+ /// Evaluates the variance from the provided full population.
+ /// 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.
+ ///
+ /// The full population data.
+ public static double PopulationVariance(this IEnumerable population)
+ {
+ var array = population as float[];
+ return array != null
+ ? ArrayStatistics.PopulationVariance(array)
+ : StreamingStatistics.PopulationVariance(population);
+ }
+
///
/// Evaluates the variance from the provided full population.
/// On a dataset of size N will use an N normalize and would thus be biased if applied to a subsetr.
@@ -240,6 +282,20 @@ namespace MathNet.Numerics.Statistics
: StreamingStatistics.StandardDeviation(samples);
}
+ ///
+ /// Estimates the unbiased population standard deviation 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.
+ ///
+ /// A subset of samples, sampled from the full population.
+ public static double StandardDeviation(this IEnumerable samples)
+ {
+ var array = samples as float[];
+ return array != null
+ ? ArrayStatistics.StandardDeviation(array)
+ : StreamingStatistics.StandardDeviation(samples);
+ }
+
///
/// Estimates the unbiased population standard deviation from the provided samples.
/// On a dataset of size N will use an N-1 normalizer (Bessel's correction).
@@ -266,6 +322,20 @@ namespace MathNet.Numerics.Statistics
: StreamingStatistics.PopulationStandardDeviation(population);
}
+ ///
+ /// Evaluates the standard deviation from the provided full population.
+ /// 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.
+ ///
+ /// The full population data.
+ public static double PopulationStandardDeviation(this IEnumerable population)
+ {
+ var array = population as float[];
+ return array != null
+ ? ArrayStatistics.PopulationStandardDeviation(array)
+ : StreamingStatistics.PopulationStandardDeviation(population);
+ }
+
///
/// Evaluates the standard deviation from the provided full population.
/// On a dataset of size N will use an N normalizer and would thus be biased if applied to a subset.
diff --git a/src/Numerics/Statistics/StreamingStatistics.cs b/src/Numerics/Statistics/StreamingStatistics.cs
index 538bd560..ddd67c08 100644
--- a/src/Numerics/Statistics/StreamingStatistics.cs
+++ b/src/Numerics/Statistics/StreamingStatistics.cs
@@ -30,6 +30,7 @@
using System;
using System.Collections.Generic;
+using System.Linq;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.Statistics
@@ -155,6 +156,16 @@ namespace MathNet.Numerics.Statistics
return any ? mean : double.NaN;
}
+ ///
+ /// Estimates the arithmetic sample mean from the enumerable, in a single pass without memoization.
+ /// Returns NaN if data is empty or any entry is NaN.
+ ///
+ /// Sample stream, no sorting is assumed.
+ public static double Mean(IEnumerable stream)
+ {
+ return Mean(stream.Select(x => (double)x));
+ }
+
///
/// Evaluates the geometric mean of the enumerable, in a single pass without memoization.
/// Returns NaN if data is empty or any entry is NaN.
@@ -226,6 +237,17 @@ namespace MathNet.Numerics.Statistics
return count > 1 ? variance/(count - 1) : double.NaN;
}
+ ///
+ /// 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 (Bessel's correction).
+ /// Returns NaN if data has less than two entries or if any entry is NaN.
+ ///
+ /// Sample stream, no sorting is assumed.
+ public static double Variance(IEnumerable samples)
+ {
+ return Variance(samples.Select(x => (double)x));
+ }
+
///
/// Evaluates the population variance from the full population provided as enumerable sequence, 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.
@@ -259,6 +281,17 @@ namespace MathNet.Numerics.Statistics
return variance/count;
}
+ ///
+ /// Evaluates the population variance from the full population provided as enumerable sequence, 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.
+ ///
+ /// Sample stream, no sorting is assumed.
+ public static double PopulationVariance(IEnumerable population)
+ {
+ return PopulationVariance(population.Select(x => (double)x));
+ }
+
///
/// 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 (Bessel's correction).
@@ -270,6 +303,17 @@ namespace MathNet.Numerics.Statistics
return Math.Sqrt(Variance(samples));
}
+ ///
+ /// 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 (Bessel's correction).
+ /// Returns NaN if data has less than two entries or if any entry is NaN.
+ ///
+ /// Sample stream, no sorting is assumed.
+ public static double StandardDeviation(IEnumerable samples)
+ {
+ return Math.Sqrt(Variance(samples));
+ }
+
///
/// Evaluates the population standard deviation from the full population provided as enumerable sequence, 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.
@@ -281,6 +325,17 @@ namespace MathNet.Numerics.Statistics
return Math.Sqrt(PopulationVariance(population));
}
+ ///
+ /// Evaluates the population standard deviation from the full population provided as enumerable sequence, 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.
+ ///
+ /// Sample stream, no sorting is assumed.
+ public static double PopulationStandardDeviation(IEnumerable population)
+ {
+ return Math.Sqrt(PopulationVariance(population));
+ }
+
///
/// Estimates the arithmetic sample mean and 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 (Bessel's correction).
@@ -318,6 +373,17 @@ namespace MathNet.Numerics.Statistics
count > 1 ? variance/(count - 1) : double.NaN);
}
+ ///
+ /// Estimates the arithmetic sample mean and 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 (Bessel's correction).
+ /// Returns NaN for mean if data is empty or any entry is NaN, and NaN for variance if data has less than two entries or if any entry is NaN.
+ ///
+ /// Sample stream, no sorting is assumed.
+ public static Tuple MeanVariance(IEnumerable samples)
+ {
+ return MeanVariance(samples.Select(x => (double)x));
+ }
+
///
/// Estimates the arithmetic sample mean and 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 (Bessel's correction).
@@ -330,6 +396,17 @@ namespace MathNet.Numerics.Statistics
return new Tuple(meanVariance.Item1, Math.Sqrt(meanVariance.Item2));
}
+ ///
+ /// Estimates the arithmetic sample mean and 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 (Bessel's correction).
+ /// Returns NaN for mean if data is empty or any entry is NaN, and NaN for standard deviation if data has less than two entries or if any entry is NaN.
+ ///
+ /// Sample stream, no sorting is assumed.
+ public static Tuple MeanStandardDeviation(IEnumerable samples)
+ {
+ return MeanStandardDeviation(samples.Select(x => (double)x));
+ }
+
///
/// 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).
@@ -432,6 +509,16 @@ namespace MathNet.Numerics.Statistics
return any ? Math.Sqrt(mean) : double.NaN;
}
+ ///
+ /// 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.
+ ///
+ /// Sample stream, no sorting is assumed.
+ public static double RootMeanSquare(IEnumerable stream)
+ {
+ return RootMeanSquare(stream.Select(x => (double)x));
+ }
+
///
/// Calculates the entropy of a stream of double values.
/// Returns NaN if any of the values in the stream are NaN.