A previous article discusses how to generate a random correlation matrix. On average, in a random correlation matrix, half of the off-diagonal entries are negative and half are positive. For any realization, the proportion of negative correlations might be greater than (or less than) half. This is in contrast to many of the well-known structured matrices that you encounter in statistics. The structured matrices often depend on a parameter, often called rho. If rho is positive, then all elements of the correlation matrix are positive. When rho is negative, the number of negative correlations depends on the structure and the size of the matrix.
It is straightforward to modify any correlation matrix to change the number of positive and negative values in the off-diagonal locations. This article shows the mathematics and a simple SAS IML function. The technique enables you to create a new correlation matrix from one that you already have. The entries in the new correlation matrix have the same magnitude, but different signs.
A simple transformation that changes correlation
Let X and Y be data vectors. (Or, they could be random variables if you prefer a more rigorous mathematical treatment.) If the correlation ρ = Corr(X,Y) is not zero, then Corr(-X,Y) has the opposite sign from ρ. In other words, multiplying a variable by -1 changes the sign of the correlation with other variables in the data set.
Of course, you could also change the sign of Y. If R is a 2x2 correlation matrix with off-diagonal element ρ, then you can construct a new correlation matrix by using the similarity transformation Q = S*R*S`, where S is a diagonal 2x2 "sign matrix." The S matrix contains ±1 on the diagonal. If S has one -1 on the diagonal, then Q has -ρ on the off-diagonal. Otherwise, Q has +ρ for the off-diagonal element.
You can extend this result to more variables. If R is a dxd correlation matrix, and S is any diagonal sign matrix with values ±1, then Q = S*R*S` is also a correlation matrix. The (i,j)th element of Q is Q[i,j] = S[i]*R[i,j]*S[j]= ±R[i,j]. Mathematically, Sylvester's Theorem ensures that Q is a valid correlation matrix. If R is symmetric and positive semi-definite, then so is Q.
Of course, if S is a symmetric matrix, then the similarity transformation simplifies to S*R*S, which is the form that I use in the next section.
A SAS IML program that changes correlation
The following program defines a function that computes a similarity matrix. Assume that R is a dxd matrix and v is a dx1 vector. The function returns the matrix S*R*S` where S = diag(v). I immediately call this function for the special case where R is a correlation matrix, and v[i]= ±1.
I use a programming technique that is worth noticing. You should never multiply by a large diagonal matrix. Instead of diag(v)*R*diag(v), you can use the more efficient computation v#R#v`, where '#' indicates elementwise multiplication. This trick speeds up the computation and uses much less memory.
proc iml; /* Given a dxd matrix, R, and a dx1 vector v, return the similar matrix Q = diag(v)*R*diag(v). An important application is to let v be a vector of +/-1. Then, if R is a valid correlation matrix, Q is also a correlation matrix where some correlations have changed signs. */ start SimilarMat(R, v); S = colvec(v); Q = S # R # S`; return(Q); finish; /* --- Example Usage --- */ /* Create AR(1) correlation matrix: https://blogs.sas.com/content/iml/2012/11/05/constructing-common-covariance-structures.html */ start AR1Corr(dim, rho); u = cuprod(j(1,dim-1,rho)); /* cumulative product */ return( toeplitz(1 || u) ); finish; /* Create a 5x5 AR(1; 0.5) matrix */ R = AR1Corr(5, 0.5); v = {1, -1, -1, 1, -1}; /* change correlations for the 2nd, 3rd, and 5th variables */ Q = SimilarMat(R, v); origNames = 'X1':'X5'; newNames = {'X1' '-X2' '-X3' 'X4' '-X5'}; print R[c=origNames r=origNames F=BestD7.], Q[c=newNames r=newNames F=BestD7.]; |
The output shows that the original matrix, R, has all positive correlations. However, in the new matrix, the correlation in the (i,j)th cell has the sign v[i]*v[j]. This creates a correlation matrix that has negative values.
Summary
This article shows a simple trick for changing the signs of elements in a correlation matrix. If R is any correlation matrix and S = diag(v) is a diagonal sign matrix with values ±1, then the product S*R*S` is a correlation matrix that has different signs than R. You can compute the product efficiently without ever forming a diagonal matrix. This trick is useful in simulation studies and in creating matrices to test statistical procedures.