changeset 10524:1c6ff93c025a

Reimplement the other discrete distribution functions using lookup
author David Bateman <dbateman@free.fr>
date Thu, 15 Apr 2010 23:40:30 +0200
parents e74bff13aa26
children 3306cfcb856e
files scripts/ChangeLog scripts/plot/private/__patch__.m scripts/statistics/distributions/discrete_cdf.m scripts/statistics/distributions/discrete_inv.m scripts/statistics/distributions/discrete_pdf.m
diffstat 5 files changed, 15 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Thu Apr 15 21:10:07 2010 +0200
+++ b/scripts/ChangeLog	Thu Apr 15 23:40:30 2010 +0200
@@ -1,3 +1,9 @@
+2010-04-13  David Bateman  <dbateman@free.fr>
+
+	* statistics/discrete_pdf.m: Reimplement using lookup.
+	* statistics/discrete_inv.m: Reimplement using lookup.
+	* statistics/discrete_cdf.m: typo in last patch.
+
 2010-04-15  Jaroslav Hajek  <highegg@gmail.com>
 
 	* statistics/distributions/stdnormal_cdf.m: Calculate using erfc.
--- a/scripts/plot/private/__patch__.m	Thu Apr 15 21:10:07 2010 +0200
+++ b/scripts/plot/private/__patch__.m	Thu Apr 15 23:40:30 2010 +0200
@@ -306,7 +306,7 @@
   if (! recursive)
     recursive = true;
     f = get (h);
-    if (isfvc)
+    if (isfv)
       set (h, setvertexdata ([fieldnames(f), struct2cell(f)].'(:)){:});
     else
       set (h, setdata ([fieldnames(f), struct2cell(f)].'(:)){:});
--- a/scripts/statistics/distributions/discrete_cdf.m	Thu Apr 15 21:10:07 2010 +0200
+++ b/scripts/statistics/distributions/discrete_cdf.m	Thu Apr 15 23:40:30 2010 +0200
@@ -45,16 +45,12 @@
   v = reshape (v, 1, m);
   p = reshape (p / sum (p), m, 1);
 
-  cdf = zeros (sz);
-  k = find (isnan (x));
-  if (any (k))
-    cdf (k) = NaN;
-  endif
+  cdf = NaN (sz);
   k = find (!isnan (x));
   if (any (k))
     n = length (k);
     [vs, vi] = sort (v);
-    cdf = [0 ; cumsum(p(vi))](lookup (vs, x(k)) + 1);
+    cdf(k) = [0 ; cumsum(p(vi))](lookup (vs, x(k)) + 1);
   endif
 
 endfunction
--- a/scripts/statistics/distributions/discrete_inv.m	Thu Apr 15 21:10:07 2010 +0200
+++ b/scripts/statistics/distributions/discrete_inv.m	Thu Apr 15 23:40:30 2010 +0200
@@ -46,13 +46,10 @@
   n = numel (x);
   x = reshape (x, 1, n);
   m = length (v);
-  v = sort (v);
-  s = reshape (cumsum (p / sum (p)), m, 1);
+  [v, idx] = sort (v);
+  p = reshape (cumsum (p (idx) / sum (p)), m, 1);
 
-  ## Allow storage allocated for P to be reclaimed.
-  p = [];
-
-  inv = NaN * ones (sz);
+  inv = NaN (sz);
   if (any (k = find (x == 0)))
     inv(k) = -Inf;
   endif
@@ -62,17 +59,7 @@
 
   if (any (k = find ((x > 0) & (x < 1))))
     n = length (k);
-
-    ## The following loop is a space/time tradeoff in favor of space,
-    ## since the dataset may be large.
-    ##
-    ## Vectorized code is:
-    ##
-    ##     inv(k) = v(sum ((ones (m, 1) * x(k)) > (s * ones (1, n))) + 1);
-
-    for q = 1:n
-      inv(k(q)) = v(sum (x(k(q)) > s) + 1);
-    endfor
+    inv (k) = v(length (p) - lookup (sort (p,"descend"), x(k)) + 1);
   endif
 
 endfunction
--- a/scripts/statistics/distributions/discrete_pdf.m	Thu Apr 15 21:10:07 2010 +0200
+++ b/scripts/statistics/distributions/discrete_pdf.m	Thu Apr 15 23:40:30 2010 +0200
@@ -49,15 +49,11 @@
   v = reshape (v, 1, m);
   p = reshape (p / sum (p), m, 1);
 
-  pdf = zeros (sz);
-  k = find (isnan (x));
-  if (any (k))
-    pdf (k) = NaN;
-  endif
+  pdf = NaN (sz);
   k = find (!isnan (x));
   if (any (k))
     n = length (k);
-    pdf (k) = ((x(k) * ones (1, m)) == (ones (n, 1) * v)) * p;
+    pdf (k) = p (lookup (v, x(k), 'm'));
   endif
 
 endfunction