public void TestMethod()
        {
            /*
             * Exponential distribution.
             */

            PIDebug.TitleBig("Exponential distribution (1)");

            PIExponentialDistribution distribution = new PIExponentialDistribution();

            /* -- 1) Get probability */

            PIDebug.Title("1) Get probability for X");

            /* lambda=1 */
            PIDebug.Title("..lambda=1");

            Console.WriteLine("Probability for x=0.0 : " + distribution.GetProbability(0.0));
            Assert.AreEqual(0.0, distribution.GetProbability(0.0), 0.001);
            Console.WriteLine("Probability for x=1.0 : " + distribution.GetProbability(1.0));
            Assert.AreEqual(0.632, distribution.GetProbability(1.0), 0.001);
            Console.WriteLine("Probability for x=2.0 : " + distribution.GetProbability(2.0));
            Assert.AreEqual(0.864, distribution.GetProbability(2.0), 0.001);
            Console.WriteLine("Probability for x=3.0 : " + distribution.GetProbability(3.0));
            Assert.AreEqual(0.950, distribution.GetProbability(3.0), 0.001);
            Console.WriteLine("Probability for x=5.0 : " + distribution.GetProbability(5.0));
            Assert.AreEqual(0.9932, distribution.GetProbability(5.0), 0.001);

            /* -- 2) Get T for probability */

            PIDebug.Title("2) Get X for probability", true);

            /* lambda=1  */
            PIDebug.Title("..lambda=1");

            Console.WriteLine("X value for probability for prob=0.0 : " + distribution.GetXForProbability(0.0));
            Assert.AreEqual(0.0, distribution.GetXForProbability(0.0), 0.001);
            Console.WriteLine("X value for probability for prob=0.632 : " + distribution.GetXForProbability(0.632));
            Assert.AreEqual(1.0, distribution.GetXForProbability(0.632), 0.001);
            Console.WriteLine("X value for probability for prob=0.864 : " + distribution.GetXForProbability(0.864));
            Assert.AreEqual(2.0, distribution.GetXForProbability(0.864), 0.01);
            Console.WriteLine("X value for probability for prob=0.950 : " + distribution.GetXForProbability(0.950));
            Assert.AreEqual(3.0, distribution.GetXForProbability(0.950), 0.01);
            Console.WriteLine("X value for probability for prob=0.9932 : " + distribution.GetXForProbability(0.9932));
            Assert.AreEqual(5.0, distribution.GetXForProbability(0.9932), 0.01);

            /* -- 3) Get probability density */

            PIDebug.Title("3) Get probability density, lambda=1", true);

            Console.WriteLine("x=0.1 : " + distribution.GetPDF(0.1));
            Console.WriteLine("x=0.5 : " + distribution.GetPDF(0.5));
            Console.WriteLine("x=1.0 : " + distribution.GetPDF(1.0));
            Console.WriteLine("x=2.0 : " + distribution.GetPDF(2.0));
            Console.WriteLine("x=5.0 : " + distribution.GetPDF(5.0));
        }
        public void TestMethod()
        {
            /* -- prepare input variable */

            PIVariable var1 = new PIVariable();

            var1.AddMoreValues(4.0, 3);
            var1.AddMoreValues(6.0, 4);
            var1.AddMoreValues(7.0, 2);
            var1.AddMoreValues(9.0, 4);
            var1.AddMoreValues(10.0, 10);
            var1.AddMoreValues(11.0, 5);
            var1.AddMoreValues(11.5, 2);
            var1.AddMoreValues(12.0, 13);
            var1.AddMoreValues(13.0, 4);
            var1.AddMoreValues(14.0, 2);
            var1.AddMoreValues(15.0, 2);
            var1.AddMoreValues(16.0, 4);
            var1.AddMoreValues(17.0, 6);
            var1.AddMoreValues(18.0, 2);
            var1.AddMoreValues(19.0, 2);
            var1.AddMoreValues(20.0, 6);
            var1.AddMoreValues(22.0, 2);

            /* -- prepare classes */

            PIDebug.Title("Statistics classes for variable");

            PIStatisticsClasses statisticsClasses = new PIStatisticsClasses(var1);

            Assert.IsNotNull(statisticsClasses);

            statisticsClasses.GenerateClasses(GeneratingMethod.FREEDMAN_DIACONISRULE);
            Assert.AreNotEqual(0, statisticsClasses.Get_classes().Count);

            statisticsClasses.AsString();
        }
        public void TestMethod()
        {
            /* -- testing versions */

            PIDebug.Title("Version");

            Console.WriteLine(PIConfiguration.Version_isStable());

            /* -- testing getPartialSums() */

            PIDebug.Title("Partial sums", true);

            PIVariable variable = new PIVariable();

            variable.AddValues(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });

            PIVariable[] varArray = variable.GetPartsForPartialSum(3, CutStyleForPartialSum.START);

            if (varArray != null)
            {
                Console.WriteLine(varArray.Length);
                PIDebug.Blank();

                if (varArray.Length >= 1)
                {
                    Console.WriteLine(varArray[0].AsString(0));
                }
                if (varArray.Length >= 2)
                {
                    Console.WriteLine(varArray[1].AsString(0));
                }
                if (varArray.Length >= 3)
                {
                    Console.WriteLine(varArray[2].AsString(0));
                }
            }

            PIDebug.Blank();
            varArray = variable.GetPartsForPartialSum(3, CutStyleForPartialSum.END);

            if (varArray != null)
            {
                if (varArray.Length >= 1)
                {
                    Console.WriteLine(varArray[0].AsString(0));
                }
                if (varArray.Length >= 2)
                {
                    Console.WriteLine(varArray[1].AsString(0));
                }
                if (varArray.Length >= 3)
                {
                    Console.WriteLine(varArray[2].AsString(0));
                }
            }

            /* -- testing order */

            PIDebug.Title("Order in variable", true);

            PIVariable varA = new PIVariable();

            varA.AddValue(175);
            varA.AddValue(166);
            varA.AddValue(170);
            varA.AddValue(169);
            varA.AddValue(188);
            varA.AddValue(175);
            varA.AddValue(176);
            varA.AddValue(171);
            varA.AddValue(173);
            varA.AddValue(175);
            varA.AddValue(173);
            varA.AddValue(174);
            varA.AddValue(169);

            PIVariable uniqueOrderedA = varA.GetUniqueOrdered();

            Console.WriteLine("1) varA - unique list with frequency and order");
            PIDebug.Blank();

            for (int i = 0; i < uniqueOrderedA.Count(); i++)
            {
                Console.WriteLine(String.Format("value = {0:f2} , frequency = {1:d}, order = {2:f2}",
                                                uniqueOrderedA.GetValue(i), uniqueOrderedA.Get(i).GetFrequency(), uniqueOrderedA.Get(i).GetOrder()));
            }

            PIDebug.Blank();
            Console.WriteLine("1a) varA - frequency and order");
            PIDebug.Blank();

            for (int i = 0; i < varA.Count(); i++)
            {
                Console.WriteLine(String.Format("value = {0:f2} , frequency = {1:d}, order = {2:f2}",
                                                varA.GetValue(i), varA.Get(i).GetFrequency(), varA.Get(i).GetOrder()));
            }

            PIVariable varB = new PIVariable();

            varB.AddValue(69);
            varB.AddValue(55);
            varB.AddValue(67);
            varB.AddValue(52);
            varB.AddValue(90);
            varB.AddValue(53);
            varB.AddValue(57);
            varB.AddValue(57);
            varB.AddValue(68);
            varB.AddValue(73);
            varB.AddValue(62);
            varB.AddValue(90);
            varB.AddValue(63);

            varB.GetUniqueOrdered();

            PIDebug.Blank();
            Console.WriteLine("2a) varB - frequency and order");
            PIDebug.Blank();

            for (int i = 0; i < varB.Count(); i++)
            {
                Console.WriteLine(String.Format("value = {0:f2} , frequency = {1:d}, order = {2:f2}",
                                                varB.GetValue(i), varB.Get(i).GetFrequency(), varB.Get(i).GetOrder()));
            }

            PIDebug.Blank();
            Console.WriteLine("-> order`s difference");
            PIDebug.Blank();

            PITwoVariables varAB = new PITwoVariables(varA, varB);

            Console.WriteLine("Sum of order`s differences = " + varAB.GetSumOrderDiff2());
        }
Beispiel #4
0
        public void TestMethod()
        {
            /* -- source 2a, 566 */

            PIDebug.Title("Two variables");

            /* - prepare X data  */

            PIVariable X = new PIVariable();

            Assert.IsNotNull(X);

            X.AddValues(new int[] { 2, 3, 3, 3, 4, 4, 5, 5, 5, 6 });

            /* - prepare Y data  */

            PIVariable Y = new PIVariable();

            Assert.IsNotNull(Y);

            Y.AddValues(new double[] { 28.7, 24.8, 26.0, 30.5, 23.8, 24.6, 23.8, 20.4, 21.6, 22.1 });

            /* - input data, show R and R2 */

            PITwoVariables twoVariables = new PITwoVariables(X, Y);

            Console.WriteLine("Correlation coefficient = " + twoVariables.GetR());
            Console.WriteLine("Coefficient of determination = " + twoVariables.GetR2());

            PIDebug.Blank();

            Assert.AreEqual(-0.818, twoVariables.GetR(), 0.001);
            Assert.AreEqual(0.670, twoVariables.GetR2(), 0.001);

            /* ------------------------ covariance, source 2i ---------------------------- */

            PIDebug.Title("X, Y covariance");

            /* - prepare X data  */

            PIVariable X1 = new PIVariable();

            Assert.IsNotNull(X1);

            X1.AddValues(new Double[] { 1.1, 1.7, 2.1, 1.4, 0.2 });

            /* - prepare Y data  */

            PIVariable Y1 = new PIVariable();

            Assert.IsNotNull(Y1);

            Y1.AddValues(new double[] { 3.0, 4.2, 4.9, 4.1, 2.5 });

            /* - X, Y */

            PITwoVariables twoVariables1 = new PITwoVariables(X1, Y1);

            Console.WriteLine("Population covariance = " + twoVariables1.GetPopulationCovariance());
            Console.WriteLine("Sample covariance = " + twoVariables1.GetSampleCovariance());

            PIDebug.Blank();

            Assert.AreEqual(0.532, twoVariables1.GetPopulationCovariance(), 0.001);
            Assert.AreEqual(0.665, twoVariables1.GetSampleCovariance(), 0.001);

            /* -- source 2n */

            PIDebug.Title("Pearson correlation coefficient");

            PIVariable varX = new PIVariable();
            PIVariable varY = new PIVariable();

            Assert.IsNotNull(varX);
            Assert.IsNotNull(varY);

            PITwoVariables varXY = new PITwoVariables(varX, varY);

            Assert.IsNotNull(varXY);

            varXY.AddXY(175, 69);
            varXY.AddXY(166, 55);
            varXY.AddXY(170, 67);
            varXY.AddXY(169, 52);
            varXY.AddXY(188, 90);
            varXY.AddXY(175, 53);
            varXY.AddXY(176, 57);
            varXY.AddXY(171, 57);
            varXY.AddXY(173, 68);
            varXY.AddXY(175, 73);
            varXY.AddXY(173, 62);
            varXY.AddXY(174, 90);
            varXY.AddXY(169, 63);

            Console.WriteLine("R = " + varXY.GetR());
            Console.WriteLine("Pearson`s correlation coeffiecient = " + varXY.GetPearsonCorrelationCoefficient());

            Assert.AreEqual(0.639, varXY.GetPearsonCorrelationCoefficient(), 0.001);

            Console.WriteLine("Spearman`s rank correlation coeffiecient = " + varXY.GetSpearmanCorrelationCoefficient());

            Assert.AreEqual(0.475, varXY.GetSpearmanCorrelationCoefficient(), 0.001);

            //Console.Write( string.Format( "{0,3}", "Test") + "x" );
        }
