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 |
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 ## @iftex |
|
23 ## @tex |
|
24 ## $y = x b + e$ |
|
25 ## with $\bar{e} = 0$ and cov(vec($e$)) = $(s^2)o$, |
|
26 ## @end tex |
|
27 ## @end iftex |
|
28 ## @ifinfo |
|
29 ## @code{@var{y} = @var{x} * @var{b} + @var{e}} with @code{mean (@var{e}) = |
|
30 ## 0} and @code{cov (vec (@var{e})) = (@var{s}^2)*@var{o}}, |
|
31 ## @end ifinfo |
|
32 ## where |
|
33 ## @iftex |
|
34 ## @tex |
|
35 ## $y$ is a $t \times p$ matrix, $x$ is a $t \times k$ matrix, $b$ is a $k |
|
36 ## \times p$ matrix, $e$ is a $t \times p$ matrix, and $o$ is a $tp \times |
|
37 ## tp$ matrix. |
|
38 ## @end tex |
|
39 ## @end iftex |
|
40 ## @ifinfo |
|
41 ## @var{Y} is a @var{T} by @var{p} matrix, @var{X} is a @var{T} by @var{k} |
|
42 ## matrix, @var{B} is a @var{k} by @var{p} matrix, @var{E} is a @var{T} by |
|
43 ## @var{p} matrix, and @var{O} is a @var{T}@var{p} by @var{T}@var{p} |
|
44 ## matrix. |
|
45 ## @end ifinfo |
|
46 ## |
|
47 ## @noindent |
|
48 ## Each row of Y and X is an observation and each column a variable. |
|
49 ## |
|
50 ## The return values @var{beta}, @var{v}, and @var{r} are defined as |
|
51 ## follows. |
|
52 ## |
|
53 ## @table @var |
|
54 ## @item beta |
|
55 ## The GLS estimator for @var{b}. |
|
56 ## |
|
57 ## @item v |
|
58 ## The GLS estimator for @code{@var{s}^2}. |
|
59 ## |
|
60 ## @item r |
|
61 ## The matrix of GLS residuals, @code{@var{r} = @var{y} - @var{x} * |
|
62 ## @var{beta}}. |
|
63 ## @end table |
|
64 ## @end deftypefn |
3200
|
65 |
|
66 ## Author: Teresa Twaroch <twaroch@ci.tuwien.ac.at> |
|
67 ## Created: May 1993 |
|
68 ## Adapted-By: jwe |
|
69 |
|
70 function [BETA, v, R] = gls (Y, X, O) |
|
71 |
|
72 if (nargin != 3) |
|
73 usage ("[BETA, v [, R]] = gls (Y, X, O)"); |
|
74 endif |
|
75 |
|
76 [rx, cx] = size (X); |
|
77 [ry, cy] = size (Y); |
|
78 if (rx != ry) |
|
79 error ("gls: incorrect matrix dimensions"); |
|
80 endif |
|
81 |
|
82 O = O^(-1/2); |
|
83 Z = kron (eye (cy), X); |
|
84 Z = O * Z; |
|
85 Y1 = O * reshape (Y, ry*cy, 1); |
|
86 U = Z' * Z; |
|
87 r = rank (U); |
|
88 |
|
89 if (r == cx*cy) |
|
90 B = inv (U) * Z' * Y1; |
|
91 else |
|
92 B = pinv (Z) * Y1; |
|
93 endif |
|
94 |
|
95 BETA = reshape (B, cx, cy); |
|
96 R = Y - X * BETA; |
|
97 v = (reshape (R, ry*cy, 1))' * (O^2) * reshape (R, ry*cy, 1) / (rx*cy - r); |
|
98 |
|
99 endfunction |