Ejemplo n.º 1
0
        /// <summary>
        /// Samples standard 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="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 Sample(Random rnd, double dof)
        {
            var n = Normal.SampleBoxMuller(rnd).Item1;
            var g = Gamma.Sample(rnd, 0.5 * dof, 0.5);

            return(Math.Sqrt(dof / g) * n);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Generates a sample from the log-normal distribution using the <i>Box-Muller</i> algorithm.
        /// </summary>
        /// <param name="rng">The random number generator to use.</param>
        /// <param name="mu">The mu of the logarithm of the distribution.</param>
        /// <param name="sigma">The standard deviation of the logarithm of the distribution.</param>
        /// <returns>a sample from the distribution.</returns>
        public static double Sample(Random rng, double mu, double sigma)
        {
            if (Control.CheckDistributionParameters && !IsValidParameterSet(mu, sigma))
            {
                throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
            }

            return(Math.Exp(mu + (sigma * Normal.SampleBoxMuller(rng).Item1)));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Generates a sequence of samples from the log-normal distribution using the <i>Box-Muller</i> algorithm.
        /// </summary>
        /// <returns>a sequence of samples from the distribution.</returns>
        public IEnumerable <double> Samples()
        {
            while (true)
            {
                var sample = Normal.SampleBoxMuller(RandomSource);
                yield return(Math.Exp(_mu + (_sigma * sample.Item1)));

                yield return(Math.Exp(_mu + (_sigma * sample.Item2)));
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Generates a sequence of samples from the log-normal distribution using the <i>Box-Muller</i> algorithm.
        /// </summary>
        /// <param name="rng">The random number generator to use.</param>
        /// <param name="mu">The mu of the logarithm of the distribution.</param>
        /// <param name="sigma">The standard deviation of the logarithm of the distribution.</param>
        /// <returns>a sequence of samples from the distribution.</returns>
        public static IEnumerable <double> Samples(Random rng, double mu, double sigma)
        {
            if (Control.CheckDistributionParameters && !IsValidParameterSet(mu, sigma))
            {
                throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
            }

            while (true)
            {
                var sample = Normal.SampleBoxMuller(rng);
                yield return(Math.Exp(mu + (sigma * sample.Item1)));

                yield return(Math.Exp(mu + (sigma * sample.Item2)));
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Samples a vector normal distributed random variable.
        /// </summary>
        /// <param name="rnd">The random number generator to use.</param>
        /// <param name="mean">The mean of the vector normal distribution.</param>
        /// <param name="cholesky">The Cholesky factorization of the covariance matrix.</param>
        /// <returns>a sequence of samples from defined distribution.</returns>
        private static Vector <double> SampleVectorNormal(Random rnd, Vector <double> mean, Cholesky <double> cholesky)
        {
            var count = mean.Count;

            // Sample a standard normal variable.
            var v = new DenseVector(count, 0.0);

            for (var d = 0; d < count; d += 2)
            {
                var sample = Normal.SampleBoxMuller(rnd);
                v[d] = sample.Item1;
                if (d + 1 < count)
                {
                    v[d + 1] = sample.Item2;
                }
            }

            // Return the transformed variable.
            return(mean + (cholesky.Factor * v));
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Generates a sample from the log-normal distribution using the <i>Box-Muller</i> algorithm.
 /// </summary>
 /// <returns>a sample from the distribution.</returns>
 public double Sample()
 {
     return(Math.Exp(_mu + (_sigma * Normal.SampleBoxMuller(RandomSource).Item1)));
 }