public void FitTest7()
        {
            UnivariateContinuousDistribution target = CreateUnivariateContinuousDistribution();

            double[] observations = { 0.12, 2, 0.52 };
            double[] weights      = { 0.25, 0.25, 0.50 };

            target.Fit(observations, weights);

            double expected = 0.79;
            double actual   = target.Mean;

            Assert.AreEqual(expected, actual);
        }
Example #2
0
        public void GetRangeTest2()
        {
            UnivariateContinuousDistribution target = CreateUnivariateContinuousDistribution();

            var range2 = target.GetRange(0.0 + Double.Epsilon);

            Assert.AreEqual(-55.834722290615161, range2.Min);
            Assert.AreEqual(Double.PositiveInfinity, range2.Max);

            var range3 = target.GetRange(1.0 - Constants.DoubleEpsilon);

            Assert.AreEqual(-55.834722290615161, range2.Min);
            Assert.AreEqual(Double.PositiveInfinity, range2.Max);
        }
Example #3
0
        public void FitTest4()
        {
            UnivariateContinuousDistribution target = CreateUnivariateContinuousDistribution();

            double[]        observations = { 0.12, 2, 0.52 };
            IFittingOptions options      = null;

            target.Fit(observations, options);

            double expected = 0.88;
            double actual   = target.Mean;

            Assert.AreEqual(expected, actual);
        }
        public void GetRangeTest_RangeException()
        {
            UnivariateContinuousDistribution target = CreateUnivariateContinuousDistribution();

            bool thrown = false;

            try { target.GetRange(0 - Double.Epsilon); }
            catch (ArgumentOutOfRangeException) { thrown = true; }
            Assert.IsTrue(thrown);

            thrown = false;
            try { target.GetRange(1.0 + 2 * Constants.DoubleEpsilon); }
            catch (ArgumentOutOfRangeException) { thrown = true; }
            Assert.IsTrue(thrown);
        }
        public void GetRangeTest()
        {
            UnivariateContinuousDistribution target = CreateUnivariateContinuousDistribution();

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

            Assert.AreEqual(19.629053173483637, range1.Min);
            Assert.AreEqual(26.370946826516363, range1.Max);
            Assert.AreEqual(18.232405574041742, range2.Min);
            Assert.AreEqual(27.767594425958258, range2.Max);
            Assert.AreEqual(18.232405574041742, range3.Min);
            Assert.AreEqual(27.767594425958258, range3.Max);
        }
        public void EmpiricalDistributionTest()
        {
            double[] sample = { 1, 5, 3, 1, 5, 2, 1 };
            UnivariateContinuousDistribution distribution = NormalDistribution.Standard;

            var target = new KolmogorovSmirnovTest(sample, distribution);

            EmpiricalDistribution actual = target.EmpiricalDistribution;

            Assert.AreNotSame(sample, actual.Samples);

            Array.Sort(sample);

            for (int i = 0; i < sample.Length; i++)
            {
                Assert.AreEqual(sample[i], actual.Samples[i]);
            }
        }
        private static double[] GenerateRandom(DistributionsEvaluator evaluator, Dictionary <string, DistributionSettings> randomSource, int samples)
        {
            double[] random = new double[samples];
            var      order  = evaluator.Parameters;

            int argsCount = order.Length;

            UnivariateContinuousDistribution[] orderedDistributions = new UnivariateContinuousDistribution[argsCount];

            ThreadLocal <double[]> argsLocal = new ThreadLocal <double[]>(() => new double[argsCount]);

            for (int i = 0; i < argsCount; i++)
            {
                string arg = order[i];

                if (randomSource.TryGetValue(arg, out DistributionSettings value))
                {
                    orderedDistributions[i] = value.GetUnivariateContinuoisDistribution();
                }
                else
                {
                    // TODO: check this out
                    // throw new DistributionsArgumentException($"Parameter value \"{arg}\" is missing", $"Отсутствует значение параметра \"{arg}\"");
                    throw new DistributionsArgumentException(DistributionsArgumentExceptionType.ParameterValueIsMissing, arg);
                }
            }

            Parallel.For(0, samples, i =>
            {
                double[] args = argsLocal.Value;
                Random rnd    = Rnd.Value;

                for (int j = 0; j < argsCount; j++)
                {
                    args[j] = orderedDistributions[j].Generate(rnd);
                }

                random[i] = evaluator.EvaluateCompiled(args);
            });

            return(random);
        }
Example #8
0
        public void EmpiricalDistributionTest_with_reestimation()
        {
            Accord.Math.Random.Generator.Seed = 1;

            double[] sample = { 1, 5, 3, 1, 5, 2, 1 };
            UnivariateContinuousDistribution distribution = NormalDistribution.Standard;

            var target = new LillieforsTest(sample, distribution);

            EmpiricalDistribution actual = target.EmpiricalDistribution;

            Assert.AreNotSame(sample, actual.Samples);

            Array.Sort(sample);

            for (int i = 0; i < sample.Length; i++)
            {
                Assert.AreEqual(sample[i], actual.Samples[i]);
            }
        }
