Beispiel #1
0
        public static string TestAgainstStandardDistribution(IList <double> sortedListOfData, LinearBinning binning, INumericColumn colBinPosition, INumericColumn colBinCounts)
        {
            var stb = new StringBuilder();

            var stat = new Calc.Regression.QuickStatistics();

            foreach (var v in sortedListOfData)
            {
                stat.Add(v);
            }

            // Test the probability against the normal distribution

            // First make a fit of the normal distribution to get mu and sigma
            // guess of mu
            double guessedMu    = stat.Mean;
            double guessedSigma = stat.SampleStandardDeviation;

            /*
             *                        Altaxo.Calc.Regression.Nonlinear.SimpleNonlinearFit fit = new Altaxo.Calc.Regression.Nonlinear.SimpleNonlinearFit(
             *                                delegate (double[] indep, double[] p, double[] res)
             *                                {
             *                                        // 3 Parameter:  mu and sigma
             *                                        res[0] = Calc.Probability.NormalDistribution.PDF(indep[0], p[0], p[1]);
             *                                },
             *                                new double[] { guessedMu, guessedSigma },
             *                                colBinPosition,
             *                                colProbabilityDensity,
             *                                0, // Start (first point)
             *                                binning.Count // point count
             *                                );
             *
             *                        fit.Fit();
             *
             *                        guessedMu = fit.GetParameter(0);
             *                        guessedSigma = fit.GetParameter(1);
             *
             */

            // Test hypothesis that we have a normal distribution

            double chiSquare = 0;

            int degreesOfFreedom = binning.NumberOfBins - 1 - 2; // -2 because we lost two degrees of freedom in the previous fitting

            for (int nBinIndex = 0; nBinIndex < binning.NumberOfBins; ++nBinIndex)
            {
                var    bin        = binning.Bins[nBinIndex];
                double lowerBound = bin.LowerBound;
                double upperBound = bin.UpperBound;

                var    probability = Calc.Probability.NormalDistribution.CDF(upperBound, guessedMu, guessedSigma) - Calc.Probability.NormalDistribution.CDF(lowerBound, guessedMu, guessedSigma);
                double n0          = probability * sortedListOfData.Count;

                chiSquare += RMath.Pow2(n0 - colBinCounts[nBinIndex]) / n0;
            }

            double chiSquareThreshold = Calc.Probability.ChiSquareDistribution.Quantile(0.95, degreesOfFreedom);

            if (chiSquare <= chiSquareThreshold)
            {
                stb.AppendFormat("The hypothesis that this is a normal distribution with mu={0} and sigma={1} can not be rejected. ChiSquare ({2}) is less than ChiSquare needed to reject hypothesis ({3})", guessedMu, guessedSigma, chiSquare, chiSquareThreshold);
            }
            else
            {
                stb.AppendFormat("The hypothesis that this is a normal distribution with mu={0} and sigma={1} must be rejected with a confidence of {2}%. ChiSquare ({3}) is greater than ChiSquare needed to reject hypothesis ({4})", guessedMu, guessedSigma, 100 * Altaxo.Calc.Probability.ChiSquareDistribution.CDF(chiSquare, degreesOfFreedom), chiSquare, chiSquareThreshold);
            }

            return(stb.ToString());
        }
Beispiel #2
0
 internal BinList(LinearBinning b)
 {
     _parent = b;
 }