Пример #1
0
        public void FisherFromChiSquared()
        {
            // we will need a RNG
            Random rng = new Random(314159);

            int n1 = 1;
            int n2 = 2;

            // define chi squared distributions
            ContinuousDistribution d1 = new ChiSquaredDistribution(n1);
            ContinuousDistribution d2 = new ChiSquaredDistribution(n2);

            // create a sample of chi-squared variates
            Sample s = new Sample();

            for (int i = 0; i < 250; i++)
            {
                double x1 = d1.GetRandomValue(rng);
                double x2 = d2.GetRandomValue(rng);
                double x  = (x1 / n1) / (x2 / n2);
                s.Add(x);
            }

            // it should match a Fisher distribution with the appropriate parameters
            ContinuousDistribution f0 = new FisherDistribution(n1, n2);
            TestResult             t0 = s.KuiperTest(f0);

            Assert.IsTrue(t0.Probability > 0.05);

            // it should be distinguished from a Fisher distribution with different parameters
            ContinuousDistribution f1 = new FisherDistribution(n1 + 1, n2);
            TestResult             t1 = s.KuiperTest(f1);

            Assert.IsTrue(t1.Probability < 0.05);
        }
Пример #2
0
        public void FitDataToPolynomialChiSquaredTest()
        {
            // we want to make sure that the chi^2 values we are producing from polynomial fits are distributed as expected

            // create a sample to hold chi^2 values
            Sample chis = new Sample();

            // define a model
            Interval r = Interval.FromEndpoints(-5.0, 15.0);
            Func <double, double> fv = delegate(double x) {
                return(1.0 * x - 2.0 * x * x);
            };
            Func <double, double> fu = delegate(double x) {
                return(1.0 + 0.5 * Math.Sin(x));
            };

            // draw 50 data sets from the model and fit year
            // store the resulting chi^2 value in the chi^2 set
            for (int i = 0; i < 50; i++)
            {
                UncertainMeasurementSample xs = CreateDataSet(r, fv, fu, 10, i);
                FitResult fit = xs.FitToPolynomial(2);
                double    chi = fit.GoodnessOfFit.Statistic;
                chis.Add(chi);
            }

            // sanity check the sample
            Assert.IsTrue(chis.Count == 50);

            // test whether the chi^2 values are distributed as expected
            ContinuousDistribution chiDistribution = new ChiSquaredDistribution(7);
            TestResult             ks = chis.KolmogorovSmirnovTest(chiDistribution);

            Assert.IsTrue(ks.LeftProbability < 0.95);
        }
Пример #3
0
        public void Bug2811()
        {
            ChiSquaredDistribution d = new ChiSquaredDistribution(1798);
            double x = d.InverseLeftProbability(0.975);

            Console.WriteLine(x);
        }
Пример #4
0
        public void ChiAndChiSquared()
        {
            ChiDistribution        chi  = new ChiDistribution(3);
            ChiSquaredDistribution chi2 = new ChiSquaredDistribution(3.0);

            Assert.IsTrue(chi.DegreesOfFreedom == chi2.DegreesOfFreedom);

            foreach (double x in TestUtilities.GenerateRealValues(1.0E-1, 1.0E1, 4))
            {
                Assert.IsTrue(TestUtilities.IsNearlyEqual(chi.LeftProbability(x), chi2.LeftProbability(x * x)));
            }
        }
        public static double ValorDeTabla(int grados, double alfa)
        {
            if (!(grados > 0))
            {
                throw new NotSupportedException("Los grados de libertad deben ser mayores a cero");
            }

            Distribution d = new ChiSquaredDistribution(grados);

            var valor = d.InverseRightProbability(alfa);

            return(valor);
        }
        public static double valorDeTabla(int grados, string alfa)
        {
            double confianza = double.Parse(alfa, System.Globalization.CultureInfo.InvariantCulture);

            if (!(grados > 0))
            {
                throw new NotSupportedException("Los grados de libertad deben ser mayores a cero!");
            }

            ContinuousDistribution d = new ChiSquaredDistribution(grados);
            var valor = d.InverseRightProbability(confianza);

            return(valor);
        }
Пример #7
0
        /// <summary>
        /// Performs a  &#x3C7;<sup>2</sup> test comparing the histogram to the given distribution.
        /// </summary>
        /// <param name="distribution">The distribution against which to test the histogram.</param>
        /// <returns>The test result.</returns>
        public TestResult ChiSquaredTest(DiscreteDistribution distribution)
        {
            if (distribution == null)
            {
                throw new ArgumentNullException(nameof(distribution));
            }

            double chi2 = 0.0;
            int    dof  = 0;

            //int lastObservedCounts = extraCounts;
            //double lastExpectedCounts = (distribution.LeftExclusiveProbability(0) + distribution.RightExclusiveProbability(counts.Length - 1)) * total;
            int    lastObservedCounts = 0;
            double lastExpectedCounts = 0.0;

            int    observedCounts = 0;
            double expectedCounts = 0.0;

            for (int i = 0; i < storage.Count; i++)
            {
                observedCounts += storage.GetCounts(i + 1);
                expectedCounts += distribution.ProbabilityMass(i) * storage.Total;
                if (expectedCounts > 4.0)
                {
                    if (lastExpectedCounts > 0.0)
                    {
                        chi2 += MoreMath.Sqr(lastObservedCounts - lastExpectedCounts) / lastExpectedCounts;
                        dof++;
                    }

                    lastObservedCounts = observedCounts;
                    lastExpectedCounts = expectedCounts;

                    observedCounts = 0;
                    expectedCounts = 0.0;
                }
            }

            lastObservedCounts += observedCounts;
            lastExpectedCounts += expectedCounts;
            if (lastExpectedCounts > 0.0)
            {
                chi2 += MoreMath.Sqr(lastObservedCounts - lastExpectedCounts) / lastExpectedCounts;
            }

            ContinuousDistribution nullDistribution = new ChiSquaredDistribution(dof);

            return(new TestResult("χ²", chi2, nullDistribution, TestType.RightTailed));
        }