Example #9
0
        /// <summary>
        ///   Creates a new One-Sample Kolmogorov 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 (which must NOT have been estimated from the data).</param>
        /// <param name="alternate">The alternative hypothesis (research hypothesis) to test.</param>
        ///
        public KolmogorovSmirnovTest(double[] sample, UnivariateContinuousDistribution hypothesizedDistribution,
                                     KolmogorovSmirnovTestHypothesis alternate = KolmogorovSmirnovTestHypothesis.SampleIsDifferent)
        {
            this.Hypothesis = alternate;

            double N = sample.Length;

            // Create the test statistic distribution with given degrees of freedom
            StatisticDistribution = new KolmogorovSmirnovDistribution(sample.Length);


            // Create a copy of the samples to prevent altering the
            // constructor's original arguments in the sorting step
            double[] Y = (double[])sample.Clone();
            double[] D = new double[sample.Length];

            // Sort sample
            Array.Sort(Y);

            // Create the theoretical and empirical distributions
            this.TheoreticalDistribution = hypothesizedDistribution;
            this.EmpiricalDistribution   = new EmpiricalDistribution(Y, smoothing: 0);

            Func <double, double> F = TheoreticalDistribution.DistributionFunction;

            // Finally, compute the test statistic and perform actual testing.
            if (alternate == KolmogorovSmirnovTestHypothesis.SampleIsDifferent)
            {
                // Test if the sample's distribution is just significantly
                //   "different" than the given theoretical distribution.

                // This is a correction on the common formulation found in many places
                //  such as in Wikipedia. Please see the Engineering Statistics Handbook,
                //  section "1.3.5.16. Kolmogorov-Smirnov Goodness-of-Fit Test" for more
                //  details: http://www.itl.nist.gov/div898/handbook/eda/section3/eda35g.htm

                for (int i = 0; i < sample.Length; i++)
                {
                    D[i] = Math.Max(Math.Abs(F(Y[i]) - i / N), Math.Abs((i + 1) / N - F(Y[i])));
                }

                base.Statistic = D.Max(); // This is the two-sided "Dn" statistic.
                base.PValue    = StatisticDistribution.ComplementaryDistributionFunction(Statistic);
                base.Tail      = Testing.DistributionTail.TwoTail;
            }
            else if (alternate == KolmogorovSmirnovTestHypothesis.SampleIsGreater)
            {
                // Test if the sample's distribution is "larger" than the
                // given theoretical distribution, in a statistical sense.

                for (int i = 0; i < sample.Length; i++)
                {
                    D[i] = Math.Max(i / N - F(Y[i]), (i + 1) / N - F(Y[i]));
                }

                base.Statistic = D.Max(); // This is the one-sided "Dn+" statistic.
                base.PValue    = StatisticDistribution.OneSideDistributionFunction(Statistic);
                base.Tail      = Testing.DistributionTail.OneUpper;
            }
            else
            {
                // Test if the sample's distribution is "smaller" than the
                // given theoretical distribution, in a statistical sense.

                for (int i = 0; i < sample.Length; i++)
                {
                    D[i] = Math.Max(F(Y[i]) - i / N, F(Y[i]) - (i + 1) / N);
                }

                base.Statistic = D.Max(); // This is the one-sided "Dn-" statistic.
                base.PValue    = StatisticDistribution.OneSideDistributionFunction(Statistic);
                base.Tail      = Testing.DistributionTail.OneLower;
            }
        }
Example #10
0
 /// <summary>
 ///   Creates a new One-Sample Kolmogorov 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 (which must NOT have been estimated from the data).</param>
 ///
 public KolmogorovSmirnovTest(double[] sample, UnivariateContinuousDistribution hypothesizedDistribution)
     : this(sample, hypothesizedDistribution, KolmogorovSmirnovTestHypothesis.SampleIsDifferent)
 {
 }
 internal ContinuousDistribution(UnivariateContinuousDistribution baseDistribution, int samples, double coeff, double offset)
     : this(baseDistribution, samples)
 {
     Coefficient = coeff;
     Offset      = offset;
 }
 internal ContinuousDistribution(UnivariateContinuousDistribution baseDistribution)
 {
     BaseDistribution = baseDistribution ?? throw new ArgumentNullException(nameof(baseDistribution));
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="CustomDistribution"/> class
 /// based on <paramref name="distribution"/>, reference for <see cref="Accord.Statistics"/> required.
 /// </summary>
 /// <param name="distribution">Base distribution, reference for <see cref="Accord.Statistics"/> required.</param>
 public CustomDistribution(UnivariateContinuousDistribution distribution)
 {
     Distribution = distribution;
 }