public void ConstructorTest()
        {
            // Valid input
            {
                var indexes = IndexCollection.Default(lastIndex: 5);

                var randomIndexPermutation = new
                                             RandomIndexPermutation(indexes: indexes);

                int i = 0;
                foreach (var index in randomIndexPermutation.Indexes)
                {
                    Assert.AreEqual(
                        expected: indexes[i],
                        actual: index);
                    i++;
                }
            }

            // indexes is null
            {
                IndexCollection indexes = null;

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new RandomIndexPermutation(
                        indexes: indexes);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "indexes");
            }
        }
Exemple #2
0
        public void StandardTest()
        {
            // dimension is not positive
            {
                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    var basis = Basis.Standard(0);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage: ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_POSITIVE"),
                    expectedParameterName: "dimension");
            }

            // dimension is valid
            {
                var basis    = Basis.Standard(2);
                var actual   = basis.GetBasisMatrix();
                var expected = DoubleMatrix.Identity(2);

                DoubleMatrixAssert.AreEqual(
                    expected,
                    actual,
                    DoubleMatrixTest.Accuracy);
            }
        }
Exemple #3
0
        public void CompleteDiameterTest()
        {
            // cluster is null
            {
                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    var partition = Distance.CompleteDiameter((DoubleMatrix)null);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "cluster");
            }

            // cluster is valid
            {
                var items         = IndexCollection.Range(0, 3);
                int numberOfItems = items.Count;
                var attributes    = IrisDataSet.GetAttributesAsDoubleMatrix()[items, ":"];

                var actual = Distance.CompleteDiameter(attributes);

                var expected = 0.6480741;

                Assert.AreEqual(expected, actual, DoubleMatrixTest.Accuracy);
            }
        }
Exemple #4
0
        public void AnalyzeTest()
        {
            // dataSet is null
            {
                string parameterName = "dataSet";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    MultipleCorrespondence.Analyze(
                        null);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

            // dataSet must have positive marginal column sums
            {
                var STR_EXCEPT_GDA_MCA_NON_POSITIVE_MARGINAL_SUMS =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_GDA_MCA_NON_POSITIVE_MARGINAL_SUMS");

                string parameterName = "dataSet";

                CategoricalVariable color = new("COLOR")
                {
                    { 0, "Red" },
Exemple #5
0
        public void SetRandomNumberGeneratorTest()
        {
            // Valid input
            {
                var distribution = GaussianDistribution.Standard();

                distribution.RandomNumberGenerator =
                    RandomNumberGenerator.CreateNextMT2203(77777);

                int sampleSize = 10000;
                var sample     = distribution.Sample(sampleSize);

                RandomNumberGeneratorTest.CheckChebyshevInequality(
                    distributionMean: 0.0,
                    distributionVariance: 1.0,
                    sampleMean: Stat.Mean(sample),
                    sampleSize: sampleSize,
                    delta: .01);
            }

            // value is null
            {
                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    GaussianDistribution.Standard()
                    .RandomNumberGenerator = null;
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "value");
            }
        }
        public void SigmaTest()
        {
            // Valid sigma
            {
                var distribution = GaussianDistribution.Standard();
                var expected     = 1.123;
                distribution.Sigma = expected;
                Assert.AreEqual(
                    expected: expected,
                    actual: distribution.Sigma,
                    delta: DoubleMatrixTest.Accuracy);
            }

            // Non positive sigma
            {
                string STR_EXCEPT_PAR_MUST_BE_POSITIVE =
                    (string)Reflector.ExecuteStaticMember(
                        typeof(ImplementationServices),
                        "GetResourceString",
                        new string[] { "STR_EXCEPT_PAR_MUST_BE_POSITIVE" });

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    GaussianDistribution.Standard().Sigma = 0.0;
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage: STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: "value");
            }
        }
Exemple #7
0
        public void ConstructorTest()
        {
            // Valid input
            {
                var lowerBound   = -1.1;
                var upperBound   = 2.2;
                var distribution = new UniformDistribution(
                    lowerBound: lowerBound,
                    upperBound: upperBound);

                Assert.AreEqual(
                    expected: lowerBound,
                    actual: distribution.LowerBound,
                    delta: DoubleMatrixTest.Accuracy);

                Assert.AreEqual(
                    expected: upperBound,
                    actual: distribution.UpperBound,
                    delta: DoubleMatrixTest.Accuracy);
            }

            // upperBound == lowerBound
            {
                string STR_EXCEPT_PAR_MUST_BE_GREATER_THAN_OTHER =
                    string.Format(
                        ImplementationServices.GetResourceString(
                            "STR_EXCEPT_PAR_MUST_BE_GREATER_THAN_OTHER"),
                        "upperBound",
                        "lowerBound");

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new UniformDistribution(lowerBound: 1.0, upperBound: 1.0);
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage: STR_EXCEPT_PAR_MUST_BE_GREATER_THAN_OTHER,
                    expectedParameterName: "upperBound");
            }

            // upperBound < lowerBound
            {
                string STR_EXCEPT_PAR_MUST_BE_GREATER_THAN_OTHER =
                    string.Format(
                        ImplementationServices.GetResourceString(
                            "STR_EXCEPT_PAR_MUST_BE_GREATER_THAN_OTHER"),
                        "upperBound",
                        "lowerBound");

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new UniformDistribution(lowerBound: 2.0, upperBound: 1.0);
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage: STR_EXCEPT_PAR_MUST_BE_GREATER_THAN_OTHER,
                    expectedParameterName: "upperBound");
            }
        }
