From 978824c05026f3fdab902817c70ab4b9cbfb607c Mon Sep 17 00:00:00 2001 From: David Prince Date: Fri, 27 Dec 2013 21:57:42 +0800 Subject: [PATCH] Fix to Categorical.Mean property. Categorical.Mean was being calculated as _pmfNormalized.Mean(), which is actually the mean probability across all possible values, rather than the mean value of the distribution. I've replaced it with a loop which performs the following standard calculation for mean value: mean = Sum(x * f(x), x=0..N) where f(x) is the probability mass function, and N is the maximum value. An alternate method of calculation is: Mean = Sum( 1 - F(x), x=0..N) or similar[1]. That might be preferable if it results in less computational error, but it's a bit obscure, so for clarity I decided to go with the standard calculation for mean value. [1]: https://en.wikipedia.org/w/index.php?title=Expected_value&oldid=585750484#Discrete_distribution_taking_only_non-negative_integer_values --- src/Numerics/Distributions/Categorical.cs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Numerics/Distributions/Categorical.cs b/src/Numerics/Distributions/Categorical.cs index ca98c341..337d74da 100644 --- a/src/Numerics/Distributions/Categorical.cs +++ b/src/Numerics/Distributions/Categorical.cs @@ -216,7 +216,18 @@ namespace MathNet.Numerics.Distributions /// public double Mean { - get { return _pmfNormalized.Mean(); } + get + { + var sum = 0.0; + // Mean = Sum(x * f(x), x=0..N) + // where f(x) is the probability mass function, and N is the maximum value. + for (int i = 0; i < _pmfNormalized.Length; i++) + { + sum += i * _pmfNormalized[i]; + } + + return sum; + } } ///