! Make minimal adjustments to an initial guessed (POSDadjOffDiag.lng)
correlation matrix to make it a valid, Positive Semi-Definite matrix.
  Application:  If we have an incomplete data set, 
e.g., not every variable appears in every observation,
we may be able to get estimates of every correlation term individually,
however, taken together, the matrix may not be Positive Definite,
a feature that should be true for any correlation or covariance matrix.
So for a correlation matrix, we would like to make minimal adjustmensts (move
towards 0) of the off diagonal terms to make the matrix POSD;
! Keywords: Correlation, Covariance, Estimation, Positive definite, POSD, SDP, Statistics,
  Nearest correlation, Semi-Definite;
SETS:
  VEC;
  MAT( VEC,VEC) | &1 #GE# &2: QINI, QADJ, QFIT;
ENDSETS
DATA: VEC = 1..3; ! Our initial estimate of the correlation matrix, ( May not be positive semi-definite); QINI = 1.000000 0.6938961 1.000000 -0.1097276 0.7972293 1.000000 ; ENDDATA ! Minimize the amount of adjustments we have to make to the off-diagonal terms of our initial estimated matrix...; MIN = @SUM( MAT(i,j) | i #GT# j: QADJ(i,j)^2); ! Fitted matrix = initial + adjustment; @FOR( MAT(i,j) | i #GT# j: QFIT(i,j) = QINI(i,j) + QADJ(i,j); ! Off diagonal adjustments or fitted might be < 0; @FREE( QADJ(i,j)); @FREE( QFIT(i,j)); ); ! Diagonal terms stay at 1; @FOR( VEC(i): QFIT(i,i) = QINI(i,i); QADJ(i,i) = 0; ); ! The adusted/fitted matrix must be Positive semi-definite; @POSD( QFIT); DATA: @TEXT() = 'Initial guess at correlation matrix that is not Positive-SemiDefinite:'; @TEXT() = @TABLE( QINI); @TEXT() = ''; @TEXT() = 'Adjustment matrix:'; @TEXT() = @TABLE( QADJ); @TEXT() = ''; @TEXT() = 'Fitted matrix that is POSD:'; @TEXT() = @TABLE( QFIT); ENDDATA