/// <summary> /// Samples student-t distributed random variables. /// </summary> /// <remarks>The algorithm is method 2 in section 5, chapter 9 /// in L. Devroye's "Non-Uniform Random Variate Generation"</remarks> /// <param name="rnd">The random number generator to use.</param> /// <param name="location">The location of the Student t-distribution.</param> /// <param name="scale">The scale of the Student t-distribution.</param> /// <param name="dof">The degrees of freedom for the standard student-t distribution.</param> /// <returns>a random number from the standard student-t distribution.</returns> internal static double SampleUnchecked(Random rnd, double location, double scale, double dof) { var n = Normal.SampleUncheckedBoxMuller(rnd).Item1; var g = Gamma.SampleUnchecked(rnd, 0.5 * dof, 0.5); return(location + (scale * n * Math.Sqrt(dof / g))); }
/// <summary> /// Samples Beta distributed random variables by sampling two Gamma variables and normalizing. /// </summary> /// <param name="rnd">The random number generator to use.</param> /// <param name="a">The α shape parameter of the Beta distribution. Range: α ≥ 0.</param> /// <param name="b">The β shape parameter of the Beta distribution. Range: β ≥ 0.</param> /// <returns>a random number from the Beta distribution.</returns> internal static double SampleUnchecked(System.Random rnd, double a, double b) { var x = Gamma.SampleUnchecked(rnd, a, 1.0); var y = Gamma.SampleUnchecked(rnd, b, 1.0); return(x / (x + y)); }
/// <summary> /// Generates a sequence of samples from the Erlang distribution. /// </summary> /// <returns>a sequence of samples from the distribution.</returns> public IEnumerable <double> Samples() { while (true) { yield return(Gamma.SampleUnchecked(_random, _shape, _rate)); } }
/// <summary> /// Samples a negative binomial distributed random variable. /// </summary> /// <param name="rnd">The random number generator to use.</param> /// <param name="r">The number of failures (r) until the experiment stopped. Range: r ≥ 0.</param> /// <param name="p">The probability (p) of a trial resulting in success. Range: 0 ≤ p ≤ 1.</param> /// <returns>a sample from the distribution.</returns> static int SampleUnchecked(System.Random rnd, double r, double p) { var lambda = Gamma.SampleUnchecked(rnd, r, p); var c = Math.Exp(-lambda); var p1 = 1.0; var k = 0; do { k = k + 1; p1 = p1 * rnd.NextDouble(); } while (p1 >= c); return(k - 1); }
/// <summary> /// Samples the distribution. /// </summary> /// <param name="rnd">The random number generator to use.</param> /// <param name="dof">The degrees of freedom.</param> /// <returns>a random number from the distribution.</returns> internal static double SampleUnchecked(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; 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 //for a justification) return(Gamma.SampleUnchecked(rnd, dof / 2.0, .5)); }
/// <summary> /// Samples the distribution. /// </summary> /// <param name="rnd">The random number generator to use.</param> /// <param name="freedom">The degrees of freedom (k) of the distribution. Range: k > 0.</param> /// <returns>a random number from the distribution.</returns> static double SampleUnchecked(System.Random rnd, double freedom) { // Use the simple method if the degrees if freedom is an integer anyway if (Math.Floor(freedom) == freedom && freedom < Int32.MaxValue) { double sum = 0; var n = (int)freedom; 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 //for a justification) return(Gamma.SampleUnchecked(rnd, freedom / 2.0, .5)); }
/// <summary> /// Samples student-t distributed random variables. /// </summary> /// <remarks>The algorithm is method 2 in section 5, chapter 9 /// in L. Devroye's "Non-Uniform Random Variate Generation"</remarks> /// <param name="rnd">The random number generator to use.</param> /// <param name="location">The location (μ) of the distribution.</param> /// <param name="scale">The scale (σ) of the distribution. Range: σ > 0.</param> /// <param name="freedom">The degrees of freedom (ν) for the distribution. Range: ν > 0.</param> /// <returns>a random number from the standard student-t distribution.</returns> static double SampleUnchecked(System.Random rnd, double location, double scale, double freedom) { var gamma = Gamma.SampleUnchecked(rnd, 0.5 * freedom, 0.5); return(Normal.Sample(rnd, location, scale * Math.Sqrt(freedom / gamma))); }
/// <summary> /// Generates a sample from the Erlang distribution. /// </summary> /// <returns>a sample from the distribution.</returns> public double Sample() { return(Gamma.SampleUnchecked(_random, _shape, _rate)); }
static double SampleUnchecked(System.Random rnd, double shape, double scale) { return(1.0 / Gamma.SampleUnchecked(rnd, shape, scale)); }