Beispiel #5
0
        public void TestMethod()
        {
            /* Cramers rule (Prachar II/15)
             *
             * x1 + 2x2 - 3x3 = -9
             * 2x1 - 3x2 - 5x3 = -8
             * x1 + x2 + x3 = 4
             *
             * */

            PIDebug.TitleBig("Cramers Rule - (degree=3)");

            PICramersRule cramersRule = new PICramersRule(3);

            Assert.IsNotNull(cramersRule);

            PIDebug.Title("A");

            cramersRule.Get_matrixA().AddValues(new int[] { 1, 2, -3, 2, -3, -5, 1, 1, 1 });
            Console.WriteLine(cramersRule.Get_matrixA().AsString(0));

            PIDebug.Title("B", true);

            cramersRule.Get_matrixB().AddValues(new int[] { -9, -8, 4 });
            Console.WriteLine(cramersRule.Get_matrixB().AsString(0));

            PIDebug.Blank();

            int i = cramersRule.Calc();

            switch (i)
            {
            case -1:
                Console.WriteLine("Some error.");
                Assert.Fail("Some error.");
                break;

            case 0:
                Console.WriteLine("System of linear equitions has no solution.");
                break;

            case 1:
                Console.WriteLine("Result = " + cramersRule.Get_results().AsString(0));

                Assert.AreEqual(2.0, (double)cramersRule.Get_results().GetValue(0));
                Assert.AreEqual(-1.0, (double)cramersRule.Get_results().GetValue(1));
                Assert.AreEqual(3.0, (double)cramersRule.Get_results().GetValue(2));
                break;
            }

            /* Cramers rule - A (1c/16a)
             *
             * x + 4y = 37
             * 2x + 5y = 53
             *
             * */

            PIDebug.TitleBig("Cramers Rule - A - (degree=2)", true);

            PICramersRule cramersRuleA = new PICramersRule(2);

            Assert.IsNotNull(cramersRuleA);

            PIDebug.Title("A");

            cramersRuleA.Get_matrixA().AddValues(new int[] { 1, 4, 2, 5 });
            Console.WriteLine(cramersRuleA.Get_matrixA().AsString(0));

            PIDebug.Title("B", true);

            cramersRuleA.Get_matrixB().AddValues(new int[] { 37, 53 });
            Console.WriteLine(cramersRuleA.Get_matrixB().AsString(0));

            PIDebug.Blank();

            i = cramersRuleA.Calc();

            switch (i)
            {
            case -1:
                Console.WriteLine("Some error.");
                Assert.Fail("Some error.");
                break;

            case 0:
                Console.WriteLine("System of linear equitions has not solution.");
                break;

            case 1:
                Console.WriteLine("Result = " + cramersRuleA.Get_results().AsString(0));

                Assert.AreEqual(9.0, (double)cramersRuleA.Get_results().GetValue(0));
                Assert.AreEqual(7.0, (double)cramersRuleA.Get_results().GetValue(1));
                break;
            }

            /* Cramers rule - B (1c/16b)
             *
             * 3x - 2y = 8
             * -9x + 6y = 5
             *
             * */

            PIDebug.TitleBig("Cramers Rule - B - (degree=2)", true);

            PICramersRule cramersRuleB = new PICramersRule(2);

            Assert.IsNotNull(cramersRuleB);

            PIDebug.Title("A");

            cramersRuleB.Get_matrixA().AddValues(new int[] { 3, -2, -9, 6 });
            Console.WriteLine(cramersRuleB.Get_matrixA().AsString(0));

            PIDebug.Title("B");

            cramersRuleB.Get_matrixB().AddValues(new int[] { 8, 5 });
            Console.WriteLine(cramersRuleB.Get_matrixB().AsString(0));

            PIDebug.Blank();

            i = cramersRuleB.Calc();

            switch (i)
            {
            case -1:
                Console.WriteLine("Some error.");
                Assert.Fail("Some error.");
                break;

            case 0:
                Console.WriteLine("System of linear equitions has not solution.");
                break;

            case 1:
                Console.WriteLine("Result = " + cramersRuleB.Get_results().AsString(0));
                break;
            }

            Assert.AreEqual(cramersRuleB.Get_results().Count(), 0);
        }
        public void TestMethod()
        {
            /* --------------------------------- */

            PIDebug.TitleBig("Variable");

            /* check variable */

            PIVariable var = new PIVariable();

            Assert.IsNotNull(var);

            /* clear variable */

            var.Clear();
            System.Console.WriteLine("Alter clear()");
            Assert.AreEqual(0, var.Count());

            /* add values */

            var.AddValue((double)10);
            var.AddValue((double)20);
            var.AddValue((double)30);

            /* set value */

            PIDebug.Blank();
            PIDebug.Title("set");
            var.SetValue(1, 25.0);
            Assert.AreEqual(25.0, (double)var.GetValue(1), 0.01);

            Console.WriteLine(var.AsString(0));

            var.SetValue(1, 20.0);

            PIDebug.Blank();

            /* median - odd */

            double median = var.GetMedian();

            Console.WriteLine("median (odd) = " + median);
            Assert.AreEqual(20.0, (double)median, 0.001);

            /* check count */

            Console.WriteLine("count() = " + var.Count());
            Assert.AreEqual(3, var.Count());

            /* check sum */

            Console.WriteLine("sum() = " + var.GetSum());
            Assert.AreEqual(60.0, var.GetSum());

            /* --------------------------------- */

            PIDebug.Blank();
            PIDebug.TitleBig("Variable - another");

            /* add values (source 2a/40) */

            var.Clear();
            var.Set_alwaysRecalc(true);

            var.AddValue(1.90);
            var.AddValue(3.00);
            var.AddValue(2.53);
            var.AddValue(3.71);
            var.AddValue(2.12);
            var.AddValue(1.76);
            var.AddValue(2.71);
            var.AddValue(1.39);
            var.AddValue(4.00);
            var.AddValue(3.33);

            Double sampleMean = var.GetSampleMean();

            Console.WriteLine("sampleMean = " + sampleMean);

            Assert.AreEqual(2.645, (double)sampleMean, 0.0001);

            /* min */

            double min = var.GetMin();

            Console.WriteLine("min = " + min);

            Assert.AreEqual(1.39, min, 0.001);

            /* max */

            double max = var.GetMax();

            Console.WriteLine("max = " + max);

            Assert.AreEqual(4.00, max, 0.001);

            /* asstring */

            Console.WriteLine("Items list");
            Console.WriteLine(var.AsString(2));

            /* sorted list */

            PIVariable sorted = var.GetSorted();

            Console.WriteLine("Sorted items list");
            Console.WriteLine(sorted.AsString(2));

            /* median - even (source 2a/45) */

            median = var.GetMedian();
            Console.WriteLine("median (even) = " + median);
            Assert.AreEqual(2.62, median, 0.001);

            /* sample standard deviation */

            double std = var.GetSampleStandardDeviation();

            Console.WriteLine("sample standard deviation = " + std);
            Assert.AreEqual(0.8674, std, 0.001);

            /* Z score (source 2a/78) */

            double ZScore = var.GetZScore(0);

            Console.WriteLine("Z score = " + ZScore);
            Assert.AreEqual(-0.8589, ZScore, 0.001);

            ZScore = var.GetZScore(1);
            Console.WriteLine("Z score = " + ZScore);
            Assert.AreEqual(0.4093, ZScore, 0.001);

            /* --------------------------------- */

            /* -- source 2d/68 */

            PIDebug.Blank();
            PIDebug.Title("Example with VAR1 and VAR2");

            PIVariable var1 = new PIVariable();
            PIVariable var2 = new PIVariable();

            Assert.IsNotNull(var1);
            Assert.IsNotNull(var2);

            var1.AddMoreValues(4.0, 3);
            var1.AddMoreValues(6.0, 4);
            var1.AddMoreValues(7.0, 2);
            var1.AddMoreValues(9.0, 4);
            var1.AddMoreValues(10.0, 10);
            var1.AddMoreValues(11.0, 5);
            var1.AddMoreValues(11.5, 2);
            var1.AddMoreValues(12.0, 13);
            var1.AddMoreValues(13.0, 4);
            var1.AddMoreValues(14.0, 2);
            var1.AddMoreValues(15.0, 2);
            var1.AddMoreValues(16.0, 4);
            var1.AddMoreValues(17.0, 6);
            var1.AddMoreValues(18.0, 2);
            var1.AddMoreValues(19.0, 2);
            var1.AddMoreValues(20.0, 6);
            var1.AddMoreValues(22.0, 2);

            Console.WriteLine("count = " + var1.Count());
            Assert.AreEqual(73, var1.Count(), 0.001);

            Console.WriteLine("mean = " + var1.GetSampleMean());
            Assert.AreEqual(12.79, var1.GetSampleMean(), 0.01);

            Console.WriteLine("geometric mean = " + var1.GetGeometricMean());
            Assert.AreEqual(11.95, var1.GetGeometricMean(), 0.01);

            Console.WriteLine("mode = " + var1.GetMode());
            Assert.AreEqual(12.00, var1.GetMode(), 0.01);

            Console.WriteLine("median = " + var1.GetMedian());
            Assert.AreEqual(12.00, var1.GetMedian(), 0.01);

            Console.WriteLine("population variance = " + var1.GetPopulationVariance());
            Assert.AreEqual(19.44, var1.GetPopulationVariance(), 0.01);

            Console.WriteLine("population standard deviation = " + var1.GetPopulationStandardDeviation());
            Assert.AreEqual(4.41, var1.GetPopulationStandardDeviation(), 0.01);

            Console.WriteLine("sample variance = " + var1.GetSampleVariance());
            Assert.AreEqual(19.71, var1.GetSampleVariance(), 0.01);

            Console.WriteLine("sample standard deviation = " + var1.GetSampleStandardDeviation());
            Assert.AreEqual(4.44, var1.GetSampleStandardDeviation(), 0.01);

            Console.WriteLine("min = " + var1.GetMin());
            Assert.AreEqual(4.00, var1.GetMin(), 0.01);

            Console.WriteLine("max = " + var1.GetMax());
            Assert.AreEqual(22.00, var1.GetMax(), 0.01);

            Console.WriteLine("range = " + var1.GetRange());
            Assert.AreEqual(18.00, var1.GetRange(), 0.01);

            Console.WriteLine("quartile1 = " + var1.GetQuartile1());
            Assert.AreEqual(10.00, var1.GetQuartile1(), 0.01);

            Console.WriteLine("quartile3 = " + var1.GetQuartile3());
            Assert.AreEqual(16.00, var1.GetQuartile3(), 0.01);

            Console.WriteLine("interquartile range = " + var1.GetInterquartileRange());
            Assert.AreEqual(6.00, var1.GetInterquartileRange(), 0.01);

            Console.WriteLine("skewness = " + var1.GetSkewness());
            Assert.AreEqual(0.16, var1.GetSkewness(), 0.01);

            Console.WriteLine("kurtosis = " + var1.GetKurtosis());

            /* -- 1.0.6. --------------------------------------------- */

            /* -- LAG -- */

            PIDebug.Blank();
            PIDebug.Title("LAG -2");

            PIVariable varLAGSource = new PIVariable();

            varLAGSource.AddMoreValuesRange(1, 10);

            Console.WriteLine("Source = " + varLAGSource.AsString(0));

            /* -2 */
            PIVariable varLAGMinus2 = new PIVariable();

            varLAGMinus2.MakeLag(varLAGSource, -2, false);

            /* LAG -2 = 3;4;5;6;7;8;9;10;<null>;<null> */
            Console.WriteLine("LAG -2 = " + varLAGMinus2.AsString(0));

            Assert.AreEqual(3.0, (double)varLAGMinus2.GetValue(0), 0.1);
            Assert.AreEqual(null, varLAGMinus2.GetValue(8));
            Assert.AreEqual(null, varLAGMinus2.GetValue(9));

            /* +2 */
            PIVariable varLAGPlus2 = new PIVariable();

            varLAGPlus2.MakeLag(varLAGSource, +2, false);

            /* LAG +2 = <null>;<null>;1;2;3;4;5;6;7;8 */
            Console.WriteLine("LAG +2 = " + varLAGPlus2.AsString(0));

            Assert.AreEqual(null, varLAGPlus2.GetValue(0));
            Assert.AreEqual(null, varLAGPlus2.GetValue(1));
            Assert.AreEqual(1.0, (double)varLAGPlus2.GetValue(2), 0.1);

            /* -- 1.0.9. --------------------------------------------- */

            /* -- standardize -- */

            PIDebug.Blank();
            PIDebug.Title("Standardize data (with Z-SCORE method)");

            PIVariable varStandardize = new PIVariable();

            varStandardize.AddValues(new int[] { 10, 20, 25, 17, 13, 16 });

            Console.WriteLine(varStandardize.AsString(0));
            varStandardize.Standardize(StandardizeMethod.Z_SCORE);
            Console.WriteLine(varStandardize.AsString(2));

            PIDebug.Blank();
            PIDebug.Title("Standardize data (with SCALING 0-1 method)");

            PIVariable varStandardize1 = new PIVariable();

            varStandardize1.AddValues(new int[] { 10, 20, 25, 17, 13, 16 });

            Console.WriteLine(varStandardize1.AsString(0));
            varStandardize1.Standardize(StandardizeMethod.SCALING_0_1);
            Console.WriteLine(varStandardize1.AsString(2));

            /* -- 1.2.4 - verifing against Kubanova`s (source 3A,42) skewness - if the result is same as in the book */

            PIDebug.Title("Verifing Kubanova`s skewness", true);

            PIVariable varSkewness = new PIVariable();

            varSkewness.AddMoreValues(1, 21);
            varSkewness.AddMoreValues(2, 43);
            varSkewness.AddMoreValues(3, 37);
            varSkewness.AddMoreValues(4, 9);
            varSkewness.AddMoreValues(5, 2);

            Console.WriteLine("mean = " + varSkewness.GetSampleMean());
            Assert.AreEqual(2.357, (double)varSkewness.GetSampleMean(), 0.001);

            Console.WriteLine("population variance = " + varSkewness.GetPopulationVariance());
            Assert.AreEqual(0.872, (double)varSkewness.GetPopulationVariance(), 0.001);

            Console.WriteLine("population sd = " + varSkewness.GetPopulationStandardDeviation());
            Assert.AreEqual(0.934, (double)varSkewness.GetPopulationStandardDeviation(), 0.001);

            Console.WriteLine("mode = " + varSkewness.GetMode());
            Assert.AreEqual(2.0, (double)varSkewness.GetMode(), 0.001);

            Console.WriteLine("median = " + varSkewness.GetMedian());
            Assert.AreEqual(2.0, (double)varSkewness.GetMedian(), 0.001);

            Console.WriteLine("skewness = " + varSkewness.GetSkewness());
            Assert.AreEqual(0.353, (double)varSkewness.GetSkewness(), 0.001);

            Console.WriteLine("kurtosis = " + varSkewness.GetKurtosis());
            Assert.AreEqual(-0.168, (double)varSkewness.GetKurtosis(), 0.001);

            Console.WriteLine("range = " + varSkewness.GetRange());
            Assert.AreEqual(4.0, (double)varSkewness.GetRange(), 0.001);
        }
