g02ab computes the nearest correlation matrix, in the Frobenius norm or weighted Frobenius norm, and optionally with bounds on the eigenvalues, to a given square, input matrix.

# Syntax

C#
```public static void g02ab(
double[,] g,
int n,
string opt,
double alpha,
double[] w,
double errtol,
int maxits,
int maxit,
double[,] x,
out int iter,
out int feval,
out double nrmgrd,
out int ifail
)```
Visual Basic
```Public Shared Sub g02ab ( _
g As Double(,), _
n As Integer, _
opt As String, _
alpha As Double, _
w As Double(), _
errtol As Double, _
maxits As Integer, _
maxit As Integer, _
x As Double(,), _
<OutAttribute> ByRef iter As Integer, _
<OutAttribute> ByRef feval As Integer, _
<OutAttribute> ByRef nrmgrd As Double, _
<OutAttribute> ByRef ifail As Integer _
)```
Visual C++
```public:
static void g02ab(
array<double,2>^ g,
int n,
String^ opt,
double alpha,
array<double>^ w,
double errtol,
int maxits,
int maxit,
array<double,2>^ x,
[OutAttribute] int% iter,
[OutAttribute] int% feval,
[OutAttribute] double% nrmgrd,
[OutAttribute] int% ifail
)```
F#
```static member g02ab :
g : float[,] *
n : int *
opt : string *
alpha : float *
w : float[] *
errtol : float *
maxits : int *
maxit : int *
x : float[,] *
iter : int byref *
feval : int byref *
nrmgrd : float byref *
ifail : int byref -> unit
```

#### Parameters

g
Type: array<System..::..Double,2>[,](,)[,][,]
An array of size [dim1, n]
Note: dim1 must satisfy the constraint: $\mathrm{dim1}\ge {\mathbf{n}}$
On entry: $G$, the initial matrix.
On exit: $G$ is overwritten.
n
Type: System..::..Int32
On entry: the order of the matrix $G$.
Constraint: ${\mathbf{n}}>0$.
opt
Type: System..::..String
On entry: indicates the problem to be solved.
${\mathbf{opt}}=\text{"A"}$
The lower bound problem is solved.
${\mathbf{opt}}=\text{"W"}$
The weighted norm problem is solved.
${\mathbf{opt}}=\text{"B"}$
Both problems are solved.
Constraint: ${\mathbf{opt}}=\text{"A"}$, $\text{"W"}$ or $\text{"B"}$.
alpha
Type: System..::..Double
On entry: the value of $\alpha$.
If ${\mathbf{opt}}=\text{"W"}$, alpha need not be set.
Constraint: $0.0<{\mathbf{alpha}}<1.0$.
w
Type: array<System..::..Double>[]()[][]
An array of size [n]
On entry: the square roots of the diagonal elements of $W$, that is the diagonal of ${W}^{\frac{1}{2}}$.
If ${\mathbf{opt}}=\text{"A"}$, w need not be set.
On exit: if ${\mathbf{opt}}=\text{"W"}$ or $\text{"B"}$, the array is scaled so $0<{\mathbf{w}}\left[\mathit{i}-1\right]\le 1$, for $\mathit{i}=1,2,\dots ,n$.
Constraint: ${\mathbf{w}}\left[\mathit{i}-1\right]>0.0$, for $\mathit{i}=1,2,\dots ,n$.
errtol
Type: System..::..Double
On entry: the termination tolerance for the Newton iteration. If ${\mathbf{errtol}}\le 0.0$ then  is used.
maxits
Type: System..::..Int32
On entry: specifies the maximum number of iterations to be used by the iterative scheme to solve the linear algebraic equations at each Newton step.
If ${\mathbf{maxits}}\le 0$, $2×{\mathbf{n}}$ is used.
maxit
Type: System..::..Int32
On entry: specifies the maximum number of Newton iterations.
If ${\mathbf{maxit}}\le 0$, $200$ is used.
x
Type: array<System..::..Double,2>[,](,)[,][,]
An array of size [dim1, n]
Note: dim1 must satisfy the constraint: $\mathrm{dim1}\ge {\mathbf{n}}$
On exit: contains the nearest correlation matrix.
iter
Type: System..::..Int32%
On exit: the number of Newton steps taken.
feval
Type: System..::..Int32%
On exit: the number of function evaluations of the dual problem.
nrmgrd
Type: System..::..Double%
On exit: the norm of the gradient of the last Newton step.
ifail
Type: System..::..Int32%
On exit: ${\mathbf{ifail}}={0}$ unless the method detects an error or a warning has been flagged (see [Error Indicators and Warnings]).

# Description

Finds the nearest correlation matrix $X$ by minimizing $\frac{1}{2}{‖G-X‖}^{2}$ where $G$ is an approximate correlation matrix.
The norm can either be the Frobenius norm or the weighted Frobenius norm $\frac{1}{2}{‖{W}^{\frac{1}{2}}\left(G-X\right){W}^{\frac{1}{2}}‖}_{F}^{2}$.
You can optionally specify a lower bound on the eigenvalues, $\alpha$, of the computed correlation matrix, forcing the matrix to be positive definite, $0<\alpha <1$.
Note that if the weights vary by several orders of magnitude from one another the algorithm may fail to converge.

# 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

# Error Indicators and Warnings

Errors or warnings detected by the method:
Some error messages may refer to parameters that are dropped from this interface (LDG, LDX) In these cases, an error in another parameter has usually caused an incorrect value to be inferred.
${\mathbf{ifail}}=-9000$
An error occured, see message report.
${\mathbf{ifail}}=-6000$
Invalid Parameters $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-4000$
Invalid dimension for array $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-8000$
Negative dimension for array $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-6000$
Invalid Parameters $〈\mathit{\text{value}}〉$

# Accuracy

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

# Parallelism and Performance

None.

Arrays are internally allocated by g02ab. The total size of these arrays is $12×{\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×{\mathbf{n}}+3$ integer elements. All allocated memory is freed before return of g02ab.

# Example

This example finds the nearest correlation matrix to:
 $G=2-100-12-100-12-100-12$
weighted by ${W}^{\frac{1}{2}}=\mathrm{diag}\left(100,20,20,20\right)$ with minimum eigenvalue $0.02$.

Example program (C#): g02abe.cs

Example program data: g02abe.d

Example program results: g02abe.r