Browse Source

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

pull/36/head
Till Hoffmann 15 years ago
committed by Christoph Ruegg
parent
commit
94c57bdf2d
  1. 18
      src/Numerics/Distributions/Continuous/ChiSquare.cs

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

@ -274,14 +274,20 @@ namespace MathNet.Numerics.Distributions
/// <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)
{ {
double sum = 0; //Use the simple method if the dof is an integer anyway
var n = (int)dof; if (Math.Floor(dof) == dof && dof < Int32.MaxValue)
for (var i = 0; i < n; i++)
{ {
sum += Math.Pow(Normal.Sample(rnd, 0.0, 1.0), 2); double sum = 0;
var n = (int)dof;
for (var i = 0; i < n; i++)
{
sum += Math.Pow(Normal.Sample(rnd, 0.0, 1.0), 2);
}
return sum;
} }
//Call the gamma function (see http://en.wikipedia.org/wiki/Gamma_distribution#Specializations
return sum; //for a justification)
return Gamma.Sample(rnd, dof / 2.0, .5);
} }
/// <summary> /// <summary>

Loading…
Cancel
Save