Mercurial > hg > octave-lyh
comparison scripts/statistics/base/qqplot.m @ 11436:e151e23f73bc
Overhaul base statistics functions and documentation of same.
Add or improve input validation.
Add input validation tests.
Add functional tests.
Improve or re-write documentation strings.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Mon, 03 Jan 2011 21:23:08 -0800 |
parents | 95c3e38098bf |
children | fd0a3ac60b0e |
comparison
equal
deleted
inserted
replaced
11435:20f53b3a558f | 11436:e151e23f73bc |
---|---|
16 ## You should have received a copy of the GNU General Public License | 16 ## You should have received a copy of the GNU General Public License |
17 ## along with Octave; see the file COPYING. If not, see | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | 18 ## <http://www.gnu.org/licenses/>. |
19 | 19 |
20 ## -*- texinfo -*- | 20 ## -*- texinfo -*- |
21 ## @deftypefn {Function File} {[@var{q}, @var{s}] =} qqplot (@var{x}, @var{dist}, @var{params}) | 21 ## @deftypefn {Function File} {[@var{q}, @var{s}] =} qqplot (@var{x}) |
22 ## @deftypefnx {Function File} {[@var{q}, @var{s}] =} qqplot (@var{x}, @var{dist}) | |
23 ## @deftypefnx {Function File} {[@var{q}, @var{s}] =} qqplot (@var{x}, @var{dist}, @var{params}) | |
22 ## Perform a QQ-plot (quantile plot). | 24 ## Perform a QQ-plot (quantile plot). |
23 ## | 25 ## |
24 ## If F is the CDF of the distribution @var{dist} with parameters | 26 ## If F is the CDF of the distribution @var{dist} with parameters |
25 ## @var{params} and G its inverse, and @var{x} a sample vector of length | 27 ## @var{params} and G its inverse, and @var{x} a sample vector of length |
26 ## @var{n}, the QQ-plot graphs ordinate @var{s}(@var{i}) = @var{i}-th | 28 ## @var{n}, the QQ-plot graphs ordinate @var{s}(@var{i}) = @var{i}-th |
27 ## largest element of x versus abscissa @var{q}(@var{i}f) = G((@var{i} - | 29 ## largest element of x versus abscissa @var{q}(@var{i}f) = G((@var{i} - |
28 ## 0.5)/@var{n}). | 30 ## 0.5)/@var{n}). |
29 ## | 31 ## |
30 ## If the sample comes from F except for a transformation of location | 32 ## If the sample comes from F, except for a transformation of location |
31 ## and scale, the pairs will approximately follow a straight line. | 33 ## and scale, the pairs will approximately follow a straight line. |
32 ## | 34 ## |
33 ## The default for @var{dist} is the standard normal distribution. The | 35 ## The default for @var{dist} is the standard normal distribution. The |
34 ## optional argument @var{params} contains a list of parameters of | 36 ## optional argument @var{params} contains a list of parameters of |
35 ## @var{dist}. For example, for a quantile plot of the uniform | 37 ## @var{dist}. For example, for a quantile plot of the uniform |
38 ## @example | 40 ## @example |
39 ## qqplot (x, "uniform", 2, 4) | 41 ## qqplot (x, "uniform", 2, 4) |
40 ## @end example | 42 ## @end example |
41 ## | 43 ## |
42 ## @noindent | 44 ## @noindent |
43 ## @var{dist} can be any string for which a function @var{dist_inv} | 45 ## @var{dist} can be any string for which a function @var{distinv} or |
44 ## that calculates the inverse CDF of distribution @var{dist} exists. | 46 ## @var{dist_inv} exists that calculates the inverse CDF of distribution |
47 ## @var{dist}. | |
45 ## | 48 ## |
46 ## If no output arguments are given, the data are plotted directly. | 49 ## If no output arguments are given, the data are plotted directly. |
47 ## @end deftypefn | 50 ## @end deftypefn |
48 | 51 |
49 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> | 52 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
53 | 56 |
54 if (nargin < 1) | 57 if (nargin < 1) |
55 print_usage (); | 58 print_usage (); |
56 endif | 59 endif |
57 | 60 |
58 if (! (isvector(x))) | 61 if (!(isnumeric (x) && isvector(x))) |
59 error ("qqplot: x must be a vector"); | 62 error ("qqplot: X must be a numeric vector"); |
60 endif | 63 endif |
64 | |
65 if (nargin == 1) | |
66 f = @stdnormal_inv; | |
67 else | |
68 if ( exist (invname = sprintf ("%sinv", dist)) | |
69 || exist (invname = sprintf ("%s_inv", dist))) | |
70 f = str2func (invname); | |
71 else | |
72 error ("qqplot: no inverse CDF found for distribution DIST"); | |
73 endif | |
74 endif; | |
61 | 75 |
62 s = sort (x); | 76 s = sort (x); |
63 n = length (x); | 77 n = length (x); |
64 t = ((1 : n)' - .5) / n; | 78 t = ((1 : n)' - .5) / n; |
65 if (nargin == 1) | |
66 f = @stdnormal_inv; | |
67 else | |
68 f = str2func (sprintf ("%s_inv", dist)); | |
69 endif; | |
70 if (nargin <= 2) | 79 if (nargin <= 2) |
71 q = feval (f, t); | 80 q = feval (f, t); |
72 q_label = func2str (f); | 81 q_label = func2str (f); |
73 else | 82 else |
74 q = feval (f, t, varargin{:}); | 83 q = feval (f, t, varargin{:}); |
75 if (nargin > 3) | 84 if (nargin > 3) |
76 tmp = sprintf (", %g", varargin{2:end}); | 85 tmp = sprintf (", %g", varargin{2:end}); |
77 else | 86 else |
78 tmp = ""; | 87 tmp = ""; |
79 endif | 88 endif |
80 q_label = sprintf ("%s with parameter(s) %g%s", func2str (f), | 89 q_label = sprintf ("%s with parameter(s) %g%s", |
81 varargin{1}, tmp); | 90 func2str (f), varargin{1}, tmp); |
82 endif | 91 endif |
83 | 92 |
84 if (nargout == 0) | 93 if (nargout == 0) |
85 plot (q, s); | 94 plot (q, s); |
86 xlabel (q_label); | 95 xlabel (q_label); |