1 ## Copyright (C) 1996-2012 John W. Eaton
3 ## This file is part of Octave.
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 3 of the License, or (at
8 ## your option) any later version.
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.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
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
24 ## with $\bar{e} = 0$ and cov(vec($e$)) = $(s^2)o$,
27 ## @w{@math{y = x*b + e}} with @math{mean (e) = 0} and
28 ## @math{cov (vec (e)) = (s^2) o},
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
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}
39 ## is a @math{t} by @math{p} matrix, and @math{o} is a @math{t*p} by
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.
50 ## The GLS estimator for @math{b}.
53 ## The GLS estimator for @math{s^2}.
56 ## The matrix of GLS residuals, @math{r = y - x*beta}.
61 ## Author: Teresa Twaroch <twaroch@ci.tuwien.ac.at>
65 function [beta, v, r] = gls (y, x, o)
71 if (! (isnumeric (x) && isnumeric (y) && isnumeric (o)))
72 error ("gls: X, Y, and O must be numeric matrices or vectors");
75 if (ndims (x) != 2 || ndims (y) != 2 || ndims (o) != 2)
76 error ("gls: X, Y and O must be 2-D matrices or vectors");
83 error ("gls: number of rows of X and Y must be equal");
85 if (!issquare (o) || ro != ry*cy)
86 error ("gls: matrix O must be square matrix with rows = rows (Y) * cols (Y)");
101 z = kron (eye (cy), x);
103 y1 = o * reshape (y, ry*cy, 1);
108 b = inv (u) * z' * y1;
113 beta = reshape (b, cx, cy);
115 if (isargout (2) || isargout (3))
118 v = (reshape (r, ry*cy, 1))' * (o^2) * reshape (r, ry*cy, 1) / (rx*cy - r);
128 %! x = [x, ones(5,1)];
129 %! o = diag (ones (5,1));
130 %! assert (gls (y,x,o), [3; 2], 50*eps);
132 %% Test input validation
136 %!error gls (1, 2, 3, 4)
137 %!error gls ([true, true], [1, 2], ones (2))
138 %!error gls ([1, 2], [true, true], ones (2))
139 %!error gls ([1, 2], [1, 2], true (2))
140 %!error gls (ones (2,2,2), ones (2,2), ones (4,4))
141 %!error gls (ones (2,2), ones (2,2,2), ones (4,4))
142 %!error gls (ones (2,2), ones (2,2), ones (4,4,4))
143 %!error gls (ones(1,2), ones(2,2), ones (2,2))
144 %!error gls (ones(2,2), ones(2,2), ones (2,2))