コード例 #1
0
        public static double Beta()
        {
            GaussianGenerator generator = new GaussianGenerator(0, 1);
            double            x         = generator.Next();

            x *= x;
            x *= 0.5;
            double y = generator.Next();

            y *= y;
            y *= 0.5;
            return(x / (x + y));
        }
コード例 #2
0
ファイル: Util.cs プロジェクト: afalchetti/monorfs
        /// <summary>
        /// Realize a random vector from a gaussian distribution around a specified mean
        /// vector with a given covariance.
        /// </summary>
        /// <param name="mean">Distribution mean vector.</param>
        /// <param name="covariance">Distribution covariance matrix.</param>
        /// <returns>Random vector.</returns>
        public static double[] RandomGaussianVector(double[] mean, double[][] covariance)
        {
            for (int i = 0; i < covariance.Length; i++)
            {
                if (covariance[i][i] < 1e-40)
                {
                    covariance[i][i] = 1e-40;
                }
            }

            // first find the square root of the covariance matrix
            // C such as C * C^T = covariance
            var cholesky = new CholeskyDecomposition(covariance.ToMatrix());

            double[] sqrtdiag = cholesky.Diagonal;
            double[,] covroot;

            for (int i = 0; i < sqrtdiag.Length; i++)
            {
                sqrtdiag[i] = Math.Sqrt(sqrtdiag[i]);
            }

            covroot = cholesky.LeftTriangularFactor.MultiplyByDiagonal(sqrtdiag);

            // and then use the random variable Y = mean + C * Z
            // with Z an independent canonical gaussian random vector ~N(0, 1)
            // to obtain the multinormal correlated  random vector
            double[] canonical = new double[mean.Length];
            for (int i = 0; i < canonical.Length; i++)
            {
                canonical[i] = Gaussian.Next();
            }

            return(mean.Add(covroot.Multiply(canonical)));
        }
コード例 #3
0
 /// <summary>
 ///   Randomizes (initializes) the weights of
 ///   the network using a Gaussian distribution.
 /// </summary>
 ///
 public void Randomize()
 {
     foreach (ActivationLayer layer in network.Layers)
     {
         foreach (ActivationNeuron neuron in layer.Neurons)
         {
             for (int i = 0; i < neuron.Weights.Length; i++)
             {
                 neuron.Weights[i] = random.Next();
             }
             if (UpdateThresholds)
             {
                 neuron.Threshold = random.Next();
             }
         }
     }
 }
コード例 #4
0
        //apply couds texture to smudge the image
        private static Bitmap Texturize(Bitmap image)
        {
            IRandomNumberGenerator generator = new GaussianGenerator(-.25f, 0.3f);

            // create filter
            Texturer filter = new Texturer(new CloudsTexture(), generator.Next(), .995);//.98

            // apply the filter
            return(filter.Apply(image));
        }
コード例 #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 GaussianGenerator(0, 1, Generator.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.Random.Generator.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
            }
        }