# The Statistician’s Apprentice: An Introduction to the SWP Operator

**MeanMean**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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

## Example

Lets try this with some real data. I have used the SWP function included with the `ISR3`

package.

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