annotate toolboxes/FullBNT-1.0.7/KPMstats/beta_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 = betarnd(a,b,m,n);
Daniel@0 2 %BETARND Random matrices from beta distribution.
Daniel@0 3 % R = BETARND(A,B) returns a matrix of random numbers chosen
Daniel@0 4 % from the beta 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 = BETARND(A,B,M,N) returns an M by N matrix.
Daniel@0 8
Daniel@0 9 % Reference:
Daniel@0 10 % [1] L. Devroye, "Non-Uniform Random Variate Generation",
Daniel@0 11 % Springer-Verlag, 1986
Daniel@0 12
Daniel@0 13 % Copyright (c) 1993-98 by The MathWorks, Inc.
Daniel@0 14 % $Revision: 1.1.1.1 $ $Date: 2005/04/26 02:29:18 $
Daniel@0 15
Daniel@0 16 if nargin < 2,
Daniel@0 17 error('Requires at least two input arguments');
Daniel@0 18 end
Daniel@0 19
Daniel@0 20 if nargin == 2
Daniel@0 21 [errorcode rows columns] = rndcheck(2,2,a,b);
Daniel@0 22 end
Daniel@0 23
Daniel@0 24 if nargin == 3
Daniel@0 25 [errorcode rows columns] = rndcheck(3,2,a,b,m);
Daniel@0 26 end
Daniel@0 27
Daniel@0 28 if nargin == 4
Daniel@0 29 [errorcode rows columns] = rndcheck(4,2,a,b,m,n);
Daniel@0 30 end
Daniel@0 31
Daniel@0 32 if errorcode > 0
Daniel@0 33 error('Size information is inconsistent.');
Daniel@0 34 end
Daniel@0 35
Daniel@0 36 r = zeros(rows,columns);
Daniel@0 37
Daniel@0 38 % Use Theorem 4.1, case A (Devroye, page 430) to derive beta
Daniel@0 39 % random numbers as a ratio of gamma random numbers.
Daniel@0 40 if prod(size(a)) == 1
Daniel@0 41 a1 = a(ones(rows,1),ones(columns,1));
Daniel@0 42 g1 = gamrnd(a1,1);
Daniel@0 43 else
Daniel@0 44 g1 = gamrnd(a,1);
Daniel@0 45 end
Daniel@0 46 if prod(size(b)) == 1
Daniel@0 47 b1 = b(ones(rows,1),ones(columns,1));
Daniel@0 48 g2 = gamrnd(b1,1);
Daniel@0 49 else
Daniel@0 50 g2 = gamrnd(b,1);
Daniel@0 51 end
Daniel@0 52 r = g1 ./ (g1 + g2);
Daniel@0 53
Daniel@0 54 % Return NaN if b is not positive.
Daniel@0 55 if any(any(b <= 0));
Daniel@0 56 if prod(size(b) == 1)
Daniel@0 57 tmp = NaN;
Daniel@0 58 r = tmp(ones(rows,columns));
Daniel@0 59 else
Daniel@0 60 k = find(b <= 0);
Daniel@0 61 tmp = NaN;
Daniel@0 62 r(k) = tmp(ones(size(k)));
Daniel@0 63 end
Daniel@0 64 end
Daniel@0 65
Daniel@0 66 % Return NaN if a is not positive.
Daniel@0 67 if any(any(a <= 0));
Daniel@0 68 if prod(size(a) == 1)
Daniel@0 69 tmp = NaN;
Daniel@0 70 r = tmp(ones(rows,columns));
Daniel@0 71 else
Daniel@0 72 k = find(a <= 0);
Daniel@0 73 tmp = NaN;
Daniel@0 74 r(k) = tmp(ones(size(k)));
Daniel@0 75 end
Daniel@0 76 end