public void EstimateTest() { // context is null { string parameterName = "context"; var estimator = new RareEventProbabilityEstimator(); ArgumentExceptionAssert.Throw( () => { estimator.Estimate( context: null, rarity: 0.1, sampleSize: 1000, estimationSampleSize: 10000); }, expectedType: typeof(ArgumentNullException), expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage, expectedParameterName: parameterName); } // rarity is less than 0 { var STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL = String.Format( ImplementationServices.GetResourceString( "STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL"), 0.0, 1.0); string parameterName = "rarity"; var estimator = new RareEventProbabilityEstimator(); ArgumentExceptionAssert.Throw( () => { estimator.Estimate( context: TestableRareEventProbabilityEstimationContext00 .Get().Context, rarity: -0.1, sampleSize: 1000, estimationSampleSize: 10000); }, expectedType: typeof(ArgumentOutOfRangeException), expectedPartialMessage: STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL, expectedParameterName: parameterName); } // rarity is equal to 0 { var STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL = String.Format( ImplementationServices.GetResourceString( "STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL"), 0.0, 1.0); string parameterName = "rarity"; var estimator = new RareEventProbabilityEstimator(); ArgumentExceptionAssert.Throw( () => { estimator.Estimate( context: TestableRareEventProbabilityEstimationContext00 .Get().Context, rarity: 0.0, sampleSize: 1000, estimationSampleSize: 10000); }, expectedType: typeof(ArgumentOutOfRangeException), expectedPartialMessage: STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL, expectedParameterName: parameterName); } // rarity is greater than 1 { var STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL = String.Format( ImplementationServices.GetResourceString( "STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL"), 0.0, 1.0); string parameterName = "rarity"; var estimator = new RareEventProbabilityEstimator(); ArgumentExceptionAssert.Throw( () => { estimator.Estimate( context: TestableRareEventProbabilityEstimationContext00 .Get().Context, rarity: 1.1, sampleSize: 1000, estimationSampleSize: 10000); }, expectedType: typeof(ArgumentOutOfRangeException), expectedPartialMessage: STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL, expectedParameterName: parameterName); } // rarity is equal to 1 { var STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL = String.Format( ImplementationServices.GetResourceString( "STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL"), 0.0, 1.0); string parameterName = "rarity"; var estimator = new RareEventProbabilityEstimator(); ArgumentExceptionAssert.Throw( () => { estimator.Estimate( context: TestableRareEventProbabilityEstimationContext00 .Get().Context, rarity: 1.0, sampleSize: 1000, estimationSampleSize: 10000); }, expectedType: typeof(ArgumentOutOfRangeException), expectedPartialMessage: STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL, expectedParameterName: parameterName); } // rarity is too low given context and sample size { var STR_EXCEPT_CEM_TOO_LOW_RARITY = String.Format( ImplementationServices.GetResourceString( "STR_EXCEPT_CEM_TOO_LOW_RARITY"), "sampleSize"); string parameterName = "rarity"; var estimator = new RareEventProbabilityEstimator(); ArgumentExceptionAssert.Throw( () => { estimator.Estimate( context: TestableRareEventProbabilityEstimationContext00 .Get().Context, rarity: .01, sampleSize: 50, estimationSampleSize: 10000); }, expectedType: typeof(ArgumentException), expectedPartialMessage: STR_EXCEPT_CEM_TOO_LOW_RARITY, expectedParameterName: parameterName); } // rarity is too high given context and sample size { var STR_EXCEPT_CEM_TOO_HIGH_RARITY = String.Format( ImplementationServices.GetResourceString( "STR_EXCEPT_CEM_TOO_HIGH_RARITY"), "sampleSize"); string parameterName = "rarity"; var estimator = new RareEventProbabilityEstimator(); ArgumentExceptionAssert.Throw( () => { estimator.Estimate( context: TestableRareEventProbabilityEstimationContext01 .Get().Context, rarity: .99, sampleSize: 50, estimationSampleSize: 10000); }, expectedType: typeof(ArgumentException), expectedPartialMessage: STR_EXCEPT_CEM_TOO_HIGH_RARITY, expectedParameterName: parameterName); } // sampleSize is not positive { var STR_EXCEPT_PAR_MUST_BE_POSITIVE = ImplementationServices.GetResourceString( "STR_EXCEPT_PAR_MUST_BE_POSITIVE"); string parameterName = "sampleSize"; var estimator = new RareEventProbabilityEstimator(); ArgumentExceptionAssert.Throw( () => { estimator.Estimate( context: TestableRareEventProbabilityEstimationContext00 .Get().Context, rarity: 0.1, sampleSize: 0, estimationSampleSize: 10000); }, expectedType: typeof(ArgumentOutOfRangeException), expectedPartialMessage: STR_EXCEPT_PAR_MUST_BE_POSITIVE, expectedParameterName: parameterName); } // estimationSampleSize is not positive { var STR_EXCEPT_PAR_MUST_BE_POSITIVE = ImplementationServices.GetResourceString( "STR_EXCEPT_PAR_MUST_BE_POSITIVE"); string parameterName = "estimationSampleSize"; var estimator = new RareEventProbabilityEstimator(); ArgumentExceptionAssert.Throw( () => { estimator.Estimate( context: TestableRareEventProbabilityEstimationContext00 .Get().Context, rarity: 0.1, sampleSize: 1000, estimationSampleSize: 0); }, expectedType: typeof(ArgumentOutOfRangeException), expectedPartialMessage: STR_EXCEPT_PAR_MUST_BE_POSITIVE, expectedParameterName: parameterName); } // Valid input - HigherThanLevel { var estimator = new RareEventProbabilityEstimator() { PerformanceEvaluationParallelOptions = { MaxDegreeOfParallelism = 1 }, SampleGenerationParallelOptions = { MaxDegreeOfParallelism = 1 } }; // Create the context. var testableContext = TestableRareEventProbabilityEstimationContext00.Get(); var context = testableContext.Context; // Check debug info context.TraceExecution = true; Trace.Listeners.RemoveAt(0); var defaultListener = new DefaultTraceListener(); Trace.Listeners.Add(defaultListener); // Set estimation parameters. double rarity = 0.1; int sampleSize = 1000; int estimationSampleSize = 10000; // Solve the problem. var results = estimator.Estimate( context, rarity, sampleSize, estimationSampleSize); Assert.AreEqual( expected: true, actual: results.HasConverged); Assert.AreEqual( expected: testableContext.RareEventProbability, actual: results.RareEventProbability, delta: 1e-6); } // Valid input - LowerThanLevel { var estimator = new RareEventProbabilityEstimator(); // Create the context. var testableContext = TestableRareEventProbabilityEstimationContext01.Get(); var context = testableContext.Context; // Check debug info context.TraceExecution = true; Trace.Listeners.RemoveAt(0); var defaultListener = new DefaultTraceListener(); Trace.Listeners.Add(defaultListener); // Set estimation parameters. double rarity = 0.1; int sampleSize = 1000; int estimationSampleSize = 10000; // Solve the problem. var results = estimator.Estimate( context, rarity, sampleSize, estimationSampleSize); Assert.AreEqual( expected: testableContext.RareEventProbability, actual: results.RareEventProbability, delta: 1e-5); } }
public void ConstructorTest() { // stateDimension is not positive { var STR_EXCEPT_PAR_MUST_BE_POSITIVE = ImplementationServices.GetResourceString( "STR_EXCEPT_PAR_MUST_BE_POSITIVE"); string parameterName = "stateDimension"; ArgumentExceptionAssert.Throw( () => { new RareEventProbabilityEstimationContext00( stateDimension: 0, thresholdLevel: 2.0, rareEventPerformanceBoundedness: RareEventPerformanceBoundedness.Lower, initialParameter: DoubleMatrix.Dense(1, 5, new double[] { 0.25, 0.4, 0.1, 0.3, 0.2 })); }, expectedType: typeof(ArgumentOutOfRangeException), expectedPartialMessage: STR_EXCEPT_PAR_MUST_BE_POSITIVE, expectedParameterName: parameterName); } // rareEventPerformanceBoundedness is not a field of RareEventPerformanceBoundedness { var STR_EXCEPT_NOT_FIELD_OF_RARE_EVENT_PERFORMANCE_BOUNDEDNESS = ImplementationServices.GetResourceString( "STR_EXCEPT_NOT_FIELD_OF_RARE_EVENT_PERFORMANCE_BOUNDEDNESS"); string parameterName = "rareEventPerformanceBoundedness"; ArgumentExceptionAssert.Throw( () => { new RareEventProbabilityEstimationContext00( stateDimension: 5, thresholdLevel: 2.0, rareEventPerformanceBoundedness: (RareEventPerformanceBoundedness)(-1), initialParameter: DoubleMatrix.Dense(1, 5, new double[] { 0.25, 0.4, 0.1, 0.3, 0.2 })); }, expectedType: typeof(ArgumentException), expectedPartialMessage: STR_EXCEPT_NOT_FIELD_OF_RARE_EVENT_PERFORMANCE_BOUNDEDNESS, expectedParameterName: parameterName); } // initialParameter is null { string parameterName = "initialParameter"; ArgumentExceptionAssert.Throw( () => { new RareEventProbabilityEstimationContext00( stateDimension: 5, thresholdLevel: 2.0, rareEventPerformanceBoundedness: RareEventPerformanceBoundedness.Lower, initialParameter: null); }, expectedType: typeof(ArgumentNullException), expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage, expectedParameterName: parameterName); } // valid input - HigherThanLevel { var testableContext = TestableRareEventProbabilityEstimationContext00.Get(); var context = testableContext.Context; Assert.AreEqual( expected: testableContext.StateDimension, actual: context.StateDimension); Assert.AreEqual( expected: testableContext.TraceExecution, actual: context.TraceExecution); Assert.AreEqual( expected: testableContext.EliteSampleDefinition, actual: context.EliteSampleDefinition); Assert.AreEqual( expected: testableContext.RareEventPerformanceBoundedness, actual: context.RareEventPerformanceBoundedness); DoubleMatrixAssert.AreEqual( expected: testableContext.NominalParameter, actual: context.InitialParameter, DoubleMatrixTest.Accuracy); } // valid input - LowerThanLevel { var testableContext = TestableRareEventProbabilityEstimationContext01.Get(); var context = testableContext.Context; Assert.AreEqual( expected: testableContext.StateDimension, actual: context.StateDimension); Assert.AreEqual( expected: testableContext.TraceExecution, actual: context.TraceExecution); Assert.AreEqual( expected: testableContext.EliteSampleDefinition, actual: context.EliteSampleDefinition); Assert.AreEqual( expected: testableContext.RareEventPerformanceBoundedness, actual: context.RareEventPerformanceBoundedness); DoubleMatrixAssert.AreEqual( expected: testableContext.NominalParameter, actual: context.InitialParameter, DoubleMatrixTest.Accuracy); } }