3 Copyright (C) 2005 Free Software Foundation, Inc.
4 Written by Jason H Stover.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or (at
9 your option) any later version.
11 This program 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.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Find the least-squares estimate of b for the linear model:
27 where Y is an n-by-1 column vector, X is an n-by-p matrix of
28 independent variables, b is a p-by-1 vector of regression coefficients,
29 and Z is an n-by-1 normally-distributed random vector with independent
30 identically distributed components with mean 0.
32 This estimate is found via the sweep operator, which is a modification
33 of Gauss-Jordan pivoting.
38 Matrix Computations, third edition. GH Golub and CF Van Loan.
39 The Johns Hopkins University Press. 1996. ISBN 0-8018-5414-8.
41 Numerical Analysis for Statisticians. K Lange. Springer. 1999.
44 Numerical Linear Algebra for Applications in Statistics. JE Gentle.
45 Springer. 1998. ISBN 0-387-98542-5.
51 The matrix A will be overwritten. In ordinary uses of the sweep
52 operator, A will be the matrix
60 X refers to the design matrix and Y to the vector of dependent
61 observations. reg_sweep sweeps on the diagonal elements of
64 The matrix A is assumed to be symmetric, so the sweep operation is
65 performed only for the upper triangle of A.
69 reg_sweep (gsl_matrix * A)
80 if (A->size1 == A->size2)
82 B = gsl_matrix_alloc (A->size1, A->size2);
83 for (k = 0; k < (A->size1 - 1); k++)
85 sweep_element = gsl_matrix_get (A, k, k);
86 if (fabs (sweep_element) > GSL_DBL_MIN)
88 tmp = -1.0 / sweep_element;
89 gsl_matrix_set (B, k, k, tmp);
91 Rows before current row k.
93 for (i = 0; i < k; i++)
95 for (j = i; j < A->size2; j++)
98 Use only the upper triangle of A.
102 tmp = gsl_matrix_get (A, i, j) -
103 gsl_matrix_get (A, i, k)
104 * gsl_matrix_get (A, j, k) / sweep_element;
105 gsl_matrix_set (B, i, j, tmp);
109 tmp = gsl_matrix_get (A, i, j) -
110 gsl_matrix_get (A, i, k)
111 * gsl_matrix_get (A, k, j) / sweep_element;
112 gsl_matrix_set (B, i, j, tmp);
116 tmp = gsl_matrix_get (A, i, k) / sweep_element;
117 gsl_matrix_set (B, i, j, tmp);
124 for (j = k + 1; j < A->size1; j++)
126 tmp = gsl_matrix_get (A, k, j) / sweep_element;
127 gsl_matrix_set (B, k, j, tmp);
130 Rows after the current row k.
132 for (i = k + 1; i < A->size1; i++)
134 for (j = i; j < A->size2; j++)
136 tmp = gsl_matrix_get (A, i, j) -
137 gsl_matrix_get (A, k, i)
138 * gsl_matrix_get (A, k, j) / sweep_element;
139 gsl_matrix_set (B, i, j, tmp);
143 for (i = 0; i < A->size1; i++)
144 for (j = i; j < A->size2; j++)
146 gsl_matrix_set (A, i, j, gsl_matrix_get (B, i, j));