Exemple #8
0
        public void ParametricHessianTest()
        {
            // function is null
            {
                string parameterName = "function";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    NumericalDifferentiation.Hessian(
                        function: null,
                        argument: EngineFailureModel.EstimatedParameters,
                        parameter: EngineFailureModel.Data);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

            // argument is null
            {
                string parameterName = "argument";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    NumericalDifferentiation.Hessian(
                        function: EngineFailureModel.LogLikelihood,
                        argument: null,
                        parameter: EngineFailureModel.Data);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

            // valid input
            {
                var expected =
                    EngineFailureModel.LogLikelihoodHessian(
                        argument: EngineFailureModel.EstimatedParameters,
                        parameter: EngineFailureModel.Data);

                var actual =
                    NumericalDifferentiation.Hessian(
                        function: EngineFailureModel.LogLikelihood,
                        argument: EngineFailureModel.EstimatedParameters,
                        parameter: EngineFailureModel.Data);

                DoubleMatrixAssert.AreEqual(
                    expected: expected,
                    actual: actual,
                    delta: DoubleMatrixTest.Accuracy);
            }
        }
        public void FactorialTest()
        {
            // n is negative
            {
                string STR_EXCEPT_PAR_MUST_BE_NON_NEGATIVE =
                    (string)Reflector.ExecuteStaticMember(
                        typeof(ImplementationServices),
                        "GetResourceString",
                        new string[] { "STR_EXCEPT_PAR_MUST_BE_NON_NEGATIVE" });

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    SpecialFunctions.Factorial(n: -1);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage: STR_EXCEPT_PAR_MUST_BE_NON_NEGATIVE,
                    expectedParameterName: "n");
            }

            // valid input
            {
                var x = new int[]
                {
                    0,
                    1,
                    2,
                    3,
                    10,
                    1000
                };

                var expected = new double[]
                {
                    1.0,
                    1.0,
                    2.0,
                    6.0,
                    3628800.0,
                    Double.PositiveInfinity
                };

                var actual = new double[6];

                for (int i = 0; i < x.Length; i++)
                {
                    actual[i] =
                        SpecialFunctions.Factorial(x[i]);
                }

                DoubleArrayAssert.AreEqual(
                    expected: expected,
                    actual: actual,
                    delta: 1e-8);
            }
        }
Exemple #10
0
        public void CtorTest()
        {
            // Wrong code base
            {
                string expectedPartialMessage =
                    RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
                    ?
                    "The specified directory does not exist or an error occurred when " +
                    "trying to determine if the specified folder exists. "
                    :
                    "The specified directory does not exist or an error occurred when " +
                    "trying to determine if the specified folder exists.";

                ArgumentExceptionAssert.IsThrown(
                    () =>
                {
                    string codeBase = Path.Combine(Environment.CurrentDirectory,
                                                   "1a793a0c-1178-42e3-a6bc-c23ca6d7289d");
                    string defaultNamespace = "SampleClassLibrary.CodeExamples";
                    var analyzer            = new CodeExamplesAnalyzer(
                        codeBase,
                        defaultNamespace);
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage:
                    "The specified directory does not exist or an error occurred when " +
                    "trying to determine if the specified folder exists. ",
                    expectedParameterName: "codeBase");
            }
            // Null default namespace
            {
                ArgumentExceptionAssert.IsThrown(
                    () =>
                {
                    string codeBase         = Environment.CurrentDirectory;
                    string defaultNamespace = null;
                    var analyzer            = new CodeExamplesAnalyzer(
                        codeBase,
                        defaultNamespace);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "defaultNamespace");
            }
            // Mainstream use case
            {
                string codeBase         = Environment.CurrentDirectory;
                string defaultNamespace = "SampleClassLibrary.CodeExamples";
                var    analyzer         = new CodeExamplesAnalyzer(
                    codeBase,
                    defaultNamespace);
                Assert.AreEqual(expected: codeBase, actual: analyzer.CodeBase);
                Assert.AreEqual(expected: defaultNamespace, actual: analyzer.DefaultNamespace);
            }
        }
Exemple #11
0
        public void SuccessProbabilityTest()
        {
            // Valid successProbability
            {
                var distribution = BernoulliDistribution.Balanced();
                var expected     = 0.12;
                distribution.SuccessProbability = expected;
                Assert.AreEqual(
                    expected: expected,
                    actual: distribution.SuccessProbability,
                    delta: DoubleMatrixTest.Accuracy);
            }


            // successProbability < 0
            {
                string STR_EXCEPT_PAR_NOT_IN_CLOSED_INTERVAL =
                    string.Format(
                        ImplementationServices.GetResourceString(
                            "STR_EXCEPT_PAR_NOT_IN_CLOSED_INTERVAL"),
                        "0", "1");

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    BernoulliDistribution.Balanced().SuccessProbability = -2.0;
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage: STR_EXCEPT_PAR_NOT_IN_CLOSED_INTERVAL,
                    expectedParameterName: "value");
            }

            // successProbability > 1
            {
                string STR_EXCEPT_PAR_NOT_IN_CLOSED_INTERVAL =
                    string.Format(
                        ImplementationServices.GetResourceString(
                            "STR_EXCEPT_PAR_NOT_IN_CLOSED_INTERVAL"),
                        "0", "1");

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    BernoulliDistribution.Balanced().SuccessProbability = 2.0;
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage: STR_EXCEPT_PAR_NOT_IN_CLOSED_INTERVAL,
                    expectedParameterName: "value");
            }
        }
Exemple #12
0
        public void ConstructorTest()
        {
            // Valid input
            {
                var successProbability = 0.18;
                var distribution       = new BernoulliDistribution(
                    successProbability: successProbability);

                Assert.AreEqual(
                    expected: successProbability,
                    actual: distribution.SuccessProbability,
                    delta: DoubleMatrixTest.Accuracy);
            }

            // successProbability < 0
            {
                string STR_EXCEPT_PAR_NOT_IN_CLOSED_INTERVAL =
                    string.Format(
                        ImplementationServices.GetResourceString(
                            "STR_EXCEPT_PAR_NOT_IN_CLOSED_INTERVAL"),
                        "0", "1");

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new BernoulliDistribution(successProbability: -2.0);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage: STR_EXCEPT_PAR_NOT_IN_CLOSED_INTERVAL,
                    expectedParameterName: "successProbability");
            }

            // successProbability > 1
            {
                string STR_EXCEPT_PAR_NOT_IN_CLOSED_INTERVAL =
                    string.Format(
                        ImplementationServices.GetResourceString(
                            "STR_EXCEPT_PAR_NOT_IN_CLOSED_INTERVAL"),
                        "0", "1");

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new BernoulliDistribution(successProbability: 2.0);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage: STR_EXCEPT_PAR_NOT_IN_CLOSED_INTERVAL,
                    expectedParameterName: "successProbability");
            }
        }
Exemple #13
0
        public void RateTest()
        {
            // Valid input
            {
                var distribution = new ExponentialDistribution(
                    rate: 1.0);

                var rate = 2.0;
                distribution.Rate = rate;

                Assert.AreEqual(
                    expected: rate,
                    actual: distribution.Rate,
                    delta: DoubleMatrixTest.Accuracy);
            }

            // value == 0
            {
                string STR_EXCEPT_PAR_MUST_BE_POSITIVE =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_POSITIVE");

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new ExponentialDistribution(rate: 1.0).Rate = 0.0;
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage: STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: "value");
            }

            // value < 0
            {
                string STR_EXCEPT_PAR_MUST_BE_POSITIVE =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_POSITIVE");

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new ExponentialDistribution(rate: 1.0).Rate = -1.0;
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage: STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: "value");
            }
        }
Exemple #14
0
        public void ConstructorTest()
        {
            // Valid input
            {
                var mu           = -1.1;
                var sigma        = 2.2;
                var xi           = -3.3;
                var distribution = new GeneralizedParetoDistribution(
                    mu: mu,
                    sigma: sigma,
                    xi: xi);

                Assert.AreEqual(
                    expected: mu,
                    actual: distribution.Mu,
                    delta: DoubleMatrixTest.Accuracy);

                Assert.AreEqual(
                    expected: sigma,
                    actual: distribution.Sigma,
                    delta: DoubleMatrixTest.Accuracy);

                Assert.AreEqual(
                    expected: xi,
                    actual: distribution.Xi,
                    delta: DoubleMatrixTest.Accuracy);
            }

            // Non positive sigma
            {
                string STR_EXCEPT_PAR_MUST_BE_POSITIVE =
                    (string)Reflector.ExecuteStaticMember(
                        typeof(ImplementationServices),
                        "GetResourceString",
                        new string[] { "STR_EXCEPT_PAR_MUST_BE_POSITIVE" });

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new GeneralizedParetoDistribution(
                        mu: 1.0, sigma: 0.0, xi: -1.0);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage: STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: "sigma");
            }
        }
        public void ConstructorTest()
        {
            // name is null
            {
                ArgumentExceptionAssert.Throw(
                    () => { new CategoricalVariable(null); },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "name");
            }

            // name is white space
            {
                ArgumentExceptionAssert.Throw(
                    () => { new CategoricalVariable(" "); },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_STRING_IS_EMPTY_OR_WHITESPACE"),
                    expectedParameterName: "name");
            }

            // name is empty
            {
                ArgumentExceptionAssert.Throw(
                    () => { new CategoricalVariable(string.Empty); },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_STRING_IS_EMPTY_OR_WHITESPACE"),
                    expectedParameterName: "name");
            }

            // Valid name
            {
                string expectedName = "The name";

                var target = new CategoricalVariable(expectedName);

                CategoricalVariableAssert.IsStateAsExpected(
                    target,
                    expectedName,
                    expectedCategories: new List <Category>(),
                    expectedReadOnlyFlag: false);
            }
        }
