Mercurial > hg > octave-nkf
annotate scripts/statistics/base/gls.m @ 11523:fd0a3ac60b0e
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 14 Jan 2011 05:47:45 -0500 |
parents | e151e23f73bc |
children | 6b2f14af2360 |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 1996-2011 John W. Eaton |
3200 | 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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
3200 | 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 | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
3200 | 18 |
3381 | 19 ## -*- texinfo -*- |
3368 | 20 ## @deftypefn {Function File} {[@var{beta}, @var{v}, @var{r}] =} gls (@var{y}, @var{x}, @var{o}) |
21 ## Generalized least squares estimation for the multivariate model | |
22 ## @tex | |
23 ## $y = x b + e$ | |
24 ## with $\bar{e} = 0$ and cov(vec($e$)) = $(s^2)o$, | |
25 ## @end tex | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7017
diff
changeset
|
26 ## @ifnottex |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
27 ## @w{@math{y = x*b + e}} with @math{mean (e) = 0} and |
3499 | 28 ## @math{cov (vec (e)) = (s^2) o}, |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7017
diff
changeset
|
29 ## @end ifnottex |
3368 | 30 ## where |
3426 | 31 ## @tex |
3368 | 32 ## $y$ is a $t \times p$ matrix, $x$ is a $t \times k$ matrix, $b$ is a $k |
33 ## \times p$ matrix, $e$ is a $t \times p$ matrix, and $o$ is a $tp \times | |
34 ## tp$ matrix. | |
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 | 37 ## @math{y} is a @math{t} by @math{p} matrix, @math{x} is a @math{t} by |
38 ## @math{k} matrix, @math{b} is a @math{k} by @math{p} matrix, @math{e} | |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
39 ## is a @math{t} by @math{p} matrix, and @math{o} is a @math{t*p} by |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
40 ## @math{t*p} matrix. |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7017
diff
changeset
|
41 ## @end ifnottex |
3426 | 42 ## |
3368 | 43 ## @noindent |
3499 | 44 ## Each row of @var{y} and @var{x} is an observation and each column a |
45 ## variable. The return values @var{beta}, @var{v}, and @var{r} are | |
46 ## defined as follows. | |
3426 | 47 ## |
3368 | 48 ## @table @var |
49 ## @item beta | |
3499 | 50 ## The GLS estimator for @math{b}. |
3426 | 51 ## |
3368 | 52 ## @item v |
3499 | 53 ## The GLS estimator for @math{s^2}. |
3426 | 54 ## |
3368 | 55 ## @item r |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
56 ## The matrix of GLS residuals, @math{r = y - x*beta}. |
3368 | 57 ## @end table |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
58 ## @seealso{ols} |
3368 | 59 ## @end deftypefn |
3200 | 60 |
61 ## Author: Teresa Twaroch <twaroch@ci.tuwien.ac.at> | |
62 ## Created: May 1993 | |
63 ## Adapted-By: jwe | |
64 | |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
65 function [beta, v, r] = gls (y, x, o) |
3200 | 66 |
67 if (nargin != 3) | |
6046 | 68 print_usage (); |
3200 | 69 endif |
70 | |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
71 if (! (isnumeric (x) && isnumeric (y) && isnumeric (o))) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
72 error ("gls: X, Y, and O must be numeric matrices or vectors"); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
73 endif |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
74 |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
75 if (ndims (x) != 2 || ndims (y) != 2 || ndims (o) != 2) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
76 error ("gls: X, Y and O must be 2-D matrices or vectors"); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
77 endif |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
78 |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
79 [rx, cx] = size (x); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
80 [ry, cy] = size (y); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
81 [ro, co] = size (o); |
3200 | 82 if (rx != ry) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
83 error ("gls: 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:
9211
diff
changeset
|
84 endif |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
85 if (!issquare(o) || ro != ry*cy) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
86 error ("gls: matrix O must be square matrix with rows = rows (Y) * cols (Y)"); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
87 endif |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
88 |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
89 o = o^(-1/2); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
90 z = kron (eye (cy), x); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
91 z = o * z; |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
92 y1 = o * reshape (y, ry*cy, 1); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
93 u = z' * z; |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
94 r = rank (u); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
95 |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
96 if (r == cx*cy) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
97 b = inv (u) * z' * y1; |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
98 else |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
99 b = pinv (z) * y1; |
3200 | 100 endif |
101 | |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
102 beta = reshape (b, cx, cy); |
3200 | 103 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
104 if (isargout (2) || isargout (3)) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
105 r = y - x * beta; |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
106 if (isargout (2)) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
107 v = (reshape (r, ry*cy, 1))' * (o^2) * reshape (r, ry*cy, 1) / (rx*cy - r); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
108 endif |
3200 | 109 endif |
110 | |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
111 endfunction |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
112 |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
113 |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
114 %!test |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
115 %! x = [1:5]'; |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
116 %! y = 3*x + 2; |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
117 %! x = [x, ones(5,1)]; |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
118 %! o = diag (ones (5,1)); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
119 %! assert (gls (y,x,o), [3; 2], 50*eps) |
3200 | 120 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
121 %% Test input validation |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
122 %!error gls () |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
123 %!error gls (1) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
124 %!error gls (1, 2) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
125 %!error gls (1, 2, 3, 4) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
126 %!error gls ([true, true], [1, 2], ones (2)) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
127 %!error gls ([1, 2], [true, true], ones (2)) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
128 %!error gls ([1, 2], [1, 2], true (2)) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
129 %!error gls (ones (2,2,2), ones (2,2), ones (4,4)) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
130 %!error gls (ones (2,2), ones (2,2,2), ones (4,4)) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
131 %!error gls (ones (2,2), ones (2,2), ones (4,4,4)) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
132 %!error gls (ones(1,2), ones(2,2), ones (2,2)) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
133 %!error gls (ones(2,2), ones(2,2), ones (2,2)) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
134 |