Пример #1
0
        public static double incompleteBetaFunction(double a, double b, double x, double accuracy, int maxIteration)
        {
            QL_REQUIRE(a > 0.0, () => "a must be greater than zero");
            QL_REQUIRE(b > 0.0, () => "b must be greater than zero");

            if (x.IsEqual(0.0))
            {
                return(0.0);
            }
            if (x.IsEqual(1.0))
            {
                return(1.0);
            }
            QL_REQUIRE(x > 0.0 && x < 1.0, () => "x must be in [0,1]");

            double result = Math.Exp(GammaFunction.logValue(a + b) -
                                     GammaFunction.logValue(a) - GammaFunction.logValue(b) +
                                     a * Math.Log(x) + b * Math.Log(1.0 - x));

            if (x < (a + 1.0) / (a + b + 2.0))
            {
                return(result *
                       betaContinuedFraction(a, b, x, accuracy, maxIteration) / a);
            }
            return(1.0 - result *
                   betaContinuedFraction(b, a, 1.0 - x, accuracy, maxIteration) / b);
        }
Пример #2
0
 public override double mu_0()
 {
     return(Math.Pow(2.0, alpha_ + beta_ + 1)
            * Math.Exp(GammaFunction.logValue(alpha_ + 1)
                       + GammaFunction.logValue(beta_ + 1)
                       - GammaFunction.logValue(alpha_ + beta_ + 2)));
 }
Пример #3
0
        public double value(double x)
        {
            if (x <= 0.0)
            {
                return(0.0);
            }

            double gln = GammaFunction.logValue(a_);

            if (x < (a_ + 1.0))
            {
                double ap  = a_;
                double del = 1.0 / a_;
                double sum = del;
                for (int n = 1; n <= 100; n++)
                {
                    ap  += 1.0;
                    del *= x / ap;
                    sum += del;
                    if (Math.Abs(del) < Math.Abs(sum) * 3.0e-7)
                    {
                        return(sum * Math.Exp(-x + a_ * Math.Log(x) - gln));
                    }
                }
            }
            else
            {
                double b = x + 1.0 - a_;
                double c = double.MaxValue;
                double d = 1.0 / b;
                double h = d;
                for (int n = 1; n <= 100; n++)
                {
                    double an = -1.0 * n * (n - a_);
                    b += 2.0;
                    d  = an * d + b;
                    if (Math.Abs(d) < Const.QL_EPSILON)
                    {
                        d = Const.QL_EPSILON;
                    }
                    c = b + an / c;
                    if (Math.Abs(c) < Const.QL_EPSILON)
                    {
                        c = Const.QL_EPSILON;
                    }
                    d = 1.0 / d;
                    double del = d * c;
                    h *= del;
                    if (Math.Abs(del - 1.0) < Const.QL_EPSILON)
                    {
                        return(h * Math.Exp(-x + a_ * Math.Log(x) - gln));
                    }
                }
            }
            Utils.QL_FAIL("too few iterations");
            return(0);
        }
 public static double ln(int i)
 {
     if (i <= tabulated)
     {
         return(Math.Log(firstFactorials[i]));
     }
     else
     {
         return(GammaFunction.logValue(i + 1));
     }
 }
 public static double get(uint i)
 {
     if (i <= tabulated)
     {
         return(firstFactorials[i]);
     }
     else
     {
         return(Math.Exp(GammaFunction.logValue(i + 1)));
     }
 }
Пример #6
0
        public static double incompleteBetaFunction(double a, double b, double x, double accuracy, int maxIteration)
        {
            if (!(a > 0.0))
            {
                throw new Exception("a must be greater than zero");
            }
            if (!(b > 0.0))
            {
                throw new Exception("b must be greater than zero");
            }


            if (x == 0.0)
            {
                return(0.0);
            }
            else if (x == 1.0)
            {
                return(1.0);
            }
            else
            if (!(x > 0.0 && x < 1.0))
            {
                throw new Exception("x must be in [0,1]");
            }

            double result = Math.Exp(GammaFunction.logValue(a + b) -
                                     GammaFunction.logValue(a) - GammaFunction.logValue(b) +
                                     a * Math.Log(x) + b * Math.Log(1.0 - x));

            if (x < (a + 1.0) / (a + b + 2.0))
            {
                return(result *
                       betaContinuedFraction(a, b, x, accuracy, maxIteration) / a);
            }
            else
            {
                return(1.0 - result *
                       betaContinuedFraction(b, a, 1.0 - x, accuracy, maxIteration) / b);
            }
        }
Пример #7
0
            private double Lambda(double t)
            {
                int    maxIter = 1000;
                double lambdaT = lambda(t);

                int    i = 0;
                double retVal = 0.0, s;

                do
                {
                    double k = i;
                    s = Math.Exp(k * Math.Log(0.5 * lambdaT) + GammaFunction.logValue(0.5 * (1 + d_) + k)
                                 - GammaFunction.logValue(k + 1) - GammaFunction.logValue(0.5 * d_ + k));
                    retVal += s;
                }while (s > Double.Epsilon && ++i < maxIter);

                Utils.QL_REQUIRE(i < maxIter, () => "can not calculate Lambda");

                retVal *= Math.Sqrt(2 * c(t)) * Math.Exp(-0.5 * lambdaT);
                return(retVal);
            }