Exemple #16
0
        public void StopExecutionTest()
        {
            // levels is null
            {
                string parameterName = "levels";

                var context =
                    TestableSystemPerformanceOptimizationContext02.Get().Context;

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    context.StopExecution(
                        iteration: 1,
                        levels: null,
                        parameters: new LinkedList <DoubleMatrix>());
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

            // parameters is null
            {
                string parameterName = "parameters";

                var context =
                    TestableSystemPerformanceOptimizationContext02.Get().Context;

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    context.StopExecution(
                        iteration: 1,
                        levels: new LinkedList <double>(),
                        parameters: null);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }
        }
Exemple #17
0
        public void RebaseTest()
        {
            // newBasis is null
            {
                var cloud = TestableCloud00.Get().Cloud;

                string parameterName = "newBasis";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    cloud.Rebase(null);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

            // newBasis must have the same dimension of this instance
            {
                var STR_EXCEPT_PAR_BASES_MUST_SHARE_DIMENSION =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_BASES_MUST_SHARE_DIMENSION");

                var cloud = TestableCloud00.Get().Cloud;

                string parameterName = "newBasis";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    cloud.Rebase(Basis.Standard(cloud.Coordinates.NumberOfColumns + 1));
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_BASES_MUST_SHARE_DIMENSION,
                    expectedParameterName: parameterName);
            }

            CloudTest.Rebase.Succeed(
                TestableCloud00.Get());
        }
Exemple #18
0
        public void SmoothParameterTest()
        {
            // parameters is null
            {
                string parameterName = "parameters";

                var context =
                    TestableSystemPerformanceOptimizationContext02.Get().Context;

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    context.SmoothParameter(
                        parameters: null);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }
        }
Exemple #19
0
        public void SampleGenerationParallelOptionsTest()
        {
            // value is null
            {
                string parameterName = "value";

                var estimator = new RareEventProbabilityEstimator();

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    estimator.SampleGenerationParallelOptions
                        = null;
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

            // value is valid
            {
                int maxDegreeOfParallelism = 2;

                var estimator = new RareEventProbabilityEstimator
                {
                    SampleGenerationParallelOptions
                        = new System.Threading.Tasks.ParallelOptions()
                        {
                        MaxDegreeOfParallelism = maxDegreeOfParallelism
                        }
                };

                Assert.AreEqual(
                    expected: 2,
                    actual: estimator
                    .SampleGenerationParallelOptions
                    .MaxDegreeOfParallelism);
            }
        }
        public void PerformanceEvaluationParallelOptionsTest()
        {
            // value is null
            {
                string parameterName = "value";

                var optimizer = new SystemPerformanceOptimizer();

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    optimizer.PerformanceEvaluationParallelOptions
                        = null;
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

            // value is valid
            {
                int maxDegreeOfParallelism = 2;

                var optimizer = new SystemPerformanceOptimizer
                {
                    PerformanceEvaluationParallelOptions
                        = new System.Threading.Tasks.ParallelOptions()
                        {
                        MaxDegreeOfParallelism = maxDegreeOfParallelism
                        }
                };

                Assert.AreEqual(
                    expected: 2,
                    actual: optimizer
                    .PerformanceEvaluationParallelOptions.MaxDegreeOfParallelism);
            }
        }
        public void ToDoubleMatrixByMethodTest()
        {
            // value is null
            {
                CategoricalVariable target = null;

                ArgumentExceptionAssert.Throw(
                    () => { var result = CategoricalVariable.ToDoubleMatrix(target); },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "value");
            }

            // Valid input
            {
                string name   = "The name";
                var    target = new CategoricalVariable(name)
                {
                    { 0.0, "Zero" },
                    { 1.0, "One" },
                    { 2.0, "Two" }
                };

                var expected = DoubleMatrix.Dense(3, 1,
                                                  new double[3] {
                    0.0, 1.0, 2.0
                });
                expected.Name = target.Name;
                expected.SetRowName(0, "Zero");
                expected.SetRowName(1, "One");
                expected.SetRowName(2, "Two");

                DoubleMatrix actual = CategoricalVariable.ToDoubleMatrix(target);

                DoubleMatrixAssert.AreEqual(expected, actual, 1e-2);
            }
        }
Exemple #22
0
        public void EuclideanTest()
        {
            // cluster is null
            {
                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    var partition = Distance.Euclidean((DoubleMatrix)null);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "cluster");
            }

            // cluster is valid
            {
                var items         = IndexCollection.Range(0, 3);
                int numberOfItems = items.Count;
                var attributes    = IrisDataSet.GetAttributesAsDoubleMatrix()[items, ":"];

                var actual = Distance.Euclidean(attributes);

                var expected = DoubleMatrix.Dense(
                    numberOfItems,
                    numberOfItems,
                    new double[] {
                    0.0000000, 0.5385165, 0.5099020, 0.6480741,
                    0.5385165, 0.0000000, 0.3000000, 0.3316625,
                    0.5099020, 0.3000000, 0.0000000, 0.2449490,
                    0.6480741, 0.3316625, 0.2449490, 0.0000000
                },
                    StorageOrder.RowMajor);

                DoubleMatrixAssert.AreEqual(expected, actual, DoubleMatrixTest.Accuracy);
            }
        }
Exemple #23
0
        public void SetMassesTest()
        {
            // Valid input
            {
                var values = DoubleMatrix.Dense(3, 2,
                                                new double[6] {
                    1, 2, 3, 4, 5, 6
                });

                var distribution = FiniteDiscreteDistribution.Uniform(
                    values: values);

                var masses = DoubleMatrix.Dense(3, 2,
                                                new double[6] {
                    .1, .2, .3, .2, .1, .1
                });

                distribution.SetMasses(masses: masses);

                DoubleMatrixAssert.AreEqual(
                    expected: masses,
                    actual: (DoubleMatrix)distribution.Masses,
                    delta: DoubleMatrixTest.Accuracy);
            }

            // masses is null
            {
                var values = DoubleMatrix.Dense(3, 2,
                                                new double[6] {
                    1, 2, 3, 4, 5, 6
                });

                var distribution = FiniteDiscreteDistribution.Uniform(
                    values: values);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    distribution.SetMasses(masses: null);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "masses");
            }

            // The number of rows in masses is not equal to that of values
            {
                string STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_ROWS =
                    string.Format(
                        ImplementationServices.GetResourceString(
                            "STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_ROWS"),
                        "values");

                var values = DoubleMatrix.Dense(3, 2,
                                                new double[6] {
                    1, 2, 3, 4, 5, 6
                });

                var distribution = FiniteDiscreteDistribution.Uniform(
                    values: values);

                var masses = DoubleMatrix.Dense(1, 2,
                                                new double[2] {
                    .5, .5
                });

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    distribution.SetMasses(masses: masses);
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage: STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_ROWS,
                    expectedParameterName: "masses");
            }

            // The number of columns in masses is not equal to that of values
            {
                string STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_COLUMNS =
                    string.Format(
                        ImplementationServices.GetResourceString(
                            "STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_COLUMNS"),
                        "values");

                var values = DoubleMatrix.Dense(3, 2,
                                                new double[6] {
                    1, 2, 3, 4, 5, 6
                });

                var distribution = FiniteDiscreteDistribution.Uniform(
                    values: values);

                var masses = DoubleMatrix.Dense(3, 1,
                                                new double[3] {
                    .2, .5, .3
                });

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    distribution.SetMasses(masses: masses);
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage: STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_COLUMNS,
                    expectedParameterName: "masses");
            }

            // At least an entry in masses is negative
            {
                string STR_EXCEPT_PAR_ENTRIES_NOT_IN_CLOSED_INTERVAL =
                    string.Format(
                        ImplementationServices.GetResourceString(
                            "STR_EXCEPT_PAR_ENTRIES_NOT_IN_CLOSED_INTERVAL"),
                        "0", "1");

                var values = DoubleMatrix.Dense(3, 2,
                                                new double[6] {
                    1, 2, 3, 4, 5, 6
                });

                var distribution = FiniteDiscreteDistribution.Uniform(
                    values: values);

                var masses = DoubleMatrix.Dense(3, 2,
                                                new double[6] {
                    .2, .5, .3, -.1, .1, 0
                });

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    distribution.SetMasses(masses: masses);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage: STR_EXCEPT_PAR_ENTRIES_NOT_IN_CLOSED_INTERVAL,
                    expectedParameterName: "masses");
            }

            // The sum of the masses is not 1
            {
                string STR_EXCEPT_PAR_ENTRIES_MUST_SUM_TO_1 =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_ENTRIES_MUST_SUM_TO_1");

                var values = DoubleMatrix.Dense(3, 2,
                                                new double[6] {
                    1, 2, 3, 4, 5, 6
                });

                var distribution = FiniteDiscreteDistribution.Uniform(
                    values: values);

                var masses = DoubleMatrix.Dense(3, 2,
                                                new double[6] {
                    .2, .5, .3, .1, .1, 0
                });

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    distribution.SetMasses(masses: masses);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage: STR_EXCEPT_PAR_ENTRIES_MUST_SUM_TO_1,
                    expectedParameterName: "masses");
            }
        }
        public void LogGammaTest()
        {
            // x is zero
            {
                string STR_EXCEPT_PAR_MUST_BE_POSITIVE =
                    (string)Reflector.ExecuteStaticMember(
                        typeof(ImplementationServices),
                        "GetResourceString",
                        new string[] { "STR_EXCEPT_PAR_MUST_BE_POSITIVE" });

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    SpecialFunctions.LogGamma(x: 0.0);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage: STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: "x");
            }

            // x is negative
            {
                string STR_EXCEPT_PAR_MUST_BE_POSITIVE =
                    (string)Reflector.ExecuteStaticMember(
                        typeof(ImplementationServices),
                        "GetResourceString",
                        new string[] { "STR_EXCEPT_PAR_MUST_BE_POSITIVE" });

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    SpecialFunctions.LogGamma(x: -1.0);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage: STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: "x");
            }

            // valid input
            {
                var x = new double[]
                {
                    1.0,
                    10.0,
                    100.0,
                    1000.0
                };

                var expected = new double[]
                {
                    0.0,
                    12.80182748008147,
                    359.13420536957540,
                    5905.22042320918081
                };

                var actual = new double[4];

                for (int i = 0; i < x.Length; i++)
                {
                    actual[i] =
                        SpecialFunctions.LogGamma(x[i]);
                }

                DoubleArrayAssert.AreEqual(
                    expected: expected,
                    actual: actual,
                    delta: 1e-9);
            }
        }
        public void BinomialCoefficientTest()
        {
            // n is negative
            {
                string STR_EXCEPT_PAR_MUST_BE_NON_NEGATIVE =
                    (string)Reflector.ExecuteStaticMember(
                        typeof(ImplementationServices),
                        "GetResourceString",
                        new string[] { "STR_EXCEPT_PAR_MUST_BE_NON_NEGATIVE" });

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    SpecialFunctions.BinomialCoefficient(
                        n: -1,
                        k: 0);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage: STR_EXCEPT_PAR_MUST_BE_NON_NEGATIVE,
                    expectedParameterName: "n");
            }

            // k is negative
            {
                string STR_EXCEPT_PAR_MUST_BE_NON_NEGATIVE =
                    (string)Reflector.ExecuteStaticMember(
                        typeof(ImplementationServices),
                        "GetResourceString",
                        new string[] { "STR_EXCEPT_PAR_MUST_BE_NON_NEGATIVE" });

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    SpecialFunctions.BinomialCoefficient(
                        n: 0,
                        k: -1);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage: STR_EXCEPT_PAR_MUST_BE_NON_NEGATIVE,
                    expectedParameterName: "k");
            }

            // n < k
            {
                string STR_EXCEPT_PAR_MUST_BE_NOT_GREATER_THAN_OTHER =
                    string.Format(
                        (string)Reflector.ExecuteStaticMember(
                            typeof(ImplementationServices),
                            "GetResourceString",
                            new string[] { "STR_EXCEPT_PAR_MUST_BE_NOT_GREATER_THAN_OTHER" }),
                        "k",
                        "n");

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    SpecialFunctions.BinomialCoefficient(
                        n: 3,
                        k: 4);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage: STR_EXCEPT_PAR_MUST_BE_NOT_GREATER_THAN_OTHER,
                    expectedParameterName: "k");
            }

            // valid input
            {
                var n = new int[]
                {
                    0,
                    1,
                    2,
                    10,
                    20
                };

                var k = new int[]
                {
                    0,
                    1,
                    2,
                    7,
                    8
                };

                var expected = new double[]
                {
                    1.0,
                    1.0,
                    1.0,
                    120.0,
                    125970.0
                };

                var actual = new double[5];

                for (int i = 0; i < n.Length; i++)
                {
                    actual[i] =
                        SpecialFunctions.BinomialCoefficient(n[i], k[i]);
                }

                DoubleArrayAssert.AreEqual(
                    expected: expected,
                    actual: actual,
                    delta: 1e-8);
            }
        }
