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); } } } }
public EstimatorTest() { // Create distribution jagged 2D array, and create // the parallel jagged 2D array of distributions dim1 = 2; dim2 = 3; ga2aDistArray = new GaussianArray2DArray(dim1, dim2); ga2aArrayOfDist = new Gaussian[dim1, dim2][]; for (int i = 0; i < dim1; i++) { for (int j = 0; j < dim2; j++) { ga2aDistArray[i, j] = new GaussianArray(i + j + 1); ga2aArrayOfDist[i, j] = new Gaussian[i + j + 1]; for (int k = 0; k < ga2aDistArray[i, j].Count; k++) { ga2aDistArray[i, j][k] = Gaussian.FromMeanAndPrecision((double)k, (double)((k + 1) * (k + 1))); ga2aArrayOfDist[i, j][k] = new Gaussian(ga2aDistArray[i, j][k]); } } } }