Browse Source

Extended the chisquare distribution to support the generation of samples for non-integer dof.

la-knuth
Till Hoffmann 15 years ago
committed by Christoph Ruegg
parent
commit
94c57bdf2d
  1. 8
      src/Numerics/Distributions/Continuous/ChiSquare.cs

8
src/Numerics/Distributions/Continuous/ChiSquare.cs

@ -273,6 +273,9 @@ namespace MathNet.Numerics.Distributions
/// <param name="dof">The degrees of freedom.</param> /// <param name="dof">The degrees of freedom.</param>
/// <returns>a random number from the distribution.</returns> /// <returns>a random number from the distribution.</returns>
private static double DoSample(Random rnd, double dof) private static double DoSample(Random rnd, double dof)
{
//Use the simple method if the dof is an integer anyway
if (Math.Floor(dof) == dof && dof < Int32.MaxValue)
{ {
double sum = 0; double sum = 0;
var n = (int)dof; var n = (int)dof;
@ -280,9 +283,12 @@ namespace MathNet.Numerics.Distributions
{ {
sum += Math.Pow(Normal.Sample(rnd, 0.0, 1.0), 2); sum += Math.Pow(Normal.Sample(rnd, 0.0, 1.0), 2);
} }
return sum; return sum;
} }
//Call the gamma function (see http://en.wikipedia.org/wiki/Gamma_distribution#Specializations
//for a justification)
return Gamma.Sample(rnd, dof / 2.0, .5);
}
/// <summary> /// <summary>
/// Generates a sample from the <c>ChiSquare</c> distribution. /// Generates a sample from the <c>ChiSquare</c> distribution.

Loading…
Cancel
Save