/// <summary>
 /// Returns the integral from <tt>x</tt> to infinity of the gamma
 /// probability density function:
 /// <pre>
 ///               inf.
 ///        b       -
 ///       a       | |   b-1  -at
 /// y =  -----    |    t    e    dt
 ///       -     | |
 ///      | (b)   -
 ///               x
 /// </pre>
 /// The incomplete gamma integral is used, according to the
 /// relation
 /// <para>
 /// y = Gamma.incompleteGammaComplement( b, a*x ).
 ///
 /// </para>
 /// </summary>
 /// <param name="a"> the paramater a (alpha) of the gamma distribution. </param>
 /// <param name="b"> the paramater b (beta, lambda) of the gamma distribution. </param>
 /// <param name="x"> integration end point. </param>
 /// <returns> result </returns>
 public static double gammaComplemented(double a, double b, double x)
 {
     if (x < 0.0)
     {
         return(0.0);
     }
     return(GammaFunctions.incompleteGammaComplement(b, a * x));
 }
        /// <summary>
        /// Returns the area under the right hand tail (from <tt>x</tt> to
        /// infinity) of the Chi square probability density function
        /// with <tt>v</tt> degrees of freedom.
        /// <pre>
        ///                                  inf.
        ///                                    -
        ///                        1          | |  v/2-1  -t/2
        ///  P( x | v )   =   -----------     |   t      e     dt
        ///                    v/2  -       | |
        ///                   2    | (v/2)   -
        ///                                   x
        /// </pre>
        /// where <tt>x</tt> is the Chi-square variable.
        ///
        /// The incomplete gamma integral is used, according to the
        /// formula
        ///
        /// <tt>y = chiSquareComplemented( v, x ) = incompleteGammaComplement( v/2.0, x/2.0 )</tt>.
        ///
        ///
        /// The arguments must both be positive.
        /// </summary>
        /// <param name="v"> degrees of freedom. </param>
        /// <param name="x"> x </param>
        /// <returns> result </returns>
        /// <exception cref="ArithmeticException"> error </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public static double chiSquareComplemented(double v, double x) throws ArithmeticException
        public static double chiSquareComplemented(double v, double x)
        {
            if (x < 0.0 || v < 1.0)
            {
                return(0.0);
            }
            return(GammaFunctions.incompleteGammaComplement(v / 2.0, x / 2.0));
        }
        /// <summary>
        /// Returns the sum of the terms <tt>k+1</tt> to <tt>Infinity</tt> of the Poisson distribution.
        /// <pre>
        ///  inf.       j
        ///   --   -m  m
        ///   >   e    --
        ///   --       j!
        ///  j=k+1
        /// </pre>
        /// The terms are not summed directly; instead the incomplete
        /// gamma integral is employed, according to the formula
        /// <para>
        /// <tt>y = poissonComplemented( k, m ) = Gamma.incompleteGamma( k+1, m )</tt>.
        ///
        /// The arguments must both be positive.
        ///
        /// </para>
        /// </summary>
        /// <param name="k"> start term. </param>
        /// <param name="mean"> the mean of the poisson distribution. </param>
        /// <returns> result </returns>
        /// <exception cref="ArithmeticException"> error </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public static double poissonComplemented(int k, double mean) throws ArithmeticException
        public static double poissonComplemented(int k, double mean)
        {
            if (mean < 0)
            {
                throw new System.ArgumentException();
            }
            if (k < -1)
            {
                return(0.0);
            }
            return(GammaFunctions.incompleteGamma((double)(k + 1), mean));
        }
        /// <summary>
        /// Returns the sum of the terms <tt>k+1</tt> to infinity of the Negative
        /// Binomial distribution.
        /// <pre>
        ///   inf
        ///   --  ( n+j-1 )   n      j
        ///   >   (       )  p  (1-p)
        ///   --  (   j   )
        ///  j=k+1
        /// </pre>
        /// The terms are not computed individually; instead the incomplete
        /// beta integral is employed, according to the formula
        /// <para>
        /// y = negativeBinomialComplemented( k, n, p ) = Gamma.incompleteBeta( k+1, n, 1-p ).
        ///
        /// All arguments must be positive,
        /// </para>
        /// </summary>
        /// <param name="k"> end term. </param>
        /// <param name="n"> the number of trials. </param>
        /// <param name="p"> the probability of success (must be in <tt>(0.0,1.0)</tt>). </param>
        /// <returns> result </returns>
        public static double negativeBinomialComplemented(int k, int n, double p)
        {
            if ((p < 0.0) || (p > 1.0))
            {
                throw new System.ArgumentException();
            }
            if (k < 0)
            {
                return(0.0);
            }

            return(GammaFunctions.incompleteBeta(k + 1, n, 1.0 - p));
        }
