Browse Source

Added constructor for Categorical distribution from Histogram.

la-knuth
Jurgen Van Gael 17 years ago
parent
commit
3c3c41bdba
  1. 46
      src/Numerics/Distributions/Discrete/Categorical.cs
  2. 24
      src/UnitTests/DistributionTests/Discrete/CategoricalTests.cs

46
src/Numerics/Distributions/Discrete/Categorical.cs

@ -50,7 +50,7 @@ namespace MathNet.Numerics.Distributions
public class Categorical : IDiscreteDistribution
{
/// <summary>
/// Stores the normalized categorical probabilities.
/// Stores the unnormalized categorical probabilities.
/// </summary>
private double[] _p;
@ -71,24 +71,31 @@ namespace MathNet.Numerics.Distributions
RandomSource = new System.Random();
}
/* TODO
/// <summary>
/// Generate a categorical distribution from histogram <paramref name="h"/>. The distribution will
/// not be automatically updated when the histogram changes.
/// Initializes a new instance of the Categorical class from a histogram <paramref name="h"/>. The distribution
/// will not be automatically updated when the histogram changes. The categorical distribution will have
/// one value for each bucket and a probability for that value proportional to the bucket count.
/// </summary>
public Categorical(Histogram h)
/// <param name="h">The histogram from which to create the categorical variable.</param>
public Categorical(Histogram histogram)
{
if (histogram == null)
{
throw new ArgumentNullException("Cannot create a categorical variable from a null histogram.");
}
// The probability distribution vector.
_p = new double[h.BinCount];
double[] p = new double[histogram.BucketCount];
// Fill in the distribution vector.
for (int i = 0; i < h.BinCount; i++)
for (int i = 0; i < histogram.BucketCount; i++)
{
_p[i] = h[i];
p[i] = histogram[i].Count;
}
RandomNumberGenerator = new System.Random();
}*/
SetParameters(p);
RandomSource = new System.Random();
}
/// <summary>
/// A string representation of the distribution.
@ -144,13 +151,28 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Gets or sets the probability of generating a one.
/// Gets or sets the normalized probability vector of the multinomial.
/// </summary>
/// <remarks>Note that sometimes the normalized probability vector cannot be represented
/// exactly in a floating point representation.</remarks>
public double[] P
{
get
{
return (double[]) _p.Clone();
double[] p = (double[]) _p.Clone();
double sum = 0.0;
for (int i = 0; i < p.Length; i++)
{
sum += p[i];
}
for (int i = 0; i < p.Length; i++)
{
p[i] /= sum;
}
return p;
}
set

24
src/UnitTests/DistributionTests/Discrete/CategoricalTests.cs

@ -32,6 +32,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests
using System.Linq;
using MbUnit.Framework;
using MathNet.Numerics.Distributions;
using MathNet.Numerics.Statistics;
[TestFixture]
public class CategoricalTests
@ -55,7 +56,28 @@ namespace MathNet.Numerics.UnitTests.DistributionTests
public void CanCreateCategorical()
{
var m = new Categorical(largeP);
AssertEx.AreEqual<double[]>(largeP, m.P);
}
[Test]
[MultipleAsserts]
public void CanCreateCategoricalFromHistogram()
{
double[] smallDataset = { 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5 };
Histogram hist = new Histogram(smallDataset, 10, 0.0, 10.0);
var m = new Categorical(hist);
for (int i = 0; i <= m.Maximum; i++)
{
AssertEx.AreEqual<double>(1.0/10.0, m.P[i]);
}
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void CategoricalCreateFailsWithNullHistogram()
{
Histogram h = null;
var m = new Categorical(h);
}
[Test]

Loading…
Cancel
Save