Exemple #1
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;
         }
     });
 }
        static void SamplesUnchecked(System.Random rnd, int[] values, double[] cdfUnnormalized)
        {
            // TODO : use binary search to speed up this procedure.
            var 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;
                    while (u > cdfUnnormalized[idx])
                    {
                        idx++;
                    }

                    values[i] = idx;
                }
            });
        }
Exemple #3
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]));
         }
     });
 }
Exemple #4
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));
         }
     });
 }
Exemple #5
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;
                }
            }
        }
Exemple #6
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;
                }
            });
        }
Exemple #7
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);
         }
     });
 }
Exemple #8
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);
         }
     });
 }
 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))));
         }
     });
 }
Exemple #10
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));
                }
            });
        }
 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;
         }
     });
 }
Exemple #12
0
 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;
         }
     });
 }
Exemple #13
0
        internal 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;
                }
            });
        }
Exemple #14
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);
                }
            });
        }
Exemple #15
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))));
     }
 }