Пример #1
0
        public void ValidateInverseCumulativeDistribution(double lambda, double x)
        {
            var n = new Exponential(lambda);

            Assert.AreEqual(x, n.InverseCumulativeDistribution(1.0 - Math.Exp(-lambda * x)));
            Assert.AreEqual(x, Exponential.InvCDF(lambda, 1.0 - Math.Exp(-lambda * x)));
        }
Пример #2
0
        public Position1 Sample(Random random)
        {
            Position1 distance = Domain.Entry + (float)distribution.InverseCumulativeDistribution(random.NextDouble());

            if (float.IsNaN(distance))
            {
                throw new InvalidOperationException($"Exponential distribution returned invalid distance {distance}");
            }
            return(distance <= Domain.Exit ? distance : Position1.PositiveInfinity);
        }
Пример #3
0
        public static void TestGEVLocation()
        {
            //var dist = new Normal(0, 1, Program.rand);
            var dist = new Exponential(2, Program.rand);

            double estimateLoc(int n) => Math.Sqrt(2 * Math.Log(n) - Math.Log(Math.Log(n)) - Math.Log(4 * Math.PI));

            for (int n = 150; n < 500; n += 10)
            {
                double proportion = (n - 0.78) / n;
                double quant      = dist.InverseCumulativeDistribution(proportion);
                double estimate   = estimateLoc(n);
                //Console.WriteLine($"Quantile: {quant} Est: {estimate} Error: {Math.Abs(estimate - quant)}");

                var props  = Interpolation.Linspace(0.000000001, 0.999999999, 10000);
                var quants = new double[10000];
                for (int i = 0; i < quants.Length; i++)
                {
                    quants[i] = dist.InverseCumulativeDistribution(props[i]);
                }
                GPDApproximation.ApproximateExcessDistributionParametersV4(quants, out double a, out double c, out double u);
                var tailApprox = new GPDApproximation(quants, GPDApproximation.FittingMethod.V4);
                var sample     = new double[10000];
                for (int i = 0; i < 10000; i++)
                {
                    double max = 0;
                    for (int j = 0; j < n; j++)
                    {
                        max = Math.Max(max, tailApprox.Sample());
                    }
                    sample[i] = max;
                }
                double shapeGuess    = tailApprox.c;
                double g1            = SpecialFunctions.Gamma(1 - shapeGuess);
                double g2            = SpecialFunctions.Gamma(1 - 2 * shapeGuess);
                double scaleGuess    = Math.Sqrt(Statistics.Variance(sample) * shapeGuess * shapeGuess / (g2 - g1 * g1));
                double locationGuess = Statistics.Mean(sample) - scaleGuess * (g1 - 1) / shapeGuess;
                Console.WriteLine($"Quantile: {quant} IntroEst: {estimate} Error: {Math.Abs(estimate - quant)} Bootstrap:{locationGuess} Shape {shapeGuess}");
            }
        }
 public double InverseCumulativeDistribution(double p)
 {
     return(exponential.InverseCumulativeDistribution(p));
 }