Browse Source

Added Dirichlet distribution.

Signed-off-by: jvangael <jurgen.vangael@gmail.com>
pull/2/head
jvangael 17 years ago
parent
commit
be690bea9b
  1. 252
      src/Numerics/Distributions/Multivariate/Dirichlet.cs
  2. 2
      src/Numerics/Numerics.csproj
  3. 62
      src/UnitTests/DistributionTests/Multivariate/DirichletTests.cs
  4. 2
      src/UnitTests/UnitTests.csproj

252
src/Numerics/Distributions/Multivariate/Dirichlet.cs

@ -0,0 +1,252 @@
// <copyright file="Dirichlet.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://mathnet.opensourcedotnet.info
//
// Copyright (c) 2009 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
namespace dnAnalytics.Statistics.Distributions
{
using System;
using Properties;
/// <summary>
/// Implements the multivariate Dirichlet distribution. For details about this distribution, see
/// <a href="http://en.wikipedia.org/wiki/Dirichlet_distribution">Wikipedia - Dirichlet distribution</a>.
/// </summary>
/// <remarks><para>The distribution will use the <see cref="System.Random"/> by default.
/// Users can get/set the random number generator by using the <see cref="RandomSource"/> property.</para>
/// <para>The statistics classes will check all the incoming parameters whether they are in the allowed
/// range. This might involve heavy computation. Optionally, by setting Control.CheckDistributionParameters
/// to false, all parameter checks can be turned off.</para></remarks>
public class Dirichlet
{
// The Dirichlet distribution parameters.
private readonly double[] _alpha;
/// <summary>
/// The distribution's random number generator.
/// </summary>
private Random _random;
/// <summary>
/// Initializes a new instance of the Dirichlet class. The distribution will
/// be initialized with the default <seealso cref="System.Random"/> random number generator.
/// </summary>
/// <param name="alpha">An array with the Dirichlet parameters.</param>
public Dirichlet(double[] alpha)
{
SetParameters(alpha);
RandomSource = new Random();
}
/// <summary>
/// Constructs a new symmetric Dirichlet distribution. The distribution will
/// be initialized with the default <seealso cref="System.Random"/> random number generator.
/// </summary>
/// <param name="alpha">The value of each parameter of the Dirichlet distribution.</param>
/// <param name="k">The dimension of the Dirichlet distribution.</param>
public Dirichlet(double alpha, int k)
{
// Create a parameter structure.
double[] parm = new double[k];
for (int i = 0; i < k; i++)
{
parm[i] = alpha;
}
SetParameters(parm);
RandomSource = new Random();
}
/// <summary>
/// Checks whether the parameters of the distribution are valid: no parameter can be less than zero and
/// at least one parameter should be larger than zero.
/// </summary>
/// <param name="alpha">The parameters of the Dirichlet distribution.</param>
/// <returns>True when the parameters are valid, false otherwise.</returns>
public static bool IsValidParameterSet(double[] alpha)
{
bool allzero = true;
for (int i = 0; i < alpha.Length; i++)
{
if (alpha[i] < 0.0)
{
return false;
}
else if (alpha[i] > 0.0)
{
allzero = false;
}
}
if (allzero)
{
return false;
}
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="alpha">The parameters of the Dirichlet distribution.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
private void SetParameters(double[] alpha)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(alpha) && ! alpha.Length == _alpha.Length)
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
for (int i = 0; i < alpha.Length; i++)
{
_alpha[i] = alpha[i];
}
}
/// <summary>
/// A string representation of the distribution.
/// </summary>
public override string ToString()
{
return "Dirichlet(Dimension = " + this.Dimension + ")";
}
/// <summary>
/// Gets the dimension of the Dirichlet distribution.
/// </summary>
public int Dimension
{
get { return _alpha.Length; }
}
/// <summary>
/// The parameters of the Dirichlet distribution.
/// </summary>
public double[] Alpha
{
get { return _alpha; }
}
/// <summary>
/// The sum of the Dirichlet parameters.
/// </summary>
private double AlphaSum
{
get
{
double s = 0.0;
for (int i = 0; i < _alpha.Length; i++)
{
s += _alpha[i];
}
return s;
}
}
/// <summary>
/// Gets the mean of the Dirichlet distribution.
/// </summary>
public double[] Mean
{
get { return _alpha / AlphaSum; }
}
/// <summary>
/// Gets the variance of the Dirichlet distribution.
/// </summary>
public double[] Variance
{
get
{
double s = this.AlphaSum;
double[] v = new double[_alpha.Length];
for (int i = 0; i < _alpha.Length; i++)
{
v[i] = _alpha[i]*(s - _alpha[i])/(s*s*(s + 1.0));
}
return v;
}
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public Random RandomSource
{
get
{
return _random;
}
set
{
if (value == null)
{
throw new ArgumentNullException();
}
_random = value;
}
}
/// <summary>
/// Samples a Dirichlet distributed random vector.
/// </summary>
public double[] Sample()
{
return Sample(RandomSource, _alpha);
}
/// <summary>
/// Samples a Dirichlet distributed random vector.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="alpha">The Dirichlet distribution parameter.</param>
public static double[] Sample(System.Random rnd, double[] alpha)
{
if (Control.CheckDistributionParameters && ! IsValidParameterSet(alpha))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
int n = alpha.Length;
double[] gv = new double[n];
double sum = 0.0;
for (int i = 0; i < n; i++)
{
gv[i] = Gamma.Sample(rnd, alpha[i], 1.0);
sum += gv[i];
}
for (int i = 0; i < n; i++)
{
gv[i] /= sum;
}
return gv;
}
}
}

