123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603 \name{VectorMatrixAddon} \alias{VectorMatrixAddon} \alias{triang} \alias{Triang} \alias{pascal} \alias{colVec} \alias{rowVec} \alias{colIds} \alias{rowIds} \alias{colIds<-} \alias{rowIds<-} \alias{inv} \alias{norm} \alias{rk} \alias{tr} \alias{kron} \alias{mexp} \alias{vec} \alias{vech} \alias{tslag} \alias{pdl} \title{Vector/Matrix Arithmetics and Linear Algebra} \description{ A collection and description of functions available for matrix arithmetics and linear algebra. \cr Only functions which have been added by Rmetrics are documented. These functions are often useful for the manipulation of multivariate time series. \cr The origin of the functions is marked in the following way: R: part of R's base packages, \cr B: part of Rmetrics' fBasics package, \cr M: part of this Rmetrics package, fMultivar. \cr The functions are listed by topic. \cr General Matrix Functions: \tabular{ll}{ \code{matrix} \tab Creates a matrix from the given set of values, \cr \code{diag} \tab R: Creates a diagonal matrix or extracts diagonals, \cr \code{triang} \tab S: Extracs the lower tridiagonal part from a matrix, \cr \code{Triang} \tab S: Extracs the upper tridiagonal part from a matrix, \cr \code{pascal} \tab S: Creates a Pascal matrix, \cr \code{colVec} \tab S: Creates a column vector from a vector, \cr \code{rowVec} \tab S: Creates a row vector from a vector, \cr \code{as.matrix} \tab R: Attempts to turn its argument into a matrix, \cr \code{is.matrix} \tab R: Tests if its argument is a (strict) matrix, \cr \code{dimnames} \tab R: Retrieves or sets the dimnames of an object, \cr \code{colnames} \tab R: Retrieves or sets the column names, \cr \code{colIds} \tab R: ... use alternatively, \cr \code{rownames} \tab R: Retrieves or sets the row names, \cr \code{rowIds} \tab R: ... use alternatively. } Simple Matrix Operations: \tabular{ll}{ \code{dim} \tab R: Returns the dimension of a matrix object, \cr \code{ncol} \tab R: Counts columns of a matrix object, \cr \code{nrow} \tab R: Counts rows of a matrix object, \cr \code{length} \tab R: Counts elements of a matrix object, \cr \code{"["} \tab R: Subsets a matrix object, \cr \code{"[["} \tab R: Subsets a matrix object, \cr \code{cbind} \tab R: Augments a matrix object by columns, \cr \code{rbind} \tab R: Augments a matrix object by rows. } Basic Statistical Functions: \tabular{ll}{ \code{var} \tab R: Returns the variance matrix, \cr \code{cov} \tab R: Returns the covariance matrix, \cr \code{colStats} \tab B: Calculates column statistics, \cr \code{rowStats} \tab B: Calculates row statistics, \cr \code{colMeans} \tab B: Calculates column means, \cr \code{rowMeans} \tab B: Calculates row means, \cr \code{colAvgs} \tab B: Calculates column averages, \cr \code{rowAvgs} \tab B: Calculates row averages, \cr \code{colVars} \tab B: Calculates column variances, \cr \code{rowVars} \tab B: Calculates row variances, \cr \code{colStdevs} \tab B: Calculates column standard deviations, \cr \code{rowStdevs} \tab B: Calculates row standard deviations, \cr \code{colSkewness} \tab B: Calculates column skewness, \cr \code{rowSkewness} \tab B: Calculates row skewness, \cr \code{colKurtosis} \tab B: Calculates column kurtosis, \cr \code{rowKurtosis} \tab B: Calculates row kurtosis, \cr \code{colCumsums} \tab B: Calculates column cumulated sums, \cr \code{rowCumsums} \tab B: Calculates row cumulated sums. } Linear algebra: \tabular{ll}{ \code{\%*\%} \tab R: Returns the product of two matrices, \cr \code{\%x\%}, \code{kron} \tab R: Returns the Kronecker product, \cr \code{mexp} \tab M: Computes the exponential of a square matrix, \cr \code{det} \tab R: Returns the determinante of a matrix, \cr \code{inv} \tab S: Returns the inverse of a matrix, \cr \code{norm} \tab S: Returns the norm of a matrix, \cr \code{rk} \tab S: Returns the rank of a matrix, \cr \code{tr} \tab S: Returns trace of a matrix, \cr \code{t} \tab R: Returns the transposed matrix, \cr \code{vech} \tab M: Is the operator that stacks the lower triangle, \cr \code{vec} \tab M: Is the operator that stacks a matrix.} More linear algebra: \tabular{ll}{ \code{chol} \tab R: Returns the Cholesky factor matrix, \cr \code{eigen} \tab R: Returns eigenvalues and eigenvectors, \cr \code{svd} \tab R: Returns the singular value decomposition, \cr \code{kappa} \tab R: Returns the condition number of a matrix, \cr \code{qr} \tab R: Returns the QR decomposition of a matrix, \cr \code{solve} \tab R: Solves a system of linear equations, \cr \code{backsolve} \tab R: ... use when the matrix is upper triangular, \cr \code{forwardsolve} \tab R: ... use when the matrix is lower triangular. } Time Series Generation: \tabular{ll}{ \code{tslag} \tab R: Lagged or leading vector/matrix of selected order(s), \cr \code{pdl} \tab R: Regressor matrix for polynomial distributed lags. } } \usage{ triang(x) Triang(x) pascal(n) colVec(x) rowVec(x) colIds(x, \dots) rowIds(x, \dots) inv(x) norm(x, p = 2) rk(x, method = c("qr", "chol")) tr(x) kron(x, y) mexp(x, order = 8, method = c("pade", "taylor")) vec(x) vech(x) pdl(x, d = 2, q = 3, trim = FALSE) tslag(x, k = 1, trim = FALSE) } \arguments{ \item{d}{ [pdl] - \cr an integer specifying the order of the polynomial. } \item{k}{ [tslag] - \cr an integer value, the number of positions the new series is to lag or to lead the input series. } \item{method}{ [mexp] - \cr Two methods are provided for the computation of the exponential of a matrix: Taylor series selected by \code{method="taylor"}, and Pad\'e approximation with scaling and squaring to increase precision selected by \code{method="pade"} which is the default method. \cr [rk] - \cr a character value, the dimension of the square matrix. One can choose from two methods: For \code{method = "qr"} the rank is computed as \code{qr(x)\$rank}, or alternatively for \code{method="chol"} the rank is computed as \code{attr(chol(x, pivot=TRUE), "rank")}. } \item{n}{ [pascal] - \cr an integer value, the dimension of the square matrix. } \item{order}{ [mexp] - \cr The order of approximation to be used, an integer value. The best value for this depends on machine precision. } \item{p}{ [norm] - \cr an integer value, \code{1}, \code{2} or \code{Inf}. \code{p=1} - The maximum absolute column sum norm which is defined as the maximum of the sum of the absolute valued elements of columns of the matrix. \code{p=2} - The spectral norm is "the norm" of a matrix \code{X}. This value is computed as the square root of the maximum eigenvalue of \code{CX} where \code{C} is the conjugate transpose. \code{p=Inf} - The maximum absolute row sum norm is defined as the maximum of the sum of the absolute valued elements of rows of the matrix. } \item{q}{ [pdl] - \cr an integer specifying the number of lags to use in creating polynomial distributed lags. This must be greater than d. } \item{trim}{ [pdl] - \cr a logical flag; if TRUE, the missing values at the beginning of the returned matrix will be trimmed. \cr [tslag] - \cr a logical flag, if TRUE, the missing values at the beginning or end of the returned series will be trimmed. The default value is FALSE. } \item{x, y}{ a numeric matrix. \cr [tslag] - \cr a numeric vector, missing values are allowed. \cr [pdl] - \cr a numeric vector. } \item{\dots}{ arguments to be passed. } } \details{ \bold{Function from R's Base Package:} \cr Most of the functions are described in their R help pages which we recommend to consult for further information. For the additiotnal functions added by Rmetrics we give a brief introduction. \cr \bold{General Functions:} \cr Functions to generate matrices and related functions are described in the help page \code{\link{matrix}}. To "decorate" these objects several naming functions are available, a description can be found on the help pages \code{\link{dimnames}} and \code{\link{rownames}}. \cr The function \code{pascal} generates a Pascal matrix of order \code{n} which is a symmetric, positive, definite matrix with integer entries made up from Pascal's triangle. The determinant of a Pascal matrix is 1. The inverse of a Pascal matrix has integer entries. If \code{lambda} is an eigenvalue of a Pascal matrix, then \code{1/lambda} is also an eigenvalue of the matrix. Pascal matrices are ill-conditioned. \cr The functions \code{triang} and \code{Triang} allow to transform a square matrix to a lower or upper triangular form. A triangular matrix is either an upper triangular matrix or lower triangular matrix. For the first case all matrix elements \code{a[i,j]} of matrix \code{A} are zero for \code{i>j}, whereas in the second case we have just the opposite situation. A lower triangular matrix is sometimes also called left triangular. In fact, triangular matrices are so useful that much computational linear algebra begins with factoring or decomposing a general matrix or matrices into triangular form. Some matrix factorization methods are the Cholesky factorization and the LU-factorization. Even including the factorization step, enough later operations are typically avoided to yield an overall time savings. Triangular matrices have the following properties: the inverse of a triangular matrix is a triangular matrix, the product of two triangular matrices is a triangular matrix, the determinant of a triangular matrix is the product of the diagonal elements, the eigenvalues of a triangular matrix are the diagonal elements. \cr The functions \code{colVec} and \code{rowVec} transform a vector into a column and row vector, respectively. A column vector is a matrix object with one column, and a row vector is a matrix object with one row. \cr The functions \code{dimnames}, \code{colname}, and \code{rowname} can be used to retrieve and set the names of matrices. The functions \code{rowIds}, \code{colIds}, are S-Plus like synonyms. \cr \bold{Simple Matrix Operations:} \cr The functions \code{\link{dim}}, \code{\link{nrow}} and \code{\link{ncol}} are functions to extract the dimension and the number of rows and columns of a matrix. \cr The usual arithmetic operators, logical operators and mathematical functions like \code{sqrt} or \code{exp} and \code{log} operate on matrices element by element. Note, that \code{"*"} is not matrix multiplication, instead we have to use \code{"\%*\%"}. \cr The methods \code{"["} and \code{"[["} are suited to extract subsets from a matrix, to delete rows and columns, or to permute rows and columns. \cr \bold{Basic Statistical Functions:} \cr The functions \code{var} and \code{cov} compute the variance and covariance of a matrix. \cr Beside these functions \code{\link{colMeans}} and \code{\link{rowMeans}} are R functions which compute the mean of columns and rows of a matrix. Rmetrics has added further functions to compute column- or rowwise variances, standard deviations, skewness, kurtosis and cumulated sums. Two general functions named \code{rowStats} and \code{colStats} allow to apply through the argument list any function to compute row and column statistics from matrices. \cr \code{Linear Algebra:} \cr Matrix multiplication is done using the operator \code{\%*\%}. \cr The \emph{Kronecker product} can be computed using the operator \code{\%x\%} or alternatively using the function \code{kron}. \cr The function \code{mexp} computes the exponential of a square matrix \eqn{x}, defined as the sum from \eqn{r=0} to infinity of \eqn{x^r/r!}. Two methods are provided Taylor series and Pad\'e approximation with scaling and squaring to increase precision. Also reported, as the \code{"accuracy"} attribute of the result, is an upper bound for the L2 norm of the Cauchy error \code{mexp(a, order + 10, method) - mexp(a, order, method)}, and the used \code{method} and \code{order}. The function \code{det} computes the determinant of a matrix. \cr The inverse of a square matrix \code{inv(X)} of dimension \code{n} is defined so that \code{X \%*\% inv(X) = inv(X) \%*\% X = diag(n)} where the matrix \code{diag(n)} is the \code{n}-dimensional identity matrix. A precondition for the existence of the matrix inverse is that the determinant of the matrix \code{det(X)} is nonzero. \cr The function \code{t} computes the transposed of a square matrix. \cr The function \code{vec} implements the operator that stacks a matrix as a column vector, to be more precise in a matrix with one column. vec(X) = (X11, X21, ..., XN1, X12, X22, ..., XNN). The function \code{vech} implements the operator that stacks the lower triangle of a NxN matrix as an N(N+1)/2x1 vector: vech(X) =(X11, X21, X22, X31, ..., XNN), to be more precise in a matrix with one row. \cr The function \code{tr} computes the trace of a square matrix which is the sum of the diagonal elements of the matrix under consideration. \cr The function \code{rk} computes the rank of a matrix which is the dimension of the range of the matrix corresponding to the number of linearly independent rows or columns of the matrix, or to the number of nonzero singular values. The rank of a matrix is also named inear map. \cr The function \code{norm} computes the norm of a matrix. Three choices are possible: \code{p=1} - The maximum absolute column sum norm which is defined as the maximum of the sum of the absolute valued elements of columns of the matrix. \code{p=2} - The spectral norm is "the norm" of a matrix \code{X}. This value is computed as the square root of the maximum eigenvalue of \code{CX} where \code{C} is the conjugate transpose. \code{p=Inf} - The maximum absolute row sum norm is defined as the maximum of the sum of the absolute valued elements of rows of the matrix. \cr \code{More Linear Algebra:} \cr The function \code{chol} returns the Cholesky factor matrix, \code{eigen} returns eigenvalues and eigenvectors, \code{svd} returns the singular value decomposition, \code{kappa} estimate the condition number of a matrix, \code{qr} returns the QR decomposition of a matrix, \code{ginv} returns the Moore-Penrose generalized inverse, \code{solve} solves a system of linear equations, use \code{backsolve} when the matrix is upper triangular, and \code{forwardsolve} when the matrix is lower triangular. \cr \code{Time Series:} \cr The function \code{pdl} returns a regressor matrix suitable for polynomial distributed lags. \cr The function \code{tslag} returns a lagged/led vector or matrix for given time series data. } \references{ Higham N.J., (2002); \emph{Accuracy and Stability of Numerical Algorithms}, 2nd ed., SIAM. Golub, van Loan, (1996); \emph{Matrix Computations}, 3rd edition. Johns Hopkins University Press. Ward, R.C., (1977); \emph{Numerical computation of the matrix exponential with accuracy estimate}, SIAM J. Num. Anal. 14, 600--610. Moler C., van Loan C., (2003); \emph{Nineteen dubious ways to compute the exponential of a matrix, twenty-five years later}, SIAM Review 45, 3--49. } \examples{ ## SOURCE("fMultivar.6A-MatrixAddon") ## Create Pascal Matrix: P = pascal(3) P # Create lower triangle matrix L = triang(P) L # Extract diagonal part diag(P) ## Add/Subtract/Multiply/Divide: X = P # Multiply matrix with a constant 3 * X # Multiply two matrices elementwise X * P # Multiplies rows/columns of a matrix by a vector X \%*\% diag(P) diag(P) \%*\% X ## Operate on Subsets of a Matrix: n = 3; i = 2; j = 3 D = diag(1:3) # Return the dimension of a matrix dim(P) # Get the last colum of a matrix P[, ncol(P)] # Delete a column of a matrix P[, -i] # Permute the columns of a matrix P[c(3, 1, 2), ] # Augments matrix horizontally cbind(P, D) ## Apply a function to all Elements of a Matrix: # Return square root for each element sqrt(P) # Exponentiate the matrix elementwise exp(P) # Compute the median of each column apply(P, 2, "median") # Test on all elements of a matrix all( P > 2 ) # test on any element in a matrix any( P > 2 ) ## More Matrix Operations: # Return the product of two matrices P \%*\% D # Return the Kronecker Product P \%x\% D # Return the transposed matrix t(P) # Return the inverse of a matrix inv(P) # Return the norm of a matrix norm(P) # Return the determinante of a matrix det(P) # Return the rank of a matrix rk(P) # Return trace of a matrix tr(P) # Return the variance matrix var(P) # Return the covariance matrix cov(P) # Stack a matrix vec(P) # Stack the lower triangle vech(P) ## Matrix Exponential: # Test case 1 from Ward (1977) test1 = t(matrix(c( 4, 2, 0, 1, 4, 1, 1, 1, 4), 3, 3)) mexp(test1) # Results on Power Mac G3 under Mac OS 10.2.8 # [,1] [,2] [,3] # [1,] 147.86662244637000 183.76513864636857 71.79703239999643 # [2,] 127.78108552318250 183.76513864636877 91.88256932318409 # [3,] 127.78108552318204 163.67960172318047 111.96810624637124 # -- these agree with ward (1977, p608) \dontrun{ # A naive alternative to mexp, using spectral decomposition: mexp2 = function(matrix){ z = eigen(matrix, sym = FALSE) Re(z$vectors \%*\% diag(exp(z$values)) \%*\% solve(z$vectors)) } mexp2(test1) # -- hopelessly inaccurate in all but the first column. # Test case 4 from Ward (1977) test4 <- structure(c( 0, 0, 0, 0, 0, 0, 0, 0, 0, 1e-10, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0), .Dim = c(10, 10)) attributes(mexp(test4)) max(abs(mexp(test4) - mexp2(test4))) #[1] 8.746826694186494e-08 # -- mexp2 is accurate to 7 d.p., whereas mexp to at least 14 d.p. } ## More Linear Algebra: X = P; b = c(1, 2, 3) # Return the Cholesky factor matrix chol(X) # Return eigenvalues and eigenvectors eigen(X) # Return the singular value decomposition svd(X) # Return the condition number of a matrix kappa(X) # Return the QR decomposition of a matrix qr(X) # Solve a system of linear equations # ... use backsolve when the matrix is upper triangular # ... use forwardsolve when the matrix is lower triangular solve(X, b) backsolve(Triang(X), b) solve(Triang(X), b) forwardsolve(triang(X), b) solve(triang(X), b) } \author{ Marina Shapira and David Firth for the \code{mexp} function, \cr Diethelm Wuertz for the Rmetrics \R-port. } \keyword{math}