Beispiel #7
0
        public void TestMethod()
        {
            /*
             * Student`s distribution.
             */

            PIDebug.TitleBig("Student`s distribution");

            PIStudentDistribution distribution = new PIStudentDistribution();

            /* -- 1) Get probability for T */

            PIDebug.Title("1) Get probability");

            /* DF=1 */

            PIDebug.Title("..df=1");

            distribution.SetDF(1);

            Console.WriteLine("Probability for x=0.0, df=1 : " + distribution.GetProbability(0.0));
            Assert.AreEqual(1.0, (double)distribution.GetProbability(0.0), 0.1);
            Console.WriteLine("Probability for x=1, df=1 : " + distribution.GetProbability(1.00));
            Assert.AreEqual(0.5, (double)distribution.GetProbability(1.00), 0.1);
            Console.WriteLine("Probability for x=15.89, df=1 : " + distribution.GetProbability(15.89));
            Assert.AreEqual(0.04, (double)distribution.GetProbability(15.89), 0.1);
            Console.WriteLine("Probability for x=636.6, df=1 : " + distribution.GetProbability(636.6));
            Assert.AreEqual(0.001, (double)distribution.GetProbability(636.6), 0.1);

            PIDebug.Blank();

            distribution.SetDF(2);

            Console.WriteLine("Probability for x=0.0, df=2 : " + distribution.GetProbability(0.0));
            Assert.AreEqual(1.0, (double)distribution.GetProbability(0.0), 0.1);
            Console.WriteLine("Probability for x=0.816, df=2 : " + distribution.GetProbability(0.816));
            Assert.AreEqual(0.5, (double)distribution.GetProbability(0.816), 0.1);
            Console.WriteLine("Probability for x=4.849, df=2 : " + distribution.GetProbability(4.849));
            Assert.AreEqual(0.04, (double)distribution.GetProbability(4.849), 0.1);
            Console.WriteLine("Probability for x=31.6, df=2 : " + distribution.GetProbability(31.6));
            Assert.AreEqual(0.001, (double)distribution.GetProbability(31.6), 0.1);

            PIDebug.Blank();

            distribution.SetDF(3);

            Console.WriteLine("Probability for x=0.0, df=3 : " + distribution.GetProbability(0.0));
            Assert.AreEqual(1.0, (double)distribution.GetProbability(0.0), 0.1);
            Console.WriteLine("Probability for x=0.765, df=3 : " + distribution.GetProbability(0.765));
            Assert.AreEqual(0.5, (double)distribution.GetProbability(0.765), 0.1);
            Console.WriteLine("Probability for x=3.482, df=3 : " + distribution.GetProbability(3.482));
            Assert.AreEqual(0.04, (double)distribution.GetProbability(3.482), 0.1);
            Console.WriteLine("Probability for x=12.92, df=3 : " + distribution.GetProbability(12.92));
            Assert.AreEqual(0.001, (double)distribution.GetProbability(12.92), 0.1);

            PIDebug.Blank();

            distribution.SetDF(4);

            Console.WriteLine("Probability for x=0.0, df=4 : " + distribution.GetProbability(0.0));
            Assert.AreEqual(1.0, (double)distribution.GetProbability(0.0), 0.1);
            Console.WriteLine("Probability for x=0.741, df=4 : " + distribution.GetProbability(0.741));
            Assert.AreEqual(0.5, (double)distribution.GetProbability(0.741), 0.1);
            Console.WriteLine("Probability for x=2.999, df=4 : " + distribution.GetProbability(2.999));
            Assert.AreEqual(0.04, (double)distribution.GetProbability(2.999), 0.1);
            Console.WriteLine("Probability for x=8.610, df=4 : " + distribution.GetProbability(8.610));
            Assert.AreEqual(0.001, (double)distribution.GetProbability(8.610), 0.1);

            PIDebug.Blank();

            distribution.SetDF(5);

            Console.WriteLine("Probability for x=2.757, df=5 : " + distribution.GetProbability(2.757));
            Assert.AreEqual(0.04, (double)distribution.GetProbability(2.757), 0.1);

            distribution.SetDF(10);

            Console.WriteLine("Probability for x=1.372, df=10 : " + distribution.GetProbability(1.372));
            Assert.AreEqual(0.2, (double)distribution.GetProbability(1.372), 0.1);


            /* -- 2) Get T for probability */

            PIDebug.Title("2) Get T for probability", true);

            /* DF=1 */

            PIDebug.Title("..df=1");

            distribution.SetDF(1);

            Console.WriteLine("X value for probability for prob=1.0, df=1 : " + distribution.GetXForProbability(1.0));
            Assert.AreEqual(0.0, (double)distribution.GetXForProbability(1.0), 0.1);
            Console.WriteLine("X value for probability for prob=0.5, df=1 : " + distribution.GetXForProbability(0.5));
            Assert.AreEqual(1.0, (double)distribution.GetXForProbability(0.5), 0.1);
            Console.WriteLine("X value for probability for prob=0.04, df=1 : " + distribution.GetXForProbability(0.04));
            Assert.AreEqual(15.89, (double)distribution.GetXForProbability(0.04), 0.1);
            Console.WriteLine("X value for probability for prob=0.001, df=1 : " + distribution.GetXForProbability(0.001));
            Assert.AreEqual(636.6, (double)distribution.GetXForProbability(0.001), 0.1);

            PIDebug.Blank();

            distribution.SetDF(2);

            Console.WriteLine("X value for probability for prob=1.0, df=2 : " + distribution.GetXForProbability(1.0));
            Assert.AreEqual(0.0, (double)distribution.GetXForProbability(1.0), 0.1);
            Console.WriteLine("X value for probability for prob=0.5, df=2 : " + distribution.GetXForProbability(0.5));
            Assert.AreEqual(0.816, (double)distribution.GetXForProbability(0.5), 0.1);
            Console.WriteLine("X value for probability for prob=0.04, df=2 : " + distribution.GetXForProbability(0.04));
            Assert.AreEqual(4.849, (double)distribution.GetXForProbability(0.04), 0.1);
            Console.WriteLine("X value for probability for prob=0.001, df=2 : " + distribution.GetXForProbability(0.001));
            Assert.AreEqual(31.6, (double)distribution.GetXForProbability(0.001), 0.1);

            PIDebug.Blank();

            distribution.SetDF(3);

            Console.WriteLine("X value for probability for prob=1.0, df=3 : " + distribution.GetXForProbability(1.0));
            Assert.AreEqual(0.0, (double)distribution.GetXForProbability(1.0), 0.1);
            Console.WriteLine("X value for probability for prob=0.5, df=3 : " + distribution.GetXForProbability(0.5));
            Assert.AreEqual(0.765, (double)distribution.GetXForProbability(0.5), 0.1);
            Console.WriteLine("X value for probability for prob=0.04, df=3 : " + distribution.GetXForProbability(0.04));
            Assert.AreEqual(3.482, (double)distribution.GetXForProbability(0.04), 0.1);
            Console.WriteLine("X value for probability for prob=0.001, df=3 : " + distribution.GetXForProbability(0.001));
            Assert.AreEqual(12.92, (double)distribution.GetXForProbability(0.001), 0.1);

            PIDebug.Blank();

            distribution.SetDF(4);

            Console.WriteLine("X value for probability for prob=1.0, df=4 : " + distribution.GetXForProbability(1.0));
            Assert.AreEqual(0.0, (double)distribution.GetXForProbability(1.0), 0.1);
            Console.WriteLine("X value for probability for prob=0.5, df=4 : " + distribution.GetXForProbability(0.5));
            Assert.AreEqual(0.741, (double)distribution.GetXForProbability(0.5), 0.1);
            Console.WriteLine("X value for probability for prob=0.04, df=4 : " + distribution.GetXForProbability(0.04));
            Assert.AreEqual(2.999, (double)distribution.GetXForProbability(0.04), 0.1);
            Console.WriteLine("X value for probability for prob=0.04, df=4 : " + distribution.GetXForProbability(0.001));
            Assert.AreEqual(8.610, (double)distribution.GetXForProbability(0.001), 0.1);

            PIDebug.Blank();

            distribution.SetDF(5);

            Console.WriteLine("X value for probability for prob=0.04, df=5 : " + distribution.GetXForProbability(0.04));
            Assert.AreEqual(2.757, (double)distribution.GetXForProbability(0.04), 0.1);

            distribution.SetDF(10);

            Console.WriteLine("X value for probability for prob=0.2, df=10 : " + distribution.GetXForProbability(0.2));
            Assert.AreEqual(1.372, (double)distribution.GetXForProbability(0.2), 0.1);

            PIDebug.Blank();

            /* -- 3) Get probability density */

            PIDebug.Title("3) Get probability density, df=1", true);

            distribution.SetDF(1);

            Console.WriteLine("x=-7.0 : " + distribution.GetPDF(-7.0));
            Console.WriteLine("x=-2.0 : " + distribution.GetPDF(-2.0));
            Console.WriteLine("x=-0.5 : " + distribution.GetPDF(-0.5));
            Console.WriteLine("x=0.0 : " + distribution.GetPDF(0.0));
            Console.WriteLine("x=0.5 : " + distribution.GetPDF(0.5));
            Console.WriteLine("x=2.0 : " + distribution.GetPDF(2.0));
            Console.WriteLine("x=7.0 : " + distribution.GetPDF(7.0));
        }
