Learn R Programming

BigDataStatMeth (version 1.0.3)

bd_wproduct: Weighted matrix–vector products and cross-products

Description

Compute weighted operations using a diagonal weight from w:

  • "xtwx": \(X' diag(w) X\) (row weights; length(w) = nrow(X))

  • "xwxt": \(X diag(w) X'\) (column weights; length(w) = ncol(X))

  • "xw" : \(X diag(w)\) (column scaling; length(w) = ncol(X))

  • "wx" : \(diag(w) X\) (row scaling; length(w) = nrow(X))

Inputs may be base numeric matrices .

Usage

bd_wproduct(X, w, op)

Value

Numeric matrix with dimensions depending on op: p x p for "xtwx", n x n for "xwxt", and n x p for "xw"/"wx".

Arguments

X

Numeric matrix (n x p).

w

Numeric weight vector (length n or p), or a 1D matrix coerced to a vector.

op

Character string (case-insensitive): one of "XtwX"/"xtwx", "XwXt"/"xwxt", "Xw"/"xw", "wX"/"wx".

Details

w is interpreted as the diagonal of a weight matrix; its required length depends on the operation: rows for "xtwx" and "wx", columns for "xwxt" and "xw".

Examples

Run this code
set.seed(1)
n <- 10; p <- 5
X <- matrix(rnorm(n * p), n, p)
u <- runif(n); w <- u * (1 - u)
bd_wproduct(X, w, "xtwx")  # p x p
bd_wproduct(X, w, "wx")    # n x p (row scaling)

v <- runif(p)
bd_wproduct(X, v, "xw")    # n x p (col scaling)
bd_wproduct(X, v, "xwxt")  # n x n

Run the code above in your browser using DataLab