Mercurial > octave
annotate scripts/linear-algebra/ols.m @ 27980:bdad8ca48700
maint: Remove "Created:" lines from code base.
* lin2mu.m, mu2lin.m, bincoeff.m, common_size.m, deal.m, nextpow2.m,
postpad.m, prepad.m, repmat.m, shift.m, xor.m, inpolygon.m, rotx.m, roty.m,
rotz.m, getappdata.m, isappdata.m, rmappdata.m, setappdata.m, colormap.m,
gray.m, gray2ind.m, image.m, imagesc.m, ind2gray.m, ind2rgb.m, ocean.m,
rgb2ind.m, javachk.m, javamem.m, commutation_matrix.m, cross.m,
duplication_matrix.m, gls.m, housh.m, isdefinite.m, ishermitian.m,
issymmetric.m, null.m, ols.m, orth.m, qzhess.m, vech.m, __w2mpth__.m,
expand_rel_paths.m, make_rel_paths.m, daspect.m, pbaspect.m, rticks.m,
thetaticks.m, xticklabels.m, xticks.m, yticklabels.m, yticks.m, zticklabels.m,
zticks.m, comet.m, errorbar.m, loglogerr.m, __errplot__.m, semilogxerr.m,
semilogyerr.m, cla.m, copyobj.m, hdl2struct.m, linkaxes.m, __ghostscript__.m,
__gnuplot_get_var__.m, __gnuplot_has_feature__.m, __gnuplot_has_terminal__.m,
__gnuplot_open_stream__.m, struct2hdl.m, compan.m, conv.m, deconv.m, mpoles.m,
poly.m, polyder.m, polyfit.m, polyint.m, polyout.m, polyreduce.m, polyval.m,
polyvalm.m, residue.m, roots.m, __parse_movargs__.m, detrend.m, fftconv.m,
fftfilt.m, fftshift.m, ifftshift.m, movfun.m, movslice.m, beta.m, betaln.m,
lcm.m, pow2.m, corr.m, kurtosis.m, skewness.m, untabify.m, calendar.m,
datestr.m, datevec.m, eomday.m, now.m, weekday.m:
Remove "Created:" lines from code base.
author | Rik <rik@octave.org> |
---|---|
date | Tue, 21 Jan 2020 13:06:02 -0800 |
parents | bd51beb6205e |
children | 7f1cec12c240 |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 ######################################################################## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 ## |
27919
1891570abac8
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
27918
diff
changeset
|
3 ## Copyright (C) 1996-2020 The Octave Project Developers |
27918
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
4 ## |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 ## See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 ## distribution or <https://octave.org/copyright/>. |
3200 | 7 ## |
8 ## This file is part of Octave. | |
9 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24512
diff
changeset
|
10 ## Octave is free software: you can redistribute it and/or modify it |
3200 | 11 ## under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24512
diff
changeset
|
12 ## the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
13 ## (at your option) any later version. |
3200 | 14 ## |
15 ## Octave is distributed in the hope that it will be useful, but | |
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
18 ## GNU General Public License for more details. |
3200 | 19 ## |
20 ## You should have received a copy of the GNU General Public License | |
7016 | 21 ## along with Octave; see the file COPYING. If not, see |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24512
diff
changeset
|
22 ## <https://www.gnu.org/licenses/>. |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
23 ## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 ######################################################################## |
3200 | 25 |
3458 | 26 ## -*- texinfo -*- |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20194
diff
changeset
|
27 ## @deftypefn {} {[@var{beta}, @var{sigma}, @var{r}] =} ols (@var{y}, @var{x}) |
24511
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
28 ## Ordinary least squares (OLS) estimation. |
20174
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
29 ## |
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
30 ## OLS applies to the multivariate model |
3368 | 31 ## @tex |
24511
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
32 ## $@var{y} = @var{x}\,@var{b} + @var{e}$ |
3368 | 33 ## @end tex |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7017
diff
changeset
|
34 ## @ifnottex |
24511
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
35 ## @w{@math{@var{y} = @var{x}*@var{b} + @var{e}}} |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7017
diff
changeset
|
36 ## @end ifnottex |
20194
b0f7ee81d974
doc: Remove extra spaces at start of docstring which show up in Info format.
Rik <rik@octave.org>
parents:
20174
diff
changeset
|
37 ## where |
3368 | 38 ## @tex |
24511
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
39 ## $@var{y}$ is a $t \times p$ matrix, $@var{x}$ is a $t \times k$ matrix, |
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
40 ## $@var{b}$ is a $k \times p$ matrix, and $@var{e}$ is a $t \times p$ matrix. |
3368 | 41 ## @end tex |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7017
diff
changeset
|
42 ## @ifnottex |
24511
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
43 ## @math{@var{y}} is a @math{t}-by-@math{p} matrix, @math{@var{x}} is a |
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
44 ## @math{t}-by-@math{k} matrix, @var{b} is a @math{k}-by-@math{p} matrix, and |
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
45 ## @var{e} is a @math{t}-by-@math{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 ## |
24511
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
48 ## Each row of @var{y} is a @math{p}-variate observation in which each column |
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
49 ## represents a variable. Likewise, the rows of @var{x} represent |
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
50 ## @math{k}-variate observations or possibly designed values. Furthermore, |
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
51 ## the collection of observations @var{x} must be of adequate rank, @math{k}, |
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
52 ## otherwise @var{b} cannot be uniquely estimated. |
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
53 ## |
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
54 ## The observation errors, @var{e}, are assumed to originate from an |
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
55 ## underlying @math{p}-variate distribution with zero mean and |
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
56 ## @math{p}-by-@math{p} covariance matrix @var{S}, both constant conditioned |
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
57 ## on @var{x}. Furthermore, the matrix @var{S} is constant with respect to |
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
58 ## each observation such that |
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
59 ## @tex |
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
60 ## $\bar{@var{e}} = 0$ and cov(vec(@var{e})) = kron(@var{s},@var{I}). |
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
61 ## @end tex |
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
62 ## @ifnottex |
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
63 ## @code{mean (@var{e}) = 0} and |
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
64 ## @code{cov (vec (@var{e})) = kron (@var{s}, @var{I})}. |
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
65 ## @end ifnottex |
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
66 ## (For cases |
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
67 ## that don't meet this criteria, such as autocorrelated errors, see |
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
68 ## generalized least squares, gls, for more efficient estimations.) |
3426 | 69 ## |
3368 | 70 ## The return values @var{beta}, @var{sigma}, and @var{r} are defined as |
71 ## follows. | |
3426 | 72 ## |
3368 | 73 ## @table @var |
74 ## @item beta | |
24511
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
75 ## The OLS estimator for matrix @var{b}. |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
76 ## @tex |
24511
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
77 ## @var{beta} is calculated directly via $(@var{x}^T@var{x})^{-1} @var{x}^T |
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
78 ## @var{y}$ if the matrix $@var{x}^T@var{x}$ is of full rank. |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
79 ## @end tex |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
80 ## @ifnottex |
24985
d85b2485af9e
doc: grammarcheck m-files ahead of 4.4 release.
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
81 ## @var{beta} is calculated directly via |
d85b2485af9e
doc: grammarcheck m-files ahead of 4.4 release.
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
82 ## @code{inv (@var{x}'*@var{x}) * @var{x}' * @var{y}} if the matrix |
d85b2485af9e
doc: grammarcheck m-files ahead of 4.4 release.
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
83 ## @code{@var{x}'*@var{x}} is of full rank. |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
84 ## @end ifnottex |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
85 ## Otherwise, @code{@var{beta} = pinv (@var{x}) * @var{y}} where |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
86 ## @code{pinv (@var{x})} denotes the pseudoinverse of @var{x}. |
3426 | 87 ## |
3368 | 88 ## @item sigma |
89 ## The OLS estimator for the matrix @var{s}, | |
3426 | 90 ## |
3368 | 91 ## @example |
92 ## @group | |
24511
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
93 ## @var{sigma} = (@var{y}-@var{x}*@var{beta})' * (@var{y}-@var{x}*@var{beta}) / (@math{t}-rank(@var{x})) |
3368 | 94 ## @end group |
95 ## @end example | |
3426 | 96 ## |
3368 | 97 ## @item r |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
98 ## The matrix of OLS residuals, @code{@var{r} = @var{y} - @var{x}*@var{beta}}. |
3368 | 99 ## @end table |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
100 ## @seealso{gls, pinv} |
3368 | 101 ## @end deftypefn |
3200 | 102 |
103 ## Author: Teresa Twaroch <twaroch@ci.tuwien.ac.at> | |
104 ## Adapted-By: jwe | |
105 | |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
106 function [beta, sigma, r] = ols (y, x) |
3200 | 107 |
108 if (nargin != 2) | |
6046 | 109 print_usage (); |
3200 | 110 endif |
111 | |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
112 if (! (isnumeric (x) && isnumeric (y))) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
113 error ("ols: X and Y must be numeric matrices or vectors"); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
114 endif |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
115 |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
116 if (ndims (x) != 2 || ndims (y) != 2) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
117 error ("ols: X and Y must be 2-D matrices or vectors"); |
3200 | 118 endif |
119 | |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
120 [nr, nc] = size (x); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
121 [ry, cy] = size (y); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
122 if (nr != ry) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
123 error ("ols: number of rows of X and Y must be equal"); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
124 endif |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
125 |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12547
diff
changeset
|
126 if (isinteger (x)) |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12547
diff
changeset
|
127 x = double (x); |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12547
diff
changeset
|
128 endif |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12547
diff
changeset
|
129 if (isinteger (y)) |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12547
diff
changeset
|
130 y = double (y); |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12547
diff
changeset
|
131 endif |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12547
diff
changeset
|
132 |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12547
diff
changeset
|
133 ## Start of algorithm |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
134 z = x' * x; |
13823
94a37dae80a9
Use a cheaper Cholesky decomposition than a rank() svd in ols.m
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12656
diff
changeset
|
135 [u, p] = chol (z); |
3200 | 136 |
13823
94a37dae80a9
Use a cheaper Cholesky decomposition than a rank() svd in ols.m
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12656
diff
changeset
|
137 if (p) |
94a37dae80a9
Use a cheaper Cholesky decomposition than a rank() svd in ols.m
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12656
diff
changeset
|
138 beta = pinv (x) * y; |
3200 | 139 else |
13824
aa0cba2256f4
Group the ols matrix operation in a smarter way
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13823
diff
changeset
|
140 beta = u \ (u' \ (x' * y)); |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
141 endif |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
142 |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
143 if (isargout (2) || isargout (3)) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
144 r = y - x * beta; |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
145 endif |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
146 if (isargout (2)) |
14341
71efccec5936
* ols.m: Compute rank if needed.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
147 |
71efccec5936
* ols.m: Compute rank if needed.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
148 ## z is of full rank, avoid the SVD in rnk |
71efccec5936
* ols.m: Compute rank if needed.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
149 if (p == 0) |
71efccec5936
* ols.m: Compute rank if needed.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
150 rnk = columns (z); |
71efccec5936
* ols.m: Compute rank if needed.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
151 else |
71efccec5936
* ols.m: Compute rank if needed.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
152 rnk = rank (z); |
71efccec5936
* ols.m: Compute rank if needed.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
153 endif |
71efccec5936
* ols.m: Compute rank if needed.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
154 |
12547
17997376291b
Fix variable name clash in ols.m
Michael Creel <michael.creel@uab.es>
parents:
11587
diff
changeset
|
155 sigma = r' * r / (nr - rnk); |
3200 | 156 endif |
157 | |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
158 endfunction |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
159 |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12547
diff
changeset
|
160 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
161 %!test |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
162 %! x = [1:5]'; |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
163 %! y = 3*x + 2; |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
164 %! x = [x, ones(5,1)]; |
21580
ecce63c99c3f
maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents:
21546
diff
changeset
|
165 %! assert (ols (y,x), [3; 2], 50*eps); |
3200 | 166 |
14342 | 167 %!test |
168 %! x = [1, 2; 3, 4]; | |
169 %! y = [1; 2]; | |
170 %! [b, s, r] = ols (x, y); | |
171 %! assert (b, [1.4, 2], 2*eps); | |
172 %! assert (s, [0.2, 0; 0, 0], 2*eps); | |
173 %! assert (r, [-0.4, 0; 0.2, 0], 2*eps); | |
174 | |
175 %!test | |
176 %! x = [1, 2; 3, 4]; | |
177 %! y = [1; 2]; | |
178 %! [b, s] = ols (x, y); | |
179 %! assert (b, [1.4, 2], 2*eps); | |
180 %! assert (s, [0.2, 0; 0, 0], 2*eps); | |
181 | |
182 %!test | |
183 %! x = [1, 2; 3, 4]; | |
184 %! y = [1; 2]; | |
185 %! b = ols (x, y); | |
186 %! assert (b, [1.4, 2], 2*eps); | |
187 | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
188 ## Test input validation |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14342
diff
changeset
|
189 %!error ols () |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14342
diff
changeset
|
190 %!error ols (1) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14342
diff
changeset
|
191 %!error ols (1, 2, 3) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14342
diff
changeset
|
192 %!error ols ([true, true], [1, 2]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14342
diff
changeset
|
193 %!error ols ([1, 2], [true, true]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14342
diff
changeset
|
194 %!error ols (ones (2,2,2), ones (2,2)) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14342
diff
changeset
|
195 %!error ols (ones (2,2), ones (2,2,2)) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14342
diff
changeset
|
196 %!error ols (ones (1,2), ones (2,2)) |