annotate toolboxes/FullBNT-1.0.7/KPMstats/gamma_sample.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function r = gamrnd(a,b,m,n);
Daniel@0 2 %GAMRND Random matrices from gamma distribution.
Daniel@0 3 % R = GAMRND(A,B) returns a matrix of random numbers chosen
Daniel@0 4 % from the gamma distribution with parameters A and B.
Daniel@0 5 % The size of R is the common size of A and B if both are matrices.
Daniel@0 6 % If either parameter is a scalar, the size of R is the size of the other
Daniel@0 7 % parameter. Alternatively, R = GAMRND(A,B,M,N) returns an M by N matrix.
Daniel@0 8 %
Daniel@0 9 % Some references refer to the gamma distribution
Daniel@0 10 % with a single parameter. This corresponds to GAMRND
Daniel@0 11 % with B = 1. (See Devroye, pages 401-402.)
Daniel@0 12
Daniel@0 13 % GAMRND uses a rejection or an inversion method depending on the
Daniel@0 14 % value of A.
Daniel@0 15
Daniel@0 16 % References:
Daniel@0 17 % [1] L. Devroye, "Non-Uniform Random Variate Generation",
Daniel@0 18 % Springer-Verlag, 1986
Daniel@0 19
Daniel@0 20 % B.A. Jones 2-1-93
Daniel@0 21 % Copyright (c) 1993-98 by The MathWorks, Inc.
Daniel@0 22 % $Revision: 1.1.1.1 $ $Date: 2005/04/26 02:29:18 $
Daniel@0 23
Daniel@0 24 if nargin < 2,
Daniel@0 25 error('Requires at least two input arguments.');
Daniel@0 26 end
Daniel@0 27
Daniel@0 28
Daniel@0 29 if nargin == 2
Daniel@0 30 [errorcode rows columns] = rndcheck(2,2,a,b);
Daniel@0 31 end
Daniel@0 32
Daniel@0 33 if nargin == 3
Daniel@0 34 [errorcode rows columns] = rndcheck(3,2,a,b,m);
Daniel@0 35 end
Daniel@0 36
Daniel@0 37 if nargin == 4
Daniel@0 38 [errorcode rows columns] = rndcheck(4,2,a,b,m,n);
Daniel@0 39 end
Daniel@0 40
Daniel@0 41 if errorcode > 0
Daniel@0 42 error('Size information is inconsistent.');
Daniel@0 43 end
Daniel@0 44
Daniel@0 45 % Initialize r to zero.
Daniel@0 46 lth = rows*columns;
Daniel@0 47 r = zeros(lth,1);
Daniel@0 48 a = a(:); b = b(:);
Daniel@0 49
Daniel@0 50 scalara = (length(a) == 1);
Daniel@0 51 if scalara
Daniel@0 52 a = a*ones(lth,1);
Daniel@0 53 end
Daniel@0 54
Daniel@0 55 scalarb = (length(b) == 1);
Daniel@0 56 if scalarb
Daniel@0 57 b = b*ones(lth,1);
Daniel@0 58 end
Daniel@0 59
Daniel@0 60 % If a == 1, then gamma is exponential. (Devroye, page 405).
Daniel@0 61 k = find(a == 1);
Daniel@0 62 if any(k)
Daniel@0 63 r(k) = -b(k) .* log(rand(size(k)));
Daniel@0 64 end
Daniel@0 65
Daniel@0 66
Daniel@0 67 k = find(a < 1 & a > 0);
Daniel@0 68 % (Devroye, page 418 Johnk's generator)
Daniel@0 69 if any(k)
Daniel@0 70 c = zeros(lth,1);
Daniel@0 71 d = zeros(lth,1);
Daniel@0 72 c(k) = 1 ./ a(k);
Daniel@0 73 d(k) = 1 ./ (1 - a(k));
Daniel@0 74 accept = k;
Daniel@0 75 while ~isempty(accept)
Daniel@0 76 u = rand(size(accept));
Daniel@0 77 v = rand(size(accept));
Daniel@0 78 x = u .^ c(accept);
Daniel@0 79 y = v .^ d(accept);
Daniel@0 80 k1 = find((x + y) <= 1);
Daniel@0 81 if ~isempty(k1)
Daniel@0 82 e = -log(rand(size(k1)));
Daniel@0 83 r(accept(k1)) = e .* x(k1) ./ (x(k1) + y(k1));
Daniel@0 84 accept(k1) = [];
Daniel@0 85 end
Daniel@0 86 end
Daniel@0 87 r(k) = r(k) .* b(k);
Daniel@0 88 end
Daniel@0 89
Daniel@0 90 % Use a rejection method for a > 1.
Daniel@0 91 k = find(a > 1);
Daniel@0 92 % (Devroye, page 410 Best's algorithm)
Daniel@0 93 bb = zeros(size(a));
Daniel@0 94 c = bb;
Daniel@0 95 if any(k)
Daniel@0 96 bb(k) = a(k) - 1;
Daniel@0 97 c(k) = 3 * a(k) - 3/4;
Daniel@0 98 accept = k;
Daniel@0 99 count = 1;
Daniel@0 100 while ~isempty(accept)
Daniel@0 101 m = length(accept);
Daniel@0 102 u = rand(m,1);
Daniel@0 103 v = rand(m,1);
Daniel@0 104 w = u .* (1 - u);
Daniel@0 105 y = sqrt(c(accept) ./ w) .* (u - 0.5);
Daniel@0 106 x = bb(accept) + y;
Daniel@0 107 k1 = find(x >= 0);
Daniel@0 108 if ~isempty(k1)
Daniel@0 109 z = 64 * (w .^ 3) .* (v .^ 2);
Daniel@0 110 k2 = (z(k1) <= (1 - 2 * (y(k1) .^2) ./ x(k1)));
Daniel@0 111 k3 = k1(find(k2));
Daniel@0 112 r(accept(k3)) = x(k3);
Daniel@0 113 k4 = k1(find(~k2));
Daniel@0 114 k5 = k4(find(log(z(k4)) <= (2*(bb(accept(k4)).*log(x(k4)./bb(accept(k4)))-y(k4)))));
Daniel@0 115 r(accept(k5)) = x(k5);
Daniel@0 116 omit = [k3; k5];
Daniel@0 117 accept(omit) = [];
Daniel@0 118 end
Daniel@0 119 end
Daniel@0 120 r(k) = r(k) .* b(k);
Daniel@0 121 end
Daniel@0 122
Daniel@0 123 % Return NaN if a or b is not positive.
Daniel@0 124 r(b <= 0 | a <= 0) = NaN;
Daniel@0 125
Daniel@0 126 r = reshape(r,rows,columns);