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 |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
3200
|
19 |
3458
|
20 ## -*- texinfo -*- |
3368
|
21 ## @deftypefn {Function File} {[@var{beta}, @var{sigma}, @var{r}] =} ols (@var{y}, @var{x}) |
|
22 ## Ordinary least squares estimation for the multivariate model |
|
23 ## @iftex |
|
24 ## @tex |
|
25 ## $y = x b + e$ |
|
26 ## with |
|
27 ## $\bar{e} = 0$, and cov(vec($e$)) = kron ($s, I$) |
|
28 ## @end tex |
|
29 ## @end iftex |
|
30 ## @ifinfo |
3499
|
31 ## @math{y = x b + e} with |
|
32 ## @math{mean (e) = 0} and @math{cov (vec (e)) = kron (s, I)}. |
3368
|
33 ## @end ifinfo |
|
34 ## where |
|
35 ## @iftex |
|
36 ## @tex |
3426
|
37 ## $y$ is a $t \times p$ matrix, $x$ is a $t \times k$ matrix, |
3368
|
38 ## $b$ is a $k \times p$ matrix, and $e$ is a $t \times p$ matrix. |
|
39 ## @end tex |
|
40 ## @end iftex |
|
41 ## @ifinfo |
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, and |
|
44 ## @math{e} is a @math{t} by @math{p} matrix. |
3368
|
45 ## @end ifinfo |
3426
|
46 ## |
3368
|
47 ## Each row of @var{y} and @var{x} is an observation and each column a |
|
48 ## variable. |
3426
|
49 ## |
3368
|
50 ## The return values @var{beta}, @var{sigma}, and @var{r} are defined as |
|
51 ## follows. |
3426
|
52 ## |
3368
|
53 ## @table @var |
|
54 ## @item beta |
|
55 ## The OLS estimator for @var{b}, @code{@var{beta} = pinv (@var{x}) * |
|
56 ## @var{y}}, where @code{pinv (@var{x})} denotes the pseudoinverse of |
|
57 ## @var{x}. |
3426
|
58 ## |
3368
|
59 ## @item sigma |
|
60 ## The OLS estimator for the matrix @var{s}, |
3426
|
61 ## |
3368
|
62 ## @example |
|
63 ## @group |
|
64 ## @var{sigma} = (@var{y}-@var{x}*@var{beta})' |
|
65 ## * (@var{y}-@var{x}*@var{beta}) |
|
66 ## / (@var{t}-rank(@var{x})) |
|
67 ## @end group |
|
68 ## @end example |
3426
|
69 ## |
3368
|
70 ## @item r |
|
71 ## The matrix of OLS residuals, @code{@var{r} = @var{y} - @var{x} * |
|
72 ## @var{beta}}. |
|
73 ## @end table |
|
74 ## @end deftypefn |
3200
|
75 |
|
76 ## Author: Teresa Twaroch <twaroch@ci.tuwien.ac.at> |
|
77 ## Created: May 1993 |
|
78 ## Adapted-By: jwe |
|
79 |
|
80 function [BETA, SIGMA, R] = ols (Y, X) |
|
81 |
|
82 if (nargin != 2) |
3456
|
83 usage ("[BETA, SIGMA, R] = ols (Y, X)"); |
3200
|
84 endif |
|
85 |
|
86 [nr, nc] = size (X); |
|
87 [ry, cy] = size (Y); |
|
88 if (nr != ry) |
|
89 error ("ols: incorrect matrix dimensions"); |
|
90 endif |
|
91 |
|
92 Z = X' * X; |
|
93 r = rank (Z); |
|
94 |
|
95 if (r == nc) |
|
96 BETA = inv (Z) * X' * Y; |
|
97 else |
|
98 BETA = pinv (X) * Y; |
|
99 endif |
|
100 |
|
101 R = Y - X * BETA; |
|
102 SIGMA = R' * R / (nr - r); |
|
103 |
|
104 endfunction |