/// <summary> /// Tests that method /// <see cref="RandomIndexPermutation.Next"/> /// terminates successfully as expected. /// </summary> /// <param name="indexes"> /// The indexes to permute. /// </param> /// <param name="numberOfRandomPermutations"> /// The number of permutations to draw. /// </param> /// <param name="criticalValue"> /// A quantile of the chi-squared distribution with a number of /// degrees of freedom equal to the <see cref="IndexCollection.Count"/> /// of <paramref name="indexes"/> /// minus <c>1</c>. /// To serve as the critical value for the Pearson's /// chi-squared test whose null hypothesis assume that the /// the distinct possible permutations /// are equiprobable. /// </param> /// <param name="delta">The required accuracy. /// Defaults to <c>.01</c>.</param> public static void Succeed( IndexCollection indexes, int numberOfRandomPermutations, double criticalValue, double delta = .01) { var randomPermutation = new RandomIndexPermutation(indexes); // Generate permutations var permutations = new IndexCollection[numberOfRandomPermutations]; for (int i = 0; i < numberOfRandomPermutations; i++) { permutations[i] = randomPermutation.Next(); } // Check the number of distinct generated permutations var permutationIdentifiers = IndexCollection.Default(numberOfRandomPermutations - 1); var actualDistinctPermutations = IndexPartition.Create( permutationIdentifiers, (i) => { return(permutations[i]); }); int numberOfActualDistinctPermutations = actualDistinctPermutations.Count; Assert.AreEqual( expected: SpecialFunctions.Factorial(indexes.Count), actual: numberOfActualDistinctPermutations); // Compute the actual permutation probabilities DoubleMatrix actualPermutationProbabilities = DoubleMatrix.Dense( numberOfActualDistinctPermutations, 1); int j = 0; foreach (var identifier in actualDistinctPermutations.Identifiers) { actualPermutationProbabilities[j] = (double)actualDistinctPermutations[identifier].Count / (double)numberOfRandomPermutations; j++; } // Check that the Chebyshev Inequality holds true // for each permutation probability var expectedPermutationProbabilities = DoubleMatrix.Dense( numberOfActualDistinctPermutations, 1, 1.0 / (double)numberOfActualDistinctPermutations); for (int i = 0; i < numberOfActualDistinctPermutations; i++) { ProbabilityDistributionTest.CheckChebyshevInequality( new BernoulliDistribution(expectedPermutationProbabilities[i]), actualPermutationProbabilities[i], numberOfRandomPermutations, delta); } // Check how good the actual permutation probabilities fit // the expected ones ProbabilityDistributionTest.CheckGoodnessOfFit( expectedPermutationProbabilities, actualPermutationProbabilities, criticalValue); }
public void GetOptimalStateTest() { // valid input - random ties resolution { var context = new CategoricalEntailmentEnsembleOptimizationContext( objectiveFunction: (DoubleMatrix state) => { return(Double.PositiveInfinity); }, featureCategoryCounts: new List <int>(1) { 6 }, numberOfResponseCategories: 4, numberOfCategoricalEntailments: 1, allowEntailmentPartialTruthValues: true, probabilitySmoothingCoefficient: .9, optimizationGoal: OptimizationGoal.Maximization, minimumNumberOfIterations: 5, maximumNumberOfIterations: 1000); int numberOfEvaluations = 10000; double delta = .01; var parameter = DoubleMatrix.Dense(1, 10, new double[10] { .5, .5, .5, .5, .5, .5, .25, .25, .25, .25 }); // Generate states var states = new int[numberOfEvaluations]; var responseIndexes = IndexCollection.Range(6, 9); for (int i = 0; i < numberOfEvaluations; i++) { var state = context.GetOptimalState(parameter); states[i] = state.Vec(responseIndexes).FindNonzero()[0]; } // Compute the actual inclusion probabilities DoubleMatrix actualInclusionProbabilities = DoubleMatrix.Dense(context.NumberOfResponseCategories, 1); var stateIndexes = IndexCollection.Default(numberOfEvaluations - 1); for (int j = 0; j < context.NumberOfResponseCategories; j++) { var samplesContainingCurrentUnit = IndexPartition.Create( stateIndexes, (i) => { return(states[i] == j); }); actualInclusionProbabilities[j] = (double)samplesContainingCurrentUnit[true].Count / (double)numberOfEvaluations; } // Check the number of distinct generated states var distinctStates = IndexPartition.Create( states); int numberOfDistinctStates = distinctStates.Count; Assert.AreEqual( expected: context.NumberOfResponseCategories, actual: numberOfDistinctStates); // Check that the Chebyshev Inequality holds true // for each inclusion probability var expectedInclusionProbabilities = DoubleMatrix.Dense(context.NumberOfResponseCategories, 1, 1.0 / context.NumberOfResponseCategories); for (int j = 0; j < context.NumberOfResponseCategories; j++) { ProbabilityDistributionTest.CheckChebyshevInequality( new BernoulliDistribution(expectedInclusionProbabilities[j]), actualInclusionProbabilities[j], numberOfEvaluations, delta); } // Check how good the actual inclusion probabilities fit // the expected ones // The following assumes a number of response // categories equal to 4. // // The quantile of order .9 for // the chi-squared distribution having 4-1 // degrees of freedom is 6.251389 // (as from R function qchisq(.9, 3)) var goodnessOfFitCriticalValue = 6.251389; ProbabilityDistributionTest.CheckGoodnessOfFit( expectedInclusionProbabilities, actualInclusionProbabilities, goodnessOfFitCriticalValue); } }