Exemple #26
0
        public void GetVariancesTest()
        {
            // supplementaryVariables is null - DoubleMatrix
            {
                string parameterName = "supplementaryVariables";

                var cloud = TestableCloud00.Get().Cloud;

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    cloud.GetVariances((DoubleMatrix)null);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

            // supplementaryVariables is null - ReadOnlyDoubleMatrix
            {
                string parameterName = "supplementaryVariables";

                var cloud = TestableCloud00.Get().Cloud;

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    cloud.GetVariances((ReadOnlyDoubleMatrix)null);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

            // supplementaryVariables must have as number of rows the number of cloud points
            {
                var STR_EXCEPT_PAR_ROW_DIM_MUST_MATCH_INDIVIDUALS_COUNT =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_ROW_DIM_MUST_MATCH_INDIVIDUALS_COUNT");

                string parameterName = "supplementaryVariables";

                var cloud = TestableCloud00.Get().Cloud;

                var supplementaryVariables =
                    DoubleMatrix.Dense(
                        cloud.Coordinates.NumberOfRows + 1,
                        10);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    cloud.GetVariances(supplementaryVariables);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_ROW_DIM_MUST_MATCH_INDIVIDUALS_COUNT,
                    expectedParameterName: parameterName);
            }

            // Valid input - DoubleMatrix
            {
                var cloud = TestableCloud00.Get().Cloud;

                var actual = cloud.GetVariances((DoubleMatrix)cloud.Coordinates);

                var cov_sa   = cloud.Covariance;
                var expected = DoubleMatrix.Dense(1, cloud.Coordinates.NumberOfColumns);
                for (int i = 0; i < expected.Count; i++)
                {
                    expected[i] = cov_sa[i, i];
                }

                DoubleMatrixAssert.AreEqual(
                    expected: expected,
                    actual: actual,
                    delta: CloudTest.Accuracy);
            }

            // Valid input - ReadOnlyDoubleMatrix
            {
                var cloud = TestableCloud00.Get().Cloud;

                var actual = cloud.GetVariances(cloud.Coordinates);

                var cov_sa   = cloud.Covariance;
                var expected = DoubleMatrix.Dense(1, cloud.Coordinates.NumberOfColumns);
                for (int i = 0; i < expected.Count; i++)
                {
                    expected[i] = cov_sa[i, i];
                }

                DoubleMatrixAssert.AreEqual(
                    expected: expected,
                    actual: actual,
                    delta: CloudTest.Accuracy);
            }
        }
Exemple #27
0
        public void ConstructorTest()
        {
            // coordinates is null
            {
                string parameterName = "coordinates";

                DoubleMatrix coordinates = null;
                DoubleMatrix weights     = DoubleMatrix.Dense(1, 1, 1.0 / 4.0);
                Basis        basis       = Basis.Standard(2);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis,
                        copyData: true);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis,
                        copyData: false);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

            // weights is null
            {
                string parameterName = "weights";

                DoubleMatrix coordinates = DoubleMatrix.Dense(4, 2);
                DoubleMatrix weights     = null;
                Basis        basis       = Basis.Standard(2);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis,
                        copyData: true);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis,
                        copyData: false);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

            // basis is null
            {
                string parameterName = "basis";

                DoubleMatrix coordinates = DoubleMatrix.Dense(4, 2);
                DoubleMatrix weights     = DoubleMatrix.Dense(4, 1, 1.0 / 4.0);
                Basis        basis       = null;

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis,
                        copyData: true);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis,
                        copyData: false);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

            // weights is not a column vector
            {
                var STR_EXCEPT_PAR_MUST_BE_COLUMN_VECTOR =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_COLUMN_VECTOR");

                string parameterName = "weights";

                DoubleMatrix coordinates = DoubleMatrix.Dense(4, 2);
                DoubleMatrix weights     = DoubleMatrix.Dense(1, 4);
                Basis        basis       = Basis.Standard(2);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis,
                        copyData: true);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_COLUMN_VECTOR,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis,
                        copyData: false);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_COLUMN_VECTOR,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_COLUMN_VECTOR,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_COLUMN_VECTOR,
                    expectedParameterName: parameterName);
            }

            // weights must have the number of rows of coordinates
            {
                var STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_ROWS =
                    string.Format(CultureInfo.InvariantCulture,
                                  ImplementationServices.GetResourceString(
                                      "STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_ROWS"),
                                  "coordinates");

                string parameterName = "weights";

                DoubleMatrix coordinates = DoubleMatrix.Dense(4, 2);
                DoubleMatrix weights     = DoubleMatrix.Dense(5, 1);
                Basis        basis       = Basis.Standard(2);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis,
                        copyData: true);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_ROWS,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis,
                        copyData: false);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_ROWS,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_ROWS,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_ROWS,
                    expectedParameterName: parameterName);
            }

            // weights must have non negative entries
            {
                var STR_EXCEPT_PAR_ENTRIES_MUST_BE_NON_NEGATIVE =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_ENTRIES_MUST_BE_NON_NEGATIVE");

                string parameterName = "weights";

                DoubleMatrix coordinates = DoubleMatrix.Dense(4, 2);
                DoubleMatrix weights     = DoubleMatrix.Dense(4, 1, -1.0);
                Basis        basis       = Basis.Standard(2);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis,
                        copyData: true);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_ENTRIES_MUST_BE_NON_NEGATIVE,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis,
                        copyData: false);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_ENTRIES_MUST_BE_NON_NEGATIVE,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_ENTRIES_MUST_BE_NON_NEGATIVE,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_ENTRIES_MUST_BE_NON_NEGATIVE,
                    expectedParameterName: parameterName);
            }

            // weights must have entries summing up to 1
            {
                var STR_EXCEPT_PAR_ENTRIES_MUST_SUM_TO_1 =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_ENTRIES_MUST_SUM_TO_1");

                string parameterName = "weights";

                DoubleMatrix coordinates = DoubleMatrix.Dense(4, 2);
                DoubleMatrix weights     = DoubleMatrix.Dense(4, 1,
                                                              new double[4] {
                    .3, .6, .2, .1
                });
                Basis basis = Basis.Standard(2);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis,
                        copyData: true);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_ENTRIES_MUST_SUM_TO_1,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis,
                        copyData: false);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_ENTRIES_MUST_SUM_TO_1,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_ENTRIES_MUST_SUM_TO_1,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_ENTRIES_MUST_SUM_TO_1,
                    expectedParameterName: parameterName);
            }

            // basis must have dimension equal to the coordinates number of columns
            {
                var STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_ROWS =
                    string.Format(CultureInfo.InvariantCulture,
                                  ImplementationServices.GetResourceString(
                                      "STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_COLUMNS"),
                                  "coordinates");

                string parameterName = "basis";

                DoubleMatrix coordinates = DoubleMatrix.Dense(4, 2);
                DoubleMatrix weights     = DoubleMatrix.Dense(4, 1, 1.0 / 4.0);
                Basis        basis       = Basis.Standard(5);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis,
                        copyData: true);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_ROWS,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis,
                        copyData: false);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_ROWS,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_ROWS,
                    expectedParameterName: parameterName);
            }

            // Valid input and copyData is true
            {
                DoubleMatrix coordinates = DoubleMatrix.Dense(4, 2);
                DoubleMatrix weights     = DoubleMatrix.Dense(4, 1, 1.0 / 4.0);
                Basis        basis       = Basis.Standard(2);

                {
                    var cloud = new Cloud(
                        coordinates,
                        weights,
                        basis,
                        copyData: true);

                    DoubleMatrixAssert.AreEqual(
                        expected: basis.GetBasisMatrix(),
                        actual: cloud.Basis.GetBasisMatrix(),
                        delta: CloudTest.Accuracy);

                    DoubleMatrixAssert.AreEqual(
                        expected: coordinates,
                        actual: cloud.Coordinates,
                        delta: CloudTest.Accuracy);

                    DoubleMatrixAssert.AreEqual(
                        expected: weights,
                        actual: cloud.Weights,
                        delta: CloudTest.Accuracy);

                    coordinates[0, 0] = Double.PositiveInfinity;

                    Assert.AreNotEqual(
                        notExpected: coordinates[0, 0],
                        actual: cloud.Coordinates[0, 0],
                        delta: CloudTest.Accuracy);
                }

                {
                    var cloud = new Cloud(
                        coordinates,
                        weights);

                    DoubleMatrixAssert.AreEqual(
                        expected: basis.GetBasisMatrix(),
                        actual: cloud.Basis.GetBasisMatrix(),
                        delta: CloudTest.Accuracy);

                    DoubleMatrixAssert.AreEqual(
                        expected: coordinates,
                        actual: cloud.Coordinates,
                        delta: CloudTest.Accuracy);

                    DoubleMatrixAssert.AreEqual(
                        expected: weights,
                        actual: cloud.Weights,
                        delta: CloudTest.Accuracy);
                }

                {
                    var cloud = new Cloud(
                        coordinates);

                    DoubleMatrixAssert.AreEqual(
                        expected: basis.GetBasisMatrix(),
                        actual: cloud.Basis.GetBasisMatrix(),
                        delta: CloudTest.Accuracy);

                    DoubleMatrixAssert.AreEqual(
                        expected: coordinates,
                        actual: cloud.Coordinates,
                        delta: CloudTest.Accuracy);

                    DoubleMatrixAssert.AreEqual(
                        expected: weights,
                        actual: cloud.Weights,
                        delta: CloudTest.Accuracy);
                }
            }

            // Valid input and copyData is false
            {
                DoubleMatrix coordinates = DoubleMatrix.Dense(4, 2);
                DoubleMatrix weights     = DoubleMatrix.Dense(4, 1, 1.0 / 4.0);
                Basis        basis       = Basis.Standard(2);

                {
                    var cloud = new Cloud(
                        coordinates,
                        weights,
                        basis,
                        copyData: false);

                    DoubleMatrixAssert.AreEqual(
                        expected: basis.GetBasisMatrix(),
                        actual: cloud.Basis.GetBasisMatrix(),
                        delta: CloudTest.Accuracy);

                    DoubleMatrixAssert.AreEqual(
                        expected: coordinates,
                        actual: cloud.Coordinates,
                        delta: CloudTest.Accuracy);

                    DoubleMatrixAssert.AreEqual(
                        expected: weights,
                        actual: cloud.Weights,
                        delta: CloudTest.Accuracy);

                    coordinates[0, 0] = Double.PositiveInfinity;

                    Assert.AreEqual(
                        expected: coordinates[0, 0],
                        actual: cloud.Coordinates[0, 0],
                        delta: CloudTest.Accuracy);
                }
            }
        }
