public void AndersonDarlingConstructorTest2()
        {
            #region doc_test_normal
            // Test against a Normal distribution

            // This time, let's see if the same sample from the previous example
            // could have originated from a standard Normal (Gaussian) distribution.
            //
            double[] sample =
            {
                0.621, 0.503, 0.203, 0.477, 0.710, 0.581, 0.329, 0.480, 0.554, 0.382
            };

            // Let's estimate a new Normal distribution using the sample
            NormalDistribution distribution = NormalDistribution.Estimate(sample);

            // Now, we can create a new Anderson-Darling's test:
            var ad = new AndersonDarlingTest(sample, distribution);

            // We can then compute the test statistic,
            // the test p-value and its significance:
            double statistic   = ad.Statistic;   // 0.1796
            double pvalue      = ad.PValue;      // 0.8884
            bool   significant = ad.Significant; // false
            #endregion

            Assert.AreEqual(distribution, ad.TheoreticalDistribution);
            Assert.AreEqual(DistributionTail.TwoTail, ad.Tail);

            Assert.AreEqual(0.1796, ad.Statistic, 1e-4);
            Assert.AreEqual(0.8884, ad.PValue, 1e-4);
            Assert.IsFalse(Double.IsNaN(ad.Statistic));

            Assert.IsFalse(ad.Significant);
        }
Exemplo n.º 2
0
        public void AndersonDarlingConstructorTest2()
        {
            // Test against a Normal distribution

            // This time, let's see if the same sample from the previous example
            // could have originated from a standard Normal (Gaussian) distribution.
            //
            double[] sample =
            {
                0.621, 0.503, 0.203, 0.477, 0.710, 0.581, 0.329, 0.480, 0.554, 0.382
            };

            NormalDistribution distribution = NormalDistribution.Estimate(sample);

            var adtest = new AndersonDarlingTest(sample, distribution);

            double statistic = adtest.Statistic;   // 0.1796
            double pvalue    = adtest.PValue;      // 0.8884

            bool significant = adtest.Significant; // false



            Assert.AreEqual(distribution, adtest.TheoreticalDistribution);
            Assert.AreEqual(DistributionTail.TwoTail, adtest.Tail);

            Assert.AreEqual(0.1796, adtest.Statistic, 1e-4);
            Assert.AreEqual(0.8884, adtest.PValue, 1e-4);
            Assert.IsFalse(Double.IsNaN(adtest.Statistic));

            Assert.IsFalse(adtest.Significant);
        }
Exemplo n.º 3
0
        public void EstimateTest()
        {
            double[] values = { 1 };

            bool thrown = false;

            try { NormalDistribution.Estimate(values); }
            catch (ArgumentException) { thrown = true; }

            Assert.IsTrue(thrown);
        }
Exemplo n.º 4
0
        public void GenerateTest()
        {
            NormalDistribution target = new NormalDistribution(2, 5);

            double[] samples = target.Generate(1000000);

            var actual = NormalDistribution.Estimate(samples);

            Assert.AreEqual(2, actual.Mean, 0.01);
            Assert.AreEqual(5, actual.StandardDeviation, 0.01);
        }
Exemplo n.º 5
0
        public void ApproximationTest()
        {
            int t = 14;

            double[] m = Matrix.Magic(6).Reshape().Get(0, t * 2);

            double[] samples = m.Rank();

            double[] rank1 = samples.Get(0, t);
            double[] rank2 = samples.Get(t, 0);

            var exact  = new MannWhitneyDistribution(rank1, rank2, exact: true);
            var approx = new MannWhitneyDistribution(rank1, rank2, exact: false);

            var nd = NormalDistribution.Estimate(exact.Table);

            Assert.AreEqual(nd.Mean, exact.Mean, 1e-10);
            Assert.AreEqual(nd.Variance, exact.Variance, 2e-5);

            foreach (double x in Vector.Range(0, 100))
            {
                double e = approx.DistributionFunction(x);
                double a = exact.DistributionFunction(x);

                if (e > 0.23)
                {
                    Assert.AreEqual(e, a, 0.1);
                }
                else
                {
                    Assert.AreEqual(e, a, 0.01);
                }
            }

            foreach (double x in Vector.Range(0, 100))
            {
                double e = approx.ComplementaryDistributionFunction(x);
                double a = exact.ComplementaryDistributionFunction(x);

                if (e > 0.23)
                {
                    Assert.AreEqual(e, a, 0.1);
                }
                else
                {
                    Assert.AreEqual(e, a, 0.01);
                }
            }
        }
Exemplo n.º 6
0
        public void RNGVentura_LillieforsTest_Has_Significant_Deviation_From_Normal_Distribution()
        {
            double[] sample = GenerateFloatingPointNumberArray();

            var distribution  = UniformContinuousDistribution.Estimate(sample);
            var ndistribution = NormalDistribution.Estimate(sample);

            var lillie  = new LillieforsTest(sample, distribution);
            var nlillie = new LillieforsTest(sample, ndistribution);

            // no significant deviation from a uniform continuous distribution estimate
            lillie.Significant.Should().BeFalse();
            // significant deviation from a normal distribution
            nlillie.Significant.Should().BeTrue();
        }