Beispiel #8
0
        public void TestMethod()
        {
            /* Source 5R4. */

            PIDebug.TitleBig("Erlang distribution (lambda=1, k=1)");

            PIErlangDistribution distribution = new PIErlangDistribution();

            /* -- 1) Get probability */

            PIDebug.Title("1) Get probability for X (CDF)");

            /* lambda=1, k=1 */
            PIDebug.Title("..lambda=1; k=1");

            Console.WriteLine("Probability for x=0.0 : " + distribution.GetCDF(0.0));
            Assert.AreEqual(0.0, distribution.GetProbability(0.0), 0.001);

            Console.WriteLine("Probability for x=0.1111 : " + distribution.GetCDF(0.1111));
            Assert.AreEqual(0.1053, distribution.GetProbability(0.1111), 0.001);

            Console.WriteLine("Probability for x=0.25 : " + distribution.GetCDF(0.25));
            Assert.AreEqual(0.2213, distribution.GetProbability(0.25), 0.001);

            Console.WriteLine("Probability for x=8 : " + distribution.GetCDF(8));
            Assert.AreEqual(0.9998, distribution.GetProbability(8), 0.001);

            /* lambda=1, k=1 */
            PIDebug.Title("..lambda=2; k=3", true);

            distribution.SetLambda(2);
            distribution.SetK(3);

            Console.WriteLine("Probability for x=0.1111 : " + distribution.GetCDF(0.1111));
            Assert.AreEqual(0.0015, distribution.GetProbability(0.1111), 0.001);

            Console.WriteLine("Probability for x=0.25 : " + distribution.GetCDF(0.25));
            Assert.AreEqual(0.0144, distribution.GetProbability(0.25), 0.001);

            Console.WriteLine("Probability for x=8 : " + distribution.GetCDF(8));
            Assert.AreEqual(1.0, distribution.GetProbability(8), 0.001);


            /* -- 2) Get T for probability */

            PIDebug.Title("2) Get X for probability (InverseCDF)", true);

            PIDebug.Title("..lambda=1, k=1");

            distribution.SetLambda(1);
            distribution.SetK(1);

            Console.WriteLine("X value for probability for prob=0.0 : " + distribution.GetInverseCDF(0.0));
            Assert.AreEqual(0.0, distribution.GetInverseCDF(0.0), 0.001);

            Console.WriteLine("X value for probability for prob=0.1053 : " + distribution.GetInverseCDF(0.1053));
            Assert.AreEqual(0.1111, distribution.GetInverseCDF(0.1053), 0.001);

            Console.WriteLine("X value for probability for prob=0.2213 : " + distribution.GetInverseCDF(0.2213));
            Assert.AreEqual(0.25, distribution.GetInverseCDF(0.2213), 0.001);

            PIDebug.Title("..lambda=2; k=3", true);

            distribution.SetLambda(2);
            distribution.SetK(3);

            Console.WriteLine("X value for probability for prob=0.0015 : " + distribution.GetInverseCDF(0.0015));
            Assert.AreEqual(0.1111, distribution.GetInverseCDF(0.0015), 0.01);

            Console.WriteLine("X value for probability for prob=0.0144 : " + distribution.GetInverseCDF(0.0144));
            Assert.AreEqual(0.25, distribution.GetInverseCDF(0.0144), 0.01);

            /* -- 3) Get probability density */

            PIDebug.Title("3) Get probability density (PDF)", true);

            PIDebug.Title("..lambda=1, k=1");

            distribution.SetLambda(1);
            distribution.SetK(1);

            Console.WriteLine("x=0.1 : " + distribution.GetPDF(0.1));
            Console.WriteLine("x=0.5 : " + distribution.GetPDF(0.5));
            Console.WriteLine("x=1.0 : " + distribution.GetPDF(1.0));
            Console.WriteLine("x=2.0 : " + distribution.GetPDF(2.0));
            Console.WriteLine("x=5.0 : " + distribution.GetPDF(5.0));
        }
Beispiel #9
0
        public void TestMethod()
        {
            /*
             * Log Normal distribution.
             */

            PIDebug.TitleBig("Log Normal distribution (0,1)");

            PILogNormalDistribution distribution = new PILogNormalDistribution();

            /* -- 1) Get probability */

            PIDebug.Title("1) Get probability for X");

            /* SD=1 */
            PIDebug.Title("..SD=1");

            Console.WriteLine("Probability for x=0.0 : " + distribution.GetProbability(0.0));
            Assert.AreEqual(0.0, distribution.GetProbability(0.0), 0.001);
            Console.WriteLine("Probability for x=0.776 : " + distribution.GetProbability(0.776));
            Assert.AreEqual(0.4, distribution.GetProbability(0.776), 0.001);
            Console.WriteLine("Probability for x=1.0 : " + distribution.GetProbability(1.0));
            Assert.AreEqual(0.5, distribution.GetProbability(1.0), 0.001);
            Console.WriteLine("Probability for x=1.288 : " + distribution.GetProbability(1.288));
            Assert.AreEqual(0.6, distribution.GetProbability(1.288), 0.001);
            Console.WriteLine("Probability for x=3.602 : " + distribution.GetProbability(3.602));
            Assert.AreEqual(0.9, distribution.GetProbability(3.602), 0.001);

            /* SD=2 */
            PIDebug.Title("..SD=2", true);

            distribution.SetSD(2.0);

            /* -- get probability from x value */

            Console.WriteLine("Probability for x=0.0 : " + distribution.GetProbability(0.0));
            Assert.AreEqual(0.0, distribution.GetProbability(0.0), 0.001);
            Console.WriteLine("Probability for x=0.602 : " + distribution.GetProbability(0.602));
            Assert.AreEqual(0.4, distribution.GetProbability(0.602), 0.001);
            Console.WriteLine("Probability for x=12.976 : " + distribution.GetProbability(12.976));
            Assert.AreEqual(0.9, distribution.GetProbability(12.976), 0.001);

            /* -- 2) Get T for probability */

            PIDebug.Title("2) Get X for probability", true);

            /* SD=1 */
            PIDebug.Title("..SD=1");

            distribution.SetSD(1);

            Console.WriteLine("X value for probability for prob=0.0 : " + distribution.GetXForProbability(0.0));
            Assert.AreEqual(0.0, distribution.GetXForProbability(0.0), 0.001);
            Console.WriteLine("X value for probability for prob=0.01 : " + distribution.GetXForProbability(0.01));
            Assert.AreEqual(0.098, distribution.GetXForProbability(0.01), 0.001);
            Console.WriteLine("X value for probability for prob=0.4 : " + distribution.GetXForProbability(0.4));
            Assert.AreEqual(0.776, distribution.GetXForProbability(0.4), 0.001);
            Console.WriteLine("X value for probability for prob=0.9 : " + distribution.GetXForProbability(0.9));
            Assert.AreEqual(3.602, distribution.GetXForProbability(0.9), 0.001);

            PIDebug.Title("..SD=2", true);

            distribution.SetSD(2.0);

            Console.WriteLine("X value for probability for prob=0.4 : " + distribution.GetXForProbability(0.4));
            Assert.AreEqual(0.602, distribution.GetXForProbability(0.4), 0.001);

            /* -- 3) Get probability density */

            PIDebug.Title("3) Get probability density, SD=1", true);

            distribution.SetSD(1);

            Console.WriteLine("x=0.1 : " + distribution.GetPDF(0.1));
            Console.WriteLine("x=0.5 : " + distribution.GetPDF(0.5));
            Console.WriteLine("x=1.0 : " + distribution.GetPDF(1.0));
            Console.WriteLine("x=2.0 : " + distribution.GetPDF(2.0));
        }
        public void TestMethod()
        {
            /* Source 5S2. */

            PIDebug.TitleBig("Rayleigh distribution");

            PIRayleighDistribution distribution = new PIRayleighDistribution();

            /* -- 1) Get probability */

            PIDebug.Title("1) Get probability for X (CDF)");

            /* sigma=1 */
            PIDebug.Title("..sigma=1");

            Console.WriteLine("Probability for x=0.0 : " + distribution.GetCDF(0.0));
            Assert.AreEqual(0.0, distribution.GetProbability(0.0), 0.001);

            Console.WriteLine("Probability for x=1.0 : " + distribution.GetCDF(1.0));
            Assert.AreEqual(0.3933, distribution.GetProbability(1.0), 0.001);

            Console.WriteLine("Probability for x=2.0 : " + distribution.GetCDF(2.0));
            Assert.AreEqual(0.8646, distribution.GetProbability(2.0), 0.001);

            Console.WriteLine("Probability for x=2.5 : " + distribution.GetCDF(2.5));
            Assert.AreEqual(0.9560, distribution.GetProbability(2.5), 0.001);

            /* sigma=3 */
            PIDebug.Title("..sigma=3", true);

            distribution.SetSigma(3);

            Console.WriteLine("Probability for x=1.0 : " + distribution.GetCDF(1.0));
            Assert.AreEqual(0.0540, distribution.GetProbability(1.0), 0.001);

            /* -- 2) Get T for probability */

            PIDebug.Title("2) Get X for probability (InverseCDF)", true);

            /* sigma=1 */
            PIDebug.Title("..sigma=1");

            distribution.SetSigma(1);

            Console.WriteLine("X value for probability for prob=0.0 : " + distribution.GetInverseCDF(0.0));
            Assert.AreEqual(0.0, distribution.GetInverseCDF(0.0), 0.001);

            Console.WriteLine("X value for probability for prob=0.3933 : " + distribution.GetInverseCDF(0.3933));
            Assert.AreEqual(1.0, distribution.GetInverseCDF(0.3933), 0.001);

            Console.WriteLine("X value for probability for prob=0.8646 : " + distribution.GetInverseCDF(0.8646));
            Assert.AreEqual(2.0, distribution.GetInverseCDF(0.8646), 0.001);

            Console.WriteLine("X value for probability for prob=0.9560 : " + distribution.GetInverseCDF(0.9560));
            Assert.AreEqual(2.5, distribution.GetInverseCDF(0.9560), 0.001);

            /* sigma=3 */
            PIDebug.Title("..sigma=3", true);

            distribution.SetSigma(3);

            Console.WriteLine("X value for probability for prob=0.0540 : " + distribution.GetInverseCDF(0.0540));
            Assert.AreEqual(1.0, distribution.GetInverseCDF(0.0540), 0.001);

            /* -- 3) Get probability density */

            PIDebug.Title("3) Get probability density (PDF)", true);

            PIDebug.Title("..sigma=1");

            distribution.SetSigma(1);

            Console.WriteLine("x=0.1 : " + distribution.GetPDF(0.1));
            Console.WriteLine("x=0.5 : " + distribution.GetPDF(0.5));
            Console.WriteLine("x=1.0 : " + distribution.GetPDF(1.0));
            Console.WriteLine("x=2.0 : " + distribution.GetPDF(2.0));
            Console.WriteLine("x=5.0 : " + distribution.GetPDF(5.0));
        }