Exemple #28
0
        public void ConstructorTest()
        {
            // Valid input
            {
                int populationSize = 9;

                int sampleSize = 4;

                var randomSampling = new SimpleRandomSampling(
                    populationSize: populationSize,
                    sampleSize: sampleSize);

                Assert.AreEqual(
                    expected: populationSize,
                    actual: randomSampling.PopulationSize);
                Assert.AreEqual(
                    expected: sampleSize,
                    actual: randomSampling.SampleSize);
            }

            // populationSize <= 1
            {
                string STR_EXCEPT_PAR_MUST_BE_GREATER_THAN_VALUE =
                    string.Format(
                        ImplementationServices.GetResourceString(
                            "STR_EXCEPT_PAR_MUST_BE_GREATER_THAN_VALUE"),
                        "1");

                int populationSize = 1;

                int sampleSize = 4;

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    var randomSampling = new SimpleRandomSampling(
                        populationSize: populationSize,
                        sampleSize: sampleSize);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage: STR_EXCEPT_PAR_MUST_BE_GREATER_THAN_VALUE,
                    expectedParameterName: "populationSize");
            }

            // sampleSize <= 0
            {
                string STR_EXCEPT_PAR_MUST_BE_POSITIVE =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_POSITIVE");

                int populationSize = 9;

                int sampleSize = 0;

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    var randomSampling = new SimpleRandomSampling(
                        populationSize: populationSize,
                        sampleSize: sampleSize);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage: STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: "sampleSize");
            }

            // populationSize <= sampleSize
            {
                string STR_EXCEPT_PAR_MUST_BE_LESS_THAN_VALUE =
                    string.Format(
                        ImplementationServices.GetResourceString(
                            "STR_EXCEPT_PAR_MUST_BE_LESS_THAN_VALUE"),
                        "the population size");

                int populationSize = 9;

                int sampleSize = 9;

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    var randomSampling = new SimpleRandomSampling(
                        populationSize: populationSize,
                        sampleSize: sampleSize);
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage: STR_EXCEPT_PAR_MUST_BE_LESS_THAN_VALUE,
                    expectedParameterName: "sampleSize");
            }
        }
