|
201 | 201 | % |
202 | 202 | %December 2014 David A. Karnick, Naval Research Laboratory, Washington D.C. |
203 | 203 |
|
| 204 | +if(nargin<6||isempty(maxIter)) |
| 205 | + maxIter=5000; |
| 206 | +end |
| 207 | + |
| 208 | +if(nargin<5||isempty(errTol)) |
| 209 | + errTol=eps(); |
| 210 | +end |
| 211 | + |
204 | 212 | if(nargin<4||isempty(lambda)) |
205 | | - lambda=0; |
| 213 | + lambda=0; |
206 | 214 | end |
207 | 215 |
|
208 | 216 | %Clip negative values to zero to ensure convergence. |
|
211 | 219 | if(lambda==0)% Central gamma case |
212 | 220 | prob=gammainc(x/theta,k); |
213 | 221 | else |
214 | | - if(nargin<6||isempty(maxIter)) |
215 | | - maxIter=5000; |
216 | | - end |
| 222 | + prob=zeros(size(x)); |
| 223 | + numX=numel(x); |
| 224 | + for curX=1:numX |
| 225 | + xCur=x(curX)/theta;%Divide x by the scale parameter. |
| 226 | + alpha=k;%The shape parameter. |
| 227 | + delta=lambda;%The noncentrality parameter. |
217 | 228 |
|
218 | | - if(nargin<5||isempty(errTol)) |
219 | | - errTol=eps(1); |
220 | | - end |
221 | | - |
222 | | - x = x/theta; |
223 | | - m = ceil(lambda); |
224 | | - a = k+m; |
225 | | - gammap = gammainc(x,a); |
226 | | - gammar = gammap; |
227 | | - gxr = x.^a.*exp(-x)./gamma(a+1)/theta; % Calculate i=m term |
228 | | - gxp = gxr*a./x; |
229 | | - gxp(x==0) = 0; |
230 | | - pp = exp(-lambda)*lambda.^(m)./gamma(m+1); %i=m Poisson weight |
231 | | - pr = pp; |
232 | | - remain = 1-pp; |
233 | | - ii = 1; |
234 | | - cdf = pp*gammap; |
| 229 | + %Mostly using the notation in Section 7 of the paper. |
| 230 | + k=ceil(delta/2); |
| 231 | + a=alpha+k; |
| 232 | + gammac=gammainc(xCur,a); |
| 233 | + gammad=gammac;%Used in the regressive sum. |
| 234 | + gxd=exp(a*log(xCur)-xCur-gammaln(a+1)); |
| 235 | + if(xCur==0) |
| 236 | + gxc=0; |
| 237 | + else |
| 238 | + gxc=gxd*(a/xCur); |
| 239 | + end |
| 240 | + ppoic=exp(-delta/2+k*log(delta/2)-log(gamma(k+1))); |
| 241 | + ppoid=ppoic; |
| 242 | + remain=1-ppoic; |
| 243 | + cdf=ppoic*gammac; |
| 244 | + i=1; |
235 | 245 |
|
236 | | - while(1) |
237 | | - gxp = gxp.*x./(a+ii-1); %Calculate progressive iteration |
238 | | - gammap = gammap-gxp; |
239 | | - pp = pp*lambda/(m+ii); |
240 | | - cdf = cdf + pp*gammap; |
241 | | - err = remain*gammap; |
242 | | - remain = remain - pp; |
243 | | - if(ii > m) |
244 | | - if(max(err)<=errTol||ii>maxIter) |
245 | | - break |
246 | | - end |
247 | | - else |
248 | | - gxr = gxr.*(a-ii)./x; %Calculate regressive iteration |
249 | | - gammar = gammar + gxr; |
250 | | - pr = pr*(m-ii+1)/lambda; |
251 | | - cdf = cdf + pr*gammar; |
252 | | - remain = remain - pr; |
253 | | - if(remain<=errTol||ii>maxIter) |
254 | | - break |
| 246 | + while(1) |
| 247 | + %The progressive (forward) sum. |
| 248 | + gxc=gxc*xCur/(a+i-1); |
| 249 | + gammac=gammac-gxc; |
| 250 | + ppoic=ppoic*(delta/2)/(k+i); |
| 251 | + cdf=cdf+ppoic*gammac; |
| 252 | + error=remain*gammac; |
| 253 | + remain=remain-ppoic; |
| 254 | + if(i>k) |
| 255 | + if((error<=errTol)||(i>maxIter)) |
| 256 | + break; |
| 257 | + end |
| 258 | + else |
| 259 | + %The regressive sum from k. |
| 260 | + gxd=gxd*(a+1-i)/xCur; |
| 261 | + gammad=gammad+gxd; |
| 262 | + ppoid=ppoid*(k-i+1)/(delta/2); |
| 263 | + cdf=cdf+ppoid*gammad; |
| 264 | + remain=remain-ppoid; |
| 265 | + if((remain<=errTol)||(i>maxIter)) |
| 266 | + break; |
| 267 | + end |
255 | 268 | end |
| 269 | + i=i+1; |
256 | 270 | end |
257 | | - ii=ii+1; |
| 271 | + prob(curX)=cdf; |
258 | 272 | end |
259 | | - prob=cdf;%Convergence achieved |
260 | 273 | end |
261 | 274 | end |
262 | 275 |
|
|
0 commit comments