Inheritance: UnivariateContinuousDistribution
        public void ProbabilityDensityFunctionTest()
        {
            int degreesOfFreedom;
            double actual, expected, x;
            ChiSquareDistribution target;

            degreesOfFreedom = 1;
            target = new ChiSquareDistribution(degreesOfFreedom);
            x = 1;
            actual = target.ProbabilityDensityFunction(x);
            expected = 0.2420;
            Assert.AreEqual(expected, actual, 1e-4);

            degreesOfFreedom = 2;
            target = new ChiSquareDistribution(degreesOfFreedom);
            x = 2;
            actual = target.ProbabilityDensityFunction(x);
            expected = 0.1839;
            Assert.AreEqual(expected, actual, 1e-4);

            degreesOfFreedom = 10;
            target = new ChiSquareDistribution(degreesOfFreedom);
            x = 2;
            actual = target.ProbabilityDensityFunction(x);
            expected = 0.0077;
            Assert.AreEqual(expected, actual, 1e-4);
        }
        public void ConstructorTest()
        {
            var chisq = new ChiSquareDistribution(degreesOfFreedom: 7);

            double mean = chisq.Mean;     // 7
            double median = chisq.Median; // 6.345811195595612
            double var = chisq.Variance;  // 14

            double cdf = chisq.DistributionFunction(x: 6.27); // 0.49139966433823956
            double pdf = chisq.ProbabilityDensityFunction(x: 6.27); // 0.11388708001184455
            double lpdf = chisq.LogProbabilityDensityFunction(x: 6.27); // -2.1725478476948092

            double ccdf = chisq.ComplementaryDistributionFunction(x: 6.27); // 0.50860033566176044
            double icdf = chisq.InverseDistributionFunction(p: cdf); // 6.2700000000852318

            double hf = chisq.HazardFunction(x: 6.27); // 0.22392254197721179
            double chf = chisq.CumulativeHazardFunction(x: 6.27); // 0.67609276602233315

            string str = chisq.ToString(); // "χ²(x; df = 7)

            Assert.AreEqual(7, mean);
            Assert.AreEqual(6.345811195595612, median, 1e-6);
            Assert.AreEqual(14, var);
            Assert.AreEqual(0.67609276602233315, chf);
            Assert.AreEqual(0.49139966433823956, cdf);
            Assert.AreEqual(0.11388708001184455, pdf);
            Assert.AreEqual(-2.1725478476948092, lpdf);
            Assert.AreEqual(0.22392254197721179, hf);
            Assert.AreEqual(0.50860033566176044, ccdf);
            Assert.AreEqual(6.2700000000852318, icdf, 1e-6);
            Assert.AreEqual("χ²(x; df = 7)", str);
        }
        /// <summary>
        ///   Constructs a Chi-Square Test.
        /// </summary>
        /// <param name="statistic">The test statistic.</param>
        /// <param name="degreesOfFreedom">The chi-square distribution degrees of freedom.</param>
        /// <param name="threshold">The significance threshold. By default, 0.05 will be used.</param>
        /// 
        public ChiSquareTest(double statistic, int degreesOfFreedom, double threshold)
        {
            this.Statistic = statistic;
            this.Threshold = threshold;
            this.distribution = new ChiSquareDistribution(degreesOfFreedom);

            this.PValue = distribution.SurvivalFunction(Statistic);
        }
        public void ConstructorTest()
        {
            var chisq = new ChiSquareDistribution(degreesOfFreedom: 7);

            double mean = chisq.Mean;     // 7
            double median = chisq.Median; // 6.345811195595612
            double var = chisq.Variance;  // 14
            double mode = chisq.Mode;     // 5.0

            double cdf = chisq.DistributionFunction(x: 6.27); // 0.49139966433823956
            double pdf = chisq.ProbabilityDensityFunction(x: 6.27); // 0.11388708001184455
            double lpdf = chisq.LogProbabilityDensityFunction(x: 6.27); // -2.1725478476948092

            double ccdf = chisq.ComplementaryDistributionFunction(x: 6.27); // 0.50860033566176044
            double icdf = chisq.InverseDistributionFunction(p: cdf); // 6.2700000000852318

            double hf = chisq.HazardFunction(x: 6.27); // 0.22392254197721179
            double chf = chisq.CumulativeHazardFunction(x: 6.27); // 0.67609276602233315

            string str = chisq.ToString(); // "χ²(x; df = 7)

            Assert.AreEqual(7, mean);
            Assert.AreEqual(6.345811195595612, median, 1e-6);
            Assert.AreEqual(14, var);
            Assert.AreEqual(5.0, mode);
            Assert.AreEqual(0.67609276602233315, chf);
            Assert.AreEqual(0.49139966433823956, cdf);
            Assert.AreEqual(0.11388708001184455, pdf);
            Assert.AreEqual(-2.1725478476948092, lpdf);
            Assert.AreEqual(0.22392254197721179, hf);
            Assert.AreEqual(0.50860033566176044, ccdf);
            Assert.AreEqual(6.2700000000852318, icdf, 1e-6);
            Assert.AreEqual("χ²(x; df = 7)", str);

            var range1 = chisq.GetRange(0.95);
            var range2 = chisq.GetRange(0.99);
            var range3 = chisq.GetRange(0.01);

            Assert.AreEqual(2.1673499092980579, range1.Min);
            Assert.AreEqual(14.067140449340167, range1.Max);
            Assert.AreEqual(1.2390423055679316, range2.Min);
            Assert.AreEqual(18.475306906582361, range2.Max);
            Assert.AreEqual(1.2390423055679316, range3.Min);
            Assert.AreEqual(18.475306906582361, range3.Max);
        }
        public void DistributionFunctionTest()
        {
            int degreesOfFreedom;
            double actual, expected, x;
            ChiSquareDistribution target;

            degreesOfFreedom = 1;
            target = new ChiSquareDistribution(degreesOfFreedom);
            x = 5;
            actual = target.DistributionFunction(x);
            expected = 0.9747;
            Assert.AreEqual(expected, actual, 1e-4);


            degreesOfFreedom = 5;
            target = new ChiSquareDistribution(degreesOfFreedom);
            x = 5;
            actual = target.DistributionFunction(x);
            expected = 0.5841;
            Assert.AreEqual(expected, actual, 1e-4);
        }
        public void LogProbabilityDensityFunctionTest()
        {
            int degreesOfFreedom;
            double actual, expected, x;
            ChiSquareDistribution target;

            degreesOfFreedom = 1;
            target = new ChiSquareDistribution(degreesOfFreedom);
            x = 1;
            actual = target.LogProbabilityDensityFunction(x);
            expected = System.Math.Log(target.ProbabilityDensityFunction(x));
            Assert.AreEqual(expected, actual, 1e-10);

            degreesOfFreedom = 2;
            target = new ChiSquareDistribution(degreesOfFreedom);
            x = 2;
            actual = target.LogProbabilityDensityFunction(x);
            expected = System.Math.Log(target.ProbabilityDensityFunction(x));
            Assert.AreEqual(expected, actual, 1e-10);

            degreesOfFreedom = 10;
            target = new ChiSquareDistribution(degreesOfFreedom);
            x = 2;
            actual = target.LogProbabilityDensityFunction(x);
            expected = System.Math.Log(target.ProbabilityDensityFunction(x));
            Assert.AreEqual(expected, actual, 1e-10);
        }
        public void MedianTest()
        {
            var target = new ChiSquareDistribution(degreesOfFreedom: 4);

            Assert.AreEqual(target.Median, target.InverseDistributionFunction(0.5));
        }
        /// <summary>
        ///   Construct a Chi-Square Test.
        /// </summary>
        /// <param name="expected">The expected variable values.</param>
        /// <param name="observed">The observed variable values.</param>
        /// <param name="degreesOfFreedom">The chi-square distribution degrees of freedom.</param>
        /// <param name="threshold">The significance threshold. By default, 0.05 will be used.</param>
        /// 
        public ChiSquareTest(double[] expected, double[] observed, int degreesOfFreedom, double threshold)
        {
            if (expected == null)
                throw new ArgumentNullException("expected");

            if (observed == null)
                throw new ArgumentNullException("observed");

            // X² = sum(o - e)²
            //          -----
            //            e

            double sum = 0.0;
            for (int i = 0; i < observed.Length; i++)
            {
                double d = observed[i] - expected[i];
                sum += (d * d) / expected[i];
            }

            this.Statistic = sum;
            this.Threshold = threshold;
            this.distribution = new ChiSquareDistribution(degreesOfFreedom);

            this.PValue = distribution.SurvivalFunction(Statistic);
        }
        public void InverseCumulativeFunctionTest()
        {
            double a;

            var target = new ChiSquareDistribution();

            a = target.InverseDistributionFunction(0);
            Assert.AreEqual(0.0, a, 1e-5);
            Assert.IsFalse(double.IsNaN(a));

            a = target.InverseDistributionFunction(1);
            Assert.AreEqual(double.PositiveInfinity, a, 1e-5);
            Assert.IsFalse(double.IsNaN(a));
        }
        public void ConstructorTest11()
        {
            var original = new ChiSquareDistribution(degreesOfFreedom: 7);

            var chisq = GeneralContinuousDistribution.FromDensityFunction(
                original.Support, original.ProbabilityDensityFunction);

            for (double i = -10; i < +10; i += 0.1)
            {
                double expected = original.DistributionFunction(i);
                double actual = chisq.DistributionFunction(i);

                Assert.IsTrue(expected.IsRelativelyEqual(actual, 1e-5));
                Assert.IsFalse(Double.IsNaN(actual));
                Assert.IsFalse(Double.IsNaN(expected));
            }

            testChiSquare(chisq);
        }
        public void ConstructorTest10()
        {
            var original = new ChiSquareDistribution(degreesOfFreedom: 7);

            var chisq = GeneralContinuousDistribution.FromDistributionFunction(
                original.Support, original.DistributionFunction);

            for (double i = -10; i < +10; i += 0.1)
            {
                double expected = original.ProbabilityDensityFunction(i);
                double actual = chisq.ProbabilityDensityFunction(i);

                double diff = Math.Abs(expected - actual);
                Assert.AreEqual(expected, actual, 1e-8);
                Assert.IsFalse(Double.IsNaN(actual));
                Assert.IsFalse(Double.IsNaN(expected));
            }

            testChiSquare(chisq);
        }
        public void ConstructorTest11()
        {
            var original = new ChiSquareDistribution(degreesOfFreedom: 7);

            var chisq = GeneralContinuousDistribution.FromDensityFunction(
                original.Support, original.ProbabilityDensityFunction);

            testChiSquare(chisq);
        }