Beispiel #11
0
        public void TestMethod()
        {
            /* Source 6G. */

            PIDebug.TitleBig("Prime");

            //Console.WriteLine( 3 % 100 );

            /* -- Simple */

            PIDebug.Title("Simple method");

            Console.WriteLine("1 : " + PIPrime.IsPrime_Simple(1));
            Assert.AreEqual(false, PIPrime.IsPrime_Simple(1));

            Console.WriteLine("99 : " + PIPrime.IsPrime_Simple(99));
            Assert.AreEqual(false, PIPrime.IsPrime_Simple(99));

            Console.WriteLine("101 : " + PIPrime.IsPrime_Simple(101));
            Assert.AreEqual(true, PIPrime.IsPrime_Simple(101));

            Console.WriteLine("221 : " + PIPrime.IsPrime_Simple(221));
            Assert.AreEqual(false, PIPrime.IsPrime_Simple(221));

            Console.WriteLine("9973 : " + PIPrime.IsPrime_Simple(9973));
            Assert.AreEqual(true, PIPrime.IsPrime_Simple(9973));

            Console.WriteLine("10001261 : " + PIPrime.IsPrime_Simple(10001261));
            Assert.AreEqual(true, PIPrime.IsPrime_Simple(10001261));

            Console.WriteLine("9007199254740880 : " + PIPrime.IsPrime_Simple(9007199254740880));
            Assert.AreEqual(false, PIPrime.IsPrime_Simple(9007199254740880));

            Console.WriteLine("9007199254740881 : " + PIPrime.IsPrime_Simple(9007199254740881));
            Assert.AreEqual(true, PIPrime.IsPrime_Simple(9007199254740881));

            /* -- Fermat */

            PIDebug.Title("Fermat test", true);

            Console.WriteLine("1 : " + PIPrime.IsPrime_Fermat(1, 3));
            Assert.AreEqual(false, PIPrime.IsPrime_Fermat(1, 3));

            Console.WriteLine("99 : " + PIPrime.IsPrime_Fermat(99, 3));
            Assert.AreEqual(false, PIPrime.IsPrime_Fermat(99, 3));

            Console.WriteLine("101 : " + PIPrime.IsPrime_Fermat(101, 3));
            Assert.AreEqual(true, PIPrime.IsPrime_Fermat(101, 3));

            Console.WriteLine("221 : " + PIPrime.IsPrime_Fermat(221, 3));
            Assert.AreEqual(false, PIPrime.IsPrime_Fermat(221, 3));

            Console.WriteLine("9973 : " + PIPrime.IsPrime_Fermat(9973, 3));
            Assert.AreEqual(true, PIPrime.IsPrime_Fermat(9973, 3));

            Console.WriteLine("10001261 : " + PIPrime.IsPrime_Fermat(10001261, 3));
            Assert.AreEqual(true, PIPrime.IsPrime_Fermat(10001261, 3));

            Console.WriteLine("9007199254740880 : " + PIPrime.IsPrime_Fermat(9007199254740880, 3));
            Assert.AreEqual(false, PIPrime.IsPrime_Fermat(9007199254740880, 3));

            //Console.WriteLine( "9007199254740881 : " + PIPrime.IsPrime_Fermat( 9007199254740881 ) );
            //Assert.AreEqual( true, PIPrime.IsPrime_Fermat( 9007199254740881 ) );

            /* -- Miller-Rabin */

            PIDebug.Title("Miller-Rabin test", true);

            Console.WriteLine("221 : " + PIPrime.IsPrime_MillerRabin(221, 5));
            Assert.AreEqual(false, PIPrime.IsPrime_MillerRabin(221, 5));

            Console.WriteLine("5 :" + PIPrime.IsPrime_MillerRabin(5, 5));
            Assert.AreEqual(true, PIPrime.IsPrime_MillerRabin(5, 5));

            Console.WriteLine("97 : " + PIPrime.IsPrime_MillerRabin(97, 5));
            Assert.AreEqual(true, PIPrime.IsPrime_MillerRabin(97, 5));

            Console.WriteLine("99: " + PIPrime.IsPrime_MillerRabin(99, 5));
            Assert.AreEqual(false, PIPrime.IsPrime_MillerRabin(99, 5));

            Console.WriteLine("101 : " + PIPrime.IsPrime_MillerRabin(101, 5));
            Assert.AreEqual(true, PIPrime.IsPrime_MillerRabin(101, 5));

            Console.WriteLine("102 : " + PIPrime.IsPrime_MillerRabin(102, 5));
            Assert.AreEqual(false, PIPrime.IsPrime_MillerRabin(102, 5));

            Console.WriteLine("983 : " + PIPrime.IsPrime_MillerRabin(983, 5));
            Assert.AreEqual(true, PIPrime.IsPrime_MillerRabin(983, 5));

            Console.WriteLine("985 : " + PIPrime.IsPrime_MillerRabin(985, 5));
            Assert.AreEqual(false, PIPrime.IsPrime_MillerRabin(985, 5));

            Console.WriteLine("9973 : " + PIPrime.IsPrime_MillerRabin(9973, 5));
            Assert.AreEqual(true, PIPrime.IsPrime_MillerRabin(9973, 5));

            Console.WriteLine("10001261 : " + PIPrime.IsPrime_MillerRabin(10001261, 5));
            Assert.AreEqual(true, PIPrime.IsPrime_MillerRabin(10001261, 5));

            Console.WriteLine("9007199254740880 : " + PIPrime.IsPrime_MillerRabin(9007199254740880, 5));
            Assert.AreEqual(false, PIPrime.IsPrime_MillerRabin(9007199254740880, 5));


            //Console.WriteLine( "900719925474000 : " + PIPrime.IsPrime_MillerRabin( 900719925474000, 5 ) );

            //Console.WriteLine( "9007199254740880 : " + PIPrime.IsPrime_MillerRabin(  9007199254740880, 5 ) );
        }
        public void TestMethod()
        {
            /*
             * F distribution.
             */

            PIDebug.TitleBig("F distribution");

            PIFDistribution distribution = new PIFDistribution();

            /* -- 1) Get probability */

            PIDebug.TitleBig("Get probability");

            /* DF1=1, DF2=1 */

            PIDebug.Title("..df1=1, df2=1");

            distribution.SetDF1(1);
            distribution.SetDF2(1);

            Console.WriteLine("Probability for x=0.0 : " + distribution.GetProbability(0.0));
            Assert.AreEqual(1.0, distribution.GetProbability(0.0), 0.001);

            Console.WriteLine("Probability for x=39.86 : " + distribution.GetProbability(39.86));
            Assert.AreEqual(0.1, distribution.GetProbability(39.86), 0.001);

            Console.WriteLine("Probability for x=161.45 : " + distribution.GetProbability(161.45));
            Assert.AreEqual(0.05, distribution.GetProbability(161.45), 0.001);

            Console.WriteLine("Probability for x=4052.2 : " + distribution.GetProbability(4052.2));
            Assert.AreEqual(0.01, distribution.GetProbability(4052.2), 0.001);

            Console.WriteLine("Probability for x=405284 : " + distribution.GetProbability(405284));
            Assert.AreEqual(0.001, distribution.GetProbability(405284), 0.001);

            /* DF1=3, DF2=3 */

            PIDebug.Title("..df1=3, df2=3", true);

            distribution.SetDF1(3);
            distribution.SetDF2(3);

            Console.WriteLine("Probability for x=5.39 : " + distribution.GetProbability(5.39));
            Assert.AreEqual(0.1, distribution.GetProbability(5.39), 0.001);

            Console.WriteLine("Probability for x=9.28 : " + distribution.GetProbability(9.28));
            Assert.AreEqual(0.05, distribution.GetProbability(9.28), 0.001);

            Console.WriteLine("Probability for x=15.44 : " + distribution.GetProbability(15.44));
            Assert.AreEqual(0.025, distribution.GetProbability(15.44), 0.001);

            Console.WriteLine("Probability for x=29.46 : " + distribution.GetProbability(29.46));
            Assert.AreEqual(0.01, distribution.GetProbability(29.46), 0.001);

            Console.WriteLine("Probability for x=141.11 : " + distribution.GetProbability(141.11));
            Assert.AreEqual(0.001, distribution.GetProbability(141.11), 0.001);

            /* DF1=5, DF2=5 */

            PIDebug.Title("..df1=5, df2=5", true);

            distribution.SetDF1(5);
            distribution.SetDF2(5);

            Console.WriteLine("Probability for x=3.45 : " + distribution.GetProbability(3.45));
            Assert.AreEqual(0.1, distribution.GetProbability(3.45), 0.001);

            Console.WriteLine("Probability for x=5.05 : " + distribution.GetProbability(5.05));
            Assert.AreEqual(0.05, distribution.GetProbability(5.05), 0.001);

            Console.WriteLine("Probability for x=7.15 : " + distribution.GetProbability(7.15));
            Assert.AreEqual(0.025, distribution.GetProbability(7.15), 0.001);

            Console.WriteLine("Probability for x=10.97 : " + distribution.GetProbability(10.97));
            Assert.AreEqual(0.01, distribution.GetProbability(10.97), 0.001);

            Console.WriteLine("Probability for x=29.75 : " + distribution.GetProbability(29.75));
            Assert.AreEqual(0.001, distribution.GetProbability(29.75), 0.001);

            /* -- 2) Get X for probability */

            PIDebug.Title("2) Get X for probability", true);

            /* DF1=1, DF2=1 */

            PIDebug.Title("..df1=1, df2=1");

            distribution.SetDF1(1);
            distribution.SetDF2(1);

            Console.WriteLine("X value for probability for prop=1.0 : " + distribution.GetXForProbability(1.0));
            Assert.AreEqual(0.0, distribution.GetXForProbability(1.0), 0.01);

            Console.WriteLine("X value for probability for prop=0.1 : " + distribution.GetXForProbability(0.1));
            Assert.AreEqual(39.86, distribution.GetXForProbability(0.1), 0.01);

            Console.WriteLine("X value for probability for prop=0.05 : " + distribution.GetXForProbability(0.05));
            Assert.AreEqual(161.45, distribution.GetXForProbability(0.05), 0.01);

            Console.WriteLine("X value for probability for prop=0.01 : " + distribution.GetXForProbability(0.01));
            Assert.AreEqual(4052.2, distribution.GetXForProbability(0.01), 0.02);

            Console.WriteLine("X value for probability for prop=0.001 : " + distribution.GetXForProbability(0.001));
            Assert.AreEqual(405284, distribution.GetXForProbability(0.001), 0.1);

            /* DF1=3, DF2=3 */

            PIDebug.Title("..df1=3, df2=3", true);

            distribution.SetDF1(3);
            distribution.SetDF2(3);

            Console.WriteLine("X value for probability for prop=0.1 : " + distribution.GetXForProbability(0.1));
            Assert.AreEqual(5.39, distribution.GetXForProbability(0.1), 0.01);

            Console.WriteLine("X value for probability for prop=0.05 : " + distribution.GetXForProbability(0.05));
            Assert.AreEqual(9.28, distribution.GetXForProbability(0.05), 0.01);

            Console.WriteLine("X value for probability for prop=0.025 : " + distribution.GetXForProbability(0.025));
            Assert.AreEqual(15.44, distribution.GetXForProbability(0.025), 0.01);

            Console.WriteLine("X value for probability for prop=0.01 : " + distribution.GetXForProbability(0.01));
            Assert.AreEqual(29.46, distribution.GetXForProbability(0.01), 0.01);

            Console.WriteLine("X value for probability for prop=0.001 : " + distribution.GetXForProbability(0.001));
            Assert.AreEqual(141.11, distribution.GetXForProbability(0.001), 0.01);

            /* DF1=5, DF2=5 */

            PIDebug.Title("..df1=5, df2=5", true);

            distribution.SetDF1(5);
            distribution.SetDF2(5);

            Console.WriteLine("X value for probability for prop=0.1 : " + distribution.GetXForProbability(0.1));
            Assert.AreEqual(3.45, distribution.GetXForProbability(0.1), 0.01);

            Console.WriteLine("X value for probability for prop=0.05 : " + distribution.GetXForProbability(0.05));
            Assert.AreEqual(5.05, distribution.GetXForProbability(0.05), 0.01);

            Console.WriteLine("X value for probability for prop=0.025 : " + distribution.GetXForProbability(0.025));
            Assert.AreEqual(7.15, distribution.GetXForProbability(0.025), 0.01);

            Console.WriteLine("X value for probability for prop=0.01 : " + distribution.GetXForProbability(0.01));
            Assert.AreEqual(10.97, distribution.GetXForProbability(0.01), 0.01);

            Console.WriteLine("X value for probability for prop=0.001 : " + distribution.GetXForProbability(0.001));
            Assert.AreEqual(29.75, distribution.GetXForProbability(0.001), 0.01);

            /* -- 3) Get probability density */

            PIDebug.Title("3) Get probability density, df1=1, df2=1", true);

            distribution.SetDF1(1);
            distribution.SetDF2(1);

            Console.WriteLine("x=0.0 : " + distribution.GetPDF(0.0));
            Console.WriteLine("x=0.001 : " + distribution.GetPDF(0.001));
            Console.WriteLine("x=0.1 : " + distribution.GetPDF(0.1));
            Console.WriteLine("x=0.5 : " + distribution.GetPDF(0.5));
            Console.WriteLine("x=2.0 : " + distribution.GetPDF(2.0));
            Console.WriteLine("x=7.0 : " + distribution.GetPDF(7.0));
        }
