예제 #1
0
        /// <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));
        }
예제 #2
0
 /// <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));
     }
 }
예제 #3
0
        /// <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 successes (r) required to stop the experiment. 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);
        }
예제 #4
0
        /// <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 of freedom is an integer anyway
            if (Math.Floor(freedom) == freedom && freedom < int.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));
        }
예제 #5
0
 static double SampleUnchecked(System.Random rnd, double shape, double scale)
 {
     return(1.0 / Gamma.SampleUnchecked(rnd, shape, scale));
 }
예제 #6
0
        /// <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)));
        }
예제 #7
0
 /// <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));
 }