Mercurial > octave-libgccjit
annotate scripts/statistics/tests/z_test_2.m @ 7540:3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
author | Ben Abbott <bpabbott@mac.com> |
---|---|
date | Thu, 28 Feb 2008 02:41:19 -0500 |
parents | a1dbe9d80eee |
children | eb63fbe60fab |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1995, 1996, 1997, 1998, 2000, 2002, 2005, 2006, 2007 |
2 ## 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 |
3454 | 20 ## -*- texinfo -*- |
21 ## @deftypefn {Function File} {[@var{pval}, @var{z}] =} z_test_2 (@var{x}, @var{y}, @var{v_x}, @var{v_y}, @var{alt}) | |
22 ## For two samples @var{x} and @var{y} from normal distributions with | |
23 ## unknown means and known variances @var{v_x} and @var{v_y}, perform a | |
24 ## Z-test of the hypothesis of equal means. Under the null, the test | |
25 ## statistic @var{z} follows a standard normal distribution. | |
3200 | 26 ## |
3454 | 27 ## With the optional argument string @var{alt}, the alternative of |
28 ## interest can be selected. If @var{alt} is @code{"!="} or | |
29 ## @code{"<>"}, the null is tested against the two-sided alternative | |
30 ## @code{mean (@var{x}) != mean (@var{y})}. If alt is @code{">"}, the | |
3457 | 31 ## one-sided alternative @code{mean (@var{x}) > mean (@var{y})} is used. |
32 ## Similarly for @code{"<"}, the one-sided alternative @code{mean | |
33 ## (@var{x}) < mean (@var{y})} is used. The default is the two-sided | |
34 ## case. | |
3200 | 35 ## |
3454 | 36 ## The p-value of the test is returned in @var{pval}. |
3426 | 37 ## |
3200 | 38 ## If no output argument is given, the p-value of the test is displayed |
39 ## along with some information. | |
3454 | 40 ## @end deftypefn |
3200 | 41 |
5428 | 42 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3456 | 43 ## Description: Compare means of two normal samples with known variances |
3200 | 44 |
45 function [pval, z] = z_test_2 (x, y, v_x, v_y, alt) | |
3426 | 46 |
3200 | 47 if ((nargin < 4) || (nargin > 5)) |
6046 | 48 print_usage (); |
3200 | 49 endif |
3426 | 50 |
4030 | 51 if (! (isvector (x) && isvector (y))) |
3456 | 52 error("z_test_2: both x and y must be vectors"); |
4030 | 53 elseif (! (isscalar (v_x) && (v_x > 0) |
54 && isscalar (v_y) && (v_y > 0))) | |
3458 | 55 error ("z_test_2: both v_x and v_y must be positive scalars"); |
3200 | 56 endif |
3426 | 57 |
3200 | 58 n_x = length (x); |
59 n_y = length (y); | |
60 mu_x = sum (x) / n_x; | |
61 mu_y = sum (y) / n_y; | |
62 z = (mu_x - mu_y) / sqrt (v_x / n_x + v_y / n_y); | |
63 cdf = stdnormal_cdf (z); | |
3426 | 64 |
3200 | 65 if (nargin == 4) |
66 alt = "!="; | |
67 endif | |
68 | |
5443 | 69 if (! ischar (alt)) |
3456 | 70 error ("z_test_2: alt must be a string"); |
3200 | 71 elseif (strcmp (alt, "!=") || strcmp (alt, "<>")) |
72 pval = 2 * min (cdf, 1 - cdf); | |
73 elseif (strcmp (alt, ">")) | |
74 pval = 1 - cdf; | |
75 elseif (strcmp (alt, "<")) | |
76 pval = cdf; | |
77 else | |
3456 | 78 error ("z_test_2: option %s not recognized", alt); |
3200 | 79 endif |
3426 | 80 |
3200 | 81 if (nargout == 0) |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
82 s = cstrcat ("Two-sample Z-test of mean(x) == mean(y) against ", |
3426 | 83 "mean(x) %s mean(y),\n", |
84 "with known var(x) == %g and var(y) == %g:\n", | |
85 " pval = %g\n"); | |
3200 | 86 printf (s, alt, v_x, v_y, pval); |
87 endif | |
88 | |
89 endfunction |