Example #5
0
        /// <summary>
        /// Power series for incomplete beta integral; formerly named <tt>pseries</tt>.
        /// Use when b*x is small and x not too close to 1.
        /// </summary>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: static double powerSeries(double a, double b, double x) throws ArithmeticException
        internal static double powerSeries(double a, double b, double x)
        {
            double s, t, u, v, n, t1, z, ai;

            ai = 1.0 / a;
            u  = (1.0 - b) * x;
            v  = u / (a + 1.0);
            t1 = v;
            t  = u;
            n  = 2.0;
            s  = 0.0;
            z  = MACHEP * ai;
            while (Math.Abs(v) > z)
            {
                u  = (n - b) * x / n;
                t *= u;
                v  = t / (a + n);
                s += v;
                n += 1.0;
            }
            s += t1;
            s += ai;

            u = a * Math.Log(x);
            if ((a + b) < MAXGAM && Math.Abs(u) < MAXLOG)
            {
                t = GammaFunctions.gamma(a + b) / (GammaFunctions.gamma(a) * GammaFunctions.gamma(b));
                s = s * t * Math.Pow(x, a);
            }
            else
            {
                t = GammaFunctions.logGamma(a + b) - GammaFunctions.logGamma(a) - GammaFunctions.logGamma(b) + u + Math.Log(s);
                if (t < MINLOG)
                {
                    s = 0.0;
                }
                else
                {
                    s = Math.Exp(t);
                }
            }
            return(s);
        }
        /// <summary>
        /// Returns the integral from minus infinity to <tt>t</tt> of the Student-t
        /// distribution with <tt>k &gt; 0</tt> degrees of freedom.
        /// <pre>
        ///                                      t
        ///                                      -
        ///                                     | |
        ///              -                      |         2   -(k+1)/2
        ///             | ( (k+1)/2 )           |  (     x   )
        ///       ----------------------        |  ( 1 + --- )        dx
        ///                     -               |  (      k  )
        ///       sqrt( k pi ) | ( k/2 )        |
        ///                                   | |
        ///                                    -
        ///                                   -inf.
        /// </pre>
        /// Relation to incomplete beta integral:
        /// <para>
        /// <tt>1 - studentT(k,t) = 0.5 * Gamma.incompleteBeta( k/2, 1/2, z )</tt>
        /// where <tt>z = k/(k + t**2)</tt>.
        /// </para>
        /// <para>
        /// Since the function is symmetric about <tt>t=0</tt>, the area under the
        /// right tail of the density is found by calling the function
        /// with <tt>-t</tt> instead of <tt>t</tt>.
        ///
        /// </para>
        /// </summary>
        /// <param name="k"> degrees of freedom. </param>
        /// <param name="t"> integration end point. </param>
        /// <returns> result </returns>
        /// <exception cref="ArithmeticException"> error </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public static double studentT(double k, double t) throws ArithmeticException
        public static double studentT(double k, double t)
        {
            if (k <= 0)
            {
                throw new System.ArgumentException();
            }
            if (t == 0)
            {
                return(0.5);
            }

            double cdf = 0.5 * GammaFunctions.incompleteBeta(0.5 * k, 0.5, k / (k + t * t));

            if (t >= 0)
            {
                cdf = 1.0 - cdf;   // fixes bug reported by [email protected]
            }

            return(cdf);
        }
        /// <summary>
        /// Returns the sum of the terms <tt>k+1</tt> through <tt>n</tt> of the Binomial
        /// probability density.
        /// <pre>
        ///   n
        ///   --  ( n )   j      n-j
        ///   >   (   )  p  (1-p)
        ///   --  ( j )
        ///  j=k+1
        /// </pre>
        /// The terms are not summed directly; instead the incomplete
        /// beta integral is employed, according to the formula
        /// <para>
        /// <tt>y = binomialComplemented( k, n, p ) = Gamma.incompleteBeta( k+1, n-k, p )</tt>.
        /// </para>
        /// <para>
        /// All arguments must be positive,
        /// </para>
        /// </summary>
        /// <param name="k"> end term. </param>
        /// <param name="n"> the number of trials. </param>
        /// <param name="p"> the probability of success (must be in <tt>(0.0,1.0)</tt>). </param>
        /// <returns> result </returns>
        /// <exception cref="ArithmeticException"> error </exception>
        public static double binomialComplemented(int k, int n, double p)
        {
            if ((p < 0.0) || (p > 1.0))
            {
                throw new System.ArgumentException();
            }
            if ((k < 0) || (n < k))
            {
                throw new System.ArgumentException();
            }

            if (k == n)
            {
                return(0.0);
            }
            if (k == 0)
            {
                return(1.0 - Math.Pow(1.0 - p, n - k));
            }

            return(GammaFunctions.incompleteBeta(k + 1, n - k, p));
        }
 /// <summary>
 /// Returns the area from zero to <tt>x</tt> under the beta density
 /// function.
 /// <pre>
 ///                          x
 ///            -             -
 ///           | (a+b)       | |  a-1      b-1
 /// P(x)  =  ----------     |   t    (1-t)    dt
 ///           -     -     | |
 ///          | (a) | (b)   -
 ///                         0
 /// </pre>
 /// This function is identical to the incomplete beta
 /// integral function <tt>Gamma.incompleteBeta(a, b, x)</tt>.
 ///
 /// The complemented function is
 ///
 /// <tt>1 - P(1-x)  =  Gamma.incompleteBeta( b, a, x )</tt>;
 /// </summary>
 /// <param name="a"> a </param>
 /// <param name="b"> b </param>
 /// <param name="x"> x </param>
 /// <returns> result </returns>
 /// <exception cref="ArithmeticException"> error </exception>
 public static double beta(double a, double b, double x)
 {
     return(GammaFunctions.incompleteBeta(a, b, x));
 }
 /// <summary>
 /// Returns the area under the right hand tail (from <tt>x</tt> to
 /// infinity) of the beta density function.
 ///
 /// This function is identical to the incomplete beta
 /// integral function <tt>Gamma.incompleteBeta(b, a, x)</tt>.
 /// </summary>
 /// <param name="a"> a </param>
 /// <param name="b"> b </param>
 /// <param name="x"> x </param>
 /// <returns> result </returns>
 /// <exception cref="ArithmeticException"> error </exception>
 public static double betaComplemented(double a, double b, double x)
 {
     return(GammaFunctions.incompleteBeta(b, a, x));
 }