# HG changeset patch # User David Bateman # Date 1271367630 -7200 # Node ID 1c6ff93c025a16a972dae03c9b6efea0535417a8 # Parent e74bff13aa26bdb96a4c572f239cfc1e4715b496 Reimplement the other discrete distribution functions using lookup diff --git a/scripts/ChangeLog b/scripts/ChangeLog --- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,3 +1,9 @@ +2010-04-13 David Bateman + + * 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 * statistics/distributions/stdnormal_cdf.m: Calculate using erfc. diff --git a/scripts/plot/private/__patch__.m b/scripts/plot/private/__patch__.m --- a/scripts/plot/private/__patch__.m +++ b/scripts/plot/private/__patch__.m @@ -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)].'(:)){:}); diff --git a/scripts/statistics/distributions/discrete_cdf.m b/scripts/statistics/distributions/discrete_cdf.m --- a/scripts/statistics/distributions/discrete_cdf.m +++ b/scripts/statistics/distributions/discrete_cdf.m @@ -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 diff --git a/scripts/statistics/distributions/discrete_inv.m b/scripts/statistics/distributions/discrete_inv.m --- a/scripts/statistics/distributions/discrete_inv.m +++ b/scripts/statistics/distributions/discrete_inv.m @@ -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 diff --git a/scripts/statistics/distributions/discrete_pdf.m b/scripts/statistics/distributions/discrete_pdf.m --- a/scripts/statistics/distributions/discrete_pdf.m +++ b/scripts/statistics/distributions/discrete_pdf.m @@ -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