Ejemplo n.º 1
0
        public static void GetIntegrationBounds(Gamma exp, Gaussian d, out double dmode, out double dmin, out double dmax)
        {
            dmode = FindMaximum(exp, d);
            double aMinus1             = exp.Shape - 1;
            double b                   = exp.Rate;
            double mode                = dmode;
            double expMode             = Math.Exp(dmode);
            double mode2               = dmode * dmode;
            Func <double, double> func = delegate(double x)
            {
                if (double.IsInfinity(x))
                {
                    return(double.NegativeInfinity);
                }
                double xMinusMode = x - mode;
                double f          = aMinus1 * xMinusMode - b * (Math.Exp(x) - expMode) + xMinusMode * d.MeanTimesPrecision + 50;
                // avoid infinity*0
                if (d.Precision != 0)
                {
                    f -= 0.5 * (x * x - mode2) * d.Precision;
                }
                return(f);
            };
            Func <double, double> deriv = delegate(double x)
            {
                return(aMinus1 - b * Math.Exp(x) - x * d.Precision + d.MeanTimesPrecision);
            };
            List <double> zeroes = GaussianOp_Slow.FindZeroes(func, deriv, new double[] { dmode }, new double[0]);

            dmin = MMath.Min(zeroes);
            dmax = MMath.Max(zeroes);
        }
Ejemplo n.º 2
0
        public void QuantileTest()
        {
            // draw many samples from N(m,v)
            Rand.Restart(0);
            int           n      = 10000;
            double        m      = 2;
            double        stddev = 3;
            Gaussian      prior  = new Gaussian(m, stddev * stddev);
            List <double> x      = new List <double>();

            for (int i = 0; i < n; i++)
            {
                x.Add(prior.Sample());
            }
            x.Sort();
            var sortedData = new OuterQuantiles(x.ToArray());

            // compute quantiles
            var quantiles = InnerQuantiles.FromDistribution(100, sortedData);

            // loop over x's and compare true quantile rank
            var    testPoints = EpTests.linspace(MMath.Min(x) - stddev, MMath.Max(x) + stddev, 100);
            double maxError   = 0;

            foreach (var testPoint in testPoints)
            {
                var trueRank = MMath.NormalCdf((testPoint - m) / stddev);
                var estRank  = quantiles.GetProbLessThan(testPoint);
                var error    = System.Math.Abs(trueRank - estRank);
                //Trace.WriteLine($"{testPoint} trueRank={trueRank} estRank={estRank} error={error}");
                Assert.True(error < 0.02);
                maxError = System.Math.Max(maxError, error);

                double estQuantile = quantiles.GetQuantile(estRank);
                error = MMath.AbsDiff(estQuantile, testPoint, 1e-8);
                //Trace.WriteLine($"{testPoint} estRank={estRank} estQuantile={estQuantile} error={error}");
                Assert.True(error < 1e-8);

                estRank = sortedData.GetProbLessThan(testPoint);
                error   = System.Math.Abs(trueRank - estRank);
                //Trace.WriteLine($"{testPoint} trueRank={trueRank} estRank={estRank} error={error}");
                Assert.True(error < 0.02);
            }
            //Trace.WriteLine($"max rank error = {maxError}");
        }
Ejemplo n.º 3
0
        private void CheckProbLessThan(CanGetProbLessThan canGetProbLessThan, List <double> x, double maximumError)
        {
            x.Sort();
            var sortedData = new OuterQuantiles(x.ToArray());
            // check that quantiles match within the desired accuracy
            var    min        = MMath.Min(x);
            var    max        = MMath.Max(x);
            var    range      = max - min;
            var    margin     = range * 0.01;
            var    testPoints = EpTests.linspace(min - margin, max + margin, 100);
            double maxError   = 0;

            foreach (var testPoint in testPoints)
            {
                var trueRank = sortedData.GetProbLessThan(testPoint);
                var estRank  = canGetProbLessThan.GetProbLessThan(testPoint);
                var error    = System.Math.Abs(trueRank - estRank);
                maxError = System.Math.Max(maxError, error);
            }
            Console.WriteLine($"max rank error = {maxError}");
            Assert.True(maxError <= maximumError);
        }