The sweep operator as defined in (Dempster, 1969), commonly referred to as the SWP operator, is a useful tool for a computational statistician working with covariance matrices. In particular, the SWP operator allows a statistician to quickly regress all variables against one specified variable, obtaining OLS estimates for regression coefficients and variances in a single application. Subsequent applications of the SWP operator allows for regressing against more variables.
In this blog post, I will define the sweep operator, provide an application of the sweep data to simulated data, and provide some references for further study. Examples will be provided in R through the
ISR3 package (Lisic, 2016).
The basic SWP operator is parameterized by a matrix and a set of indices associated with rows and columns of the matrix being swept. E.g. to sweep the matrix by the index, such that , the operator has the form . The operator may also be written more descriptively by listing the elements of as . The application of the SWP operator to the row and column of produces a new matrix with elements
As an example, consider the application of the SWP operator to the first row and column of the covariance matrix for the random variables ,
How is this useful? Well, if we substitute the covariance matrix with the sample covariance matrix, where is an estimator of and
Lets try this with some real data. I have used the SWP function included with the
library(ISR3) set.seed(12) n <- 100 p <- 3 # generate a positive definite matrix for covariance Sigma <- rWishart(1,p+1,diag(p))[,,1] Sigma_inv <- chol2inv(chol(Sigma)) Sigma_inv_chol <- chol(Sigma_inv) # generate 'n' multivate normal deviates X <- Sigma_inv_chol %*% matrix(rnorm(n*p),nrow=p) X <- t(X) colnames(X) <- sprintf("X_%d",1:p) XX <- t(X) %*% X #Sweep by the first row/column of XX SWP_1 <- SWP(XX,1) SWP_1
## X_1 X_2 X_3 ## X_1 -0.002381092 0.1375626 -0.01674788 ## X_2 0.137562633 50.0987932 -4.95181624 ## X_3 -0.016747875 -4.9518162 41.73979419
# linear models fit_21 <- lm(X_2 ~ -1 + X_1, data=as.data.frame(X)) fit_31 <- lm(X_3 ~ -1 + X_1, data=as.data.frame(X)) # SLR coefficents for X_1 print(fit_21$coefficients)
## X_1 ## 0.1375626
## X_1 ## -0.01674788
# sum of squares print(sum(fit_21$residuals^2))
##  50.09879
##  41.73979
# covariance between X_2 and X_3 given X_1 print(sum(fit_21$residuals * fit_31$residuals))
##  -4.951816
# negative inverse of the covariance matrix of X_1 print( -1/((n-1)*var(X[,1])) )
##  -0.002381293
lmmatch up to our swept results. Likewise, the diagonal terms for the second and third column are equivalent to the sum of squares or the residuals. The remaining unmatched term is just the negative inverse of the covariance matrix of unscaled by .
A subsequent sweep on the second rows and columns produces the matrix,
lmto our SWP operator.
A final sweep by the third row and column produces , the negative inverse of the unscaled covariance matrix of . This can be quickly verified through
SWP(XX,1:3) + chol2inv(chol(XX))
## X_1 X_2 X_3 ## X_1 0.000000e+00 0.000000e+00 1.355253e-20 ## X_2 0.000000e+00 3.469447e-18 8.673617e-19 ## X_3 1.355253e-20 8.673617e-19 0.000000e+00
We can also undo sweeps with the reverse sweep (RSWP) operator through the following element-wise operations
A few good references on the subject beyond (Dempster, 1969) can be found in (Goodknight, 1979) and (Little and Rubin, 2014). There are a few slight differences between these implementations, but all generally do the same thing.
One function that doesn't do the same thing is the
sweep function in
R. This function can be used to write a SWP implementation, but doesn't sweep by itself.
Code examples can be found in the
ISR3 package on
CRAN and in lots of other packages.
C can be found in
ISR3 and as an internal function in the
MNP package (Imai and Van Dyk, 2013).
Enjoy your sweeping, next time I'll go over sweeping with the generalized inverse!
Dempster, A.P. (1969). Elements of continuous multivariate analysis. Reading, MA: Addison-Wesley.
Goodnight, J. H. (1979). A tutorial on the SWEEP operator. The American Statistician, 33(3), 149-158.
Imai, K., and Van Dyk, D. A. (2013). MNP: R Package for fitting the Multinomial Probit Model, R package version 2.6-4, https://CRAN.R-project.org/package=MNP
Lisic, J. J. (2016). ISR3: Iterative Sequential Regression, R package version 0.98, https://CRAN.R-project.org/package=ISR3
Little, R. J., and Rubin, D. B. (2014). Statistical analysis with missing data. John Wiley & Sons.