コード例 #1
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);
                    }
                }
            }
        }
コード例 #2
0
        public void JaggedSetValues()
        {
            Array jaggedGaussian = JaggedArray.ConvertToNew(
                jagged, typeof(int), typeof(Gaussian),
                delegate(object elt) { return(new Gaussian(0.0, 1.0)); });

            Assert.Equal(
                JaggedArray.GetLength(jaggedGaussian, typeof(Gaussian)),
                JaggedArray.GetLength(jagged, typeof(int)));

            JaggedArray.ConvertElements(
                jaggedGaussian, typeof(Gaussian),
                delegate(object elt) { return(Gaussian.FromMeanAndPrecision(1.0, 2.0)); });

            foreach (Gaussian g in JaggedArray.ElementIterator(jaggedGaussian, typeof(Gaussian)))
            {
                Assert.Equal(1.0, g.GetMean());
            }
        }