]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/base/qqplot.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / base / qqplot.m
1 ## Copyright (C) 1995-2012 Kurt Hornik
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn  {Function File} {[@var{q}, @var{s}] =} qqplot (@var{x})
21 ## @deftypefnx {Function File} {[@var{q}, @var{s}] =} qqplot (@var{x}, @var{dist})
22 ## @deftypefnx {Function File} {[@var{q}, @var{s}] =} qqplot (@var{x}, @var{dist}, @var{params})
23 ## @deftypefnx {Function File} {} qqplot (@dots{})
24 ## Perform a QQ-plot (quantile plot).
25 ##
26 ## If F is the CDF of the distribution @var{dist} with parameters
27 ## @var{params} and G its inverse, and @var{x} a sample vector of length
28 ## @var{n}, the QQ-plot graphs ordinate @var{s}(@var{i}) = @var{i}-th
29 ## largest element of x versus abscissa @var{q}(@var{i}f) = G((@var{i} -
30 ## 0.5)/@var{n}).
31 ##
32 ## If the sample comes from F, except for a transformation of location
33 ## and scale, the pairs will approximately follow a straight line.
34 ##
35 ## The default for @var{dist} is the standard normal distribution.  The
36 ## optional argument @var{params} contains a list of parameters of
37 ## @var{dist}.  For example, for a quantile plot of the uniform
38 ## distribution on [2,4] and @var{x}, use
39 ##
40 ## @example
41 ## qqplot (x, "unif", 2, 4)
42 ## @end example
43 ##
44 ## @noindent
45 ## @var{dist} can be any string for which a function @var{distinv} or
46 ## @var{dist_inv} exists that calculates the inverse CDF of distribution
47 ## @var{dist}.
48 ##
49 ## If no output arguments are given, the data are plotted directly.
50 ## @end deftypefn
51
52 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
53 ## Description: Perform a QQ-plot (quantile plot)
54
55 function [q, s] = qqplot (x, dist, varargin)
56
57   if (nargin < 1)
58     print_usage ();
59   endif
60
61   if (!(isnumeric (x) && isvector(x)))
62     error ("qqplot: X must be a numeric vector");
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;
75
76   s = sort (x);
77   n = length (x);
78   t = ((1 : n)' - .5) / n;
79   if (nargin <= 2)
80     q = feval (f, t);
81     q_label = func2str (f);
82   else
83     q = feval (f, t, varargin{:});
84     if (nargin > 3)
85       tmp = sprintf (", %g", varargin{2:end});
86     else
87       tmp = "";
88     endif
89     q_label = sprintf ("%s with parameter(s) %g%s",
90                         func2str (f),        varargin{1}, tmp);
91   endif
92
93   if (nargout == 0)
94     plot (q, s);
95     xlabel (q_label);
96     ylabel ("sample points");
97   endif
98
99 endfunction