Exemplo n.º 1
0
        /// <summary>
        /// Samples one multinomial distributed random variable.
        /// </summary>
        /// <param name="rnd">The random number generator to use.</param>
        /// <param name="p">An array of nonnegative ratios: this array does not need to be normalized
        /// as this is often impossible using floating point arithmetic.</param>
        /// <param name="n">The number of trials.</param>
        /// <returns>the counts for each of the different possible values.</returns>
        public static int[] Sample(Random rnd, double[] p, int n)
        {
            if (Control.CheckDistributionParameters && !IsValidParameterSet(p, n))
            {
                throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
            }

            // The cumulative density of p.
            var cp = Categorical.UnnormalizedCdf(p);

            // The variable that stores the counts.
            var ret = new int[p.Length];

            for (var i = 0; i < n; i++)
            {
                ret[Categorical.DoSample(rnd, cp)]++;
            }

            return(ret);
        }