Mercurial > octave-nkf
comparison scripts/statistics/distributions/betainv.m @ 19867:9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Try to trim long lines to < 80 chars.
Use '##' for single line comments.
Use '(...)' around tests for if/elseif/switch/while.
Abut cell indexing operator '{' next to variable.
Abut array indexing operator '(' next to variable.
Use space between negation operator '!' and following expression.
Use two newlines between endfunction and start of %!test or %!demo code.
Remove unnecessary parens grouping between short-circuit operators.
Remove stray extra spaces (typos) between variables and assignment operators.
Remove stray extra spaces from ends of lines.
author | Rik <rik@octave.org> |
---|---|
date | Mon, 23 Feb 2015 14:54:39 -0800 |
parents | 4197fc428c7d |
children | d9341b422488 |
comparison
equal
deleted
inserted
replaced
19866:a1acca0c2216 | 19867:9fc020886ae9 |
---|---|
31 | 31 |
32 if (nargin != 3) | 32 if (nargin != 3) |
33 print_usage (); | 33 print_usage (); |
34 endif | 34 endif |
35 | 35 |
36 if (!isscalar (a) || !isscalar (b)) | 36 if (! isscalar (a) || ! isscalar (b)) |
37 [retval, x, a, b] = common_size (x, a, b); | 37 [retval, x, a, b] = common_size (x, a, b); |
38 if (retval > 0) | 38 if (retval > 0) |
39 error ("betainv: X, A, and B must be of common size or scalars"); | 39 error ("betainv: X, A, and B must be of common size or scalars"); |
40 endif | 40 endif |
41 endif | 41 endif |
56 k = (x == 1) & (a > 0) & (b > 0); | 56 k = (x == 1) & (a > 0) & (b > 0); |
57 inv(k) = 1; | 57 inv(k) = 1; |
58 | 58 |
59 k = find ((x > 0) & (x < 1) & (a > 0) & (b > 0)); | 59 k = find ((x > 0) & (x < 1) & (a > 0) & (b > 0)); |
60 if (any (k)) | 60 if (any (k)) |
61 if (!isscalar (a) || !isscalar (b)) | 61 if (! isscalar (a) || ! isscalar (b)) |
62 a = a(k); | 62 a = a(k); |
63 b = b(k); | 63 b = b(k); |
64 y = a ./ (a + b); | 64 y = a ./ (a + b); |
65 else | 65 else |
66 y = a / (a + b) * ones (size (k)); | 66 y = a / (a + b) * ones (size (k)); |
86 for i = 1 : 10000 | 86 for i = 1 : 10000 |
87 h = (betacdf (y_old, a, b) - x) ./ betapdf (y_old, a, b); | 87 h = (betacdf (y_old, a, b) - x) ./ betapdf (y_old, a, b); |
88 y_new = y_old - h; | 88 y_new = y_old - h; |
89 ind = find (y_new <= myeps); | 89 ind = find (y_new <= myeps); |
90 if (any (ind)) | 90 if (any (ind)) |
91 y_new (ind) = y_old (ind) / 10; | 91 y_new(ind) = y_old(ind) / 10; |
92 endif | 92 endif |
93 ind = find (y_new >= 1 - myeps); | 93 ind = find (y_new >= 1 - myeps); |
94 if (any (ind)) | 94 if (any (ind)) |
95 y_new (ind) = 1 - (1 - y_old (ind)) / 10; | 95 y_new(ind) = 1 - (1 - y_old(ind)) / 10; |
96 endif | 96 endif |
97 h = y_old - y_new; | 97 h = y_old - y_new; |
98 if (max (abs (h)) < sqrt (myeps)) | 98 if (max (abs (h)) < sqrt (myeps)) |
99 break; | 99 break; |
100 endif | 100 endif |
114 %!assert (betainv (x, ones (1,5), 2), [NaN 0 0.5 1 NaN]) | 114 %!assert (betainv (x, ones (1,5), 2), [NaN 0 0.5 1 NaN]) |
115 %!assert (betainv (x, [1 0 NaN 1 1], 2), [NaN NaN NaN 1 NaN]) | 115 %!assert (betainv (x, [1 0 NaN 1 1], 2), [NaN NaN NaN 1 NaN]) |
116 %!assert (betainv (x, 1, 2*[1 0 NaN 1 1]), [NaN NaN NaN 1 NaN]) | 116 %!assert (betainv (x, 1, 2*[1 0 NaN 1 1]), [NaN NaN NaN 1 NaN]) |
117 %!assert (betainv ([x(1:2) NaN x(4:5)], 1, 2), [NaN 0 NaN 1 NaN]) | 117 %!assert (betainv ([x(1:2) NaN x(4:5)], 1, 2), [NaN 0 NaN 1 NaN]) |
118 | 118 |
119 %% Test class of input preserved | 119 ## Test class of input preserved |
120 %!assert (betainv ([x, NaN], 1, 2), [NaN 0 0.5 1 NaN NaN]) | 120 %!assert (betainv ([x, NaN], 1, 2), [NaN 0 0.5 1 NaN NaN]) |
121 %!assert (betainv (single ([x, NaN]), 1, 2), single ([NaN 0 0.5 1 NaN NaN])) | 121 %!assert (betainv (single ([x, NaN]), 1, 2), single ([NaN 0 0.5 1 NaN NaN])) |
122 %!assert (betainv ([x, NaN], single (1), 2), single ([NaN 0 0.5 1 NaN NaN])) | 122 %!assert (betainv ([x, NaN], single (1), 2), single ([NaN 0 0.5 1 NaN NaN])) |
123 %!assert (betainv ([x, NaN], 1, single (2)), single ([NaN 0 0.5 1 NaN NaN])) | 123 %!assert (betainv ([x, NaN], 1, single (2)), single ([NaN 0 0.5 1 NaN NaN])) |
124 | 124 |
125 %% Test input validation | 125 ## Test input validation |
126 %!error betainv () | 126 %!error betainv () |
127 %!error betainv (1) | 127 %!error betainv (1) |
128 %!error betainv (1,2) | 128 %!error betainv (1,2) |
129 %!error betainv (1,2,3,4) | 129 %!error betainv (1,2,3,4) |
130 %!error betainv (ones (3), ones (2), ones (2)) | 130 %!error betainv (ones (3), ones (2), ones (2)) |