Beispiel #1
0
        static double GetX(double p, ContinuousDistribution dist)
        {
            double x, xNext = 1;

            do
            {
                x     = xNext;
                xNext = x - (dist.CumulativeDistribution(x) - p) /
                        dist.ProbabilityDensity(x);
            }while (Math.Abs(x - xNext) > epsilon);
            return(xNext);
        }
        private void DistributionProbabilityTestHelper(ContinuousDistribution distribution, double x)
        {
            Console.WriteLine("{0} {1}", distribution.GetType().Name, x);
            double P = distribution.LeftProbability(x);
            double Q = distribution.RightProbability(x);

            Assert.IsTrue((0.0 <= P) && (P <= 1.0));
            Assert.IsTrue((0.0 <= Q) && (Q <= 1.0));
            Assert.IsTrue(TestUtilities.IsNearlyEqual(P + Q, 1.0));
            double p = distribution.ProbabilityDensity(x);

            Assert.IsTrue(p >= 0.0);
        }
Beispiel #3
0
        private void DistributionProbabilityTestHelper(ContinuousDistribution distribution, double x)
        {
            double P = distribution.LeftProbability(x);
            double Q = distribution.RightProbability(x);

            Assert.IsTrue((0.0 <= P) && (P <= 1.0));
            Assert.IsTrue((0.0 <= Q) && (Q <= 1.0));
            Assert.IsTrue(TestUtilities.IsNearlyEqual(P + Q, 1.0));
            double p = distribution.ProbabilityDensity(x);

            Assert.IsTrue(p >= 0.0);
            double h = distribution.Hazard(x);

            if (p > 0.0 && Q > 0.0)
            {
                Assert.IsTrue(TestUtilities.IsNearlyEqual(h, p / Q));
            }
        }
Beispiel #4
0
        internal static DistributionFitResult <ContinuousDistribution> MaximumLikelihoodFit(IReadOnlyList <double> sample, Func <IReadOnlyList <double>, ContinuousDistribution> factory, IReadOnlyList <double> start, IReadOnlyList <string> names)
        {
            Debug.Assert(sample != null);
            Debug.Assert(factory != null);
            Debug.Assert(start != null);
            Debug.Assert(names != null);
            Debug.Assert(start.Count == names.Count);

            // Define a log likelihood function
            Func <IReadOnlyList <double>, double> logL = (IReadOnlyList <double> a) => {
                ContinuousDistribution d = factory(a);
                double lnP = 0.0;
                foreach (double value in sample)
                {
                    double P = d.ProbabilityDensity(value);
                    if (P == 0.0)
                    {
                        throw new InvalidOperationException();
                    }
                    lnP += Math.Log(P);
                }
                return(lnP);
            };

            // Maximize it
            MultiExtremum         maximum = MultiFunctionMath.FindLocalMaximum(logL, start);
            ColumnVector          b       = maximum.Location;
            SymmetricMatrix       C       = maximum.HessianMatrix;
            CholeskyDecomposition CD      = C.CholeskyDecomposition();

            if (CD == null)
            {
                throw new DivideByZeroException();
            }
            C = CD.Inverse();

            ContinuousDistribution distribution = factory(maximum.Location);
            TestResult             test         = sample.KolmogorovSmirnovTest(distribution);

            return(new ContinuousDistributionFitResult(names, b, C, distribution, test));
        }