vb版GammaDist()和GammaInv()两函数的代码.doc
《vb版GammaDist()和GammaInv()两函数的代码.doc》由会员分享,可在线阅读,更多相关《vb版GammaDist()和GammaInv()两函数的代码.doc(6页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、vb版GammaDist()和GammaInv()两函数的代码从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
2、 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 mus
3、t also be nonnegative.% The incomplete gamma function is defined as:http:/zanjero。% 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) = xa, so gammainc(0,0) = 1。Public Functio
4、n 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 = a
5、+ 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)
6、 - GAMMALN(ak) * g End If Else End IfEnd 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))% and is obtained without computing GAMMA(X)。Since the gamma% function can range over
7、 very large or very small values, its logarithm is sometimes more useful.http:/zanjero.ygblog。com/Public Function GAMMALN(XX)Dim COF(6) As Double, stp As Double, half As Double, one As DoubleDim fpf As Double, x As Double, tmp As Double, ser As DoubleDim j As Integer COF(1) = 76。18009173 COF(2) = 86
8、。50532033 COF(3) = 24.01409822 COF(4) = -1。231739516 COF(5) = 0.00120858003 COF(6) = 0.00000536382 stp = 2.50662827465 half = 0.5 one = 1# fpf = 5.5 x = XX - one tmp = x + fpf tmp = (x + half) * Log(tmp) - tmp ser = one For j = 1 To 6 x = x + one ser = ser + COF(j) / x Next j GAMMALN = tmp + Log(stp
9、 ser)End FunctionGammaInv:(要调用上面的GamCdf和GAMMALN函数)GAMINV Inverse of the gamma cumulative distribution function (cdf).% X = GAMINV(P,A,B)returns the inverse of the gamma cdf with% parameters A and B, at the probabilities in P。http:/zanjero。ygblog。com/对应Excel中GammaInv(p,a,b)函数,计算P的p值 pCs/2GammaInv(1-p
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- vb GammaDist GammaInv 函数 代码
限制150内