Daniel@0: function r = gamrnd(a,b,m,n); Daniel@0: %GAMRND Random matrices from gamma distribution. Daniel@0: % R = GAMRND(A,B) returns a matrix of random numbers chosen Daniel@0: % from the gamma distribution with parameters A and B. Daniel@0: % The size of R is the common size of A and B if both are matrices. Daniel@0: % If either parameter is a scalar, the size of R is the size of the other Daniel@0: % parameter. Alternatively, R = GAMRND(A,B,M,N) returns an M by N matrix. Daniel@0: % Daniel@0: % Some references refer to the gamma distribution Daniel@0: % with a single parameter. This corresponds to GAMRND Daniel@0: % with B = 1. (See Devroye, pages 401-402.) Daniel@0: Daniel@0: % GAMRND uses a rejection or an inversion method depending on the Daniel@0: % value of A. Daniel@0: Daniel@0: % References: Daniel@0: % [1] L. Devroye, "Non-Uniform Random Variate Generation", Daniel@0: % Springer-Verlag, 1986 Daniel@0: Daniel@0: % B.A. Jones 2-1-93 Daniel@0: % Copyright (c) 1993-98 by The MathWorks, Inc. Daniel@0: % $Revision: 1.1.1.1 $ $Date: 2005/04/26 02:29:18 $ Daniel@0: Daniel@0: if nargin < 2, Daniel@0: error('Requires at least two input arguments.'); Daniel@0: end Daniel@0: Daniel@0: Daniel@0: if nargin == 2 Daniel@0: [errorcode rows columns] = rndcheck(2,2,a,b); Daniel@0: end Daniel@0: Daniel@0: if nargin == 3 Daniel@0: [errorcode rows columns] = rndcheck(3,2,a,b,m); Daniel@0: end Daniel@0: Daniel@0: if nargin == 4 Daniel@0: [errorcode rows columns] = rndcheck(4,2,a,b,m,n); Daniel@0: end Daniel@0: Daniel@0: if errorcode > 0 Daniel@0: error('Size information is inconsistent.'); Daniel@0: end Daniel@0: Daniel@0: % Initialize r to zero. Daniel@0: lth = rows*columns; Daniel@0: r = zeros(lth,1); Daniel@0: a = a(:); b = b(:); Daniel@0: Daniel@0: scalara = (length(a) == 1); Daniel@0: if scalara Daniel@0: a = a*ones(lth,1); Daniel@0: end Daniel@0: Daniel@0: scalarb = (length(b) == 1); Daniel@0: if scalarb Daniel@0: b = b*ones(lth,1); Daniel@0: end Daniel@0: Daniel@0: % If a == 1, then gamma is exponential. (Devroye, page 405). Daniel@0: k = find(a == 1); Daniel@0: if any(k) Daniel@0: r(k) = -b(k) .* log(rand(size(k))); Daniel@0: end Daniel@0: Daniel@0: Daniel@0: k = find(a < 1 & a > 0); Daniel@0: % (Devroye, page 418 Johnk's generator) Daniel@0: if any(k) Daniel@0: c = zeros(lth,1); Daniel@0: d = zeros(lth,1); Daniel@0: c(k) = 1 ./ a(k); Daniel@0: d(k) = 1 ./ (1 - a(k)); Daniel@0: accept = k; Daniel@0: while ~isempty(accept) Daniel@0: u = rand(size(accept)); Daniel@0: v = rand(size(accept)); Daniel@0: x = u .^ c(accept); Daniel@0: y = v .^ d(accept); Daniel@0: k1 = find((x + y) <= 1); Daniel@0: if ~isempty(k1) Daniel@0: e = -log(rand(size(k1))); Daniel@0: r(accept(k1)) = e .* x(k1) ./ (x(k1) + y(k1)); Daniel@0: accept(k1) = []; Daniel@0: end Daniel@0: end Daniel@0: r(k) = r(k) .* b(k); Daniel@0: end Daniel@0: Daniel@0: % Use a rejection method for a > 1. Daniel@0: k = find(a > 1); Daniel@0: % (Devroye, page 410 Best's algorithm) Daniel@0: bb = zeros(size(a)); Daniel@0: c = bb; Daniel@0: if any(k) Daniel@0: bb(k) = a(k) - 1; Daniel@0: c(k) = 3 * a(k) - 3/4; Daniel@0: accept = k; Daniel@0: count = 1; Daniel@0: while ~isempty(accept) Daniel@0: m = length(accept); Daniel@0: u = rand(m,1); Daniel@0: v = rand(m,1); Daniel@0: w = u .* (1 - u); Daniel@0: y = sqrt(c(accept) ./ w) .* (u - 0.5); Daniel@0: x = bb(accept) + y; Daniel@0: k1 = find(x >= 0); Daniel@0: if ~isempty(k1) Daniel@0: z = 64 * (w .^ 3) .* (v .^ 2); Daniel@0: k2 = (z(k1) <= (1 - 2 * (y(k1) .^2) ./ x(k1))); Daniel@0: k3 = k1(find(k2)); Daniel@0: r(accept(k3)) = x(k3); Daniel@0: k4 = k1(find(~k2)); Daniel@0: k5 = k4(find(log(z(k4)) <= (2*(bb(accept(k4)).*log(x(k4)./bb(accept(k4)))-y(k4))))); Daniel@0: r(accept(k5)) = x(k5); Daniel@0: omit = [k3; k5]; Daniel@0: accept(omit) = []; Daniel@0: end Daniel@0: end Daniel@0: r(k) = r(k) .* b(k); Daniel@0: end Daniel@0: Daniel@0: % Return NaN if a or b is not positive. Daniel@0: r(b <= 0 | a <= 0) = NaN; Daniel@0: Daniel@0: r = reshape(r,rows,columns);