matr_var - ObjectVision/GeoDMS GitHub Wiki

Matrix functions matr_var

The matr_var function computes the covariance matrix (X'X) from a data matrix using Boost uBLAS.

syntax

matr_var(dataMatrix: NxM->Float, resultDomain: MxM) -> MxM->Float

definition

Computes the covariance matrix by multiplying the transpose of the data matrix with itself: X'X.

If the data matrix has N rows (observations) and M columns (variables), the result is an M×M symmetric matrix where each element [i,j] represents the sum of products of columns i and j:

result[i,j] = Σ(k=0 to N-1) dataMatrix[k,i] × dataMatrix[k,j]

This is commonly used in:

  • Ordinary Least Squares (OLS) regression: (X'X)⁻¹ X'Y
  • Principal Component Analysis (PCA)
  • Computing sample covariance matrices

arguments

argument description type
dataMatrix Input data matrix (N observations × M variables) attribute
resultDomain Domain unit for the result (M × M) unit

performance

Uses Boost uBLAS for efficient computation. Complexity is O(N × M²) where:

  • N = number of observations (rows)
  • M = number of variables (columns)

For large datasets, this is more efficient than explicitly forming the transpose and multiplying.

conditions

  • Width(resultDomain) must equal Width(dataMatrix)
  • Height(resultDomain) must equal Width(dataMatrix)
  • Value types must be Float32 or Float64

example

// Data: 100 observations, 5 variables
unit<SPoint> DataDomain := range(SPoint, point_xy(0s, 0s), point_xy(100s, 5s));  // 100x5
unit<SPoint> CovDomain  := range(SPoint, point_xy(0s, 0s), point_xy(5s, 5s));    // 5x5

attribute<Float64> X (DataDomain);  // input data

// Compute X'X (covariance components)
attribute<Float64> XtX (CovDomain) := matr_var(X, CovDomain);

// For OLS regression: beta = (X'X)^-1 * X'Y
attribute<Float64> XtX_inv (CovDomain) := matr_inv(XtX);

see also

since version

7.0

⚠️ **GitHub.com Fallback** ⚠️