Exemplo n.º 7
0
        public void NormalGenerateTest()
        {
            // Create a Normal with mean 2 and sigma 5
            var normal = new NormalDistribution(2, 5);

            // Generate 1000000 samples from it
            double[] samples = normal.Generate(10000000);

            // Try to estimate a new Normal distribution from the
            // generated samples to check if they indeed match
            var actual = NormalDistribution.Estimate(samples);

            string result = actual.ToString("N2"); // N(x; μ = 2.01, σ² = 25.03)

            Assert.AreEqual("N(x; μ = 2.00, σ² = 25.00)", result);
        }
Exemplo n.º 8
0
        public void KolmogorovSmirnovTestConstructorTest2_with_reestimation()
        {
            #region doc_normal
            // Test against a Normal distribution

            // Make this example reproducible
            Accord.Math.Random.Generator.Seed = 1;

            // This time, let's see if the same sample from the previous example
            // could have originated from a fitted Normal (Gaussian) distribution.
            //
            double[] sample =
            {
                0.021, 0.003, 0.203, 0.177, 0.910, 0.881, 0.929, 0.180, 0.854, 0.982
            };

            // Before we could not rule out the possibility that the sample came from
            // a uniform distribution, which means the sample was not very far from
            // uniform. This would be an indicative that it would be far from what
            // would be expected from a Normal distribution:

            NormalDistribution distribution = NormalDistribution.Estimate(sample);

            // Create the test
            var lillie = new LillieforsTest(sample, distribution);

            double statistic = lillie.Statistic;   // 0.2882
            double pvalue    = lillie.PValue;      // 0.0207

            bool significant = lillie.Significant; // true

            // Since the test says that the null hypothesis should be rejected, then
            // this can be regarded as a strong indicative that the sample does not
            // comes from a Normal distribution, just as we expected.
            #endregion

            Assert.AreEqual(distribution, lillie.TheoreticalDistribution);
            Assert.AreEqual(KolmogorovSmirnovTestHypothesis.SampleIsDifferent, lillie.Hypothesis);
            Assert.AreEqual(DistributionTail.TwoTail, lillie.Tail);

            Assert.AreEqual(0.28823410018244089, lillie.Statistic, 1e-5);
            Assert.IsTrue(lillie.PValue < 0.03);

            // The null hypothesis can be rejected:
            // the sample is not from a standard Normal distribution
            Assert.IsTrue(lillie.Significant);
        }
Exemplo n.º 9
0
        public void GenerateTest2()
        {
            NormalDistribution target = new NormalDistribution(4, 2);

            double[] samples = new double[1000000];
            for (int i = 0; i < samples.Length; i++)
            {
                samples[i] = target.Generate();
            }

            var actual = NormalDistribution.Estimate(samples);

            actual.Fit(samples);

            Assert.AreEqual(4, actual.Mean, 0.01);
            Assert.AreEqual(2, actual.StandardDeviation, 0.01);
        }
Exemplo n.º 10
0
        public void ApproximationTest()
        {
            double[] m = Matrix.Magic(5).Reshape().Get(0, 20);

            double[] samples = m.Rank();

            var exact  = new WilcoxonDistribution(samples, exact: true);
            var approx = new WilcoxonDistribution(samples, exact: false);

            var nd = NormalDistribution.Estimate(exact.Table);

            Assert.AreEqual(nd.Mean, exact.Mean, 1e-10);
            Assert.AreEqual(nd.Variance, exact.Variance, 1e-3);

            foreach (double x in Vector.Range(0, 100))
            {
                double e = approx.DistributionFunction(x);
                double a = exact.DistributionFunction(x);

                if (e > 0.25)
                {
                    Assert.AreEqual(e, a, 0.1);
                }
                else
                {
                    Assert.AreEqual(e, a, 0.01);
                }
            }

            foreach (double x in Vector.Range(0, 100))
            {
                double e = approx.ComplementaryDistributionFunction(x);
                double a = exact.ComplementaryDistributionFunction(x);

                if (e > 0.25)
                {
                    Assert.AreEqual(e, a, 0.1);
                }
                else
                {
                    Assert.AreEqual(e, a, 0.01);
                }
            }
        }
