Try out NAG Library functions

Explore NAG maths and stats routines with interactive demos
Function ID
Computes the nearest correlation matrix to a real square matrix, augmented to incorporate weights and bounds
Qi and Sun algorithm | correlation matrix | nearest correlation matrix
This example finds the nearest correlation matrix to:
weighted by W12=diag100,20,20,20 with minimum eigenvalue 0.02.
The example data reflects that shown in the "Example" section of the routine documentation. You can change this here to try alternative inputs. The formatting will need to be kept as it is here, otherwise the program is likely to fail to run correctly.

Please note that incompatible data will however cause the example output to display an error message. These error messages are fully explained in the Routine document
    Program g02abfe

!     G02ABF Example Program Text

!     Mark 26 Release. NAG Copyright 2016.

!     .. Use Statements ..
      Use nag_library, Only: dsyev, g02abf, nag_wp, x04caf
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nin = 5, nout = 6
!     .. Local Scalars ..
      Real (Kind=nag_wp)               :: alpha, errtol, nrmgrd
      Integer                          :: feval, i, ifail, iter, ldg, ldx,     &
                                          lwork, maxit, maxits, n
      Character (1)                    :: opt
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: eig(:), g(:,:), w(:), work(:),       &
!     .. Executable Statements ..
      Write (nout,*) 'G02ABF Example Program Results'
      Write (nout,*)
      Flush (nout)

!     Skip heading in data file
      Read (nin,*)

!     Read in the problem size, opt and alpha
      Read (nin,*) n, opt, alpha

      ldg = n
      ldx = n
      lwork = 66*n
      Allocate (g(ldg,n),w(n),x(ldx,n),eig(n),work(lwork))

!     Read in the matrix G
      Read (nin,*)(g(i,1:n),i=1,n)

!     Read in the vector W
      Read (nin,*) w(1:n)

!     Use the defaults for ERRTOL, MAXITS and MAXIT
      errtol = 0.0E0_nag_wp
      maxits = 0
      maxit = 0

!     Calculate nearest correlation matrix
      ifail = 0

      Call g02abf(g,ldg,n,opt,alpha,w,errtol,maxits,maxit,x,ldx,iter,feval,    &

!     Display results
      ifail = 0
      Call x04caf('General',' ',n,n,x,ldx,'Nearest Correlation Matrix X',      &
      Write (nout,*)
      Write (nout,99999) 'Number of Newton steps taken:', iter
      Write (nout,99998) 'Number of function evaluations:', feval

      Write (nout,*)
      Write (nout,99997) 'ALPHA: ', alpha

      ifail = 0
!     The NAG name equivalent of dsyev is f08faf
      Call dsyev('N','U',n,x,ldx,eig,work,lwork,ifail)
      Write (nout,*)
      Flush (nout)
      Call x04caf('General',' ',1,n,eig,1,'Eigenvalues of X',ifail)

99999 Format (1X,A,I11)
99998 Format (1X,A,I9)
99997 Format (1X,A,F37.3)

    End Program g02abfe
The NAG Library
The world’s largest collection of robust, documented, tested and maintained numerical algorithms.
Learn more here or contact us for purchasing information