Compute Quadratic Forms
Calculates certain quadratic forms of matrices.
sumouter(x, w) quadform(x, v)
- A matrix, whose rows are the vectors in the quadratic form.
- Vector of weights
- Matrix determining the quadratic form
x will be interpreted as a collection of row vectors.
sumouter computes the sum of the outer
products of these vectors, weighted by the entries of
$$M = \sum_i w_i x_i x_i^\top$$
where the sum is over all rows of
(after removing any rows containing
NA or other non-finite values).
The result is a $p \times p$ matrix where
p = ncol(x).
quadform evaluates the quadratic form, defined by
v, for each of the row vectors of
$$y_i = x_i V x_i^\top$$
y is a numeric vector of length
n = nrow(x). If
other non-finite values, then
y[i] = NA.
- A vector or matrix.
x <- matrix(1:12, 4, 3) dimnames(x) <- list(c("Wilma", "Fred", "Barney", "Betty"), letters[1:3]) x w <- 4:1 sumouter(x, w) v <- matrix(1, 3, 3) quadform(x, v) # See what happens with NA's x[3,2] <- NA sumouter(x, w) quadform(x, v)