Beispiel #13
0
        public void TestMethod()
        {
            /* Source 5S2. */

            PIDebug.TitleBig("Pareto distribution (alpha=3, xm=1)");

            PIParetoDistribution distribution = new PIParetoDistribution();

            /* -- 1) Get probability */

            PIDebug.Title("1) Get probability for X (CDF)");

            /* alpha=1, xm=2 */
            PIDebug.Title("..alpha=3; xm=1");

            distribution.SetAlpha(3);
            distribution.SetXM(1);

            Console.WriteLine("Probability for x=0.0 : " + distribution.GetCDF(0.0));
            Assert.AreEqual(0.0, distribution.GetProbability(0.0), 0.001);

            Console.WriteLine("Probability for x=1.0 : " + distribution.GetCDF(1.0));
            Assert.AreEqual(0.0, distribution.GetProbability(1.0), 0.001);

            Console.WriteLine("Probability for x=2.0 : " + distribution.GetCDF(2.0));
            Assert.AreEqual(0.8752, distribution.GetProbability(2.0), 0.001);

            Console.WriteLine("Probability for x=2.5 : " + distribution.GetCDF(2.5));
            Assert.AreEqual(0.9363, distribution.GetProbability(2.5), 0.001);

            /* -- 2) Get T for probability */

            PIDebug.Title("2) Get X for probability (InverseCDF)", true);

            /* alpha=1, xm=2 */
            PIDebug.Title("..alpha=3; xm=1");

            distribution.SetAlpha(3);
            distribution.SetXM(1);

            Console.WriteLine("X value for probability for prob=0.0 : " + distribution.GetInverseCDF(0.0));
            Assert.AreEqual(0.0, distribution.GetInverseCDF(0.0), 0.001);

            Console.WriteLine("X value for probability for prob=0.8752 : " + distribution.GetInverseCDF(0.8752));
            Assert.AreEqual(2.0, distribution.GetInverseCDF(0.8752), 0.01);

            Console.WriteLine("X value for probability for prob=0.9363 : " + distribution.GetInverseCDF(0.9363));
            Assert.AreEqual(2.5, distribution.GetInverseCDF(0.9363), 0.01);

            /* -- 3) Get probability density */

            PIDebug.Title("3) Get probability density (PDF)", true);

            /* alpha=1, xm=2 */
            PIDebug.Title("..alpha=3; xm=1");

            distribution.SetAlpha(3);
            distribution.SetXM(1);

            Console.WriteLine("x=0.1 : " + distribution.GetPDF(0.1));
            Console.WriteLine("x=0.5 : " + distribution.GetPDF(0.5));
            Console.WriteLine("x=1.0 : " + distribution.GetPDF(1.0));
            Console.WriteLine("x=2.0 : " + distribution.GetPDF(2.0));
            Console.WriteLine("x=5.0 : " + distribution.GetPDF(5.0));
        }
Beispiel #14
0
        public void TestMethod()
        {
            PIDebug.TitleBig("Poisson distribution (1)");

            PIPoissonDistribution distribution = new PIPoissonDistribution();

            /* -- 1) Get probability */

            PIDebug.Title("1) Get probability for X");

            /* lambda=1 */
            PIDebug.Title("..lambda=1");

            Console.WriteLine("Probability for x=0.0 : " + distribution.GetCDF(0.0));
            Assert.AreEqual(0.3679, distribution.GetCDF(0.0), 0.001);

            Console.WriteLine("Probability for x=1.0 : " + distribution.GetCDF(1.0));
            Assert.AreEqual(0.7358, distribution.GetCDF(1.0), 0.001);

            Console.WriteLine("Probability for x=3.0 : " + distribution.GetCDF(3.0));
            Assert.AreEqual(0.981, distribution.GetCDF(3.0), 0.001);

            Console.WriteLine("Probability for x=5.0 : " + distribution.GetCDF(5.0));
            Assert.AreEqual(0.9994, distribution.GetCDF(5.0), 0.001);

            /* lambda=3 */
            PIDebug.Title("..lambda=3", true);

            distribution.SetLambda(3);

            Console.WriteLine("Probability for x=0.0 : " + distribution.GetCDF(0.0));
            Assert.AreEqual(0.0498, distribution.GetCDF(0.0), 0.001);

            Console.WriteLine("Probability for x=3.0 : " + distribution.GetCDF(3.0));
            Assert.AreEqual(0.6472, distribution.GetCDF(3.0), 0.001);

            /* -- 2) Get T for probability */

            PIDebug.Title("2) Get X for probability", true);

            /* lambda=1  */
            PIDebug.Title("..lambda=1");

            distribution.SetLambda(1);

            Console.WriteLine("X value for probability for prob=0.7358 : " + distribution.GetXForProbability(0.7358));
            Assert.AreEqual(1.0, distribution.GetXForProbability(0.7358), 0.001);

            Console.WriteLine("X value for probability for prob=0.9810 : " + distribution.GetXForProbability(0.9810));
            Assert.AreEqual(3.0, distribution.GetXForProbability(0.9810), 0.001);

            Console.WriteLine("X value for probability for prob=0.9994 : " + distribution.GetXForProbability(0.9994));
            Assert.AreEqual(5.0, distribution.GetXForProbability(0.9994), 0.001);

            Console.WriteLine("X value for probability for prob=1 : " + distribution.GetXForProbability(1));
            //Assert.AreEqual( 5.0, distribution.GetXForProbability( 1 ), 0.001 );

            /* lambda=3 */
            PIDebug.Title("..lambda=3", true);

            distribution.SetLambda(3);

            Console.WriteLine("X value for probability for prob=0.0498 : " + distribution.GetXForProbability(0.0498));
            Assert.AreEqual(0.0, distribution.GetXForProbability(0.0498), 0.001);

            Console.WriteLine("X value for probability for prob=0.6472 : " + distribution.GetXForProbability(0.6472));
            Assert.AreEqual(3.0, distribution.GetXForProbability(0.6472), 0.001);

            /* -- 3) Get probability density */

            PIDebug.Title("3) Get probability density, lambda=1", true);

            distribution.SetLambda(1);

            Console.WriteLine("x=0.1 : " + distribution.GetPDF(0.1));
            Console.WriteLine("x=0.5 : " + distribution.GetPDF(0.5));
            Console.WriteLine("x=1.0 : " + distribution.GetPDF(1.0));
            Console.WriteLine("x=2.0 : " + distribution.GetPDF(2.0));
            Console.WriteLine("x=5.0 : " + distribution.GetPDF(5.0));
        }
        public void TestMethod()
        {
            PIDebug.TitleBig("Matrices");

            /* -- transpose matrix */

            PIDebug.Title("Transpose");

            PIMatrix matrixTransposeA = new PIMatrix(3, 3);

            Assert.IsNotNull(matrixTransposeA);
            matrixTransposeA.AddValues(new int[] { 1, 3, 5, 2, 4, 7, 0, 2, 1 });
            Console.WriteLine(matrixTransposeA.AsString(0));

            PIDebug.Blank();

            PIMatrix matrixTranspose = matrixTransposeA.Transpose();

            Assert.IsNotNull(matrixTranspose);

            Console.WriteLine(matrixTranspose.AsString(0));

            Assert.AreEqual(1.0, (double)matrixTranspose.GetValue(1, 1));
            Assert.AreEqual(2.0, (double)matrixTranspose.GetValue(1, 2));
            Assert.AreEqual(0.0, (double)matrixTranspose.GetValue(1, 3));
            Assert.AreEqual(3.0, (double)matrixTranspose.GetValue(2, 1));
            Assert.AreEqual(4.0, (double)matrixTranspose.GetValue(2, 2));
            Assert.AreEqual(2.0, (double)matrixTranspose.GetValue(2, 3));
            Assert.AreEqual(5.0, (double)matrixTranspose.GetValue(3, 1));
            Assert.AreEqual(7.0, (double)matrixTranspose.GetValue(3, 2));
            Assert.AreEqual(1.0, (double)matrixTranspose.GetValue(3, 3));

            /* -- addition - new data */

            PIMatrix matrixAdditionA = new PIMatrix(2, 3);
            PIMatrix matrixAdditionB = new PIMatrix(2, 3);

            Assert.IsNotNull(matrixAdditionA);
            Assert.IsNotNull(matrixAdditionB);

            matrixAdditionA.AddValues(new double[] { 3.0, 2.0, 0.0, -1.0, 0.0, 4.0 });
            matrixAdditionB.AddValues(new double[] { 1.0, -5.0, 2.0, 3.0, 4.0, 7.0 });

            /* addition - show data */

            PIDebug.Title("MatrixAdditionA", true);
            Console.WriteLine(matrixAdditionA.AsString(0));

            PIDebug.Title("MatrixAdditionB", true);
            Console.WriteLine(matrixAdditionB.AsString(0));

            /* addition */

            PIDebug.Title("Addition", true);

            PIMatrix matrixAddition = matrixAdditionA.Addition(matrixAdditionB);

            Assert.IsNotNull(matrixAddition);

            //Console.WriteLine( matrixAddition.asString( 0 ) );

            Assert.AreEqual(4.0, (double)matrixAddition.GetValue(1, 1));
            Assert.AreEqual(-3.0, (double)matrixAddition.GetValue(1, 2));
            Assert.AreEqual(2.0, (double)matrixAddition.GetValue(1, 3));
            Assert.AreEqual(2.0, (double)matrixAddition.GetValue(2, 1));
            Assert.AreEqual(4.0, (double)matrixAddition.GetValue(2, 2));
            Assert.AreEqual(11.0, (double)matrixAddition.GetValue(2, 3));

            /* subtraction (1a/1) */

            PIDebug.Title("Subtraction");

            PIMatrix matrixSubtractionA = new PIMatrix(2, 3);
            PIMatrix matrixSubtractionB = new PIMatrix(2, 3);

            Assert.IsNotNull(matrixSubtractionA);
            Assert.IsNotNull(matrixSubtractionB);

            matrixSubtractionA.AddValues(new int[] { 8, 4, 2, 6, 1, 5 });
            matrixSubtractionB.AddValues(new int[] { 3, 10, 4, 5, 6, 1 });

            PIMatrix matrixSubtraction = matrixSubtractionA.Subtraction(matrixSubtractionB);

            Console.WriteLine(matrixSubtraction.AsString(0));

            Assert.AreEqual(5.0, (double)matrixSubtraction.GetValue(1, 1));
            Assert.AreEqual(-6.0, (double)matrixSubtraction.GetValue(1, 2));
            Assert.AreEqual(-2.0, (double)matrixSubtraction.GetValue(1, 3));
            Assert.AreEqual(1.0, (double)matrixSubtraction.GetValue(2, 1));
            Assert.AreEqual(-5.0, (double)matrixSubtraction.GetValue(2, 2));
            Assert.AreEqual(4.0, (double)matrixSubtraction.GetValue(2, 3));

            /* multiplication */

            PIDebug.Title("Multiplication", true);

            PIMatrix matrixMultiplicationA = new PIMatrix(4, 3);
            PIMatrix matrixMultiplicationB = new PIMatrix(3, 2);

            Assert.IsNotNull(matrixMultiplicationA);
            Assert.IsNotNull(matrixMultiplicationB);

            matrixMultiplicationA.AddValues(new int[] { 2, 0, 1, 3, 4, 2, 1, 1, 0, -1, -2, -3 });
            matrixMultiplicationB.AddValues(new int[] { 1, -1, 2, 1, 3, -2 });

            PIMatrix matrixMultiplication = matrixMultiplicationA.Multiply(matrixMultiplicationB);

            Console.WriteLine(matrixMultiplication.AsString(0));

            Assert.AreEqual(5.0, (double)matrixMultiplication.GetValue(1, 1));
            Assert.AreEqual(-4.0, (double)matrixMultiplication.GetValue(1, 2));
            Assert.AreEqual(17.0, (double)matrixMultiplication.GetValue(2, 1));
            Assert.AreEqual(-3.0, (double)matrixMultiplication.GetValue(2, 2));
            Assert.AreEqual(3.0, (double)matrixMultiplication.GetValue(3, 1));
            Assert.AreEqual(0.0, (double)matrixMultiplication.GetValue(3, 2));
            Assert.AreEqual(-14.0, (double)matrixMultiplication.GetValue(4, 1));
            Assert.AreEqual(5.0, (double)matrixMultiplication.GetValue(4, 2));

            /* constant multiplication */

            PIDebug.Title("Constant multiplication", true);

            PIMatrix matrixScalarMultiplicationA = new PIMatrix(2, 3);

            Assert.IsNotNull(matrixScalarMultiplicationA);

            matrixScalarMultiplicationA.AddValues(new Double[] { 1.0, -5.0, 2.0, 3.0, 4.0, 7.0 });

            PIMatrix matrixScalarMultiplication = matrixAdditionB.ConstantOperation(3, ScalarOperation.MULTIPLICATION);

            Assert.IsNotNull(matrixScalarMultiplication);

            Console.WriteLine(matrixScalarMultiplication.AsString(0));

            Assert.AreEqual(3.0, (double)matrixScalarMultiplication.GetValue(1, 1));
            Assert.AreEqual(-15.0, (double)matrixScalarMultiplication.GetValue(1, 2));
            Assert.AreEqual(6.0, (double)matrixScalarMultiplication.GetValue(1, 3));
            Assert.AreEqual(9.0, (double)matrixScalarMultiplication.GetValue(2, 1));
            Assert.AreEqual(12.0, (double)matrixScalarMultiplication.GetValue(2, 2));
            Assert.AreEqual(21.0, (double)matrixScalarMultiplication.GetValue(2, 3));

            /* inversion */

            PIDebug.Title("Inversion", true);

            PIMatrix matrixInverseA = new PIMatrix(2, 2);

            Assert.IsNotNull(matrixInverseA);

            matrixInverseA.AddValues(new int[] { 1, -2, -3, 8 });
            Console.WriteLine(matrixInverseA.AsString(1));

            PIMatrix matrixInverse = matrixInverseA.Inverse();

            Assert.IsNotNull(matrixInverse);

            PIDebug.Blank();
            Console.WriteLine(matrixInverse.AsString(1));

            Assert.AreEqual(4.0, (double)matrixInverse.GetValue(1, 1));
            Assert.AreEqual(1.0, (double)matrixInverse.GetValue(1, 2));
            Assert.AreEqual(1.5, (double)matrixInverse.GetValue(2, 1));
            Assert.AreEqual(0.5, (double)matrixInverse.GetValue(2, 2));

            /* determinant 2x2 (source 1c) */

            PIDebug.Title("Determinant 1x1", true);

            PIMatrix matrixDeterminant11 = new PIMatrix(1, 1);

            Assert.IsNotNull(matrixDeterminant11);

            matrixDeterminant11.AddValues(new int[] { 10 });

            double valueDeterminant11 = matrixDeterminant11.Determinant();

            Console.WriteLine(valueDeterminant11);

            Assert.AreEqual(10.0, valueDeterminant11);

            /* determinant 2x2 (source 1c) */

            PIDebug.Title("Determinant 2x2", true);

            PIMatrix matrixDeterminant22 = new PIMatrix(2, 2);

            Assert.IsNotNull(matrixDeterminant22);

            matrixDeterminant22.AddValues(new int[] { 4, -3, 8, -5 });

            double valueDeterminant22 = matrixDeterminant22.Determinant();

            Console.WriteLine(valueDeterminant22);

            Assert.AreEqual(4.0, valueDeterminant22);

            /* determinant 3x3 (source 1c) */

            PIDebug.Title("Determinant 3x3", true);

            PIMatrix matrixDeterminant33 = new PIMatrix(3, 3);

            Assert.IsNotNull(matrixDeterminant33);

            matrixDeterminant33.AddValues(new int[] { 1, 2, 5, 3, 4, 7, 6, 8, 9 });

            double valueDeterminant33 = matrixDeterminant33.Determinant();

            Console.WriteLine(valueDeterminant33);

            Assert.AreEqual(10.0, valueDeterminant33);

            /* determinant 4x4 (source 1b/5) */

            PIDebug.Title("Determinant 4x4", true);

            PIMatrix matrixDeterminant44 = new PIMatrix(4, 4);

            Assert.IsNotNull(matrixDeterminant44);

            matrixDeterminant44.AddValues(new int[] { 1, 2, 1, 1, 2, 5, 1, 2, 3, 2, 2, 1, 1, 3, 2, 2 });

            double valueDeterminant44 = matrixDeterminant44.Determinant();

            Console.WriteLine(valueDeterminant44);

            Assert.AreEqual(-1.0, valueDeterminant44);

            /* rank (source 1b/14) */

            PIDebug.Title("rank", true);

            PIMatrix matrixRank = new PIMatrix(3, 6);

            Assert.IsNotNull(matrixRank);

            matrixRank.AddValues(new int[] { 3, 1, -8, 2, 1, 0, 2, -2, -3, -7, 2, 0, 1, -5, 2, -16, 3, 0 });

            int valueRank = matrixRank.Rank();

            Console.WriteLine(valueRank);

            Assert.AreEqual((int)2, (int)valueRank);

            /* rank (source 1d) */

            PIDebug.Title("rank 2", true);

            PIMatrix matrixRank2 = new PIMatrix(3, 3);

            Assert.IsNotNull(matrixRank2);

            matrixRank2.AddValues(new int[] { 1, 2, 1, 0, -1, -2, 1, 3, 3 });

            int valueRank2 = matrixRank2.Rank();

            Console.WriteLine(valueRank2);

            Assert.AreEqual((int)2, (int)valueRank2);

            /* rank (source 1d) */

            PIDebug.Title("rank 3", true);

            PIMatrix matrixRank3 = new PIMatrix(4, 4);

            Assert.IsNotNull(matrixRank3);

            matrixRank3.AddValues(new int[] { 7, 2, 5, 1, 1, 3, 5, -7, 4, -5, 1, 0, 2, 8, 10, -9 });

            int valueRank3 = matrixRank3.Rank();

            Console.WriteLine(valueRank3);

            Assert.AreEqual((int)4, (int)valueRank3);
        }
