# HG changeset patch # User dbateman # Date 1172240959 0 # Node ID d285f4ee2b0c6be739046e1a06b992d915966ff1 # Parent 859f7aaea2540532fd4827ac2fb5ed6fcf67d171 [project @ 2007-02-23 14:29:19 by dbateman] diff -r 859f7aaea254 -r d285f4ee2b0c scripts/statistics/distributions/discrete_rnd.m --- a/scripts/statistics/distributions/discrete_rnd.m Fri Feb 23 14:20:42 2007 +0000 +++ b/scripts/statistics/distributions/discrete_rnd.m Fri Feb 23 14:29:19 2007 +0000 @@ -74,24 +74,5 @@ error ("discrete_rnd: p must be a nonzero, nonnegative vector"); endif - n = prod (sz); - m = length (v); - u = rand (1, n); - s = reshape (cumsum (p / sum (p)), m, 1); - - ## The following loop is a space/time tradeoff in favor of space, - ## since the dataset may be large. - ## - ## Vectorized code is: - ## - rnd = v (1 + sum ((s * ones (1, n)) <= ((ones (m, 1) * u)))); - rnd = reshape (rnd, sz); - ## - ## Non-vectorized code is: - ## - ## rnd = zeros (sz); - ## for q=1:n - ## rnd (q) = v (sum (s <= u (q)) + 1); - ## endfor - + rnd = v (lookup (cumsum (p (1 : end-1)) / sum(p), rand (sz)) + 1); endfunction diff -r 859f7aaea254 -r d285f4ee2b0c scripts/statistics/distributions/geornd.m --- a/scripts/statistics/distributions/geornd.m Fri Feb 23 14:20:42 2007 +0000 +++ b/scripts/statistics/distributions/geornd.m Fri Feb 23 14:29:19 2007 +0000 @@ -73,12 +73,12 @@ elseif (p == 0) rnd = Inf * ones (sz); elseif ((p > 0) & (p < 1)); - rnd = floor (log (rand (sz)) / log (1 - p)); + rnd = floor (- rande(sz) ./ log (1 - p)); else rnd = zeros (sz); endif else - rnd = zeros (sz); + rnd = floor (- rande(sz) ./ log (1 - p)); k = find (!(p >= 0) | !(p <= 1)); if (any (k)) @@ -89,11 +89,6 @@ if (any (k)) rnd(k) = Inf * ones (1, length (k)); endif - - k = find ((p > 0) & (p < 1)); - if (any (k)) - rnd(k) = floor (log (rand (size (k))) ./ log (1 - p(k))); - endif endif endfunction diff -r 859f7aaea254 -r d285f4ee2b0c scripts/statistics/distributions/lognrnd.m --- a/scripts/statistics/distributions/lognrnd.m Fri Feb 23 14:20:42 2007 +0000 +++ b/scripts/statistics/distributions/lognrnd.m Fri Feb 23 14:29:19 2007 +0000 @@ -78,23 +78,16 @@ endif if (isscalar (mu) && isscalar (sigma)) - if (!(mu > 0) | !(mu < Inf) | !(sigma > 0) | !(sigma < Inf)) + if (!(sigma > 0) || !(sigma < Inf)) rnd = NaN * ones (sz); - elseif find ((mu > 0) & (mu < Inf) & (sigma > 0) & (sigma < Inf)); - rnd = exp (mu) * exp (sigma .* randn (sz)); else - rnd = zeros (sz); + rnd = exp(mu + sigma .* randn (sz)); endif else - rnd = zeros (sz); - k = find (!(mu > 0) | !(mu < Inf) | !(sigma > 0) | !(sigma < Inf)); + rnd = exp (mu + sigma .* randn (sz)); + k = find ((sigma < 0) | (sigma == Inf)); if (any (k)) - rnd(k) = NaN * ones (1, length (k)); - endif - - k = find ((mu > 0) & (mu < Inf) & (sigma > 0) & (sigma < Inf)); - if (any (k)) - rnd(k) = exp (mu(k)) .* exp (sigma(k) .* randn (1, length (k))); + rnd(k) = NaN endif endif diff -r 859f7aaea254 -r d285f4ee2b0c scripts/statistics/distributions/wblrnd.m --- a/scripts/statistics/distributions/wblrnd.m Fri Feb 23 14:20:42 2007 +0000 +++ b/scripts/statistics/distributions/wblrnd.m Fri Feb 23 14:29:19 2007 +0000 @@ -79,16 +79,15 @@ if (isscalar (shape) && isscalar (scale)) if ((shape > 0) & (shape < Inf) & (scale > 0) & (scale < Inf)) - rnd = (scale * (- log (1 - rand (sz))) .^ (1 / shape)); + rnd = scale .* rande(sz) .^ (1./shape); else rnd = NaN * ones (sz); endif else - rnd = NaN * ones (sz); - k = find ((shape > 0) & (shape < Inf) & (scale > 0) & (scale < Inf)); - if (any (k)) - rnd(k) = (scale(k) - .* (- log (1 - rand (size (k)))) .^ (1 ./ shape(k))); + rnd = scale .* rande(sz) .^ (1./shape); + k = find ((shape <= 0) | (shape == Inf) | (scale <= 0) & (scale == Inf)); + if (any(k)) + rnd(k) = NaN; endif endif