示例#1
0
        static void SamplesUnchecked(System.Random rnd, int[] values, double p)
        {
            if (p == 1.0)
            {
                CommonParallel.For(0, values.Length, 4096, (a, b) =>
                {
                    for (int i = a; i < b; i++)
                    {
                        values[i] = 1;
                    }
                });
                return;
            }

            var    uniform = rnd.NextDoubles(values.Length);
            double rp      = 1.0 - p;

            CommonParallel.For(0, values.Length, 4096, (a, b) =>
            {
                for (int i = a; i < b; i++)
                {
                    values[i] = (int)Math.Ceiling(Math.Log(1.0 - uniform[i], rp));
                }
            });
        }
示例#2
0
        static void SamplesUnchecked(System.Random rnd, int[] values, double[] cdfUnnormalized)
        {
            // TODO : use binary search to speed up this procedure.
            double[] uniform = rnd.NextDoubles(values.Length);
            double   w       = cdfUnnormalized[cdfUnnormalized.Length - 1];

            CommonParallel.For(0, values.Length, 4096, (a, b) =>
            {
                for (int i = a; i < b; i++)
                {
                    var u   = uniform[i] * w;
                    var idx = 0;

                    if (u == 0.0d)
                    {
                        // skip zero-probability categories
                        while (0.0d == cdfUnnormalized[idx])
                        {
                            idx++;
                        }
                    }

                    while (u > cdfUnnormalized[idx])
                    {
                        idx++;
                    }

                    values[i] = idx;
                }
            });
        }
示例#3
0
        internal static void SamplesUnchecked(System.Random rnd, double[] values, double mean, double stddev)
        {
            if (values.Length == 0)
            {
                return;
            }

            // Since we only accept points within the unit circle
            // we need to generate roughly 4/pi=1.27 times the numbers needed.
            int n = (int)Math.Ceiling(values.Length * 4 * Constants.InvPi);

            if (n.IsOdd())
            {
                n++;
            }
            var uniform = rnd.NextDoubles(n);

            // Polar transform
            double x, y;
            int    index = 0;

            for (int i = 0; i < uniform.Length && index < values.Length; i += 2)
            {
                if (!PolarTransform(uniform[i], uniform[i + 1], out x, out y))
                {
                    continue;
                }
                values[index++] = mean + stddev * x;
                if (index == values.Length)
                {
                    return;
                }
                values[index++] = mean + stddev * y;
                if (index == values.Length)
                {
                    return;
                }
            }

            // remaining, if any
            while (index < values.Length)
            {
                if (!PolarTransform(rnd.NextDouble(), rnd.NextDouble(), out x, out y))
                {
                    continue;
                }
                values[index++] = mean + stddev * x;
                if (index == values.Length)
                {
                    return;
                }
                values[index++] = mean + stddev * y;
                if (index == values.Length)
                {
                    return;
                }
            }
        }
示例#4
0
 static void SamplesUnchecked(System.Random rnd, double[] values, double location, double scale)
 {
     rnd.NextDoubles(values);
     for (int i = 0; i < values.Length; i++)
     {
         var u = values[i] - 0.5;
         values[i] = location - (scale * Math.Sign(u) * Math.Log(1.0 - (2.0 * Math.Abs(u))));
     }
 }
示例#5
0
        public void TestDoubles()
        {
            var random1 = new System.Random(1);
            var random2 = new System.Random(1);

            Assert.Equal(
                actual: random1.NextDoubles().Take(10).ToArray(),
                expected: Enumerable.Range(0, 10).Select(_ => random2.NextDouble()).ToArray()
                );
        }
示例#6
0
 static void SamplesUnchecked(System.Random rnd, double[] values, double location, double scale)
 {
     rnd.NextDoubles(values);
     CommonParallel.For(0, values.Length, 4096, (a, b) =>
     {
         for (int i = a; i < b; i++)
         {
             values[i] = location + scale * Math.Tan(Constants.Pi * (values[i] - 0.5));
         }
     });
 }
示例#7
0
 static void SamplesUnchecked(System.Random rnd, double[] values, double scale)
 {
     rnd.NextDoubles(values);
     CommonParallel.For(0, values.Length, 4096, (a, b) =>
     {
         for (int i = a; i < b; i++)
         {
             values[i] = scale * Math.Sqrt(-2.0 * Math.Log(values[i]));
         }
     });
 }
示例#8
0
 static void SamplesUnchecked(System.Random rnd, double[] values, double location, double scale)
 {
     rnd.NextDoubles(values);
     CommonParallel.For(0, values.Length, 4096, (a, b) =>
     {
         for (int i = a; i < b; i++)
         {
             var u     = values[i] - 0.5;
             values[i] = location - (scale * Math.Sign(u) * Math.Log(1.0 - (2.0 * Math.Abs(u))));
         }
     });
 }
示例#9
0
        static void SamplesUnchecked(System.Random rnd, int[] values, double p)
        {
            var uniform = rnd.NextDoubles(values.Length);

            CommonParallel.For(0, values.Length, 4096, (a, b) =>
            {
                for (int i = a; i < b; i++)
                {
                    values[i] = uniform[i] < p ? 1 : 0;
                }
            });
        }