Exemple #29
0
        public void GetCategoricalEntailmentEnsembleClassifierTest()
        {
            // state is null
            {
                string parameterName = "state";

                var testableContext = TestableCategoricalEntailmentEnsembleOptimizationContext01.Get();

                var context = testableContext.Context;

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    context.GetCategoricalEntailmentEnsembleClassifier(
                        state: null,
                        featureVariables: testableContext.FeatureVariables,
                        responseVariable: testableContext.ResponseVariable);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

            // state has wrong count
            {
                string STR_EXCEPT_CEE_INVALID_STATE_COUNT
                    = ImplementationServices.GetResourceString(
                          "STR_EXCEPT_CEE_INVALID_STATE_COUNT");

                string parameterName = "state";

                var testableContext = TestableCategoricalEntailmentEnsembleOptimizationContext01.Get();

                var context = testableContext.Context;

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    context.GetCategoricalEntailmentEnsembleClassifier(
                        state: DoubleMatrix.Dense(1, 8),
                        featureVariables: testableContext.FeatureVariables,
                        responseVariable: testableContext.ResponseVariable);
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage: STR_EXCEPT_CEE_INVALID_STATE_COUNT,
                    expectedParameterName: parameterName);
            }

            // featureVariables is null
            {
                string parameterName = "featureVariables";

                var testableContext = TestableCategoricalEntailmentEnsembleOptimizationContext01.Get();

                var context = testableContext.Context;

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    context.GetCategoricalEntailmentEnsembleClassifier(
                        state: DoubleMatrix.Dense(1, 9,
                                                  new double[9] {
                        0, 0, 0, 1, 1, 1, 1, 0, 1.0
                    }),
                        featureVariables: null,
                        responseVariable: testableContext.ResponseVariable);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

            // featureVariables has wrong count
            {
                string STR_EXCEPT_CEE_MUST_HAVE_SAME_FEATURES_COUNT
                    = ImplementationServices.GetResourceString(
                          "STR_EXCEPT_CEE_MUST_HAVE_SAME_FEATURES_COUNT");

                string parameterName = "featureVariables";

                var testableContext = TestableCategoricalEntailmentEnsembleOptimizationContext01.Get();

                var context = testableContext.Context;

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    context.GetCategoricalEntailmentEnsembleClassifier(
                        state: DoubleMatrix.Dense(1, 9,
                                                  new double[9] {
                        0, 0, 0, 1, 1, 1, 1, 0, 1.0
                    }),
                        featureVariables: new List <CategoricalVariable>(),
                        responseVariable: testableContext.ResponseVariable);
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage: STR_EXCEPT_CEE_MUST_HAVE_SAME_FEATURES_COUNT,
                    expectedParameterName: parameterName);
            }

            // featureVariables contains a variable having wrong number of categories
            {
                string STR_EXCEPT_CEE_INVALID_FEATURE_CATEGORIES_COUNT
                    = ImplementationServices.GetResourceString(
                          "STR_EXCEPT_CEE_INVALID_FEATURE_CATEGORIES_COUNT");

                string parameterName = "featureVariables";

                var testableContext = TestableCategoricalEntailmentEnsembleOptimizationContext01.Get();

                var context = testableContext.Context;

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    context.GetCategoricalEntailmentEnsembleClassifier(
                        state: DoubleMatrix.Dense(1, 9,
                                                  new double[9] {
                        0, 0, 0, 1, 1, 1, 1, 0, 1.0
                    }),
                        featureVariables: new List <CategoricalVariable>()
                    {
                        new CategoricalVariable("F")
                        {
                            { 0.0, "A" }, 1.0
                        }
                    },
                        responseVariable: testableContext.ResponseVariable);
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage: STR_EXCEPT_CEE_INVALID_FEATURE_CATEGORIES_COUNT,
                    expectedParameterName: parameterName);
            }

            // responseVariable is null
            {
                string parameterName = "responseVariable";

                var testableContext = TestableCategoricalEntailmentEnsembleOptimizationContext01.Get();

                var context = testableContext.Context;

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    context.GetCategoricalEntailmentEnsembleClassifier(
                        state: DoubleMatrix.Dense(1, 9,
                                                  new double[9] {
                        0, 0, 0, 1, 1, 1, 1, 0, 1.0
                    }),
                        featureVariables: testableContext.FeatureVariables,
                        responseVariable: null);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

            // responseVariable has a wrong number of categories
            {
                string STR_EXCEPT_CEE_INVALID_RESPONSE_CATEGORIES_COUNT
                    = ImplementationServices.GetResourceString(
                          "STR_EXCEPT_CEE_INVALID_RESPONSE_CATEGORIES_COUNT");

                string parameterName = "responseVariable";

                var testableContext = TestableCategoricalEntailmentEnsembleOptimizationContext01.Get();

                var context = testableContext.Context;

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    context.GetCategoricalEntailmentEnsembleClassifier(
                        state: DoubleMatrix.Dense(1, 9,
                                                  new double[9] {
                        0, 0, 0, 1, 1, 1, 1, 0, 1.0
                    }),
                        featureVariables: testableContext.FeatureVariables,
                        responseVariable: new CategoricalVariable("R")
                    {
                        { 0.0, "A" }
                    });
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage: STR_EXCEPT_CEE_INVALID_RESPONSE_CATEGORIES_COUNT,
                    expectedParameterName: parameterName);
            }

            // valid input
            {
                var testableContext = TestableCategoricalEntailmentEnsembleOptimizationContext01.Get();

                var context = testableContext.Context;

                var actual = context.GetCategoricalEntailmentEnsembleClassifier(
                    state: DoubleMatrix.Dense(1, 9,
                                              new double[9] {
                    0, 0, 0, 1, 1, 1, 1, 0, .99
                }),
                    featureVariables: testableContext.FeatureVariables,
                    responseVariable: testableContext.ResponseVariable);

                var expected =
                    new CategoricalEntailmentEnsembleClassifier(
                        featureVariables: testableContext.FeatureVariables,
                        responseVariable: testableContext.ResponseVariable);

                expected.Add(
                    new CategoricalEntailment(
                        featureVariables: testableContext.FeatureVariables,
                        responseVariable: testableContext.ResponseVariable,
                        featurePremises: new List <SortedSet <double> >()
                {
                    new SortedSet <double>()
                    {
                        3.0, 4.0, 5.0
                    }
                },
                        responseConclusion: 0.0,
                        truthValue: .99));

                ListAssert <CategoricalEntailment> .ContainSameItems(
                    expected : new List <CategoricalEntailment>(expected.Entailments),
                    actual : new List <CategoricalEntailment>(actual.Entailments),
                    areEqual : CategoricalEntailmentAssert.AreEqual);
            }
        }
