예제 #1
0
        public void Array2DEstimatorTest()
        {
            int             length1 = 3;
            int             length2 = 2;
            GaussianArray2D garray  = new GaussianArray2D(length1, length2, (i, j) => new Gaussian(i, j + 1));
            Array2DEstimator <GaussianEstimator, GaussianArray2D, Gaussian, double> est =
                new Array2DEstimator <GaussianEstimator, GaussianArray2D, Gaussian, double>(
                    Utilities.Util.ArrayInit(length1, length2, (i, j) => new GaussianEstimator()));

            double[,] sum  = new double[length1, length2];
            double[,] sum2 = new double[length1, length2];
            int count = 5;

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

            actual = est.GetDistribution(actual);
            Console.WriteLine(Utilities.StringUtil.JoinColumns("result = ", actual, " should be ", expected));
            Assert.True(expected.MaxDiff(actual) < 1e-9);
        }
예제 #2
0
        public void Array2DArrayEstimatorTest()
        {
            // Create the estimator
            // element type is double[]
            //Estimator<GaussianArray2DArray> est = null;
            int length1 = ga2aArrayOfDist.GetLength(0);
            int length2 = ga2aArrayOfDist.GetLength(1);
            Array2DEstimator <GaussianArrayEstimator, GaussianArray2DArray, GaussianArray, double[]> est =
                new Array2DEstimator <GaussianArrayEstimator, GaussianArray2DArray, GaussianArray, double[]>(
                    Utilities.Util.ArrayInit(length1, length2, (i, j) =>
                                             new GaussianArrayEstimator(Utilities.Util.ArrayInit(ga2aArrayOfDist[i, j].Length, k => new GaussianEstimator()))
                                             ));

            // Add some samples to the estimator
            int nSamples = 5;

            // Create a sample an mean variable of the right structure
            double[, ][] mean = (double[, ][]) JaggedArray.ConvertToNew(
                ga2aArrayOfDist, typeof(double), delegate(object elt) { return(0.0); });
            double[, ][] sample = (double[, ][]) JaggedArray.ConvertToNew(
                ga2aArrayOfDist, typeof(double), delegate(object elt) { return(0.0); });

            // Create samples and add them to the estimator. Accumulate sum of samples at the same time
            for (int nSamp = 0; nSamp < nSamples; nSamp++)
            {
                JaggedArray.ConvertElements2(
                    sample, ga2aArrayOfDist, delegate(object smp, object dst) { return(((Gaussian)dst).Sample()); });
                est.Add(sample);

                JaggedArray.ConvertElements2(
                    mean, sample, delegate(object mn, object smp) { return((double)mn + (double)smp); });
            }

            // Hand calculate the sample mean
            JaggedArray.ConvertElements(
                mean, delegate(object mn) { return(((double)mn) / ((double)nSamples)); });

            // Let the estimator do the work
            GaussianArray2DArray result = new GaussianArray2DArray(length1, length2, (i, j) => new GaussianArray(ga2aArrayOfDist[i, j].Length));

            result = est.GetDistribution(result);

            // The results should be identical to a very high precision
            for (int i = 0; i < dim1; i++)
            {
                for (int j = 0; j < dim2; j++)
                {
                    for (int k = 0; k < result[i, j].Count; k++)
                    {
                        Assert.True(System.Math.Abs(result[i, j][k].GetMean() - mean[i, j][k]) < 1e-9);
                    }
                }
            }
        }