3200
|
1 ## Copyright (C) 1996, 1997 John W. Eaton |
|
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 |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
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 |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
18 ## 02111-1307, USA. |
|
19 |
3381
|
20 ## -*- texinfo -*- |
3368
|
21 ## @deftypefn {Function File} {[@var{beta}, @var{v}, @var{r}] =} gls (@var{y}, @var{x}, @var{o}) |
|
22 ## Generalized least squares estimation for the multivariate model |
|
23 ## @iftex |
|
24 ## @tex |
|
25 ## $y = x b + e$ |
|
26 ## with $\bar{e} = 0$ and cov(vec($e$)) = $(s^2)o$, |
|
27 ## @end tex |
|
28 ## @end iftex |
|
29 ## @ifinfo |
|
30 ## @code{@var{y} = @var{x} * @var{b} + @var{e}} with @code{mean (@var{e}) = |
|
31 ## 0} and @code{cov (vec (@var{e})) = (@var{s}^2)*@var{o}}, |
|
32 ## @end ifinfo |
|
33 ## where |
|
34 ## @iftex |
3426
|
35 ## @tex |
3368
|
36 ## $y$ is a $t \times p$ matrix, $x$ is a $t \times k$ matrix, $b$ is a $k |
|
37 ## \times p$ matrix, $e$ is a $t \times p$ matrix, and $o$ is a $tp \times |
|
38 ## tp$ matrix. |
|
39 ## @end tex |
|
40 ## @end iftex |
|
41 ## @ifinfo |
|
42 ## @var{Y} is a @var{T} by @var{p} matrix, @var{X} is a @var{T} by @var{k} |
|
43 ## matrix, @var{B} is a @var{k} by @var{p} matrix, @var{E} is a @var{T} by |
|
44 ## @var{p} matrix, and @var{O} is a @var{T}@var{p} by @var{T}@var{p} |
|
45 ## matrix. |
|
46 ## @end ifinfo |
3426
|
47 ## |
3368
|
48 ## @noindent |
|
49 ## Each row of Y and X is an observation and each column a variable. |
3426
|
50 ## |
3368
|
51 ## The return values @var{beta}, @var{v}, and @var{r} are defined as |
|
52 ## follows. |
3426
|
53 ## |
3368
|
54 ## @table @var |
|
55 ## @item beta |
|
56 ## The GLS estimator for @var{b}. |
3426
|
57 ## |
3368
|
58 ## @item v |
|
59 ## The GLS estimator for @code{@var{s}^2}. |
3426
|
60 ## |
3368
|
61 ## @item r |
|
62 ## The matrix of GLS residuals, @code{@var{r} = @var{y} - @var{x} * |
|
63 ## @var{beta}}. |
|
64 ## @end table |
|
65 ## @end deftypefn |
3200
|
66 |
|
67 ## Author: Teresa Twaroch <twaroch@ci.tuwien.ac.at> |
|
68 ## Created: May 1993 |
|
69 ## Adapted-By: jwe |
|
70 |
|
71 function [BETA, v, R] = gls (Y, X, O) |
|
72 |
|
73 if (nargin != 3) |
|
74 usage ("[BETA, v [, R]] = gls (Y, X, O)"); |
|
75 endif |
|
76 |
|
77 [rx, cx] = size (X); |
|
78 [ry, cy] = size (Y); |
|
79 if (rx != ry) |
|
80 error ("gls: incorrect matrix dimensions"); |
|
81 endif |
|
82 |
|
83 O = O^(-1/2); |
|
84 Z = kron (eye (cy), X); |
|
85 Z = O * Z; |
|
86 Y1 = O * reshape (Y, ry*cy, 1); |
|
87 U = Z' * Z; |
|
88 r = rank (U); |
|
89 |
|
90 if (r == cx*cy) |
|
91 B = inv (U) * Z' * Y1; |
|
92 else |
|
93 B = pinv (Z) * Y1; |
|
94 endif |
|
95 |
|
96 BETA = reshape (B, cx, cy); |
|
97 R = Y - X * BETA; |
|
98 v = (reshape (R, ry*cy, 1))' * (O^2) * reshape (R, ry*cy, 1) / (rx*cy - r); |
|
99 |
|
100 endfunction |