Ejemplo n.º 1
0
        /// <summary>
        ///   Generates a random vector of observations from the current distribution.
        /// </summary>
        ///
        /// <param name="samples">The number of samples to generate.</param>
        /// <returns>A random vector of observations drawn from this distribution.</returns>
        ///
        public double[] Generate(int samples)
        {
            var g = new AForge.Math.Random.GaussianGenerator(0, 1);
            var u = Accord.Math.Tools.Random;

            double[] r = new double[samples];
            for (int i = 0; i < r.Length; i++)
            {
                double v = g.Next();
                double y = v * v;
                double x = mean + (mean * mean * y) / (2 * lambda) - (mean / (2 * lambda)) * Math.Sqrt(4 * mean * lambda * y + mean * mean * y * y);

                double t = u.NextDouble();

                if (t <= (mean) / (mean + x))
                {
                    r[i] = x;
                }
                else
                {
                    r[i] = (mean * mean) / x;
                }
            }

            return(r);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///   Generates a random vector of observations from the current distribution.
        /// </summary>
        ///
        /// <returns>A random vector of observations drawn from this distribution.</returns>
        ///
        public override double Generate()
        {
            var g = new AForge.Math.Random.GaussianGenerator(
                (float)mean, (float)stdDev, Accord.Math.Tools.Random.Next());

            return(g.Next());
        }
Ejemplo n.º 3
0
        /// <summary>
        ///   Generates a random vector of observations from the current distribution.
        /// </summary>
        ///
        /// <param name="samples">The number of samples to generate.</param>
        ///
        /// <returns>A random vector of observations drawn from this distribution.</returns>
        ///
        public override double[] Generate(int samples)
        {
            double[] r = new double[samples];

            var g = new AForge.Math.Random.GaussianGenerator(
                (float)mean, (float)stdDev, Accord.Math.Tools.Random.Next());

            for (int i = 0; i < r.Length; i++)
            {
                r[i] = g.Next();
            }

            return(r);
        }
Ejemplo n.º 4
0
        /// <summary>
        ///   Generates a random observation from the
        ///   Inverse Gaussian distribution with the given parameters.
        /// </summary>
        ///
        /// <param name="mean">The mean parameter mu.</param>
        /// <param name="shape">The shape parameter lambda.</param>
        ///
        /// <returns>A random double value sampled from the specified Uniform distribution.</returns>
        ///
        public static double Random(double mean, double shape)
        {
            var u = Accord.Math.Tools.Random;
            var g = new AForge.Math.Random.GaussianGenerator(0, 1, u.Next());

            double v = g.Next();
            double y = v * v;
            double x = mean + (mean * mean * y) / (2 * shape) - (mean / (2 * shape)) * Math.Sqrt(4 * mean * shape * y + mean * mean * y * y);

            double t = u.NextDouble();

            if (t <= (mean) / (mean + x))
            {
                return(x);
            }

            return((mean * mean) / x);
        }
Ejemplo n.º 5
0
        /// <summary>
        ///   Random Gamma-distribution number generation
        ///   based on Marsaglia's Simple Method (2000).
        /// </summary>
        ///
        public static double Random(double d, double c)
        {
            var g = new AForge.Math.Random.GaussianGenerator(0,
                                                             1, Accord.Math.Tools.Random.Next());

            // References:
            //
            // - Marsaglia, G. A Simple Method for Generating Gamma Variables, 2000
            //

            while (true)
            {
                // 2. Generate v = (1+cx)^3 with x normal
                double x, t, v;

                do
                {
                    x = g.Next();
                    t = (1.0 + c * x);
                    v = t * t * t;
                } while (v <= 0);


                // 3. Generate uniform U
                double U = Accord.Math.Tools.Random.NextDouble();

                // 4. If U < 1-0.0331*x^4 return d*v.
                double x2 = x * x;
                if (U < 1 - 0.0331 * x2 * x2)
                {
                    return(d * v);
                }

                // 5. If log(U) < 0.5*x^2 + d*(1-v+log(v)) return d*v.
                if (Math.Log(U) < 0.5 * x2 + d * (1.0 - v + Math.Log(v)))
                {
                    return(d * v);
                }

                // 6. Goto step 2
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        ///   Generates a random vector of observations from the current distribution.
        /// </summary>
        /// 
        /// <param name="samples">The number of samples to generate.</param>
        /// <returns>A random vector of observations drawn from this distribution.</returns>
        /// 
        public double[] Generate(int samples)
        {
            var g = new AForge.Math.Random.GaussianGenerator(0, 1);
            var u = Accord.Math.Tools.Random;

            double[] r = new double[samples];
            for (int i = 0; i < r.Length; i++)
            {
                double v = g.Next();
                double y = v * v;
                double x = mean + (mean * mean * y) / (2 * lambda) - (mean / (2 * lambda)) * Math.Sqrt(4 * mean * lambda * y + mean * mean * y * y);

                double t = u.NextDouble();

                if (t <= (mean) / (mean + x))
                    r[i] = x;
                else
                    r[i] = (mean * mean) / x;
            }

            return r;
        }
Ejemplo n.º 7
0
        /// <summary>
        ///   Random Gamma-distribution number generation 
        ///   based on Marsaglia's Simple Method (2000).
        /// </summary>
        /// 
        public static double Random(double d, double c)
        {
            var g = new AForge.Math.Random.GaussianGenerator(0,
                1, Accord.Math.Tools.Random.Next());

            // References:
            //
            // - Marsaglia, G. A Simple Method for Generating Gamma Variables, 2000
            //

            while (true)
            {
                // 2. Generate v = (1+cx)^3 with x normal
                double x, t, v;

                do
                {
                    x = g.Next();
                    t = (1.0 + c * x);
                    v = t * t * t;
                } while (v <= 0);


                // 3. Generate uniform U
                double U = Accord.Math.Tools.Random.NextDouble();

                // 4. If U < 1-0.0331*x^4 return d*v.
                double x2 = x * x;
                if (U < 1 - 0.0331 * x2 * x2)
                    return d * v;

                // 5. If log(U) < 0.5*x^2 + d*(1-v+log(v)) return d*v.
                if (Math.Log(U) < 0.5 * x2 + d * (1.0 - v + Math.Log(v)))
                    return d * v;

                // 6. Goto step 2
            }
        }