Exemplo n.º 11
0
        public void Test_Normal_Estimation(int d, int n)
        {
            // generate mu and sigma
            Vector means = Vector.Zeros(d);
            // assuming diagonal covariance matrix
            // for generation purposes equal to the
            // sqrt of the mean (easy to test)
            Vector sigma = Vector.Zeros(d);

            for (int i = 0; i < d; i++)
            {
                means[i] = Sampling.GetUniform() * 10;
                sigma[i] = sqrt(means[i]);
            }


            Matrix data = Matrix.Zeros(n, d);

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < d; j++)
                {
                    data[i, j] = Sampling.GetNormal(means[j], sigma[j]);
                }
            }

            NormalDistribution dstrb = new NormalDistribution();

            dstrb.Estimate(data);

            var cov = dstrb.Sigma.Diag();

            for (int i = 0; i < d; i++)
            {
                // test mean (should be 0, but with 10% tolerance)
                Almost.Equal(diff(means[i], dstrb.Mu[i]), 0, 0.1);
                // test covariance (should be 0, but with 10% tolerance)
                Almost.Equal(diff(means[i], cov[i]), 0, 0.1);
            }
        }
        public static double CalculateCovariance(double[] set1, double[] set2, bool regulate = true)
        {
            var x = NormalDistribution.Estimate(set1, new NormalOptions()
            {
                Regularization = regulate ? 1 : 0
            });
            var y = NormalDistribution.Estimate(set2, new NormalOptions()
            {
                Regularization = regulate ? 1 : 0
            });

            double totalSum = 0;

            for (int i = 0; i < set1.Length; i++)
            {
                totalSum += (set1[i] - x.Mean) * (set2[i] - y.Mean);
            }

            totalSum /= (set1.Length - 1);

            return(totalSum);
        }
Exemplo n.º 13
0
        /// <summary>
        ///   Generic learn method implementation that should work for any input type.
        ///   This method is useful for re-using code between methods that accept Bitmap,
        ///   BitmapData, UnmanagedImage, filenames as strings, etc.
        /// </summary>
        ///
        /// <typeparam name="T">The input type.</typeparam>
        ///
        /// <param name="x">The inputs.</param>
        /// <param name="weights">The weights.</param>
        /// <param name="extractor">A function that knows how to process the input
        ///   and extract features from them.</param>
        ///
        /// <returns>The trained model.</returns>
        ///
        protected TModel InnerLearn <T>(T[] x, double[] weights,
                                        Func <T, TExtractor, IEnumerable <TPoint> > extractor)
        {
            var descriptorsPerInstance = new TFeature[x.Length][];
            var totalDescriptorCounts  = new double[x.Length];
            int takenDescriptorCount   = 0;

            // For all instances
            For(0, x.Length, (i, detector) =>
            {
                if (NumberOfDescriptors > 0 && takenDescriptorCount >= NumberOfDescriptors)
                {
                    return;
                }

                TFeature[] desc = extractor(x[i], detector).Select(p => p.Descriptor).ToArray();

                totalDescriptorCounts[i] = desc.Length;

                if (MaxDescriptorsPerInstance > 0)
                {
                    desc = desc.Sample(MaxDescriptorsPerInstance);
                }

                Interlocked.Add(ref takenDescriptorCount, desc.Length);

                descriptorsPerInstance[i] = desc;
            });

            if (NumberOfDescriptors >= 0 && takenDescriptorCount < NumberOfDescriptors)
            {
                throw new InvalidOperationException("There were not enough descriptors to sample the desired amount " +
                                                    "of samples ({0}). Please either increase the number of images, or increase the number of ".Format(NumberOfDescriptors) +
                                                    "descriptors that are sampled from each image by adjusting the MaxSamplesPerImage property ({0}).".Format(MaxDescriptorsPerInstance));
            }

            var totalDescriptors = new TFeature[takenDescriptorCount];
            var totalWeights     = weights != null ? new double[takenDescriptorCount] : null;

            int[] instanceIndices = new int[takenDescriptorCount];

            int c = 0, w = 0;

            for (int i = 0; i < descriptorsPerInstance.Length; i++)
            {
                if (descriptorsPerInstance[i] != null)
                {
                    if (weights != null)
                    {
                        totalWeights[w++] = weights[i];
                    }
                    for (int j = 0; j < descriptorsPerInstance[i].Length; j++)
                    {
                        totalDescriptors[c] = descriptorsPerInstance[i][j];
                        instanceIndices[c]  = i;
                        c++;
                    }
                }
            }

            if (NumberOfDescriptors > 0)
            {
                int[] idx = Vector.Sample(NumberOfDescriptors);
                totalDescriptors = totalDescriptors.Get(idx);
                instanceIndices  = instanceIndices.Get(idx);
            }

            int[] hist = instanceIndices.Histogram();

            Debug.Assert(hist.Sum() == (NumberOfDescriptors > 0 ? NumberOfDescriptors : takenDescriptorCount));

            this.Statistics = new BagOfWordsStatistics()
            {
                TotalNumberOfInstances              = x.Length,
                TotalNumberOfDescriptors            = (int)totalDescriptorCounts.Sum(),
                TotalNumberOfDescriptorsPerInstance = NormalDistribution.Estimate(totalDescriptorCounts, new NormalOptions {
                    Robust = true
                }),
                TotalNumberOfDescriptorsPerInstanceRange = new IntRange((int)totalDescriptorCounts.Min(), (int)totalDescriptorCounts.Max()),

                NumberOfInstancesTaken              = hist.Length,
                NumberOfDescriptorsTaken            = totalDescriptors.Length,
                NumberOfDescriptorsTakenPerInstance = NormalDistribution.Estimate(hist.ToDouble(), new NormalOptions {
                    Robust = true
                }),
                NumberOfDescriptorsTakenPerInstanceRange = new IntRange(hist.Min(), hist.Max())
            };

            return(learn(totalDescriptors, totalWeights));
        }