From c276df6d05d91b2766f3cf4852c3cb3e9347c38a Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Mon, 31 Mar 2014 11:23:54 +0200 Subject: [PATCH] Statistics: MeanStandardDeviation (analog to MeanVariance) --- src/Numerics/Distributions/LogNormal.cs | 4 ++-- src/Numerics/Distributions/Normal.cs | 4 ++-- src/Numerics/Statistics/ArrayStatistics.cs | 11 +++++++++++ src/Numerics/Statistics/Statistics.cs | 15 +++++++++++++++ src/Numerics/Statistics/StreamingStatistics.cs | 12 ++++++++++++ 5 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/Numerics/Distributions/LogNormal.cs b/src/Numerics/Distributions/LogNormal.cs index e418d593..3e9af3a0 100644 --- a/src/Numerics/Distributions/LogNormal.cs +++ b/src/Numerics/Distributions/LogNormal.cs @@ -110,8 +110,8 @@ namespace MathNet.Numerics.Distributions /// MATLAB: lognfit public static LogNormal Estimate(IEnumerable samples, System.Random randomSource = null) { - var muSigma2 = samples.Select(s => Math.Log(s)).MeanVariance(); - return new LogNormal(muSigma2.Item1, Math.Sqrt(muSigma2.Item2), randomSource); + var muSigma = samples.Select(s => Math.Log(s)).MeanStandardDeviation(); + return new LogNormal(muSigma.Item1, muSigma.Item2, randomSource); } /// diff --git a/src/Numerics/Distributions/Normal.cs b/src/Numerics/Distributions/Normal.cs index ca47d3bf..e7097a9a 100644 --- a/src/Numerics/Distributions/Normal.cs +++ b/src/Numerics/Distributions/Normal.cs @@ -139,8 +139,8 @@ namespace MathNet.Numerics.Distributions /// MATLAB: normfit public static Normal Estimate(IEnumerable samples, System.Random randomSource = null) { - var meanVariance = samples.MeanVariance(); - return new Normal(meanVariance.Item1, Math.Sqrt(meanVariance.Item2), randomSource); + var meanStdDev = samples.MeanStandardDeviation(); + return new Normal(meanStdDev.Item1, meanStdDev.Item2, randomSource); } /// diff --git a/src/Numerics/Statistics/ArrayStatistics.cs b/src/Numerics/Statistics/ArrayStatistics.cs index d2ec678f..9c25938b 100644 --- a/src/Numerics/Statistics/ArrayStatistics.cs +++ b/src/Numerics/Statistics/ArrayStatistics.cs @@ -218,6 +218,17 @@ namespace MathNet.Numerics.Statistics 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). + /// 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(double[] 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). diff --git a/src/Numerics/Statistics/Statistics.cs b/src/Numerics/Statistics/Statistics.cs index 29851b4b..6cd3fa0f 100644 --- a/src/Numerics/Statistics/Statistics.cs +++ b/src/Numerics/Statistics/Statistics.cs @@ -235,6 +235,21 @@ namespace MathNet.Numerics.Statistics : StreamingStatistics.MeanVariance(samples); } + /// + /// Estimates the sample mean and 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 for mean if data is empty or if any entry is NaN and NaN for standard deviation if data has less than two entries or if any entry is NaN. + /// + /// The data to calculate the mean of. + /// The mean of the sample. + public static Tuple MeanStandardDeviation(this IEnumerable samples) + { + var array = samples as double[]; + return array != null + ? ArrayStatistics.MeanStandardDeviation(array) + : StreamingStatistics.MeanStandardDeviation(samples); + } + /// /// Estimates the unbiased population covariance from the provided samples. /// On a dataset of size N will use an N-1 normalizer (Bessel's correction). diff --git a/src/Numerics/Statistics/StreamingStatistics.cs b/src/Numerics/Statistics/StreamingStatistics.cs index b0a65c60..422799d8 100644 --- a/src/Numerics/Statistics/StreamingStatistics.cs +++ b/src/Numerics/Statistics/StreamingStatistics.cs @@ -220,6 +220,18 @@ namespace MathNet.Numerics.Statistics count > 1 ? variance/(count - 1) : double.NaN); } + /// + /// 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) + { + var meanVariance = MeanVariance(samples); + return new Tuple(meanVariance.Item1, Math.Sqrt(meanVariance.Item2)); + } + /// /// 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).