Beispiel #16
0
        public void TestMethod()
        {
            /* Source 5S2. */

            PIDebug.TitleBig("Weibull distribution (lambda=1, k=1)");

            PIWeibullDistribution distribution = new PIWeibullDistribution();

            /* -- 1) Get probability */

            PIDebug.Title("1) Get probability for X (CDF)");

            /* lambda=1, k=2 */
            PIDebug.Title("..lambda=1; k=2");

            distribution.SetK(2);

            Console.WriteLine("Probability for x=0.0 : " + distribution.GetCDF(0.0));
            Assert.AreEqual(0.0, distribution.GetProbability(0.0), 0.001);

            Console.WriteLine("Probability for x=0.5 : " + distribution.GetCDF(0.5));
            Assert.AreEqual(0.2212, distribution.GetProbability(0.5), 0.001);

            Console.WriteLine("Probability for x=1.0 : " + distribution.GetCDF(1.0));
            Assert.AreEqual(0.6321, distribution.GetProbability(1.0), 0.001);

            Console.WriteLine("Probability for x=2.0 : " + distribution.GetCDF(2.0));
            Assert.AreEqual(0.9817, distribution.GetProbability(2.0), 0.001);

            /* lambda=2, k=3 */
            PIDebug.Title("..lambda=2; k=3", true);

            distribution.SetLambda(2);
            distribution.SetK(3);

            Console.WriteLine("Probability for x=1.0 : " + distribution.GetCDF(1.0));
            Assert.AreEqual(0.1175, distribution.GetProbability(1.0), 0.001);

            /* -- 2) Get T for probability */

            PIDebug.Title("2) Get X for probability (InverseCDF)", true);

            /* lambda=1, k=2 */
            PIDebug.Title("..lambda=1; k=2");

            distribution.SetLambda(1);
            distribution.SetK(2);

            Console.WriteLine("X value for probability for prob=0.0 : " + distribution.GetInverseCDF(0.0));
            Assert.AreEqual(0.0, distribution.GetInverseCDF(0.0), 0.001);

            Console.WriteLine("X value for probability for prob=0.2212 : " + distribution.GetInverseCDF(0.2212));
            Assert.AreEqual(0.5, distribution.GetInverseCDF(0.2212), 0.001);

            Console.WriteLine("X value for probability for prob=0.6321 : " + distribution.GetInverseCDF(0.6321));
            Assert.AreEqual(1.0, distribution.GetInverseCDF(0.6321), 0.001);

            Console.WriteLine("X value for probability for prob=0.9817 : " + distribution.GetInverseCDF(0.9817));
            Assert.AreEqual(2.0, distribution.GetInverseCDF(0.9817), 0.001);

            /* lambda=2, k=3 */
            PIDebug.Title("..lambda=2; k=3", true);

            distribution.SetLambda(2);
            distribution.SetK(3);

            Console.WriteLine("X value for probability for prob=0.1175 : " + distribution.GetInverseCDF(0.1175));
            Assert.AreEqual(1.0, distribution.GetInverseCDF(0.1175), 0.001);

            /* -- 3) Get probability density */

            PIDebug.Title("3) Get probability density (PDF)", true);

            PIDebug.Title("..lambda=1, k=1");

            distribution.SetLambda(1);
            distribution.SetK(1);

            Console.WriteLine("x=0.1 : " + distribution.GetPDF(0.1));
            Console.WriteLine("x=0.5 : " + distribution.GetPDF(0.5));
            Console.WriteLine("x=1.0 : " + distribution.GetPDF(1.0));
            Console.WriteLine("x=2.0 : " + distribution.GetPDF(2.0));
            Console.WriteLine("x=5.0 : " + distribution.GetPDF(5.0));
        }
