# NAG Library Routine Document

## 1Purpose

g02ajf computes the nearest correlation matrix, using element-wise weighting in the Frobenius norm and optionally with bounds on the eigenvalues, to a given square, input matrix.

## 2Specification

Fortran Interface
 Subroutine g02ajf ( g, ldg, n, h, ldh, x, ldx, iter, norm,
 Integer, Intent (In) :: ldg, n, ldh, maxit, ldx Integer, Intent (Inout) :: ifail Integer, Intent (Out) :: iter Real (Kind=nag_wp), Intent (In) :: alpha, errtol Real (Kind=nag_wp), Intent (Inout) :: g(ldg,n), h(ldh,n), x(ldx,n) Real (Kind=nag_wp), Intent (Out) :: norm
#include nagmk26.h
 void g02ajf_ (double g[], const Integer *ldg, const Integer *n, const double *alpha, double h[], const Integer *ldh, const double *errtol, const Integer *maxit, double x[], const Integer *ldx, Integer *iter, double *norm, Integer *ifail)

## 3Description

g02ajf finds the nearest correlation matrix, $X$, to an approximate correlation matrix, $G$, using element-wise weighting, this minimizes ${‖H\circ \left(G-X\right)‖}_{F}$, where $C=A\circ B$ denotes the matrix $C$ with elements ${C}_{ij}={A}_{ij}×{B}_{ij}$.
You can optionally specify a lower bound on the eigenvalues, $\alpha$, of the computed correlation matrix, forcing the matrix to be strictly positive definite, if $0<\alpha <1$.
Zero elements in $H$ should be used when you wish to put no emphasis on the corresponding element of $G$. The algorithm scales $H$ so that the maximum element is $1$. It is this scaled matrix that is used in computing the norm above and for the stopping criteria described in Section 7.
Note that if the elements in $H$ vary by several orders of magnitude from one another the algorithm may fail to converge.

## 4References

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
Jiang K, Sun D and Toh K-C (2012) An inexact accelerated proximal gradient method for large scale linearly constrained convex SDP SIAM J. Optim. 22(3) 1042–1064
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

## 5Arguments

1:     $\mathbf{g}\left({\mathbf{ldg}},{\mathbf{n}}\right)$ – Real (Kind=nag_wp) arrayInput/Output
On entry: $G$, the initial matrix.
On exit: $G$ is overwritten.
2:     $\mathbf{ldg}$ – IntegerInput
On entry: the first dimension of the array g as declared in the (sub)program from which g02ajf is called.
Constraint: ${\mathbf{ldg}}\ge {\mathbf{n}}$.
3:     $\mathbf{n}$ – IntegerInput
On entry: the order of the matrix $G$.
Constraint: ${\mathbf{n}}>0$.
4:     $\mathbf{alpha}$ – Real (Kind=nag_wp)Input
On entry: the value of $\alpha$.
If ${\mathbf{alpha}}<0.0$, $0.0$ is used.
Constraint: ${\mathbf{alpha}}<1.0$.
5:     $\mathbf{h}\left({\mathbf{ldh}},{\mathbf{n}}\right)$ – Real (Kind=nag_wp) arrayInput/Output
On entry: the matrix of weights $H$.
On exit: a symmetric matrix $\frac{1}{2}\left(H+{H}^{\mathrm{T}}\right)$ with its diagonal elements set to zero and the remaining elements scaled so that the maximum element is $1.0$.
Constraint: $\mathit{H}\left(\mathit{i},\mathit{j}\right)\ge 0.0$, for all $i$ and $j=1,2,\dots ,n$, $i\ne j$.
6:     $\mathbf{ldh}$ – IntegerInput
On entry: the first dimension of the array h as declared in the (sub)program from which g02ajf is called.
Constraint: ${\mathbf{ldh}}\ge {\mathbf{n}}$.
7:     $\mathbf{errtol}$ – Real (Kind=nag_wp)Input
On entry: the termination tolerance for the iteration. If ${\mathbf{errtol}}\le 0.0$ then  is used. See Section 7 for further details.
8:     $\mathbf{maxit}$ – IntegerInput
On entry: specifies the maximum number of iterations to be used.
If ${\mathbf{maxit}}\le 0$, $200$ is used.
9:     $\mathbf{x}\left({\mathbf{ldx}},{\mathbf{n}}\right)$ – Real (Kind=nag_wp) arrayOutput
On exit: contains the nearest correlation matrix.
10:   $\mathbf{ldx}$ – IntegerInput
On entry: the first dimension of the array x as declared in the (sub)program from which g02ajf is called.
Constraint: ${\mathbf{ldx}}\ge {\mathbf{n}}$.
11:   $\mathbf{iter}$ – IntegerOutput
On exit: the number of iterations taken.
12:   $\mathbf{norm}$ – Real (Kind=nag_wp)Output
On exit: the value of ${‖H\circ \left(G-X\right)‖}_{F}$ after the final iteration.
13:   $\mathbf{ifail}$ – IntegerInput/Output
On entry: ifail must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this argument you should refer to Section 3.4 in How to Use the NAG Library and its Documentation for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this argument, the recommended value is $0$. When the value $-\mathbf{1}\text{​ or ​}\mathbf{1}$ is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-1$, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
${\mathbf{ifail}}=1$
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}>0$.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{ldg}}=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ldg}}\ge {\mathbf{n}}$.
${\mathbf{ifail}}=3$
On entry, ${\mathbf{ldh}}=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ldh}}\ge {\mathbf{n}}$.
${\mathbf{ifail}}=4$
On entry, ${\mathbf{ldx}}=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ldx}}\ge {\mathbf{n}}$.
${\mathbf{ifail}}=5$
On entry, ${\mathbf{alpha}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{alpha}}<1.0$.
${\mathbf{ifail}}=6$
On entry, one or more of the off-diagonal elements of $H$ were negative.
${\mathbf{ifail}}=7$
Routine fails to converge in $〈\mathit{\text{value}}〉$ iterations.
Increase maxit or check the call to the routine.
${\mathbf{ifail}}=8$
${\mathbf{ifail}}=-99$
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 3.8 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.

## 7Accuracy

The returned accuracy is controlled by errtol and limited by machine precision. If ${e}_{i}$ is the value of norm at the $i$th iteration, that is
 $ei = H∘G-XF ,$
where $H$ has been scaled as described above, then the algorithm terminates when:
 $ei-ei-1 1+ maxei,ei-1 ≤ errtol .$

## 8Parallelism and Performance

g02ajf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
g02ajf makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

Arrays are internally allocated by g02ajf. The total size of these arrays is $15×{\mathbf{n}}+5×{\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 g02ajf.

## 10Example

This example finds the nearest correlation matrix to:
 $G = 2 -1 0 0 -1 2 -1 0 0 -1 2 -1 0 0 -1 2$
weighted by:
 $H = 0.0 10.0 0.0 0.0 10.0 0.0 1.5 1.5 0.0 1.5 0.0 0.0 0.0 1.5 0.0 0.0$
with minimum eigenvalue $0.04$.

### 10.1Program Text

Program Text (g02ajfe.f90)

### 10.2Program Data

Program Data (g02ajfe.d)

### 10.3Program Results

Program Results (g02ajfe.r)

© The Numerical Algorithms Group Ltd, Oxford, UK. 2017