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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
7 ##
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
8 ## This file is part of Octave.
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
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
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
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
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
14 ##
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
15 ## Octave is distributed in the hope that it will be useful, but
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
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
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
19 ##
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
20 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
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
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
25
3458
d25bc039237b [project @ 2000-01-19 09:36:14 by jwe]
jwe
parents: 3456
diff changeset
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
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
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
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
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
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
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
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
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
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3368
diff changeset
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
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3368
diff changeset
69 ##
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
70 ## The return values @var{beta}, @var{sigma}, and @var{r} are defined as
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
71 ## follows.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3368
diff changeset
72 ##
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
73 ## @table @var
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
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
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3368
diff changeset
87 ##
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
88 ## @item sigma
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
89 ## The OLS estimator for the matrix @var{s},
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3368
diff changeset
90 ##
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
91 ## @example
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
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
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
94 ## @end group
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
95 ## @end example
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3368
diff changeset
96 ##
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
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
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
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
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3200
diff changeset
101 ## @end deftypefn
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
102
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
103 ## Author: Teresa Twaroch <twaroch@ci.tuwien.ac.at>
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
104 ## Adapted-By: jwe
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
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
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
107
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
108 if (nargin != 2)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5307
diff changeset
109 print_usage ();
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
110 endif
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
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
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
118 endif
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
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
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
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
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
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
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
156 endif
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
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
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
166
14342
2cd56a5e3a66 new tests
John W. Eaton <jwe@octave.org>
parents: 14341
diff changeset
167 %!test
2cd56a5e3a66 new tests
John W. Eaton <jwe@octave.org>
parents: 14341
diff changeset
168 %! x = [1, 2; 3, 4];
2cd56a5e3a66 new tests
John W. Eaton <jwe@octave.org>
parents: 14341
diff changeset
169 %! y = [1; 2];
2cd56a5e3a66 new tests
John W. Eaton <jwe@octave.org>
parents: 14341
diff changeset
170 %! [b, s, r] = ols (x, y);
2cd56a5e3a66 new tests
John W. Eaton <jwe@octave.org>
parents: 14341
diff changeset
171 %! assert (b, [1.4, 2], 2*eps);
2cd56a5e3a66 new tests
John W. Eaton <jwe@octave.org>
parents: 14341
diff changeset
172 %! assert (s, [0.2, 0; 0, 0], 2*eps);
2cd56a5e3a66 new tests
John W. Eaton <jwe@octave.org>
parents: 14341
diff changeset
173 %! assert (r, [-0.4, 0; 0.2, 0], 2*eps);
2cd56a5e3a66 new tests
John W. Eaton <jwe@octave.org>
parents: 14341
diff changeset
174
2cd56a5e3a66 new tests
John W. Eaton <jwe@octave.org>
parents: 14341
diff changeset
175 %!test
2cd56a5e3a66 new tests
John W. Eaton <jwe@octave.org>
parents: 14341
diff changeset
176 %! x = [1, 2; 3, 4];
2cd56a5e3a66 new tests
John W. Eaton <jwe@octave.org>
parents: 14341
diff changeset
177 %! y = [1; 2];
2cd56a5e3a66 new tests
John W. Eaton <jwe@octave.org>
parents: 14341
diff changeset
178 %! [b, s] = ols (x, y);
2cd56a5e3a66 new tests
John W. Eaton <jwe@octave.org>
parents: 14341
diff changeset
179 %! assert (b, [1.4, 2], 2*eps);
2cd56a5e3a66 new tests
John W. Eaton <jwe@octave.org>
parents: 14341
diff changeset
180 %! assert (s, [0.2, 0; 0, 0], 2*eps);
2cd56a5e3a66 new tests
John W. Eaton <jwe@octave.org>
parents: 14341
diff changeset
181
2cd56a5e3a66 new tests
John W. Eaton <jwe@octave.org>
parents: 14341
diff changeset
182 %!test
2cd56a5e3a66 new tests
John W. Eaton <jwe@octave.org>
parents: 14341
diff changeset
183 %! x = [1, 2; 3, 4];
2cd56a5e3a66 new tests
John W. Eaton <jwe@octave.org>
parents: 14341
diff changeset
184 %! y = [1; 2];
2cd56a5e3a66 new tests
John W. Eaton <jwe@octave.org>
parents: 14341
diff changeset
185 %! b = ols (x, y);
2cd56a5e3a66 new tests
John W. Eaton <jwe@octave.org>
parents: 14341
diff changeset
186 %! assert (b, [1.4, 2], 2*eps);
2cd56a5e3a66 new tests
John W. Eaton <jwe@octave.org>
parents: 14341
diff changeset
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))