Beispiel #1
0
        /// <summary>
        ///   Creates a new Anderson-Darling test.
        /// </summary>
        ///
        /// <param name="sample">The sample we would like to test as belonging to the <paramref name="hypothesizedDistribution"/>.</param>
        /// <param name="hypothesizedDistribution">A fully specified distribution.</param>
        ///
        public AndersonDarlingTest(double[] sample, IUnivariateDistribution <double> hypothesizedDistribution)
        {
            // Create the test statistic distribution
            this.TheoreticalDistribution = hypothesizedDistribution;
            if (hypothesizedDistribution is UniformContinuousDistribution)
            {
                StatisticDistribution = new AndersonDarlingDistribution(AndersonDarlingDistributionType.Uniform, sample.Length);
            }
            else if (hypothesizedDistribution is NormalDistribution)
            {
                StatisticDistribution = new AndersonDarlingDistribution(AndersonDarlingDistributionType.Normal, sample.Length);
            }
            else
            {
                Trace.WriteLine(String.Format("Unsupported distribution in AndersonDarling: {0}. P-values will not be computed, but test statistic may be useful.",
                                              hypothesizedDistribution.ToString(), hypothesizedDistribution.GetType().ToString()));
            }

            // Create a copy of the samples to prevent altering the
            // constructor's original arguments in the sorting step
            double[] sortedSamples = sample.Sorted();

            // Create the theoretical and empirical distributions
            this.TheoreticalDistribution = hypothesizedDistribution;

            this.Statistic = GetStatistic(sortedSamples, TheoreticalDistribution);
            this.PValue    = StatisticToPValue(Statistic);
        }
        /// <summary>
        /// Vapnik Chervonenkis test.
        /// </summary>
        /// <param name="epsilon">The error we are willing to tolerate.</param>
        /// <param name="delta">The error probability we are willing to tolerate.</param>
        /// <param name="s">The samples to use for testing.</param>
        /// <param name="dist">The distribution we are testing.</param>
        public static void VapnikChervonenkisTest(double epsilon, double delta, IEnumerable <double> s, IUnivariateDistribution dist)
        {
            // Using VC-dimension, we can bound the probability of making an error when estimating empirical probability
            // distributions. We are using Theorem 2.41 in "All Of Nonparametric Statistics".
            // http://books.google.com/books?id=MRFlzQfRg7UC&lpg=PP1&dq=all%20of%20nonparametric%20statistics&pg=PA22#v=onepage&q=%22shatter%20coe%EF%AC%83cients%20do%20not%22&f=false .</para>
            // For intervals on the real line the VC-dimension is 2.
            double n = s.Count();

            Assert.Greater(n, Math.Ceiling(32.0 * Math.Log(16.0 / delta) / epsilon / epsilon));

            var histogram = new Histogram(s, NumberOfBuckets);

            for (var i = 0; i < NumberOfBuckets; i++)
            {
                var p  = dist.CumulativeDistribution(histogram[i].UpperBound) - dist.CumulativeDistribution(histogram[i].LowerBound);
                var pe = histogram[i].Count / n;
                Assert.Less(Math.Abs(p - pe), epsilon, dist.ToString());
            }
        }
        /// <summary>
        /// Vapnik Chervonenkis test.
        /// </summary>
        /// <param name="epsilon">The error we are willing to tolerate.</param>
        /// <param name="delta">The error probability we are willing to tolerate.</param>
        /// <param name="s">The samples to use for testing.</param>
        /// <param name="dist">The distribution we are testing.</param>
        public static void VapnikChervonenkisTest(double epsilon, double delta, IEnumerable<double> s, IUnivariateDistribution dist)
        {
            // Using VC-dimension, we can bound the probability of making an error when estimating empirical probability
            // distributions. We are using Theorem 2.41 in "All Of Nonparametric Statistics".
            // http://books.google.com/books?id=MRFlzQfRg7UC&lpg=PP1&dq=all%20of%20nonparametric%20statistics&pg=PA22#v=onepage&q=%22shatter%20coe%EF%AC%83cients%20do%20not%22&f=false .</para>
            // For intervals on the real line the VC-dimension is 2.
            double n = s.Count();
            Assert.Greater(n, Math.Ceiling(32.0 * Math.Log(16.0 / delta) / epsilon / epsilon));

            var histogram = new Histogram(s, NumberOfBuckets);
            for (var i = 0; i < NumberOfBuckets; i++)
            {
                var p = dist.CumulativeDistribution(histogram[i].UpperBound) - dist.CumulativeDistribution(histogram[i].LowerBound);
                var pe = histogram[i].Count / n;
                Assert.Less(Math.Abs(p - pe), epsilon, dist.ToString());
            }
        }
        private PlotModel createBaseModel(DoubleRange?range, string title, double[] x, double[] y, bool discrete)
        {
            var plotModel = new PlotModel();

            plotModel.Series.Clear();
            plotModel.Axes.Clear();

            double ymin = y.FirstOrDefault(a => !Double.IsNaN(a) && !Double.IsInfinity(a));
            double ymax = ymin;

            for (int i = 0; i < y.Length; i++)
            {
                if (Double.IsNaN(y[i]) || Double.IsInfinity(y[i]))
                {
                    continue;
                }

                if (y[i] > ymax)
                {
                    ymax = y[i];
                }
                if (y[i] < ymin)
                {
                    ymin = y[i];
                }
            }

            double maxGrace = ymax * 0.1;
            double minGrace = ymin * 0.1;


            if (!discrete)
            {
                var xAxis = new OxyPlot.Axes.LinearAxis()
                {
                    Position           = AxisPosition.Bottom,
                    Minimum            = range.Value.Min,
                    Maximum            = range.Value.Max,
                    Key                = "xAxis",
                    MajorGridlineStyle = LineStyle.Solid,
                    MinorGridlineStyle = LineStyle.Dot,
                    IntervalLength     = 80
                };

                var yAxis = new LinearAxis()
                {
                    Position           = AxisPosition.Left,
                    Minimum            = ymin - minGrace,
                    Maximum            = ymax + maxGrace,
                    Key                = "yAxis",
                    MajorGridlineStyle = LineStyle.Solid,
                    MinorGridlineStyle = LineStyle.Dot,
                    Title              = title
                };

                plotModel.Axes.Add(xAxis);
                plotModel.Axes.Add(yAxis);

                var lineSeries = new LineSeries
                {
                    YAxisKey        = yAxis.Key,
                    XAxisKey        = xAxis.Key,
                    StrokeThickness = 2,
                    MarkerSize      = 3,
                    MarkerStroke    = OxyColor.FromRgb(0, 0, 0),
                    MarkerType      = MarkerType.None,
                    Smooth          = true,
                };

                for (int i = 0; i < x.Length; i++)
                {
                    if (Double.IsNaN(y[i]) || Double.IsInfinity(y[i]))
                    {
                        continue;
                    }

                    lineSeries.Points.Add(new DataPoint(x[i], y[i]));
                }

                plotModel.Series.Add(lineSeries);
            }
            else
            {
                var xAxis = new OxyPlot.Axes.CategoryAxis()
                {
                    Position           = AxisPosition.Bottom,
                    Key                = "xAxis",
                    MajorGridlineStyle = LineStyle.Solid,
                    MinorGridlineStyle = LineStyle.Dot,
                };

                var yAxis = new LinearAxis()
                {
                    Position           = AxisPosition.Left,
                    Minimum            = ymin - minGrace,
                    Maximum            = ymax + maxGrace,
                    Key                = "yAxis",
                    MajorGridlineStyle = LineStyle.Solid,
                    MinorGridlineStyle = LineStyle.Dot,
                    Title              = title
                };

                plotModel.Axes.Add(xAxis);
                plotModel.Axes.Add(yAxis);

                var boxSeries = new ColumnSeries
                {
                    YAxisKey        = yAxis.Key,
                    XAxisKey        = xAxis.Key,
                    StrokeThickness = 2,
                    ColumnWidth     = 1,
                };

                for (int i = 0; i < x.Length; i++)
                {
                    xAxis.Labels.Add(x[i].ToString("G2"));
                    var item = new ColumnItem(y[i]);
                    boxSeries.Items.Add(item);
                }

                plotModel.Series.Add(boxSeries);
            }

            var formattable = instance as IFormattable;

            if (formattable != null)
            {
                plotModel.Title = formattable.ToString("G3", CultureInfo.CurrentUICulture);
            }
            else
            {
                plotModel.Title = instance.ToString();
            }

            plotModel.TitlePadding    = 2;
            plotModel.TitleFontSize   = 15;
            plotModel.TitleFontWeight = 1;
            plotModel.TitlePadding    = 2;


            return(plotModel);
        }