The R Project SVN R

Rev

Rev 68947 | Rev 82142 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 68947 Rev 72555
Line 1... Line 1...
1
/*
1
/*
2
 *  R : A Computer Language for Statistical Data Analysis
2
 *  R : A Computer Language for Statistical Data Analysis
-
 
3
 *  Copyright (C) 1999-2017   The R Core Team
-
 
4
 *  Copyright (C) 2003-2017   The R Foundation
3
 *  Copyright (C) 1997-1999   Saikat DebRoy
5
 *  Copyright (C) 1997-1999   Saikat DebRoy
4
 *  Copyright (C) 1999-2015   The R Core Team
-
 
5
 *  Copyright (C) 2003-2010   The R Foundation
-
 
6
 *
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  (at your option) any later version.
10
 *  (at your option) any later version.
Line 237... Line 237...
237
} /* lltslv */
237
} /* lltslv */
238
 
238
 
239
static void
239
static void
240
choldc(int nr, int n, double *a, double diagmx, double tol, double *addmax)
240
choldc(int nr, int n, double *a, double diagmx, double tol, double *addmax)
241
{
241
{
242
/* Find the perturbed l(l-transpose) [written ll+] decomposition
242
/* Find the perturbed L(L-transpose) [written LL+] decomposition
243
 * of a+d, where d is a non-negative diagonal matrix added to a if
243
 * of a+d, where d is a non-negative diagonal matrix added to a if
244
 * necessary to allow the cholesky decomposition to continue.
244
 * necessary to allow the cholesky decomposition to continue.
245
 
245
 
246
 * PARAMETERS :
246
 * PARAMETERS :
247
 
247
 
248
 *	nr	     --> row dimension of matrix
248
 *	nr	     --> row dimension of matrix
249
 *	n	     --> dimension of problem
249
 *	n	     --> dimension of problem
250
 *	a(n,n)	    <--> on entry: matrix for which to find perturbed
250
 *	a(n,n)	    <--> on entry: matrix for which to find perturbed
251
 *			      cholesky decomposition
251
 *			      cholesky decomposition
252
 *			 on exit:  contains l of ll+ decomposition
252
 *			 on exit:  contains L of LL+ decomposition
253
 *			 in lower triangular part and diagonal of "a"
253
 *			 in lower triangular part and diagonal of "a"
254
 *	diagmx	     --> maximum diagonal element of "a"
254
 *	diagmx	     --> maximum diagonal element of "a"
255
 *	tol	     --> tolerance
255
 *	tol	     --> tolerance
256
 *	addmax	    <--	 maximum amount implicitly added to diagonal of "a"
256
 *	addmax	    <--	 maximum amount implicitly added to diagonal of "a"
257
 *			 in forming the cholesky decomposition of a+d
257
 *			 in forming the cholesky decomposition of a+d
258
 *	internal variables
258
 *	internal variables
259
 
259
 
260
 *	aminl	 smallest element allowed on diagonal of l
260
 *	aminl	 smallest element allowed on diagonal of L
261
 *	amnlsq	 =aminl**2
261
 *	amnlsq	 =aminl**2
262
 *	offmax	 maximum off-diagonal element in column of a
262
 *	offmax	 maximum off-diagonal element in column of a
263
 
263
 
264
 
264
 
265
 *	description
265
 *	description
266
 
266
 
267
 *	the normal cholesky decomposition is performed.	 however, if at any
267
 *	the normal cholesky decomposition is performed.	 however, if at any
268
 *	point the algorithm would attempt to set l(i,i)=sqrt(temp)
268
 *	point the algorithm would attempt to set L(i,i)=sqrt(temp)
269
 *	with temp < tol*diagmx, then l(i,i) is set to sqrt(tol*diagmx)
269
 *	with temp < tol*diagmx, then L(i,i) is set to sqrt(tol*diagmx)
270
 *	instead.  this is equivalent to adding tol*diagmx-temp to a(i,i)
270
 *	instead.  this is equivalent to adding tol*diagmx-temp to a(i,i)
271
 */
271
 */
272
 
272
 
273
    double tmp1, tmp2;
273
    double tmp1, tmp2;
274
    int i, j, k;
274
    int i, j, k;
Line 277... Line 277...
277
 
277
 
278
    *addmax = 0.0;
278
    *addmax = 0.0;
279
    aminl = sqrt(diagmx * tol);
279
    aminl = sqrt(diagmx * tol);
280
    amnlsq = aminl * aminl;
280
    amnlsq = aminl * aminl;
281
 
281
 
282
    /*	form row i of l */
282
    /*	form row i of L */
283
 
283
 
284
    for (i = 0; i < n; ++i) {
284
    for (i = 0; i < n; ++i) {
-
 
285
 
-
 
286
	// A[i,j] := * || find i,j element of lower triangular matrix L
-
 
287
	for (j = 0; j < i; ++j) {
-
 
288
	    sum = 0.;
-
 
289
	    for (k = 0; k < j; ++k)
-
 
290
		sum += a[i + k * nr] * a[j + k * nr];
-
 
291
	    a[i + j * nr] = (a[i + j * nr] - sum) / a[j + j * nr];
-
 
292
	}
-
 
293
 
285
	/*	find diagonal elements of l */
294
	// A[i,i] := * || find diagonal elements of L
286
	sum = 0.;
295
	sum = 0.;
287
	for (k = 0; k < i; ++k)
296
	for (k = 0; k < i; ++k)
288
	    sum += a[i + k * nr] * a[i + k * nr];
297
	    sum += a[i + k * nr] * a[i + k * nr];
289
 
298
 
290
	tmp1 = a[i + i * nr] - sum;
299
	tmp1 = a[i + i * nr] - sum;
291
	if (tmp1 >= amnlsq) {
300
	if (tmp1 >= amnlsq) { // normal Cholesky
292
	    a[i + i * nr] = sqrt(tmp1);
301
	    a[i + i * nr] = sqrt(tmp1);
293
	}
302
	}
294
	else {
303
	else { // augment diagonal of L
295
	    /*	find maximum off-diagonal element in row */
304
	    /*	find maximum off-diagonal element in row */
296
	    offmax = 0.;
305
	    offmax = 0.;
297
	    for (j = 0; j < i; ++j) {
306
	    for (j = 0; j < i; ++j) {
298
		if(offmax < (tmp2 = fabs(a[i + j * nr])))
307
		if(offmax < (tmp2 = fabs(a[i + j * nr])))
299
		    offmax = tmp2;
308
		    offmax = tmp2;
Line 303... Line 312...
303
	    /* add to diagonal element to
312
	    /* add to diagonal element to
304
	     * allow cholesky decomposition to continue */
313
	     * allow cholesky decomposition to continue */
305
	    a[i + i * nr] = sqrt(offmax);
314
	    a[i + i * nr] = sqrt(offmax);
306
	    if(*addmax < (tmp2 = offmax - tmp1)) *addmax = tmp2;
315
	    if(*addmax < (tmp2 = offmax - tmp1)) *addmax = tmp2;
307
	}
316
	}
308
	/*	find i,j element of lower triangular matrix */
-
 
309
	for (j = 0; j < i; ++j) {
-
 
310
	    sum = 0.;
-
 
311
	    for (k = 0; k < j; ++k)
-
 
312
		sum += a[i + k * nr] * a[j + k * nr];
-
 
313
	    a[i + j * nr] = (a[i + j * nr] - sum) / a[j + j * nr];
-
 
314
	}
-
 
315
    }
317
    }
316
} /* choldc */
318
} /* choldc */
317
 
319
 
318
static void qraux1(int nr, int n, double *r, int i)
320
static void qraux1(int nr, int n, double *r, int i)
319
{
321
{
Line 1354... Line 1356...
1354
} /* secfac */
1356
} /* secfac */
1355
 
1357
 
1356
static void
1358
static void
1357
chlhsn(int nr, int n, double *a, double epsm, double *sx, double *udiag)
1359
chlhsn(int nr, int n, double *a, double epsm, double *sx, double *udiag)
1358
{
1360
{
1359
/*	find the l(l-transpose) [written ll+] decomposition of the perturbed
1361
/*	find the l(l-transpose) [written LL+] decomposition of the perturbed
1360
 *	model hessian matrix a+mu*i(where mu\0 and i is the identity matrix)
1362
 *	model hessian matrix a+mu*i(where mu\0 and i is the identity matrix)
1361
 *	which is safely positive definite.  if a is safely positive definite
1363
 *	which is safely positive definite.  if a is safely positive definite
1362
 *	upon entry, then mu=0.
1364
 *	upon entry, then mu=0.
1363
 
1365
 
1364
 * PARAMETERS :
1366
 * PARAMETERS :
1365
 
1367
 
1366
 *	nr	     --> row dimension of matrix
1368
 *	nr	     --> row dimension of matrix
1367
 *	n	     --> dimension of problem
1369
 *	n	     --> dimension of problem
1368
 *	a(n,n)	    <--> on entry; "a" is model hessian (only lower
1370
 *	a(n,n)	    <--> on entry; "a" is model hessian (only lower
1369
 *			 triangular part and diagonal stored)
1371
 *			 triangular part and diagonal stored)
1370
 *			 on exit:  a contains l of ll+ decomposition of
1372
 *			 on exit:  a contains l of LL+ decomposition of
1371
 *			 perturbed model hessian in lower triangular
1373
 *			 perturbed model hessian in lower triangular
1372
 *			 part and diagonal and contains hessian in upper
1374
 *			 part and diagonal and contains hessian in upper
1373
 *			 triangular part and udiag
1375
 *			 triangular part and udiag
1374
 *	epsm	     --> machine epsilon
1376
 *	epsm	     --> machine epsilon
1375
 *	sx(n)	     --> diagonal scaling matrix for x
1377
 *	sx(n)	     --> diagonal scaling matrix for x
Line 1391... Line 1393...
1391
 *	such that the diagonal of a:=a+mu*i is all positive
1393
 *	such that the diagonal of a:=a+mu*i is all positive
1392
 *	with the ratio of its smallest to largest element on the
1394
 *	with the ratio of its smallest to largest element on the
1393
 *	order of sqrt(epsm).
1395
 *	order of sqrt(epsm).
1394
 
1396
 
1395
 *	2. "a" undergoes a perturbed cholesky decomposition which
1397
 *	2. "a" undergoes a perturbed cholesky decomposition which
1396
 *	results in an ll+ decomposition of a+d, where d is a
1398
 *	results in an LL+ decomposition of a+d, where d is a
1397
 *	non-negative diagonal matrix which is implicitly added to
1399
 *	non-negative diagonal matrix which is implicitly added to
1398
 *	"a" during the decomposition if "a" is not positive definite.
1400
 *	"a" during the decomposition if "a" is not positive definite.
1399
 *	"a" is retained and not changed during this process by
1401
 *	"a" is retained and not changed during this process by
1400
 *	copying l into the upper triangular part of "a" and the
1402
 *	copying l into the upper triangular part of "a" and the
1401
 *	diagonal into udiag.  then the cholesky decomposition routine
1403
 *	diagonal into udiag.  then the cholesky decomposition routine
Line 1474... Line 1476...
1474
 
1476
 
1475
 
1477
 
1476
    /*	step3
1478
    /*	step3
1477
 
1479
 
1478
     *	if addmax=0, "a" was positive definite going into step 2,
1480
     *	if addmax=0, "a" was positive definite going into step 2,
1479
     *	the ll+ decomposition has been done, and we return.
1481
     *	the LL+ decomposition has been done, and we return.
1480
     *	otherwise, addmax>0.  perturb "a" so that it is safely
1482
     *	otherwise, addmax>0.  perturb "a" so that it is safely
1481
     *	diagonally dominant and find ll+ decomposition */
1483
     *	diagonally dominant and find LL+ decomposition */
1482
 
1484
 
1483
    if (addmax > 0.0) {
1485
    if (addmax > 0.0) {
1484
 
1486
 
1485
	/*	restore original "a" (lower triangular part and diagonal) */
1487
	/*	restore original "a" (lower triangular part and diagonal) */
1486
 
1488