annotate scripts/statistics/base/ols.m @ 14342:2cd56a5e3a66 stable

new tests * ols.m: New tests.
author John W. Eaton <jwe@octave.org>
date Tue, 07 Feb 2012 11:41:33 -0500
parents 71efccec5936
children f3d52523cde1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14138
72c96de7a403 maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents: 13824
diff changeset
1 ## Copyright (C) 1996-2012 John W. Eaton
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
2 ##
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
3 ## This file is part of Octave.
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
4 ##
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
781c930425fd [project @ 1998-10-29 05:23:08 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.
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
9 ##
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
13 ## General Public License for more details.
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
14 ##
781c930425fd [project @ 1998-10-29 05:23:08 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/>.
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
18
3458
d25bc039237b [project @ 2000-01-19 09:36:14 by jwe]
jwe
parents: 3456
diff changeset
19 ## -*- texinfo -*-
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
20 ## @deftypefn {Function File} {[@var{beta}, @var{sigma}, @var{r}] =} ols (@var{y}, @var{x})
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
21 ## Ordinary least squares estimation for the multivariate model
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
22 ## @tex
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
23 ## $y = x b + e$
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
24 ## with
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
25 ## $\bar{e} = 0$, and cov(vec($e$)) = kron ($s, I$)
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
26 ## @end tex
8517
81d6ab3ac93c Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents: 7017
diff changeset
27 ## @ifnottex
10687
a8ce6bdecce5 Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents: 9211
diff changeset
28 ## @w{@math{y = x*b + e}} with
3499
3e3e14ad5149 [project @ 2000-01-31 05:18:07 by jwe]
jwe
parents: 3458
diff changeset
29 ## @math{mean (e) = 0} and @math{cov (vec (e)) = kron (s, I)}.
8517
81d6ab3ac93c Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents: 7017
diff changeset
30 ## @end ifnottex
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
31 ## where
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
32 ## @tex
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3368
diff changeset
33 ## $y$ is a $t \times p$ matrix, $x$ is a $t \times k$ matrix,
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
34 ## $b$ is a $k \times p$ matrix, and $e$ is a $t \times p$ matrix.
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
35 ## @end tex
8517
81d6ab3ac93c Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents: 7017
diff changeset
36 ## @ifnottex
3499
3e3e14ad5149 [project @ 2000-01-31 05:18:07 by jwe]
jwe
parents: 3458
diff changeset
37 ## @math{y} is a @math{t} by @math{p} matrix, @math{x} is a @math{t} by
3e3e14ad5149 [project @ 2000-01-31 05:18:07 by jwe]
jwe
parents: 3458
diff changeset
38 ## @math{k} matrix, @math{b} is a @math{k} by @math{p} matrix, and
3e3e14ad5149 [project @ 2000-01-31 05:18:07 by jwe]
jwe
parents: 3458
diff changeset
39 ## @math{e} is a @math{t} by @math{p} matrix.
8517
81d6ab3ac93c Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents: 7017
diff changeset
40 ## @end ifnottex
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3368
diff changeset
41 ##
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
42 ## Each row of @var{y} and @var{x} is an observation and each column a
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
43 ## variable.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3368
diff changeset
44 ##
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
45 ## The return values @var{beta}, @var{sigma}, and @var{r} are defined as
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
46 ## follows.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3368
diff changeset
47 ##
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
48 ## @table @var
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
49 ## @item beta
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
50 ## The OLS estimator for @math{b}.
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
51 ## @tex
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
52 ## $beta$ is calculated directly via $(x^Tx)^{-1} x^T y$ if the matrix $x^Tx$ is
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
53 ## of full rank.
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
54 ## @end tex
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
55 ## @ifnottex
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
56 ## @var{beta} is calculated directly via @code{inv (x'*x) * x' * y} if the
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
57 ## matrix @code{x'*x} is of full rank.
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
58 ## @end ifnottex
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
59 ## Otherwise, @code{@var{beta} = pinv (@var{x}) * @var{y}} where
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
60 ## @code{pinv (@var{x})} denotes the pseudoinverse of @var{x}.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3368
diff changeset
61 ##
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
62 ## @item sigma
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
63 ## The OLS estimator for the matrix @var{s},
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3368
diff changeset
64 ##
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
65 ## @example
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
66 ## @group
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
67 ## @var{sigma} = (@var{y}-@var{x}*@var{beta})'
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
68 ## * (@var{y}-@var{x}*@var{beta})
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
69 ## / (@var{t}-rank(@var{x}))
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
70 ## @end group
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
71 ## @end example
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3368
diff changeset
72 ##
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
73 ## @item r
10687
a8ce6bdecce5 Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents: 9211
diff changeset
74 ## The matrix of OLS residuals, @code{@var{r} = @var{y} - @var{x}*@var{beta}}.
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
75 ## @end table
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
76 ## @seealso{gls, pinv}
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
77 ## @end deftypefn
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
78
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
79 ## Author: Teresa Twaroch <twaroch@ci.tuwien.ac.at>
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
80 ## Created: May 1993
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
81 ## Adapted-By: jwe
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
82
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
83 function [beta, sigma, r] = ols (y, x)
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
84
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
85 if (nargin != 2)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5307
diff changeset
86 print_usage ();
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
87 endif
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
88
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
89 if (! (isnumeric (x) && isnumeric (y)))
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
90 error ("ols: X and Y must be numeric matrices or vectors");
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
91 endif
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
92
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
93 if (ndims (x) != 2 || ndims (y) != 2)
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
94 error ("ols: X and Y must be 2-D matrices or vectors");
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
95 endif
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
96
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
97 [nr, nc] = size (x);
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
98 [ry, cy] = size (y);
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
99 if (nr != ry)
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
100 error ("ols: number of rows of X and Y must be equal");
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
101 endif
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
102
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12547
diff changeset
103 if (isinteger (x))
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12547
diff changeset
104 x = double (x);
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12547
diff changeset
105 endif
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12547
diff changeset
106 if (isinteger (y))
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12547
diff changeset
107 y = double (y);
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12547
diff changeset
108 endif
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12547
diff changeset
109
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12547
diff changeset
110 ## Start of algorithm
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
111 z = x' * x;
13823
94a37dae80a9 Use a cheaper Cholesky decomposition than a rank() svd in ols.m
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12656
diff changeset
112 [u, p] = chol (z);
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
113
13823
94a37dae80a9 Use a cheaper Cholesky decomposition than a rank() svd in ols.m
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12656
diff changeset
114 if (p)
94a37dae80a9 Use a cheaper Cholesky decomposition than a rank() svd in ols.m
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12656
diff changeset
115 beta = pinv (x) * y;
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
116 else
13824
aa0cba2256f4 Group the ols matrix operation in a smarter way
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13823
diff changeset
117 beta = u \ (u' \ (x' * y));
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
118 endif
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
119
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
120 if (isargout (2) || isargout (3))
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
121 r = y - x * beta;
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
122 endif
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
123 if (isargout (2))
14341
71efccec5936 * ols.m: Compute rank if needed.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
124
71efccec5936 * ols.m: Compute rank if needed.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
125 ## z is of full rank, avoid the SVD in rnk
71efccec5936 * ols.m: Compute rank if needed.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
126 if (p == 0)
71efccec5936 * ols.m: Compute rank if needed.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
127 rnk = columns (z);
71efccec5936 * ols.m: Compute rank if needed.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
128 else
71efccec5936 * ols.m: Compute rank if needed.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
129 rnk = rank (z);
71efccec5936 * ols.m: Compute rank if needed.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
130 endif
71efccec5936 * ols.m: Compute rank if needed.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
131
12547
17997376291b Fix variable name clash in ols.m
Michael Creel <michael.creel@uab.es>
parents: 11587
diff changeset
132 sigma = r' * r / (nr - rnk);
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
133 endif
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
134
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
135 endfunction
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
136
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12547
diff changeset
137
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
138 %!test
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
139 %! x = [1:5]';
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
140 %! y = 3*x + 2;
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
141 %! x = [x, ones(5,1)];
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
142 %! assert (ols(y,x), [3; 2], 50*eps)
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
143
14342
2cd56a5e3a66 new tests
John W. Eaton <jwe@octave.org>
parents: 14341
diff changeset
144 %!test
2cd56a5e3a66 new tests
John W. Eaton <jwe@octave.org>
parents: 14341
diff changeset
145 %! x = [1, 2; 3, 4];
2cd56a5e3a66 new tests
John W. Eaton <jwe@octave.org>
parents: 14341
diff changeset
146 %! y = [1; 2];
2cd56a5e3a66 new tests
John W. Eaton <jwe@octave.org>
parents: 14341
diff changeset
147 %! [b, s, r] = ols (x, y);
2cd56a5e3a66 new tests
John W. Eaton <jwe@octave.org>
parents: 14341
diff changeset
148 %! assert (b, [1.4, 2], 2*eps);
2cd56a5e3a66 new tests
John W. Eaton <jwe@octave.org>
parents: 14341
diff changeset
149 %! assert (s, [0.2, 0; 0, 0], 2*eps);
2cd56a5e3a66 new tests
John W. Eaton <jwe@octave.org>
parents: 14341
diff changeset
150 %! assert (r, [-0.4, 0; 0.2, 0], 2*eps);
2cd56a5e3a66 new tests
John W. Eaton <jwe@octave.org>
parents: 14341
diff changeset
151
2cd56a5e3a66 new tests
John W. Eaton <jwe@octave.org>
parents: 14341
diff changeset
152 %!test
2cd56a5e3a66 new tests
John W. Eaton <jwe@octave.org>
parents: 14341
diff changeset
153 %! x = [1, 2; 3, 4];
2cd56a5e3a66 new tests
John W. Eaton <jwe@octave.org>
parents: 14341
diff changeset
154 %! y = [1; 2];
2cd56a5e3a66 new tests
John W. Eaton <jwe@octave.org>
parents: 14341
diff changeset
155 %! [b, s] = ols (x, y);
2cd56a5e3a66 new tests
John W. Eaton <jwe@octave.org>
parents: 14341
diff changeset
156 %! assert (b, [1.4, 2], 2*eps);
2cd56a5e3a66 new tests
John W. Eaton <jwe@octave.org>
parents: 14341
diff changeset
157 %! assert (s, [0.2, 0; 0, 0], 2*eps);
2cd56a5e3a66 new tests
John W. Eaton <jwe@octave.org>
parents: 14341
diff changeset
158
2cd56a5e3a66 new tests
John W. Eaton <jwe@octave.org>
parents: 14341
diff changeset
159 %!test
2cd56a5e3a66 new tests
John W. Eaton <jwe@octave.org>
parents: 14341
diff changeset
160 %! x = [1, 2; 3, 4];
2cd56a5e3a66 new tests
John W. Eaton <jwe@octave.org>
parents: 14341
diff changeset
161 %! y = [1; 2];
2cd56a5e3a66 new tests
John W. Eaton <jwe@octave.org>
parents: 14341
diff changeset
162 %! b = ols (x, y);
2cd56a5e3a66 new tests
John W. Eaton <jwe@octave.org>
parents: 14341
diff changeset
163 %! assert (b, [1.4, 2], 2*eps);
2cd56a5e3a66 new tests
John W. Eaton <jwe@octave.org>
parents: 14341
diff changeset
164
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
165 %% Test input validation
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
166 %!error ols ();
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
167 %!error ols (1);
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
168 %!error ols (1, 2, 3);
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
169 %!error ols ([true, true], [1, 2]);
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
170 %!error ols ([1, 2], [true, true]);
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
171 %!error ols (ones (2,2,2), ones (2,2));
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
172 %!error ols (ones (2,2), ones (2,2,2));
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
173 %!error ols (ones(1,2), ones(2,2));