7017
|
1 ## Copyright (C) 1995, 1996, 1997, 1998, 2000, 2001, 2002, 2003, 2005, |
|
2 ## 2006, 2007 Kurt Hornik |
3426
|
3 ## |
3922
|
4 ## This file is part of Octave. |
|
5 ## |
|
6 ## Octave is free software; you can redistribute it and/or modify it |
|
7 ## under the terms of the GNU General Public License as published by |
7016
|
8 ## the Free Software Foundation; either version 3 of the License, or (at |
|
9 ## your option) any later version. |
3426
|
10 ## |
3922
|
11 ## Octave is distributed in the hope that it will be useful, but |
3200
|
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3426
|
14 ## General Public License for more details. |
|
15 ## |
3200
|
16 ## You should have received a copy of the GNU General Public License |
7016
|
17 ## along with Octave; see the file COPYING. If not, see |
|
18 ## <http://www.gnu.org/licenses/>. |
3200
|
19 |
3453
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {[@var{q}, @var{s}] =} qqplot (@var{x}, @var{dist}, @var{params}) |
|
22 ## Perform a QQ-plot (quantile plot). |
3200
|
23 ## |
3453
|
24 ## 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 |
|
26 ## @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} - |
|
28 ## 0.5)/@var{n}). |
3200
|
29 ## |
|
30 ## If the sample comes from F except for a transformation of location |
|
31 ## and scale, the pairs will approximately follow a straight line. |
|
32 ## |
3453
|
33 ## The default for @var{dist} is the standard normal distribution. The |
|
34 ## optional argument @var{params} contains a list of parameters of |
|
35 ## @var{dist}. For example, for a quantile plot of the uniform |
|
36 ## distribution on [2,4] and @var{x}, use |
|
37 ## |
|
38 ## @example |
|
39 ## qqplot (x, "uniform", 2, 4) |
|
40 ## @end example |
3200
|
41 ## |
6754
|
42 ## @noindent |
|
43 ## @var{dist} can be any string for which a function @var{dist_inv} |
|
44 ## that calculates the inverse CDF of distribution @var{dist} exists. |
|
45 ## |
3200
|
46 ## If no output arguments are given, the data are plotted directly. |
3453
|
47 ## @end deftypefn |
3200
|
48 |
5428
|
49 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3456
|
50 ## Description: Perform a QQ-plot (quantile plot) |
3200
|
51 |
3979
|
52 function [q, s] = qqplot (x, dist, varargin) |
3426
|
53 |
3200
|
54 if (nargin < 1) |
6046
|
55 print_usage (); |
3200
|
56 endif |
3426
|
57 |
4030
|
58 if (! (isvector(x))) |
3458
|
59 error ("qqplot: x must be a vector"); |
3200
|
60 endif |
|
61 |
|
62 s = sort (x); |
|
63 n = length (x); |
|
64 t = ((1 : n)' - .5) / n; |
|
65 if (nargin == 1) |
4348
|
66 f = @stdnormal_inv; |
3200
|
67 else |
4348
|
68 f = str2func (sprintf ("%s_inv", dist)); |
3200
|
69 endif; |
|
70 if (nargin <= 2) |
|
71 q = feval (f, t); |
4348
|
72 q_label = func2str (f); |
3200
|
73 else |
5153
|
74 q = feval (f, t, varargin{:}); |
|
75 if (nargin > 3) |
|
76 tmp = sprintf (", %g", varargin{2:end}); |
|
77 else |
|
78 tmp = ""; |
|
79 endif |
|
80 q_label = sprintf ("%s with parameter(s) %g%s", func2str (f), |
|
81 varargin{1}, tmp); |
3200
|
82 endif |
3426
|
83 |
3200
|
84 if (nargout == 0) |
6447
|
85 plot (q, s); |
3200
|
86 xlabel (q_label); |
|
87 ylabel ("sample points"); |
|
88 endif |
|
89 |
|
90 endfunction |