2
src/Numerics/Numerics.csproj

@ -56,6 +56,7 @@
<Compile Include="Distributions\IContinuousDistribution.cs" />
<Compile Include="Distributions\IDiscreteDistribution.cs" />
<Compile Include="Distributions\IDistribution.cs" />
<Compile Include="Distributions\Multivariate\Dirichlet.cs" />
<Compile Include="IntegralTransforms\Algorithms\DiscreteHartleyTransform.Naive.cs" />
<Compile Include="IntegralTransforms\Algorithms\DiscreteHartleyTransform.Options.cs" />
<Compile Include="IntegralTransforms\HartleyOptions.cs" />
@ -121,7 +122,6 @@
</ItemGroup>
<ItemGroup>
<Folder Include="Algorithms\LinearAlgebra\" />
<Folder Include="Distributions\Multivariate\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

62
src/UnitTests/DistributionTests/Multivariate/DirichletTests.cs

@ -0,0 +1,62 @@
// <copyright file="DirichletTests.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://mathnet.opensourcedotnet.info
//
// Copyright (c) 2009 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
namespace dnAnalytics.Tests.Statistics.Distributions
{
using dnAnalytics.Statistics.Distributions;
using NUnit.Framework;
[TestFixture]
public class DirichletTests
{
private const double mAcceptableError = 1e-12;
[Test]
public void SymmetricDirichlet()
{
Dirichlet d = new Dirichlet(0.3, 5);
for (int i = 0; i < 5; i++)
{
Assert.AreEqual(0.3, d.Mean[i], mAcceptableError);
Assert.AreEqual(0.3 * (1.5 - 0.3) / (1.5 * 1.5 * 2.5), d.Variance[i], mAcceptableError);
}
}
[Test]
public void GetSetRNG()
{
Dirichlet d = new Dirichlet(0.3, 5);
// Try getting the random number generator.
System.Random rnd = d.RandomNumberGenerator;
// Try setting the random number generator.
d.RandomNumberGenerator = new System.Random();
}
}
}

2
src/UnitTests/UnitTests.csproj

@ -68,6 +68,7 @@
<Compile Include="DistributionTests\Continuous\ContinuousUniformTests.cs" />
<Compile Include="DistributionTests\Continuous\GammaTests.cs" />
<Compile Include="DistributionTests\Continuous\NormalTests.cs" />
<Compile Include="DistributionTests\Multivariate\DirichletTests.cs" />
<Compile Include="IntegralTransformsTests\HartleyTest.cs" />
<Compile Include="IntegralTransformsTests\FourierTest.cs" />
<Compile Include="IntegralTransformsTests\MatchingNaiveTransformTest.cs" />
@ -109,7 +110,6 @@
</ItemGroup>
<ItemGroup>
<Folder Include="DistributionTests\Discrete\" />
<Folder Include="DistributionTests\Multivariate\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

Loading…
Cancel
Save