Exemple #30
0
        public void ConstructorTest()
        {
            // optimizationGoal is not a field of OptimizationGoal
            {
                var STR_EXCEPT_NOT_FIELD_OF_OPTIMIZATION_GOAL =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_NOT_FIELD_OF_OPTIMIZATION_GOAL");

                string parameterName = "optimizationGoal";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new CategoricalEntailmentEnsembleOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        featureCategoryCounts: new List <int>(2)
                    {
                        5, 6
                    },
                        numberOfResponseCategories: 3,
                        numberOfCategoricalEntailments: 4,
                        allowEntailmentPartialTruthValues: true,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: (OptimizationGoal)(-1),
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage:
                    STR_EXCEPT_NOT_FIELD_OF_OPTIMIZATION_GOAL,
                    expectedParameterName: parameterName);
            }

            // objectiveFunction is null
            {
                string parameterName = "objectiveFunction";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new CategoricalEntailmentEnsembleOptimizationContext(
                        objectiveFunction: null,
                        featureCategoryCounts: new List <int>(3)
                    {
                        5, 6, 7
                    },
                        numberOfResponseCategories: 3,
                        numberOfCategoricalEntailments: 4,
                        allowEntailmentPartialTruthValues: true,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

            // featureCategoryCounts is null
            {
                string parameterName = "featureCategoryCounts";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new CategoricalEntailmentEnsembleOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        featureCategoryCounts: null,
                        numberOfResponseCategories: 3,
                        numberOfCategoricalEntailments: 4,
                        allowEntailmentPartialTruthValues: true,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

            // featureCategoryCounts is empty
            {
                var STR_EXCEPT_PAR_MUST_BE_NON_EMPTY =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_NON_EMPTY");

                string parameterName = "featureCategoryCounts";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new CategoricalEntailmentEnsembleOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        featureCategoryCounts: new List <int>(),
                        numberOfResponseCategories: 3,
                        numberOfCategoricalEntailments: 4,
                        allowEntailmentPartialTruthValues: true,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_NON_EMPTY,
                    expectedParameterName: parameterName);
            }

            // featureCategoryCounts has zero entries
            {
                var STR_EXCEPT_PAR_ENTRIES_MUST_BE_POSITIVE =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_ENTRIES_MUST_BE_POSITIVE");

                string parameterName = "featureCategoryCounts";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new CategoricalEntailmentEnsembleOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        featureCategoryCounts: new List <int>(3)
                    {
                        5, 0, 7
                    },
                        numberOfResponseCategories: 3,
                        numberOfCategoricalEntailments: 4,
                        allowEntailmentPartialTruthValues: true,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_ENTRIES_MUST_BE_POSITIVE,
                    expectedParameterName: parameterName);
            }

            // featureCategoryCounts has negative entries
            {
                var STR_EXCEPT_PAR_ENTRIES_MUST_BE_POSITIVE =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_ENTRIES_MUST_BE_POSITIVE");

                string parameterName = "featureCategoryCounts";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new CategoricalEntailmentEnsembleOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        featureCategoryCounts: new List <int>(3)
                    {
                        5, -1, 7
                    },
                        numberOfResponseCategories: 3,
                        numberOfCategoricalEntailments: 4,
                        allowEntailmentPartialTruthValues: true,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_ENTRIES_MUST_BE_POSITIVE,
                    expectedParameterName: parameterName);
            }

            // numberOfResponseCategories is zero
            {
                var STR_EXCEPT_PAR_MUST_BE_POSITIVE =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_POSITIVE");

                string parameterName = "numberOfResponseCategories";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new CategoricalEntailmentEnsembleOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        featureCategoryCounts: new List <int>(3)
                    {
                        5, 6, 7
                    },
                        numberOfResponseCategories: 0,
                        numberOfCategoricalEntailments: 4,
                        allowEntailmentPartialTruthValues: true,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: parameterName);
            }

            // numberOfResponseCategories is negative
            {
                var STR_EXCEPT_PAR_MUST_BE_POSITIVE =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_POSITIVE");

                string parameterName = "numberOfResponseCategories";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new CategoricalEntailmentEnsembleOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        featureCategoryCounts: new List <int>(3)
                    {
                        5, 6, 7
                    },
                        numberOfResponseCategories: -1,
                        numberOfCategoricalEntailments: 4,
                        allowEntailmentPartialTruthValues: true,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: parameterName);
            }

            // numberOfCategoricalEntailments is zero
            {
                var STR_EXCEPT_PAR_MUST_BE_POSITIVE =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_POSITIVE");

                string parameterName = "numberOfCategoricalEntailments";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new CategoricalEntailmentEnsembleOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        featureCategoryCounts: new List <int>(3)
                    {
                        5, 6, 7
                    },
                        numberOfResponseCategories: 3,
                        numberOfCategoricalEntailments: 0,
                        allowEntailmentPartialTruthValues: true,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: parameterName);
            }

            // numberOfCategoricalEntailments is negative
            {
                var STR_EXCEPT_PAR_MUST_BE_POSITIVE =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_POSITIVE");

                string parameterName = "numberOfCategoricalEntailments";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new CategoricalEntailmentEnsembleOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        featureCategoryCounts: new List <int>(3)
                    {
                        5, 6, 7
                    },
                        numberOfResponseCategories: 3,
                        numberOfCategoricalEntailments: -1,
                        allowEntailmentPartialTruthValues: true,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: parameterName);
            }

            // minimumNumberOfIterations is zero
            {
                var STR_EXCEPT_PAR_MUST_BE_POSITIVE =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_POSITIVE");

                string parameterName = "minimumNumberOfIterations";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new CategoricalEntailmentEnsembleOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        featureCategoryCounts: new List <int>(3)
                    {
                        5, 6, 7
                    },
                        numberOfResponseCategories: 3,
                        numberOfCategoricalEntailments: 4,
                        allowEntailmentPartialTruthValues: true,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 0,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: parameterName);
            }

            // minimumNumberOfIterations is negative
            {
                var STR_EXCEPT_PAR_MUST_BE_POSITIVE =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_POSITIVE");

                string parameterName = "minimumNumberOfIterations";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new CategoricalEntailmentEnsembleOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        featureCategoryCounts: new List <int>(3)
                    {
                        5, 6, 7
                    },
                        numberOfResponseCategories: 3,
                        numberOfCategoricalEntailments: 4,
                        allowEntailmentPartialTruthValues: true,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: -1,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: parameterName);
            }

            // maximumNumberOfIterations is zero
            {
                var STR_EXCEPT_PAR_MUST_BE_POSITIVE =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_POSITIVE");

                string parameterName = "maximumNumberOfIterations";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new CategoricalEntailmentEnsembleOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        featureCategoryCounts: new List <int>(3)
                    {
                        5, 6, 7
                    },
                        numberOfResponseCategories: 3,
                        numberOfCategoricalEntailments: 4,
                        allowEntailmentPartialTruthValues: true,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 0);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: parameterName);
            }

            // maximumNumberOfIterations is negative
            {
                var STR_EXCEPT_PAR_MUST_BE_POSITIVE =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_POSITIVE");

                string parameterName = "maximumNumberOfIterations";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new CategoricalEntailmentEnsembleOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        featureCategoryCounts: new List <int>(3)
                    {
                        5, 6, 7
                    },
                        numberOfResponseCategories: 3,
                        numberOfCategoricalEntailments: 4,
                        allowEntailmentPartialTruthValues: true,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: -1);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: parameterName);
            }

            // minimumNumberOfIterations is greater than maximumNumberOfIterations
            {
                var STR_EXCEPT_PAR_MUST_BE_GREATER_THAN_OTHER =
                    string.Format(
                        ImplementationServices.GetResourceString(
                            "STR_EXCEPT_PAR_MUST_BE_GREATER_THAN_OTHER"),
                        "maximumNumberOfIterations",
                        "minimumNumberOfIterations");

                string parameterName = "maximumNumberOfIterations";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new CategoricalEntailmentEnsembleOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        featureCategoryCounts: new List <int>(3)
                    {
                        5, 6, 7
                    },
                        numberOfResponseCategories: 3,
                        numberOfCategoricalEntailments: 4,
                        allowEntailmentPartialTruthValues: true,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 2);
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_GREATER_THAN_OTHER,
                    expectedParameterName: parameterName);
            }

            // probabilitySmoothingCoefficient is zero
            {
                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 = "probabilitySmoothingCoefficient";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new CategoricalEntailmentEnsembleOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        featureCategoryCounts: new List <int>(3)
                    {
                        5, 6, 7
                    },
                        numberOfResponseCategories: 3,
                        numberOfCategoricalEntailments: 4,
                        allowEntailmentPartialTruthValues: true,
                        probabilitySmoothingCoefficient: .0,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL,
                    expectedParameterName: parameterName);
            }

            // probabilitySmoothingCoefficient is negative
            {
                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 = "probabilitySmoothingCoefficient";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new CategoricalEntailmentEnsembleOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        featureCategoryCounts: new List <int>(3)
                    {
                        5, 6, 7
                    },
                        numberOfResponseCategories: 3,
                        numberOfCategoricalEntailments: 4,
                        allowEntailmentPartialTruthValues: true,
                        probabilitySmoothingCoefficient: -.1,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL,
                    expectedParameterName: parameterName);
            }

            // probabilitySmoothingCoefficient is one
            {
                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 = "probabilitySmoothingCoefficient";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new CategoricalEntailmentEnsembleOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        featureCategoryCounts: new List <int>(3)
                    {
                        5, 6, 7
                    },
                        numberOfResponseCategories: 3,
                        numberOfCategoricalEntailments: 4,
                        allowEntailmentPartialTruthValues: true,
                        probabilitySmoothingCoefficient: 1.0,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL,
                    expectedParameterName: parameterName);
            }

            // probabilitySmoothingCoefficient is greater than one
            {
                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 = "probabilitySmoothingCoefficient";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new CategoricalEntailmentEnsembleOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        featureCategoryCounts: new List <int>(3)
                    {
                        5, 6, 7
                    },
                        numberOfResponseCategories: 3,
                        numberOfCategoricalEntailments: 4,
                        allowEntailmentPartialTruthValues: true,
                        probabilitySmoothingCoefficient: 1.1,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL,
                    expectedParameterName: parameterName);
            }

            // valid input - HigherThanLevel
            {
                var testableContext = TestableCategoricalEntailmentEnsembleOptimizationContext00.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.OptimizationGoal,
                    actual: context.OptimizationGoal);

                DoubleMatrixAssert.AreEqual(
                    expected: testableContext.InitialParameter,
                    actual: context.InitialParameter,
                    DoubleMatrixTest.Accuracy);

                Assert.AreEqual(
                    expected: testableContext.MinimumNumberOfIterations,
                    actual: context.MinimumNumberOfIterations);

                Assert.AreEqual(
                    expected: testableContext.MaximumNumberOfIterations,
                    actual: context.MaximumNumberOfIterations);

                Assert.AreEqual(
                    expected: testableContext.FeatureCategoryCounts.Count,
                    actual: context.FeatureCategoryCounts.Count);

                for (int i = 0; i < context.FeatureCategoryCounts.Count; i++)
                {
                    Assert.AreEqual(
                        expected: testableContext.FeatureCategoryCounts[i],
                        actual: context.FeatureCategoryCounts[i]);
                }

                Assert.AreEqual(
                    expected: testableContext.NumberOfResponseCategories,
                    actual: context.NumberOfResponseCategories);

                Assert.AreEqual(
                    expected: testableContext.NumberOfCategoricalEntailments,
                    actual: context.NumberOfCategoricalEntailments);

                Assert.AreEqual(
                    expected: testableContext.AllowEntailmentPartialTruthValues,
                    actual: context.AllowEntailmentPartialTruthValues);

                Assert.AreEqual(
                    expected: testableContext.ProbabilitySmoothingCoefficient,
                    actual: context.ProbabilitySmoothingCoefficient,
                    delta: DoubleMatrixTest.Accuracy);
            }
        }