annotate scripts/statistics/distributions/tinv.m @ 10549:95c3e38098bf

Untabify .m scripts
author Rik <code@nomad.inbox5.com>
date Fri, 23 Apr 2010 11:28:50 -0700
parents 16f53d29049f
children 1740012184f9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9245
16f53d29049f update copyright notices
John W. Eaton <jwe@octave.org>
parents: 9069
diff changeset
1 ## Copyright (C) 1995, 1996, 1997, 2005, 2006, 2007, 2008, 2009 Kurt Hornik
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
2 ##
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
3 ## This file is part of Octave.
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
4 ##
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
8 ## your option) any later version.
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
9 ##
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
13 ## General Public License for more details.
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
14 ##
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
17 ## <http://www.gnu.org/licenses/>.
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
18
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
19 ## -*- texinfo -*-
5411
bee21f388110 [project @ 2005-07-13 17:53:44 by jwe]
jwe
parents: 5410
diff changeset
20 ## @deftypefn {Function File} {} tinv (@var{x}, @var{n})
8347
fa78cb8d8a5c corrections for typos
Brian Gough<bjg@network-theory.co.uk>
parents: 7737
diff changeset
21 ## For each probability value @var{x}, compute the inverse of the
7737
15d3a35b8ff1 updated help for tcdf and tinv
jstickel@jstickel-10631s.nrel.gov
parents: 7017
diff changeset
22 ## cumulative distribution function (CDF) of the t (Student)
15d3a35b8ff1 updated help for tcdf and tinv
jstickel@jstickel-10631s.nrel.gov
parents: 7017
diff changeset
23 ## distribution with degrees of freedom @var{n}. This function is
9069
634274aaa183 Cleanup documentation for stats.texi
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
24 ## analogous to looking in a table for the t-value of a single-tailed
7737
15d3a35b8ff1 updated help for tcdf and tinv
jstickel@jstickel-10631s.nrel.gov
parents: 7017
diff changeset
25 ## distribution.
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
26 ## @end deftypefn
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
27
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
28 ## For very large n, the "correct" formula does not really work well,
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
29 ## and the quantiles of the standard normal distribution are used
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
30 ## directly.
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
31
5428
2a16423e4aa0 [project @ 2005-08-23 18:38:27 by jwe]
jwe
parents: 5411
diff changeset
32 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
33 ## Description: Quantile function of the t distribution
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
34
5411
bee21f388110 [project @ 2005-07-13 17:53:44 by jwe]
jwe
parents: 5410
diff changeset
35 function inv = tinv (x, n)
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
36
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
37 if (nargin != 2)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5482
diff changeset
38 print_usage ();
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
39 endif
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
40
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
41 if (!isscalar (n))
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
42 [retval, x, n] = common_size (x, n);
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
43 if (retval > 0)
5411
bee21f388110 [project @ 2005-07-13 17:53:44 by jwe]
jwe
parents: 5410
diff changeset
44 error ("tinv: x and n must be of common size or scalar");
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
45 endif
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
46 endif
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
47
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
48 inv = zeros (size (x));
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
49
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
50 k = find ((x < 0) | (x > 1) | isnan (x) | !(n > 0));
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
51 if (any (k))
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
52 inv(k) = NaN;
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
53 endif
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
54
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
55 k = find ((x == 0) & (n > 0));
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
56 if (any (k))
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
57 inv(k) = -Inf;
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
58 endif
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
59
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
60 k = find ((x == 1) & (n > 0));
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
61 if (any (k))
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
62 inv(k) = Inf;
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
63 endif
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
64
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
65 k = find ((x > 0) & (x < 1) & (n > 0) & (n < 10000));
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
66 if (any (k))
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
67 if (isscalar (n))
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
68 inv(k) = (sign (x(k) - 1/2)
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9245
diff changeset
69 .* sqrt (n .* (1 ./ betainv (2*min (x(k), 1 - x(k)),
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9245
diff changeset
70 n/2, 1/2) - 1)));
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
71 else
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
72 inv(k) = (sign (x(k) - 1/2)
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9245
diff changeset
73 .* sqrt (n(k) .* (1 ./ betainv (2*min (x(k), 1 - x(k)),
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9245
diff changeset
74 n(k)/2, 1/2) - 1)));
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
75 endif
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
76 endif
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
77
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
78 ## For large n, use the quantiles of the standard normal
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
79 k = find ((x > 0) & (x < 1) & (n >= 10000));
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
80 if (any (k))
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
81 inv(k) = stdnormal_inv (x(k));
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
82 endif
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
83
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
84 endfunction