2024年4月30日发(作者:)

从matlab里翻译过来的

GammaDist(x,a,b,true):

'%GAMCDF Gamma cumulative distribution function.

'% P = GAMCDF(X,A,B) returns the gamma cumulative distribution

'% function with parameters A and B at the values in X.

Public Function GamCdf(x, a, b) '对应excel中GammaDist(x,a,b,true)函数,计算S曲线

If a <= 0 Or b <= 0 Then GammCdf = "NaN"

GamCdf = GAMMAINC(x / b, a)

p = IIf(p > 1, 1, p)

End Function

'%GAMMAINC Incomplete gamma function.

'% Y = GAMMAINC(X,A) evaluates the incomplete gamma function for

'% corresponding elements of X and A. X and A must be real and the same

'% size (or either can be a scalar). A must also be non-negative.

'% The incomplete gamma function is defined as:

'%/

'% gammainc(x,a) = 1 ./ gamma(a) .*

'% integral from 0 to x of t^(a-1) exp(-t) dt

'%

'% For any a>=0, as x approaches infinity, gammainc(x,a) approaches 1.

'% For small x and a, gammainc(x,a) ~= x^a, so gammainc(0,0) = 1.

Public Function GAMMAINC(x, a)

Dim amax As Double

amax = 2 ^ 20

ascalar = 1

If a <= amax Then

If a <> 0 And x <> 0 And x < a + 1 Then

xk = x

ak = a

ap = ak

Sum = 1 / ap

del = Sum

Dim i As Double

For i = 1 To 10000

ap = ap + 1

del = xk * del / ap

Sum = Sum + del

Next

GAMMAINC = Sum * Exp(-xk + ak * Log(xk) - GAMMALN(ak))

ElseIf a <> 0 And x <> 0 And x >= a + 1 Then

xk = x

a0 = 1

a1 = x

b0 = 0

b1 = a0

ak = a

fac = 1

n = 1

g = b1

gold = b0

For i = 1 To 10000

gold = g

ana = n - ak

a0 = (a1 + a0 * ana) * fac

b0 = (b1 + b0 * ana) * fac

anf = n * fac

a1 = xk * a0 + anf * a1

b1 = xk * b0 + anf * b1

fac = 1 / a1

g = b1 * fac

n = n + 1

Next

GAMMAINC = 1 - Exp(-xk + ak * Log(xk) - GAMMALN(ak)) * g

End If

Else

End If

End Function

'*****************************************************************************************************

**************

'%GAMMALN Logarithm of gamma function.

'% Y = GAMMALN(X) computes the natural logarithm of the gamma

'% function for each element of X. GAMMALN is defined as

'%

'% LOG(GAMMA(X))