changeset 6350:d285f4ee2b0c

[project @ 2007-02-23 14:29:19 by dbateman]
author dbateman
date Fri, 23 Feb 2007 14:29:19 +0000
parents 859f7aaea254
children 9b525feab43b
files scripts/statistics/distributions/discrete_rnd.m scripts/statistics/distributions/geornd.m scripts/statistics/distributions/lognrnd.m scripts/statistics/distributions/wblrnd.m
diffstat 4 files changed, 13 insertions(+), 45 deletions(-) [+]
line wrap: on
line diff
--- 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
--- 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
--- 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
 
--- 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