コード例 #1
0
        /// <summary>
        ///   Generates a random vector of observations from the current distribution.
        /// </summary>
        ///
        /// <param name="samples">The number of samples to generate.</param>
        /// <param name="result">The location where to store the samples.</param>
        /// <param name="source">The random number generator to use as a source of randomness.
        ///   Default is to use <see cref="Accord.Math.Random.Generator.Random"/>.</param>
        ///
        /// <returns>A random vector of observations drawn from this distribution.</returns>
        ///
        public override double[][] Generate(int samples, double[][] result, Random source)
        {
            double[] z = new double[Dimension];
            double[] u = Mean;

            if (leftTriangularFactor == null)
            {
                if (svd != null)
                {
                    leftTriangularFactor = new double[Dimension, Dimension];
                    double[,] V          = svd.RightSingularVectors;
                    for (int i = 0; i < svd.Diagonal.Length; i++)
                    {
                        double d = Math.Sqrt(svd.Diagonal[i]);
                        for (int j = 0; j < Dimension; j++)
                        {
                            leftTriangularFactor[j, i] = d * V[j, i];
                        }
                    }
                }
                else
                {
                    leftTriangularFactor = chol.LeftTriangularFactor;
                }
            }

            for (int i = 0; i < samples; i++)
            {
                NormalDistribution.Random(Dimension, result: z, source: source);
                leftTriangularFactor.Dot(z, result: result[i]);
                result[i].Add(u, result: result[i]);
            }

            return(result);
        }
コード例 #2
0
        /// <summary>
        ///   Generates a random vector of observations from the current distribution.
        /// </summary>
        ///
        /// <param name="samples">The number of samples to generate.</param>
        /// <param name="dimension">The number of dimensions in the n-dimensional sphere.</param>
        /// <param name="result">The location where to store the samples.</param>
        /// <param name="source">The random number generator to use as a source of randomness.
        ///   Default is to use <see cref="Accord.Math.Random.Generator.Random"/>.</param>
        ///
        /// <returns>A random vector of observations drawn from this distribution.</returns>
        ///
        public static double[][] Random(int samples, int dimension, double[][] result, Random source)
        {
            for (int i = 0; i < result.Length; i++)
            {
                // Generate independent normally-distributed vectors
                NormalDistribution.Random(dimension, result: result[i], source: source);
                result[i].Normalize(inPlace: true); // make unit norm
            }

            return(result);
        }
コード例 #3
0
        /// <summary>
        ///   Generates a random vector of observations from the current distribution.
        /// </summary>
        ///
        /// <param name="samples">The number of samples to generate.</param>
        /// <param name="result">The location where to store the samples.</param>
        /// <param name="source">The random number generator to use as a source of randomness.
        ///   Default is to use <see cref="Accord.Math.Random.Generator.Random"/>.</param>
        ///
        /// <returns>A random vector of observations drawn from this distribution.</returns>
        ///
        public override double[][] Generate(int samples, double[][] result, Random source)
        {
            double[] normal = new double[Dimension + 2];

            for (int i = 0; i < result.Length; i++)
            {
                // Generate n + 2 independent normal distributed values
                NormalDistribution.Random(normal.Length, result: normal, source: source);
                normal.Normalize(inPlace: true);    // make unit norm
                for (int j = 0; j < Dimension; j++) // take n dimensions
                {
                    result[i][j] = normal[j] * radius + center[j];
                }
            }

            return(result);
        }
コード例 #4
0
        /// <summary>
        ///   Generates a random vector of observations from the current distribution.
        /// </summary>
        ///
        /// <param name="samples">The number of samples to generate.</param>
        /// <param name="result">The location where to store the samples.</param>
        ///
        /// <returns>A random vector of observations drawn from this distribution.</returns>
        ///
        public override double[][] Generate(int samples, double[][] result)
        {
            if (chol == null)
            {
                throw new NonPositiveDefiniteMatrixException("Covariance matrix is not positive definite.");
            }

            double[,] A = chol.LeftTriangularFactor;
            double[] z = new double[Dimension];
            double[] u = Mean;

            for (int i = 0; i < samples; i++)
            {
                NormalDistribution.Random(Dimension, result: z);
                Matrix.Dot(A, z, result: result[i]);
                Elementwise.Add(result[i], u, result: result[i]);
            }

            return(result);
        }