comparison scripts/statistics/distributions/tpdf.m @ 13171:19b9f17d22af

Overhaul of statistical distribution functions Support class "single" 75% reduction in memory usage More Matlab compatibility for corner cases * betacdf.m, betainv.m, betapdf.m, betarnd.m, binocdf.m, binoinv.m, binopdf.m, binornd.m, cauchy_cdf.m, cauchy_inv.m, cauchy_pdf.m, cauchy_rnd.m, chi2cdf.m, chi2inv.m, chi2pdf.m, chi2rnd.m, discrete_cdf.m, discrete_inv.m, discrete_pdf.m, discrete_rnd.m, empirical_cdf.m, empirical_inv.m, empirical_pdf.m, empirical_rnd.m, expcdf.m, expinv.m, exppdf.m, exprnd.m, fcdf.m, finv.m, fpdf.m, frnd.m, gamcdf.m, gaminv.m, gampdf.m, gamrnd.m, geocdf.m, geoinv.m, geopdf.m, geornd.m, hygecdf.m, hygeinv.m, hygepdf.m, hygernd.m, kolmogorov_smirnov_cdf.m, laplace_cdf.m, laplace_inv.m, laplace_pdf.m, laplace_rnd.m, logistic_cdf.m, logistic_inv.m, logistic_pdf.m, logistic_rnd.m, logncdf.m, logninv.m, lognpdf.m, lognrnd.m, nbincdf.m, nbininv.m, nbinpdf.m, nbinrnd.m, normcdf.m, norminv.m, normpdf.m, normrnd.m, poisscdf.m, poissinv.m, poisspdf.m, poissrnd.m, stdnormal_cdf.m, stdnormal_inv.m, stdnormal_pdf.m, stdnormal_rnd.m, tcdf.m, tinv.m, tpdf.m, trnd.m, unidcdf.m, unidinv.m, unidpdf.m, unidrnd.m, unifcdf.m, unifinv.m, unifpdf.m, unifrnd.m, wblcdf.m, wblinv.m, wblpdf.m, wblrnd.m: Return "single" outputs for "single" inputs, Use logical indexing rather than find() for 75% memory savings, Add tests for all functions, Use consistent documentation across all functions, More Matlab compatibilitcy for corner cases.
author Rik <octave@nomad.inbox5.com>
date Tue, 20 Sep 2011 12:13:13 -0700
parents c792872f8942
children 583830ce6afa
comparison
equal deleted inserted replaced
13169:078729410a0d 13171:19b9f17d22af
1 ## Copyright (C) 2011 Rik Wehbring
1 ## Copyright (C) 1995-2011 Kurt Hornik 2 ## Copyright (C) 1995-2011 Kurt Hornik
2 ## 3 ##
3 ## This file is part of Octave. 4 ## This file is part of Octave.
4 ## 5 ##
5 ## Octave is free software; you can redistribute it and/or modify it 6 ## Octave is free software; you can redistribute it and/or modify it
33 endif 34 endif
34 35
35 if (!isscalar (n)) 36 if (!isscalar (n))
36 [retval, x, n] = common_size (x, n); 37 [retval, x, n] = common_size (x, n);
37 if (retval > 0) 38 if (retval > 0)
38 error ("tpdf: X and N must be of common size or scalar"); 39 error ("tpdf: X and N must be of common size or scalars");
39 endif 40 endif
40 endif 41 endif
41 42
42 pdf = zeros (size (x)); 43 if (iscomplex (x) || iscomplex (n))
43 44 error ("tpdf: X and N must not be complex");
44 k = find (isnan (x) | !(n > 0) | !(n < Inf));
45 if (any (k))
46 pdf(k) = NaN;
47 endif 45 endif
48 46
49 k = find (!isinf (x) & !isnan (x) & (n > 0) & (n < Inf)); 47 if (isa (x, "single") || isa (n, "single"))
50 if (any (k)) 48 pdf = zeros (size (x), "single");
51 if (isscalar (n)) 49 else
52 pdf(k) = (exp (- (n + 1) .* log (1 + x(k) .^ 2 ./ n)/2) 50 pdf = zeros (size (x));
53 / (sqrt (n) * beta (n/2, 1/2))); 51 endif
54 else 52
55 pdf(k) = (exp (- (n(k) + 1) .* log (1 + x(k) .^ 2 ./ n(k))/2) 53 k = isnan (x) | !(n > 0) | !(n < Inf);
56 ./ (sqrt (n(k)) .* beta (n(k)/2, 1/2))); 54 pdf(k) = NaN;
57 endif 55
56 k = !isinf (x) & !isnan (x) & (n > 0) & (n < Inf);
57 if (isscalar (n))
58 pdf(k) = (exp (- (n + 1) * log (1 + x(k) .^ 2 / n)/2)
59 / (sqrt (n) * beta (n/2, 1/2)));
60 else
61 pdf(k) = (exp (- (n(k) + 1) .* log (1 + x(k) .^ 2 ./ n(k))/2)
62 ./ (sqrt (n(k)) .* beta (n(k)/2, 1/2)));
58 endif 63 endif
59 64
60 endfunction 65 endfunction
66
67
68 %!test
69 %! x = rand (10,1);
70 %! y = 1./(pi * (1 + x.^2));
71 %! assert(tpdf (x, 1), y, 5*eps);
72
73 %!shared x,y
74 %! x = [-Inf 0 0.5 1 Inf];
75 %! y = 1./(pi * (1 + x.^2));
76 %!assert(tpdf (x, ones(1,5)), y, eps);
77 %!assert(tpdf (x, 1), y, eps);
78 %!assert(tpdf (x, [0 NaN 1 1 1]), [NaN NaN y(3:5)], eps);
79
80 %% Test class of input preserved
81 %!assert(tpdf ([x, NaN], 1), [y, NaN]);
82 %!assert(tpdf (single([x, NaN]), 1), single([y, NaN]), eps("single"));
83 %!assert(tpdf ([x, NaN], single(1)), single([y, NaN]), eps("single"));
84
85 %% Test input validation
86 %!error tpdf ()
87 %!error tpdf (1)
88 %!error tpdf (1,2,3)
89 %!error tpdf (ones(3),ones(2))
90 %!error tpdf (ones(2),ones(3))
91 %!error tpdf (i, 2)
92 %!error tpdf (2, i)
93