1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
|
/* normal.c
*
* Copyright (C) 2015, 2016 Patrick Alken
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <config.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_linalg.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_blas.h>
#include <gsl/gsl_eigen.h>
#include <gsl/gsl_multifit.h>
#include <gsl/gsl_multilarge.h>
#include <gsl/gsl_permutation.h>
typedef struct
{
size_t p; /* number of columns of LS matrix */
gsl_matrix *ATA; /* A^T A, p-by-p */
gsl_vector *ATb; /* A^T b, p-by-1 */
double normb; /* || b || */
gsl_matrix *work_ATA; /* workspace for chol(ATA), p-by-p */
gsl_permutation *perm; /* permutation vector */
gsl_vector *workp; /* workspace size p */
gsl_vector *work3p; /* workspace size 3*p */
gsl_vector *D; /* scale factors for ATA, size p */
gsl_vector *c; /* solution vector for L-curve */
int eigen; /* 1 if eigenvalues computed */
double eval_min; /* minimum eigenvalue */
double eval_max; /* maximum eigenvalue */
gsl_eigen_symm_workspace *eigen_p;
} normal_state_t;
static void *normal_alloc(const size_t p);
static void normal_free(void *vstate);
static int normal_reset(void *vstate);
static int normal_accumulate(gsl_matrix * A, gsl_vector * b,
void * vstate);
static int normal_solve(const double lambda, gsl_vector * x,
double * rnorm, double * snorm,
void * vstate);
static int normal_rcond(double * rcond, void * vstate);
static int normal_lcurve(gsl_vector * reg_param, gsl_vector * rho,
gsl_vector * eta, void * vstate);
static int normal_solve_system(const double lambda, gsl_vector * x,
normal_state_t *state);
static int normal_solve_cholesky(gsl_matrix * ATA, const gsl_vector * ATb,
gsl_vector * x, normal_state_t *state);
static int normal_calc_norms(const gsl_vector *x, double *rnorm,
double *snorm, normal_state_t *state);
static int normal_eigen(normal_state_t *state);
/*
normal_alloc()
Allocate workspace for solving large linear least squares
problems using the normal equations approach
Inputs: p - number of columns of LS matrix
Return: pointer to workspace
*/
static void *
normal_alloc(const size_t p)
{
normal_state_t *state;
if (p == 0)
{
GSL_ERROR_NULL("p must be a positive integer",
GSL_EINVAL);
}
state = calloc(1, sizeof(normal_state_t));
if (!state)
{
GSL_ERROR_NULL("failed to allocate normal state", GSL_ENOMEM);
}
state->p = p;
state->ATA = gsl_matrix_alloc(p, p);
if (state->ATA == NULL)
{
normal_free(state);
GSL_ERROR_NULL("failed to allocate ATA matrix", GSL_ENOMEM);
}
state->work_ATA = gsl_matrix_alloc(p, p);
if (state->work_ATA == NULL)
{
normal_free(state);
GSL_ERROR_NULL("failed to allocate temporary ATA matrix", GSL_ENOMEM);
}
state->ATb = gsl_vector_alloc(p);
if (state->ATb == NULL)
{
normal_free(state);
GSL_ERROR_NULL("failed to allocate ATb vector", GSL_ENOMEM);
}
state->perm = gsl_permutation_alloc(p);
if (state->perm == NULL)
{
normal_free(state);
GSL_ERROR_NULL("failed to allocate perm", GSL_ENOMEM);
}
state->D = gsl_vector_alloc(p);
if (state->D == NULL)
{
normal_free(state);
GSL_ERROR_NULL("failed to allocate D vector", GSL_ENOMEM);
}
state->workp = gsl_vector_alloc(p);
if (state->workp == NULL)
{
normal_free(state);
GSL_ERROR_NULL("failed to allocate temporary ATb vector", GSL_ENOMEM);
}
state->work3p = gsl_vector_alloc(3 * p);
if (state->work3p == NULL)
{
normal_free(state);
GSL_ERROR_NULL("failed to allocate work3p", GSL_ENOMEM);
}
state->c = gsl_vector_alloc(p);
if (state->c == NULL)
{
normal_free(state);
GSL_ERROR_NULL("failed to allocate c vector", GSL_ENOMEM);
}
state->eigen_p = gsl_eigen_symm_alloc(p);
if (state->eigen_p == NULL)
{
normal_free(state);
GSL_ERROR_NULL("failed to allocate eigen workspace", GSL_ENOMEM);
}
normal_reset(state);
return state;
}
static void
normal_free(void *vstate)
{
normal_state_t *state = (normal_state_t *) vstate;
if (state->ATA)
gsl_matrix_free(state->ATA);
if (state->work_ATA)
gsl_matrix_free(state->work_ATA);
if (state->ATb)
gsl_vector_free(state->ATb);
if (state->perm)
gsl_permutation_free(state->perm);
if (state->D)
gsl_vector_free(state->D);
if (state->workp)
gsl_vector_free(state->workp);
if (state->work3p)
gsl_vector_free(state->work3p);
if (state->c)
gsl_vector_free(state->c);
if (state->eigen_p)
gsl_eigen_symm_free(state->eigen_p);
free(state);
}
static int
normal_reset(void *vstate)
{
normal_state_t *state = (normal_state_t *) vstate;
gsl_matrix_set_zero(state->ATA);
gsl_vector_set_zero(state->ATb);
state->normb = 0.0;
state->eigen = 0;
state->eval_min = 0.0;
state->eval_max = 0.0;
return GSL_SUCCESS;
}
/*
normal_accumulate()
Add a new block of rows to the normal equations system
Inputs: A - new block of rows, n-by-p
b - new rhs vector n-by-1
vstate - workspace
Return: success/error
*/
static int
normal_accumulate(gsl_matrix * A, gsl_vector * b, void * vstate)
{
normal_state_t *state = (normal_state_t *) vstate;
const size_t n = A->size1;
if (A->size2 != state->p)
{
GSL_ERROR("columns of A do not match workspace", GSL_EBADLEN);
}
else if (n != b->size)
{
GSL_ERROR("A and b have different numbers of rows", GSL_EBADLEN);
}
else
{
int s;
/* ATA += A^T A, using only the lower half of the matrix */
s = gsl_blas_dsyrk(CblasLower, CblasTrans, 1.0, A, 1.0, state->ATA);
if (s)
return s;
/* ATb += A^T b */
s = gsl_blas_dgemv(CblasTrans, 1.0, A, b, 1.0, state->ATb);
if (s)
return s;
/* update || b || */
state->normb = gsl_hypot(state->normb, gsl_blas_dnrm2(b));
return GSL_SUCCESS;
}
}
/*
normal_solve()
Solve normal equations system:
(A^T A + \lambda^2 I) x = A^T b
using Cholesky decomposition
Inputs: lambda - regularization parameter
x - (output) solution vector p-by-1
rnorm - (output) residual norm ||b - A x||
snorm - (output) solution norm ||x||
vstate - workspace
Return: success/error
*/
static int
normal_solve(const double lambda, gsl_vector * x,
double * rnorm, double * snorm,
void * vstate)
{
normal_state_t *state = (normal_state_t *) vstate;
if (x->size != state->p)
{
GSL_ERROR("solution vector does not match workspace", GSL_EBADLEN);
}
else
{
int status;
/* solve system (A^T A) x = A^T b */
status = normal_solve_system(lambda, x, state);
if (status)
{
GSL_ERROR("failed to solve normal equations", status);
}
/* compute residual norm ||y - X c|| and solution norm ||x|| */
normal_calc_norms(x, rnorm, snorm, state);
return GSL_SUCCESS;
}
}
static int
normal_rcond(double * rcond, void * vstate)
{
normal_state_t *state = (normal_state_t *) vstate;
int status = GSL_SUCCESS;
double rcond_ATA;
status = gsl_linalg_pcholesky_rcond(state->work_ATA, state->perm, &rcond_ATA, state->work3p);
if (status == GSL_SUCCESS)
*rcond = sqrt(rcond_ATA);
return status;
}
/*
normal_lcurve()
Compute L-curve of least squares system
Inputs: reg_param - (output) vector of regularization parameters
rho - (output) vector of residual norms
eta - (output) vector of solution norms
vstate - workspace
Return: success/error
*/
static int
normal_lcurve(gsl_vector * reg_param, gsl_vector * rho,
gsl_vector * eta, void * vstate)
{
normal_state_t *state = (normal_state_t *) vstate;
int status;
double smin, smax; /* minimum/maximum singular values */
size_t i;
if (state->eigen == 0)
{
status = normal_eigen(state);
if (status)
return status;
}
if (state->eval_max < 0.0)
{
GSL_ERROR("matrix is not positive definite", GSL_EDOM);
}
/* compute singular values which are sqrts of eigenvalues */
smax = sqrt(state->eval_max);
if (state->eval_min > 0.0)
smin = sqrt(state->eval_min);
else
smin = 0.0;
/* compute vector of regularization parameters */
gsl_multifit_linear_lreg(smin, smax, reg_param);
/* solve normal equations for each regularization parameter */
for (i = 0; i < reg_param->size; ++i)
{
double lambda = gsl_vector_get(reg_param, i);
double rnorm, snorm;
status = normal_solve_system(lambda, state->c, state);
if (status)
return status;
/* compute ||y - X c|| and ||c|| */
normal_calc_norms(state->c, &rnorm, &snorm, state);
gsl_vector_set(rho, i, rnorm);
gsl_vector_set(eta, i, snorm);
}
return GSL_SUCCESS;
}
/*
normal_solve_system()
Compute solution to normal equations:
(A^T A + lambda^2*I) x = A^T b
using LDL decomposition.
Inputs: x - (output) solution vector
state - workspace
Return: success/error
*/
static int
normal_solve_system(const double lambda, gsl_vector * x, normal_state_t *state)
{
int status;
const double lambda_sq = lambda * lambda;
gsl_vector_view d = gsl_matrix_diagonal(state->work_ATA);
/* copy ATA matrix to temporary workspace and regularize */
gsl_matrix_tricpy('L', 1, state->work_ATA, state->ATA);
gsl_vector_add_constant(&d.vector, lambda_sq);
/* solve with LDL decomposition */
status = normal_solve_cholesky(state->work_ATA, state->ATb, x, state);
if (status)
return status;
return status;
}
static int
normal_solve_cholesky(gsl_matrix * ATA, const gsl_vector * ATb,
gsl_vector * x, normal_state_t *state)
{
int status;
status = gsl_linalg_pcholesky_decomp2(ATA, state->perm, state->D);
if (status)
return status;
status = gsl_linalg_pcholesky_solve2(ATA, state->perm, state->D, ATb, x);
if (status)
return status;
return GSL_SUCCESS;
}
/*
normal_calc_norms()
Compute residual norm ||y - X c|| and solution
norm ||c||
Inputs: x - solution vector
rnorm - (output) residual norm ||y - X c||
snorm - (output) solution norm ||c||
state - workspace
*/
static int
normal_calc_norms(const gsl_vector *x, double *rnorm,
double *snorm, normal_state_t *state)
{
double r2;
/* compute solution norm ||x|| */
*snorm = gsl_blas_dnrm2(x);
/* compute residual norm ||b - Ax|| */
/* compute: A^T A x - 2 A^T b */
gsl_vector_memcpy(state->workp, state->ATb);
gsl_blas_dsymv(CblasLower, 1.0, state->ATA, x, -2.0, state->workp);
/* compute: x^T A^T A x - 2 x^T A^T b */
gsl_blas_ddot(x, state->workp, &r2);
/* add b^T b */
r2 += state->normb * state->normb;
*rnorm = sqrt(r2);
return GSL_SUCCESS;
}
/*
normal_eigen()
Compute eigenvalues of A^T A matrix, which
are stored in state->workp on output. Also,
state->eval_min and state->eval_max are set
to the minimum/maximum eigenvalues
*/
static int
normal_eigen(normal_state_t *state)
{
int status;
/* copy lower triangle of ATA to temporary workspace */
gsl_matrix_tricpy('L', 1, state->work_ATA, state->ATA);
/* compute eigenvalues of ATA */
status = gsl_eigen_symm(state->work_ATA, state->workp, state->eigen_p);
if (status)
return status;
gsl_vector_minmax(state->workp, &state->eval_min, &state->eval_max);
state->eigen = 1;
return GSL_SUCCESS;
}
static const gsl_multilarge_linear_type normal_type =
{
"normal",
normal_alloc,
normal_reset,
normal_accumulate,
normal_solve,
normal_rcond,
normal_lcurve,
normal_free
};
const gsl_multilarge_linear_type * gsl_multilarge_linear_normal =
&normal_type;
|