示例#10
0
 internal static void SamplesUnchecked(System.Random rnd, double[] values, double scale, double shape, double truncation)
 {
     if (values.Length == 0)
     {
         return;
     }
     double[] uniforms = rnd.NextDoubles(values.Length);
     for (var j = 0; j < values.Length; ++j)
     {
         values[j] = InvCDFUncheckedImpl(scale, shape, truncation, uniforms[j]);
     }
 }
示例#11
0
        internal static void SamplesUnchecked(System.Random rnd, double[] values, double lower, double upper)
        {
            rnd.NextDoubles(values);
            var difference = upper - lower;

            CommonParallel.For(0, values.Length, 4096, (a, b) =>
            {
                for (int i = a; i < b; i++)
                {
                    values[i] = lower + values[i] * difference;
                }
            });
        }
示例#12
0
        static void SamplesUnchecked(System.Random rnd, double[] values, double scale, double shape)
        {
            var power = -1.0 / shape;

            rnd.NextDoubles(values);
            CommonParallel.For(0, values.Length, 4096, (a, b) =>
            {
                for (int i = a; i < b; i++)
                {
                    values[i] = scale * Math.Pow(values[i], power);
                }
            });
        }
示例#13
0
        static void SamplesUnchecked(System.Random rnd, double[] values, double shape, double scale)
        {
            var exponent = 1.0 / shape;

            rnd.NextDoubles(values);
            CommonParallel.For(0, values.Length, 4096, (a, b) =>
            {
                for (int i = a; i < b; i++)
                {
                    values[i] = scale * Math.Pow(-Math.Log(values[i]), exponent);
                }
            });
        }
示例#14
0
 internal static void SamplesUnchecked(System.Random rnd, double[] values, double mu, double lambda)
 {
     if (values.Length == 0)
     {
         return;
     }
     double[] v = new double[values.Length];
     MathNet.Numerics.Distributions.Normal.Samples(rnd, v, 0, 1);
     double[] test = rnd.NextDoubles(values.Length);
     for (var j = 0; j < values.Length; ++j)
     {
         values[j] = InverseGaussianSampleImpl(mu, lambda, v[j], test[j]);
     }
 }
示例#15
0
        internal static void SamplesUnchecked(System.Random rnd, double[] values, double a, double c, double k)
        {
            if (values.Length == 0)
            {
                return;
            }
            var k_inv = 1 / k;
            var c_inv = 1 / c;

            double[] u = rnd.NextDoubles(values.Length);

            for (var j = 0; j < values.Length; ++j)
            {
                values[j] = a * Math.Pow(Math.Pow(1 - u[j], -k_inv) - 1, c_inv);
            }
        }
示例#16
0
        static void SamplesUnchecked(System.Random rnd, double[] values, double lower, double upper, double mode)
        {
            double ml = mode - lower, ul = upper - lower, um = upper - mode;
            double u = ml / ul, v = ul * ml, w = ul * um;

            rnd.NextDoubles(values);
            CommonParallel.For(0, values.Length, 4096, (a, b) =>
            {
                for (int i = a; i < b; i++)
                {
                    values[i] = values[i] < u
                        ? lower + Math.Sqrt(values[i] * v)
                        : upper - Math.Sqrt((1 - values[i]) * w);
                }
            });
        }
示例#17
0
 static void SamplesUnchecked(System.Random rnd, double[] values, double rate)
 {
     rnd.NextDoubles(values);
     CommonParallel.For(0, values.Length, 4096, (a, b) =>
     {
         for (int i = a; i < b; i++)
         {
             // this happens very rarely
             var r = values[i];
             while (r == 0.0)
             {
                 r = rnd.NextDouble();
             }
             values[i] = -Math.Log(r) / rate;
         }
     });
 }
示例#18
0
        static void SamplesUnchecked(System.Random rnd, int[] values, double p, int n)
        {
            var uniform = rnd.NextDoubles(values.Length*n);
            CommonParallel.For(0, values.Length, 4096, (a, b) =>
            {
                for (int i = a; i < b; i++)
                {
                    int k = i*n;
                    int sum = 0;
                    for (int j = 0; j < n; j++)
                    {
                        sum += uniform[k + j] < p ? 1 : 0;
                    }

                    values[i] = sum;
                }
            });
        }
示例#19
0
        static void SamplesUnchecked(System.Random rnd, int[] values, double lambda, double nu, double z)
        {
            var uniform = rnd.NextDoubles(values.Length);

            CommonParallel.For(0, values.Length, 4096, (a, b) =>
            {
                for (int i = a; i < b; i++)
                {
                    var u   = uniform[i];
                    var p   = 1.0 / z;
                    var cdf = p;
                    var k   = 0;
                    while (u > cdf)
                    {
                        k++;
                        p    = p * lambda / Math.Pow(k, nu);
                        cdf += p;
                    }

                    values[i] = k;
                }
            });
        }