Relative error between two matrices
Given two float matrices in R, I would like to get the relative error between every entry, and th开发者_如何学JAVAen among all errors search the max of all of them and compare it against 10%, i.e. MAX_ERROR <= 10%
I know that the raltive error is (for each entry):
|v_ij(MATRIX1) - v_ij(MATRIX2)| / | v_ij(MATRIX1)|
How to do this in R
, avoiding a for loop?
If you want to handle cases where the matrix has zeroes in it (which otherwise leads to division by zero), this answer has some solutions: Do you reassign == and != to isTRUE( all.equal() )?
A slight modification of the almostEqual
function I suggested there would yield:
relativeError <- function(x, y, tolerance=1e-8) {
diff <- abs(x - y)
mag <- pmax( abs(x), abs(y) )
ifelse( mag > tolerance, diff/mag, diff)
}
m1 <- cbind(c(0,1), c(1,1))
m2 <- cbind(c(0,1), c(1,1.11))
any(relativeError(m1, m2) > 0.01) # TRUE
# Building on @DWin's answer:
which(relativeError(m1, m2) > 0.01, arr.ind=TRUE) # 2 2
Note that this calculates the relative error slightly differently than your definition: it's symmetric and handles small values - and it's a bit slower because of it...
If you wnat to identify which elements fail that test then try this:
over_err <- which( abs(MATRIX1-MATRIX2)/abs(MATRIX1) > 0.1, arr.ind=TRUE)
If you want to dispaly a list of indices and values in MATRIX1 that satisfy (or fail to satisfy) that condition then:
cbind(over_err, MATRIX1[over_err])
The following should work:
maxerr <- max(abs((a - b) / a))
where a
and b
are the two matrices. To convert the result to percentage, multiply by 100
.
If the Exact matrix M_exact and the approximated matrix is M_app; then using Mathematica, the percentage error can be calculated as
error(%) = 100(Norm[M_app - M_exact]/Norm[M_exact])
To check the relative error in matrix element
Relative_error = (Abs[M_app - M_exact])/Abs[M_exact]
For further detail: http://www.netlib.org/lapack/lug/node75.html
精彩评论