mse {RcppML}R Documentation

Mean Squared Error loss of a factor model

Description

MSE of factor models w and h given sparse matrix AA

Usage

mse(A, w, d = NULL, h, mask_zeros = FALSE)

Arguments

A

matrix of features-by-samples in dense or sparse format (preferred classes are "matrix" or "Matrix::dgCMatrix", respectively). Prefer sparse storage when more than half of all values are zero.

w

dense matrix of class matrix with factors (columns) by features (rows)

d

diagonal scaling vector of rank length

h

dense matrix of class matrix with samples (columns) by factors (rows)

mask_zeros

handle zeros as missing values, available only when A is sparse

Details

Mean squared error of a matrix factorization of the form A=wdhA = wdh is given by

i,j(Awdh)2ij\frac{\sum_{i,j}{(A - wdh)^2}}{ij}

where ii and jj are the number of rows and columns in AA.

Thus, this function simply calculates the cross-product of whwh or wdhwdh (if dd is specified), subtracts that from AA, squares the result, and calculates the mean of all values.

If no diagonal scaling vector is present in the model, input d = rep(1, k) where k is the rank of the model.

Parallelization. Calculation of mean squared error is performed in parallel across columns in A using the number of threads set by setRcppMLthreads. By default, all available threads are used, see getRcppMLthreads.

Value

mean squared error of the factorization model

Author(s)

Zach DeBruine

Examples

## Not run: 
library(Matrix)
A <- Matrix::rsparsematrix(1000, 1000, 0.1)
model <- nmf(A, k = 10, tol = 0.01)
c_mse <- mse(A, model$w, model$d, model$h)
R_mse <- mean((A - model$w %*% Diagonal(x = model$d) %*% model$h)^2)
all.equal(c_mse, R_mse)

## End(Not run)

[Package RcppML version 0.3.7 Index]