Beispiel #17
0
        public void TestMethod()
        {
            /*
             *  KUBANOVA example - Source 3i,35. **
             *
             *  Values for partial sums are cutted from START.
             *
             *  */

            PIDebug.TitleBig("KUBANOVA, source 3i,36");

            /* -- 1) PARTIAL_SUMS method */

            PIDebug.Title("Regression 1-A (PARTIAL SUMS method)");

            /* - prepare X data for regression */

            PIVariable X = new PIVariable();

            Assert.IsNotNull(X);

            X.AddMoreValuesRange(1, 29);

            /* - prepare Y data for regression */

            PIVariable Y = new PIVariable();

            Assert.IsNotNull(Y);

            Y.AddValues(new int[] { 3, 5, 7, 9, 14 });
            Y.AddValues(new int[] { 17, 21, 27, 36, 40 });
            Y.AddValues(new int[] { 47, 54, 65, 71, 78 });
            Y.AddValues(new int[] { 84, 96, 102, 108, 113 });
            Y.AddValues(new int[] { 125, 129, 132, 136, 145 });
            Y.AddValues(new int[] { 146, 148, 149, 151 });

            /* - create and compute regression */

            PIGompertzRegression regression = new PIGompertzRegression(X, Y);

            Assert.IsNotNull(regression);

            /* cut partial sums from START */
            regression.SetCutStyleForPartialSum(CutStyleForPartialSum.START);

            regression.Calc();

            Console.WriteLine(regression.GetTextFormula());
            Console.WriteLine(regression.GetTextFormulaFilled());

            Assert.AreEqual((double)188.18, (double)regression.Get_gama(), 0.01);
            Assert.AreEqual((double)0.023, (double)regression.Get_A(), 0.001);
            Assert.AreEqual((double)0.894, (double)regression.Get_B(), 0.001);

            /* - calc prediction for X = 15 */

            PIDebug.Blank();

            Console.WriteLine("Prediction for X=15 : " + regression.CalcPredictedY(55.0));

            PIDebug.Blank();
            Console.WriteLine("Prediction errors:");
            Console.WriteLine(regression.GetErrors().AsString(5));

            PIDebug.Blank();
            Console.WriteLine("SSE = " + regression.GetSSE());
            Console.WriteLine("ME = " + regression.GetME());
            Console.WriteLine("MSE = " + regression.GetMSE());
            Console.WriteLine("MAE = " + regression.GetMAE());
            Console.WriteLine("MAPE = " + regression.GetMAPE());
            Console.WriteLine("MPE = " + regression.GetMPE());

            /* -- 2) PARTLY_AVERAGES method */

            PIDebug.Title("Regression 1-B (PARTLY AVERAGES method)", true);

            regression.SetCalcMethod(CalcMethod.PARTIAL_AVERAGES);
            regression.Calc();

            Console.WriteLine(regression.GetTextFormula());
            Console.WriteLine(regression.GetTextFormulaFilled());

            Assert.AreEqual((double)232.74, (double)regression.Get_gama(), 0.01);
            Assert.AreEqual((double)0.011, (double)regression.Get_A(), 0.001);
            Assert.AreEqual((double)0.91, (double)regression.Get_B(), 0.01);

            PIDebug.Blank();
            Console.WriteLine("SSE = " + regression.GetSSE());
            Console.WriteLine("ME = " + regression.GetME());
            Console.WriteLine("MSE = " + regression.GetMSE());
            Console.WriteLine("MAE = " + regression.GetMAE());
            Console.WriteLine("MAPE = " + regression.GetMAPE());
            Console.WriteLine("MPE = " + regression.GetMPE());

            /* -- 3) SELECTED_POINTS method */

            PIDebug.Title("Regression 1-C (SELECTED POINTS method)", true);

            regression.SetCalcMethod(CalcMethod.SELECTED_POINTS);
            regression.Calc();

            //Assert.AreEqual( (double)224.42, (double)regression.get_gama(), 0.01 );
            //Assert.AreEqual( (double)0.021, (double)regression.get_A(), 0.001 );
            //Assert.AreEqual( (double)0.905, (double)regression.get_B(), 0.01 );

            Console.WriteLine(regression.GetTextFormula());
            Console.WriteLine(regression.GetTextFormulaFilled());

            PIDebug.Blank();
            Console.WriteLine("Prediction errors:");
            Console.WriteLine(regression.GetErrors().AsString(5));

            PIDebug.Blank();
            Console.WriteLine("SSE = " + regression.GetSSE());
            Console.WriteLine("ME = " + regression.GetME());
            Console.WriteLine("MSE = " + regression.GetMSE());
            Console.WriteLine("MAE = " + regression.GetMAE());
            Console.WriteLine("MAPE = " + regression.GetMAPE());
            Console.WriteLine("MPE = " + regression.GetMPE());

            /*
             *  LINDA example - Source 3j. **
             *
             *  Values for partial sums are cutted from END.
             *
             *  */

            PIDebug.TitleBig("LINDA, source 3j", true);

            /* -- 1) PARTIAL_SUMS method */

            PIDebug.Title("Regression 1-A (PARTIAL SUMS method)");

            /* - prepare X data for regression */

            PIVariable X1 = new PIVariable();

            Assert.IsNotNull(X1);

            X1.AddMoreValuesRange(1, 17);

            /* - prepare Y data for regression */

            PIVariable Y1 = new PIVariable();

            Assert.IsNotNull(Y1);

            Y1.AddValues(new int[] { 28, 66, 105, 236, 348 });
            Y1.AddValues(new int[] { 496, 593, 696, 748, 825 });
            Y1.AddValues(new int[] { 904, 894, 897, 953, 936 });
            Y1.AddValues(new int[] { 1002, 951 });

            /* - create and compute regression */

            PIGompertzRegression regression1 = new PIGompertzRegression(X1, Y1);

            Assert.IsNotNull(regression1);

            /* set cutting from end */
            regression1.SetCutStyleForPartialSum(CutStyleForPartialSum.END);

            regression1.Calc();

            Console.WriteLine(regression1.GetTextFormula());
            Console.WriteLine(regression1.GetTextFormulaFilled());

            //regression.Assert.AreEqual( (double)1.336, (double)regression.get_A(), 0.001 );
            //Assert.AreEqual( (double)0.00097, (double)regression.get_B(), 0.00001 );

            /* - calc prediction for X = 15 */

            PIDebug.Blank();

            Console.WriteLine("Prediction for X=15 : " + regression1.CalcPredictedY(55.0));

            PIDebug.Blank();
            Console.WriteLine("Prediction errors:");
            Console.WriteLine(regression1.GetErrors().AsString(5));

            PIDebug.Blank();
            Console.WriteLine("SSE = " + regression1.GetSSE());
            Console.WriteLine("ME = " + regression1.GetME());
            Console.WriteLine("MSE = " + regression1.GetMSE());
            Console.WriteLine("MAE = " + regression1.GetMAE());
            Console.WriteLine("MAPE = " + regression1.GetMAPE());
            Console.WriteLine("MPE = " + regression1.GetMPE());

            /* -- 2) PARTLY_AVERAGES method */

            PIDebug.Title("Regression 1-B (PARTLY AVERAGES method)", true);

            regression1.SetCalcMethod(CalcMethod.PARTIAL_AVERAGES);
            regression1.Calc();

            Console.WriteLine(regression1.GetTextFormula());
            Console.WriteLine(regression1.GetTextFormulaFilled());

            PIDebug.Blank();
            Console.WriteLine("SSE = " + regression1.GetSSE());
            Console.WriteLine("ME = " + regression1.GetME());
            Console.WriteLine("MSE = " + regression1.GetMSE());
            Console.WriteLine("MAE = " + regression1.GetMAE());
            Console.WriteLine("MAPE = " + regression1.GetMAPE());
            Console.WriteLine("MPE = " + regression1.GetMPE());

            /* -- 3) SELECTED_POINTS method */

            PIDebug.Title("Regression 1-C (SELECTED POINTS method)", true);

            regression1.SetCalcMethod(CalcMethod.SELECTED_POINTS);
            regression1.Calc();

            Console.WriteLine(regression1.GetTextFormula());
            Console.WriteLine(regression1.GetTextFormulaFilled());

            PIDebug.Blank();
            Console.WriteLine("Prediction errors:");
            Console.WriteLine(regression1.GetErrors().AsString(5));

            PIDebug.Blank();
            Console.WriteLine("SSE = " + regression1.GetSSE());
            Console.WriteLine("ME = " + regression1.GetME());
            Console.WriteLine("MSE = " + regression1.GetMSE());
            Console.WriteLine("MAE = " + regression1.GetMAE());
            Console.WriteLine("MAPE = " + regression1.GetMAPE());
            Console.WriteLine("MPE = " + regression1.GetMPE());
        }
        public void TestMethod()
        {
            /*
             * CHI-SQUARE distribution.
             */

            PIDebug.TitleBig("Chi-square distribution");

            PICHISquareDistribution distribution = new PICHISquareDistribution();

            /* -- 1) Get probability */

            PIDebug.Title("1) Get probability");

            /* DF=1 */

            PIDebug.Title("..df=1");

            distribution.SetDF(1);

            Console.WriteLine("Probability for x=0.0, df=1 : " + distribution.GetProbability(0.0));
            Assert.AreEqual(1.0, (double)distribution.GetProbability(0.0), 0.1);

            Console.WriteLine("Probability for x=0.001, df=1 : " + distribution.GetProbability(0.001));
            Assert.AreEqual(0.975, (double)distribution.GetProbability(0.001), 0.001);

            Console.WriteLine("Probability for x=2.706, df=1 : " + distribution.GetProbability(2.706));
            Assert.AreEqual(0.100, (double)distribution.GetProbability(2.706), 0.001);

            Console.WriteLine("Probability for x=5.024, df=1 : " + distribution.GetProbability(5.024));
            Assert.AreEqual(0.025, (double)distribution.GetProbability(5.024), 0.001);

            Console.WriteLine("Probability for x=200, df=1 : " + distribution.GetProbability(200));
            Assert.AreEqual(1.0, (double)distribution.GetProbability(200), 0.001);

            /* DF=2 */

            PIDebug.Title("..df=2", true);

            distribution.SetDF(2);

            Console.WriteLine("Probability for x=0.103, df=2 : " + distribution.GetProbability(0.103));
            Assert.AreEqual(0.95, (double)distribution.GetProbability(0.103), 0.001);

            Console.WriteLine("Probability for x=10.597, df=2 : " + distribution.GetProbability(10.597));
            Assert.AreEqual(0.005, (double)distribution.GetProbability(10.597), 0.001);

            /* DF=40 */

            PIDebug.Title("..df=40", true);

            distribution.SetDF(40);

            Console.WriteLine("Probability for x=29.051, df=40 : " + distribution.GetProbability(29.051));
            Assert.AreEqual(0.9, (double)distribution.GetProbability(29.051), 0.001);

            /* -- 2) Get X */

            PIDebug.Title("2) Get X", true);

            /* DF=1 */

            PIDebug.Title("..df=1");

            distribution.SetDF(1);

            Console.WriteLine("X for probability=1.0, df=1 : " + distribution.GetXForProbability(1.0));
            Assert.AreEqual(0.0, (double)distribution.GetXForProbability(1.0), 0.001);

            Console.WriteLine("X for probability=0.975, df=1 : " + distribution.GetXForProbability(0.975));
            Assert.AreEqual(0.001, (double)distribution.GetXForProbability(0.975), 0.001);

            Console.WriteLine("X for probability=0.1, df=1 : " + distribution.GetXForProbability(0.1));
            Assert.AreEqual(2.706, (double)distribution.GetXForProbability(0.1), 0.001);

            Console.WriteLine("X for probability=0.025, df=1 : " + distribution.GetXForProbability(0.025));
            Assert.AreEqual(5.023, (double)distribution.GetXForProbability(0.025), 0.001);

            /* DF=2 */

            PIDebug.Title("..df=2", true);

            distribution.SetDF(2);

            Console.WriteLine("X for probability=0.95, df=2 : " + distribution.GetXForProbability(0.95));
            Assert.AreEqual(0.103, (double)distribution.GetXForProbability(0.95), 0.001);

            Console.WriteLine("X for probability=0.005, df=2 : " + distribution.GetXForProbability(0.005));
            Assert.AreEqual(10.597, (double)distribution.GetXForProbability(0.005), 0.001);

            /* DF=40 */

            PIDebug.Title("..df=40", true);

            distribution.SetDF(40);

            Console.WriteLine("X for probability=0.9, df=40 : " + distribution.GetXForProbability(0.9));
            Assert.AreEqual(29.051, (double)distribution.GetXForProbability(0.9), 0.001);

            /* -- 3) Get probability density */

            PIDebug.Title("3) Get probability density, df=1", true);

            distribution.SetDF(1);

            Console.WriteLine("x=0.5 : " + distribution.GetPDF(0.5));
            Console.WriteLine("x=0.7 : " + distribution.GetPDF(0.7));
            Console.WriteLine("x=2.0 : " + distribution.GetPDF(2.0));
            Console.WriteLine("x=7.0 : " + distribution.GetPDF(7.0));
        }