1## Copyright (C) 1995-2017 Kurt Hornik
2##
3## This program is free software: you can redistribute it and/or
4## modify it under the terms of the GNU General Public License as
5## published by the Free Software Foundation, either version 3 of the
6## License, or (at your option) any later version.
7##
8## This program is distributed in the hope that it will be useful, but
9## WITHOUT ANY WARRANTY; without even the implied warranty of
10## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11## General Public License for more details.
12##
13## You should have received a copy of the GNU General Public License
14## along with this program; see the file COPYING.  If not, see
15## <http://www.gnu.org/licenses/>.
16
17## -*- texinfo -*-
18## @deftypefn {} {[@var{pval}, @var{t}, @var{df}] =} t_test (@var{x}, @var{m}, @var{alt})
19## For a sample @var{x} from a normal distribution with unknown mean and
20## variance, perform a t-test of the null hypothesis
21## @code{mean (@var{x}) == @var{m}}.
22##
23## Under the null, the test statistic @var{t} follows a Student distribution
24## with @code{@var{df} = length (@var{x}) - 1} degrees of freedom.
25##
26## With the optional argument string @var{alt}, the alternative of interest
27## can be selected.  If @var{alt} is @qcode{"!="} or @qcode{"<>"}, the null
28## is tested against the two-sided alternative @code{mean (@var{x}) !=
29## @var{m}}.  If @var{alt} is @qcode{">"}, the one-sided alternative
30## @code{mean (@var{x}) > @var{m}} is considered.  Similarly for @var{"<"},
31## the one-sided alternative @code{mean (@var{x}) < @var{m}} is considered.
32## The default is the two-sided case.
33##
34## The p-value of the test is returned in @var{pval}.
35##
36## If no output argument is given, the p-value of the test is displayed.
37## @end deftypefn
38
39## Author: KH <Kurt.Hornik@wu-wien.ac.at>
40## Description: Student's one-sample t test
41
42function [pval, t, df] = t_test (x, m, alt)
43
44  if (nargin < 2 || nargin > 3)
45    print_usage ();
46  endif
47
48  if (! isvector (x))
49    error ("t_test: X must be a vector");
50  endif
51  if (! isscalar (m))
52    error ("t_test: M must be a scalar");
53  endif
54
55  n   = length (x);
56  df  = n - 1;
57  t   = sqrt (n) * (sum (x) / n - m) / std (x);
58  cdf = tcdf (t, df);
59
60  if (nargin == 2)
61    alt = "!=";
62  endif
63
64  if (! ischar (alt))
65    error ("t_test: ALT must be a string");
66  endif
67  if (strcmp (alt, "!=") || strcmp (alt, "<>"))
68    pval = 2 * min (cdf, 1 - cdf);
69  elseif (strcmp (alt, ">"))
70    pval = 1 - cdf;
71  elseif (strcmp (alt, "<"))
72    pval = cdf;
73  else
74    error ("t_test: option %s not recognized", alt);
75  endif
76
77  if (nargout == 0)
78    printf ("  pval: %g\n", pval);
79  endif
80
81endfunction
82
83
84%!test
85%! ## Two-sided (also the default option)
86%! x = rand (10,1); n = length (x);
87%! u0 = 0.5; # true mean
88%! xbar = mean (x);
89%! pval = t_test (x, u0, "!=");
90%! if (xbar >= u0)
91%!   tval = abs (tinv (0.5*pval, n-1));
92%! else
93%!   tval = -abs (tinv (0.5*pval, n-1));
94%! endif
95%! unew = tval * std(x)/sqrt(n) + u0;
96%! assert (xbar, unew, 1e6*eps);
97
98%!test
99%! x = rand (10,1); n = length (x);
100%! u0 = 0.5;
101%! pval = t_test (x, u0, ">");
102%! tval = tinv (1-pval, n-1);
103%! unew = tval * std(x)/sqrt(n) + u0;
104%! assert (mean (x), unew, 1e6*eps);
105
106%!test
107%! x = rand (10,1); n = length (x);
108%! u0 = 0.5;
109%! pval = t_test (x, u0, "<");
110%! tval = tinv (pval, n-1);
111%! unew = tval * std(x)/sqrt(n) + u0;
112%! assert (mean (x), unew, 1e6*eps);
113