コード例 #1
0
        public static void MaxOfOthers_MonteCarlo(IList <Gaussian> array, IList <Gaussian> result)
        {
            if (array.Count == 0)
            {
                return;
            }
            if (array.Count == 1)
            {
                result[0] = Gaussian.Uniform();
                return;
            }
            int iterCount = 1000000;

            double[] x   = new double[array.Count];
            var      est = new ArrayEstimator <GaussianEstimator, IList <Gaussian>, Gaussian, double>(array.Count, i => new GaussianEstimator());

            for (int iter = 0; iter < iterCount; iter++)
            {
                for (int i = 0; i < x.Length; i++)
                {
                    x[i] = array[i].Sample();
                }
                double[] maxOfOthers = Factor.MaxOfOthers(x);
                est.Add(maxOfOthers);
            }
            est.GetDistribution(result);
        }
コード例 #2
0
        public void ArrayEstimatorTest()
        {
            int           length = 3;
            GaussianArray garray = new GaussianArray(length, i => new Gaussian(i, i + 1));
            ArrayEstimator <GaussianEstimator, GaussianArray, Gaussian, double> est =
                new ArrayEstimator <GaussianEstimator, GaussianArray, Gaussian, double>(
                    Utilities.Util.ArrayInit(length, i => new GaussianEstimator()));

            double[] sum   = new double[length];
            double[] sum2  = new double[length];
            int      count = 5;

            for (int nSamp = 0; nSamp < count; nSamp++)
            {
                double[] sample = garray.Sample();
                est.Add(sample);
                for (int i = 0; i < length; i++)
                {
                    sum[i]  += sample[i];
                    sum2[i] += sample[i] * sample[i];
                }
            }
            GaussianArray expected = new GaussianArray(length, delegate(int i)
            {
                double m = sum[i] / count;
                double v = sum2[i] / count - m * m;
                return(new Gaussian(m, v));
            });
            GaussianArray actual = new GaussianArray(length);

            actual = est.GetDistribution(actual);
            Console.WriteLine(Utilities.StringUtil.JoinColumns("result = ", actual, " should be ", expected));
            Assert.True(expected.MaxDiff(actual) < 1e-10);
        }