Exemplo n.º 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);
        }
Exemplo n.º 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)));
 }
Exemplo n.º 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)));
     }
 }
Exemplo n.º 6
0
        public static Complex modifiedBesselFunction_i_impl <T, I>(double nu, Complex x)
            where T : Weight <Complex>, new()
            where I : baseValue <Complex>, new()
        {
            if (Complex.Abs(x) < 13.0)
            {
                Complex alpha = Complex.Pow(0.5 * x, nu) / GammaFunction.value(1.0 + nu);
                Complex Y = 0.25 * x * x;
                int     k = 1;
                Complex sum = alpha, B_k = alpha;

                while (Complex.Abs(B_k *= Y / (k * (k + nu))) > Complex.Abs(sum) * Const.QL_EPSILON)
                {
                    sum += B_k;
                    Utils.QL_REQUIRE(++k < 1000, () => "max iterations exceeded");
                }
                return(sum * FastActivator <T> .Create().weightSmallX(x));
            }
            else
            {
                double  na_k = 1.0, sign = 1.0;
                Complex da_k = new Complex(1.0, 0.0);

                Complex s1 = new Complex(1.0, 0.0), s2 = new Complex(1.0, 0.0);
                for (int k = 1; k < 30; ++k)
                {
                    sign *= -1;
                    na_k *= (4.0 * nu * nu -
                             (2.0 * (double)k - 1.0) *
                             (2.0 * (double)k - 1.0));
                    da_k *= (8.0 * k) * x;
                    Complex a_k = na_k / da_k;

                    s2 += a_k;
                    s1 += sign * a_k;
                }

                Complex i = FastActivator <I> .Create().value();

                return(1.0 / Complex.Sqrt(2 * Const.M_PI * x) *
                       (FastActivator <T> .Create().weight1LargeX(x) * s1 +
                        i * Complex.Exp(i * nu * Const.M_PI) * FastActivator <T> .Create().weight2LargeX(x) * s2));
            }
        }
Exemplo n.º 7
0
        public static double modifiedBesselFunction_i_impl <T, I>(double nu, double x)
            where T : Weight <double>, new()
            where I : baseValue <double>, new()
        {
            if (Math.Abs(x) < 13.0)
            {
                double alpha = Math.Pow(0.5 * x, nu) / GammaFunction.value(1.0 + nu);
                double Y = 0.25 * x * x;
                int    k = 1;
                double sum = alpha, B_k = alpha;

                while (Math.Abs(B_k *= Y / (k * (k + nu))) > Math.Abs(sum) * Const.QL_EPSILON)
                {
                    sum += B_k;
                    Utils.QL_REQUIRE(++k < 1000, () => "max iterations exceeded");
                }
                return(sum * FastActivator <T> .Create().weightSmallX(x));
            }
            else
            {
                double na_k = 1.0, sign = 1.0;
                double da_k = 1.0;

                double s1 = 1.0, s2 = 1.0;
                for (int k = 1; k < 30; ++k)
                {
                    sign *= -1;
                    na_k *= (4.0 * nu * nu -
                             (2.0 * k - 1.0) *
                             (2.0 * k - 1.0));
                    da_k *= (8.0 * k) * x;
                    double a_k = na_k / da_k;

                    s2 += a_k;
                    s1 += sign * a_k;
                }

                double i = FastActivator <I> .Create().value();

                return(1.0 / Math.Sqrt(2 * Const.M_PI * x) *
                       (FastActivator <T> .Create().weight1LargeX(x) * s1 +
                        i * Math.Exp(i * nu * Const.M_PI) * FastActivator <T> .Create().weight2LargeX(x) * s2));
            }
        }
Exemplo n.º 8
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);
            }
        }
Exemplo n.º 9
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);
            }
Exemplo n.º 10
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);
            }
Exemplo n.º 11
0
 public override double mu_0()
 {
     return(Math.Exp(GammaFunction.logValue(s_ + 1)));
 }
Exemplo n.º 12
0
 public static double betaFunction(double z, double w)
 {
     return(Math.Exp(GammaFunction.logValue(z) +
                     GammaFunction.logValue(w) -
                     GammaFunction.logValue(z + w)));
 }
Exemplo n.º 13
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);
        }
Exemplo n.º 14
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);
        }