Пример #8
0
            public Complex value(double u)
            {
                double gamma2 = gamma_ * gamma_;

                double a, b, c;

                if (8.0 * kappa_ * theta_ / gamma2 > 1.0)
                {
                    a = Math.Sqrt(theta_ - gamma2 / (8.0 * kappa_));
                    b = Math.Sqrt(v0_) - a;
                    c = -Math.Log((LambdaApprox(1.0) - a) / b);
                }
                else
                {
                    a = Math.Sqrt(gamma2 / (2.0 * kappa_))
                        * Math.Exp(GammaFunction.logValue(0.5 * (d_ + 1.0))
                                   - GammaFunction.logValue(0.5 * d_));

                    double t1 = 0.0;
                    double t2 = 1.0 / kappa_;

                    double Lambda_t1 = Math.Sqrt(v0_);
                    double Lambda_t2 = Lambda(t2);

                    c = Math.Log((Lambda_t2 - a) / (Lambda_t1 - a)) / (t1 - t2);
                    b = Math.Exp(c * t1) * (Lambda_t1 - a);
                }

                Complex I4 =
                    -1.0 / lambda_ * new Complex(u * u, ((j_ == 1u) ? -u : u))
                    * (b / c * (1.0 - Math.Exp(-c * term_))
                       + a * term_
                       + a / lambda_ * (Math.Exp(-lambda_ * term_) - 1.0)
                       + b / (c - lambda_) * Math.Exp(-c * term_)
                       * (1.0 - Math.Exp(-term_ * (lambda_ - c))));

                return(eta_ * rhoSr_ * I4);
            }
Пример #9
0
 public override double mu_0()
 {
     return(Math.Exp(GammaFunction.logValue(s_ + 1)));
 }
Пример #10
0
 public static double betaFunction(double z, double w)
 {
     return(Math.Exp(GammaFunction.logValue(z) +
                     GammaFunction.logValue(w) -
                     GammaFunction.logValue(z + w)));
 }
Пример #11
0
        public double value(double x)
        {
        if (x <= 0.0)
            return 0.0;

        double errmax = 1e-12;
        const int itrmax = 10000;
        double lam = 0.5*ncp_;

        double u = Math.Exp(-lam);
        double v = u;
        double x2 = 0.5*x;
        double f2 = 0.5*df_;
        double f_x_2n = df_ - x;

        double t = 0.0;
        if (f2 * Const.QL_EPSILON > 0.125 &&
            Math.Abs(x2-f2) < Math.Sqrt(Const.QL_EPSILON)*f2) {
            t = Math.Exp((1 - t) *
                         (2 - t / (f2 + 1))) / Math.Sqrt(2.0 * Const.M_PI * (f2 + 1.0));
        }
        else {
            t = Math.Exp(f2*Math.Log(x2) - x2 -
                         GammaFunction.logValue(f2 + 1));
        }

        double ans = v*t;

        bool flag = false;
        int n = 1;
        double f_2n = df_ + 2.0;
        f_x_2n += 2.0;

        double bound;
        for (;;) {
            if (f_x_2n > 0) {
                flag = true;
                bound = t * x / f_x_2n;
                if (bound <= errmax || n > itrmax)
                    goto L_End;
            }
            for (;;) {
                u *= lam / n;
                v += u;
                t *= x / f_2n;
                ans += v*t;
                n++;
                f_2n += 2.0;
                f_x_2n += 2.0;
                if (!flag && n <= itrmax)
                    break;
            
                bound = t * x / f_x_2n;
                if (bound <= errmax || n > itrmax)
                    goto L_End;
            }
        }
    L_End:
        Utils.QL_REQUIRE(bound <= errmax,()=> "didn't converge");
        return (ans);
        }
Пример #12
0
        public double value(double x)
        {
            if (x <= 0.0)
            {
                return(0.0);
            }

            double    errmax = 1e-12;
            const int itrmax = 10000;
            double    lam    = 0.5 * ncp_;

            double u      = Math.Exp(-lam);
            double v      = u;
            double x2     = 0.5 * x;
            double f2     = 0.5 * df_;
            double f_x_2n = df_ - x;

            double t = 0.0;

            if (f2 * Const.QL_Epsilon > 0.125 &&
                Math.Abs(x2 - f2) < Math.Sqrt(Const.QL_Epsilon) * f2)
            {
                t = Math.Exp((1 - t) *
                             (2 - t / (f2 + 1))) / Math.Sqrt(2.0 * Const.M_PI * (f2 + 1.0));
            }
            else
            {
                t = Math.Exp(f2 * Math.Log(x2) - x2 -
                             GammaFunction.logValue(f2 + 1));
            }

            double ans = v * t;

            bool   flag = false;
            int    n    = 1;
            double f_2n = df_ + 2.0;

            f_x_2n += 2.0;

            double bound;

            for (;;)
            {
                if (f_x_2n > 0)
                {
                    flag = true;
                    //goto L10;
                    bound = t * x / f_x_2n;
                    if (bound <= errmax || n > itrmax)
                    {
                        goto L_End;
                    }
                }
                for (;;)
                {
                    u   *= lam / n;
                    v   += u;
                    t   *= x / f_2n;
                    ans += v * t;
                    n++;
                    f_2n   += 2.0;
                    f_x_2n += 2.0;
                    if (!flag && n <= itrmax)
                    {
                        break;
                    }

                    bound = t * x / f_x_2n;
                    if (bound <= errmax || n > itrmax)
                    {
                        goto L_End;
                    }
                }
            }
L_End:
            if (bound > errmax)
            {
                throw new ApplicationException("didn't converge");
            }
            return(ans);
        }