Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_correg_corrmat_nearest (g02aa)

## Purpose

nag_correg_corrmat_nearest (g02aa) computes the nearest correlation matrix, in the Frobenius norm, to a given square, input matrix.

## Syntax

[g, x, iter, feval, nrmgrd, ifail] = g02aa(g, 'n', n, 'errtol', errtol, 'maxits', maxits, 'maxit', maxit)
[g, x, iter, feval, nrmgrd, ifail] = nag_correg_corrmat_nearest(g, 'n', n, 'errtol', errtol, 'maxits', maxits, 'maxit', maxit)
Note: the interface to this routine has changed since earlier releases of the toolbox:
Mark 23: errtol, maxits, maxit now optional
.

## Description

A correlation matrix may be characterised as a real square matrix that is symmetric, has a unit diagonal and is positive semidefinite.
nag_correg_corrmat_nearest (g02aa) applies an inexact Newton method to a dual formulation of the problem, as described by Qi and Sun (2006). It applies the improvements suggested by Borsdorf and Higham (2010).

## References

Borsdorf R and Higham N J (2010) A preconditioned (Newton) algorithm for the nearest correlation matrix IMA Journal of Numerical Analysis 30(1) 94–107
Qi H and Sun D (2006) A quadratically convergent Newton method for computing the nearest correlation matrix SIAM J. Matrix AnalAppl 29(2) 360–385

## Parameters

### Compulsory Input Parameters

1:     g(ldg,n) – double array
ldg, the first dimension of the array, must satisfy the constraint ldgn$\mathit{ldg}\ge {\mathbf{n}}$.
G$G$, the initial matrix.

### Optional Input Parameters

1:     n – int64int32nag_int scalar
Default: The first dimension of the array g and the second dimension of the array g. (An error is raised if these dimensions are not equal.)
The size of the matrix G$G$.
Constraint: n > 0${\mathbf{n}}>0$.
2:     errtol – double scalar
The termination tolerance for the Newton iteration. If errtol0.0${\mathbf{errtol}}\le 0.0$ then n × sqrt(machine precision) is used.
Default: 0.0$0.0$
3:     maxits – int64int32nag_int scalar
maxits specifies the maximum number of iterations used for the iterative scheme used to solve the linear algebraic equations at each Newton step.
If maxits0${\mathbf{maxits}}\le 0$, 2 × n$2×{\mathbf{n}}$ is used.
Default: 0$0$
4:     maxit – int64int32nag_int scalar
Specifies the maximum number of Newton iterations.
If maxit0${\mathbf{maxit}}\le 0$, 200$200$ is used.
Default: 0$0$

ldg ldx

### Output Parameters

1:     g(ldg,n) – double array
ldgn$\mathit{ldg}\ge {\mathbf{n}}$.
A symmetric matrix (1/2)(G + GT)$\frac{1}{2}\left(G+{G}^{\mathrm{T}}\right)$ with the diagonal set to I$I$.
2:     x(ldx,n) – double array
ldxn$\mathit{ldx}\ge {\mathbf{n}}$.
Contains the nearest correlation matrix.
3:     iter – int64int32nag_int scalar
The number of Newton steps taken.
4:     feval – int64int32nag_int scalar
The number of function evaluations of the dual problem.
5:     nrmgrd – double scalar
The norm of the gradient of the last Newton step.
6:     ifail – int64int32nag_int scalar
${\mathrm{ifail}}={\mathbf{0}}$ unless the function detects an error (see [Error Indicators and Warnings]).

## Error Indicators and Warnings

Errors or warnings detected by the function:

Cases prefixed with W are classified as warnings and do not generate an error of type NAG:error_n. See nag_issue_warnings.

ifail = 1${\mathbf{ifail}}=1$
 On entry, n ≤ 0${\mathbf{n}}\le 0$, or ldg < n$\mathit{ldg}<{\mathbf{n}}$, or ldx < n$\mathit{ldx}<{\mathbf{n}}$.
ifail = 2${\mathbf{ifail}}=2$
The function fails to converge in maxit iterations. Increase maxit or check the call to the function.
W ifail = 3${\mathbf{ifail}}=3$
Machine precision is limiting convergence. In this instance the returned value of x may be useful.
ifail = 4${\mathbf{ifail}}=4$
ifail = 999${\mathbf{ifail}}=-999$
Internal memory allocation failed.

## Accuracy

The returned accuracy is controlled by errtol and limited by machine precision.

Arrays are internally allocated by nag_correg_corrmat_nearest (g02aa). The total size of these arrays is 11 × n + 3 × n × n + max (2 × n × n + 6 × n + 1,120 + 9 × n)$11×{\mathbf{n}}+3×{\mathbf{n}}×{\mathbf{n}}+\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(2×{\mathbf{n}}×{\mathbf{n}}+6×{\mathbf{n}}+1,120+9×{\mathbf{n}}\right)$ real elements and 5 × n + 3$5×{\mathbf{n}}+3$ integer elements.

## Example

```function nag_correg_corrmat_nearest_example
g = [2, -1, 0, 0;
-1, 2, -1, 0;
0, -1, 2, -1;
0, 0, -1, 2];
[gOut, x, iter, feval, nrmgrd, ifail] = nag_correg_corrmat_nearest(g);
if (ifail == 0)
fprintf('\n     Nearest Correlation Matrix\n');
disp(x);
fprintf('\n Number of Newton steps taken:   %d\n', iter);
fprintf(' Number of function evaluations: %d\n', feval);
if (nrmgrd > 4*nag_machine_precision)
fprintf(' Norm of gradient of last Newton step: %6.4f\n', nrmgrd);
end
end
```
```

Nearest Correlation Matrix
1.0000   -0.8084    0.1916    0.1068
-0.8084    1.0000   -0.6562    0.1916
0.1916   -0.6562    1.0000   -0.8084
0.1068    0.1916   -0.8084    1.0000

Number of Newton steps taken:   3
Number of function evaluations: 4
Norm of gradient of last Newton step: 0.0000

```
```function g02aa_example
g = [2, -1, 0, 0;
-1, 2, -1, 0;
0, -1, 2, -1;
0, 0, -1, 2];
[gOut, x, iter, feval, nrmgrd, ifail] = g02aa(g);
if (ifail == 0)
fprintf('\n     Nearest Correlation Matrix\n');
disp(x);
fprintf('\n Number of Newton steps taken:   %d\n', iter);
fprintf(' Number of function evaluations: %d\n', feval);
if (nrmgrd > 4*x02aj)
fprintf(' Norm of gradient of last Newton step: %6.4f\n', nrmgrd);
end
end
```
```

Nearest Correlation Matrix
1.0000   -0.8084    0.1916    0.1068
-0.8084    1.0000   -0.6562    0.1916
0.1916   -0.6562    1.0000   -0.8084
0.1068    0.1916   -0.8084    1.0000

Number of Newton steps taken:   3
Number of function evaluations: 4
Norm of gradient of last Newton step: 0.0000

```