Browse Source

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
provider
David Prince 13 years ago
parent
commit
978824c050
  1. 13
      src/Numerics/Distributions/Categorical.cs

13
src/Numerics/Distributions/Categorical.cs

@ -216,7 +216,18 @@ namespace MathNet.Numerics.Distributions
/// </summary> /// </summary>
public double Mean 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;
}
} }
/// <summary> /// <summary>

Loading…
Cancel
Save