Mercurial > hg > octave-nkf
annotate scripts/statistics/base/gls.m @ 8517:81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
author | sh@sh-laptop |
---|---|
date | Wed, 14 Jan 2009 20:44:25 -0500 |
parents | a1dbe9d80eee |
children | eb63fbe60fab |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1996, 1997, 1998, 1999, 2000, 2005, 2006, 2007 |
2 ## John W. Eaton | |
3200 | 3 ## |
4 ## This file is part of Octave. | |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
3200 | 10 ## |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
3200 | 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 | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7017
diff
changeset
|
29 ## @ifnottex |
3499 | 30 ## @math{y = x b + e} with @math{mean (e) = 0} and |
31 ## @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
|
32 ## @end ifnottex |
3368 | 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 | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7017
diff
changeset
|
41 ## @ifnottex |
3499 | 42 ## @math{y} is a @math{t} by @math{p} matrix, @math{x} is a @math{t} by |
43 ## @math{k} matrix, @math{b} is a @math{k} by @math{p} matrix, @math{e} | |
44 ## is a @math{t} by @math{p} matrix, and @math{o} is a @math{t p} by | |
45 ## @math{t p} matrix. | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7017
diff
changeset
|
46 ## @end ifnottex |
3426 | 47 ## |
3368 | 48 ## @noindent |
3499 | 49 ## Each row of @var{y} and @var{x} is an observation and each column a |
50 ## variable. The return values @var{beta}, @var{v}, and @var{r} are | |
51 ## defined as follows. | |
3426 | 52 ## |
3368 | 53 ## @table @var |
54 ## @item beta | |
3499 | 55 ## The GLS estimator for @math{b}. |
3426 | 56 ## |
3368 | 57 ## @item v |
3499 | 58 ## The GLS estimator for @math{s^2}. |
3426 | 59 ## |
3368 | 60 ## @item r |
3499 | 61 ## The matrix of GLS residuals, @math{r = y - x beta}. |
3368 | 62 ## @end table |
63 ## @end deftypefn | |
3200 | 64 |
65 ## Author: Teresa Twaroch <twaroch@ci.tuwien.ac.at> | |
66 ## Created: May 1993 | |
67 ## Adapted-By: jwe | |
68 | |
69 function [BETA, v, R] = gls (Y, X, O) | |
70 | |
71 if (nargin != 3) | |
6046 | 72 print_usage (); |
3200 | 73 endif |
74 | |
75 [rx, cx] = size (X); | |
76 [ry, cy] = size (Y); | |
77 if (rx != ry) | |
78 error ("gls: incorrect matrix dimensions"); | |
79 endif | |
80 | |
81 O = O^(-1/2); | |
82 Z = kron (eye (cy), X); | |
83 Z = O * Z; | |
84 Y1 = O * reshape (Y, ry*cy, 1); | |
85 U = Z' * Z; | |
86 r = rank (U); | |
87 | |
88 if (r == cx*cy) | |
89 B = inv (U) * Z' * Y1; | |
90 else | |
91 B = pinv (Z) * Y1; | |
92 endif | |
93 | |
94 BETA = reshape (B, cx, cy); | |
95 R = Y - X * BETA; | |
96 v = (reshape (R, ry*cy, 1))' * (O^2) * reshape (R, ry*cy, 1) / (rx*cy - r); | |
97 | |
98 endfunction |