简体   繁体   English

R ginv和Matlab pinv产生不同的结果

[英]R ginv and Matlab pinv produce different results

ginv() function from MASS package in R produce totally different values compared to MATLAB pinv() function. 与MATLAB pinv()函数相比,R中的MASS包中的ginv()函数产生的值完全不同。 They both claim to produce Moore-Penrose generalized inverse of a matrix. 他们都声称产生矩阵的Moore-Penrose广义逆。

I tried to set the same tolerance for the R implementation but the difference persists. 我尝试为R实现设置相同的公差,但差异仍然存在。

  • MATLAB default tol : max(size(A)) * norm(A) * eps(class(A)) MATLAB默认值tol: max(size(A)) * norm(A) * eps(class(A))
  • R default tol : sqrt(.Machine$double.eps) R默认tol: sqrt(.Machine$double.eps)

Reproduction: 再生产:

R: R:

library(MASS)
A <- matrix(c(47,94032,149, 94032, 217179406,313679,149,313679,499),3,3)
ginv(A)

outputs: 输出:

              [,1]          [,2]          [,3]
[1,]  1.675667e-03 -8.735203e-06  5.545605e-03
[2,] -8.735203e-06  5.014084e-08 -2.890907e-05
[3,]  5.545605e-03 -2.890907e-05  1.835313e-02

svd(A)

outputs: 输出:

$d
[1] 2.171799e+08 4.992800e+01 2.302544e+00

$u
              [,1]         [,2]          [,3]
[1,] -0.0004329688  0.289245088 -9.572550e-01
[2,] -0.9999988632 -0.001507826 -3.304234e-06
[3,] -0.0014443299  0.957253888  2.892454e-01

$v
              [,1]         [,2]          [,3]
[1,] -0.0004329688  0.289245088 -9.572550e-01
[2,] -0.9999988632 -0.001507826 -3.304234e-06
[3,] -0.0014443299  0.957253888  2.892454e-01

MATLAB: MATLAB:

A = [47 94032 149; 94032 217179406 313679; 149 313679 499]
pinv(A)

outputs: 输出:

ans =

    0.3996   -0.0000   -0.1147
   -0.0000    0.0000   -0.0000
   -0.1147   -0.0000    0.0547

svd: svd:

[U, S, V] = svd(A)

U =

   -0.0004    0.2892   -0.9573
   -1.0000   -0.0015   -0.0000
   -0.0014    0.9573    0.2892


S =

  1.0e+008 *

    2.1718         0         0
         0    0.0000         0
         0         0    0.0000


V =

   -0.0004    0.2892   -0.9573
   -1.0000   -0.0015   -0.0000
   -0.0014    0.9573    0.2892

Solution: to make R ginv like MATLAB pinv use this function: 解决方案:使R ginv像MATLAB pinv使用此函数:

#' Pseudo-Inverse of Matrix
#' @description
#' This is the modified version of ginv function in MASS package.
#' It produces MATLAB like pseudo-inverse of a matrix
#' @param X The matrix to compute the pseudo-inverse
#' @param tol The default is the same as MATLAB pinv function
#'
#' @return The pseudo inverse of the matrix
#' @export
#'
#' @examples
#' A <- matrix(1:6,3,2)
#' pinv(A)
pinv <- function (X, tol = max(dim(X)) * max(X) * .Machine$double.eps)
{
  if (length(dim(X)) > 2L || !(is.numeric(X) || is.complex(X)))
    stop("'X' must be a numeric or complex matrix")
  if (!is.matrix(X))
    X <- as.matrix(X)
  Xsvd <- svd(X)
  if (is.complex(X))
    Xsvd$u <- Conj(Xsvd$u)
  Positive <- any(Xsvd$d > max(tol * Xsvd$d[1L], 0))
  if (Positive)
    Xsvd$v %*% (1 / Xsvd$d * t(Xsvd$u))
  else
    array(0, dim(X)[2L:1L])
}

Running debugonce(MASS::ginv) , we see that the difference lies in what is done with the singular value decomposition. 运行debugonce(MASS::ginv) ,我们看到的区别在于奇异值分解所完成的工作。

Specifically, R checks the following: 具体来说,R检查以下内容:

Xsvd <- svd(A)
Positive <- Xsvd$d > max(tol * Xsvd$d[1L], 0)
Positive
# [1]  TRUE  TRUE FALSE

If the third element were true (which we can force by setting tol = 0 , as suggested by @nicola), MASS::ginv would return: 如果第三个元素为true(我们可以通过设置tol = 0来强制设置,如@nicola所建议),则MASS::ginv将返回:

Xsvd$v %*% (1/Xsvd$d * t(Xsvd$u))
#               [,1]          [,2]          [,3]
# [1,]  3.996430e-01 -7.361507e-06 -1.147047e-01
# [2,] -7.361507e-06  5.014558e-08 -2.932415e-05
# [3,] -1.147047e-01 -2.932415e-05  5.468812e-02

(ie, the same as MATLAB). (即与MATLAB相同)。

Instead, it returns: 相反,它返回:

Xsvd$v[, Positive, drop = FALSE] %*% ((1/Xsvd$d[Positive]) * 
  t(Xsvd$u[, Positive, drop = FALSE]))
#               [,1]          [,2]          [,3]
# [1,]  1.675667e-03 -8.735203e-06  5.545605e-03
# [2,] -8.735203e-06  5.014084e-08 -2.890907e-05
# [3,]  5.545605e-03 -2.890907e-05  1.835313e-02

Thanks to @FaridCher for pointing out the source code of pinv . 由于@FaridCher您指出的源代码pinv

I'm not sure I 100% understand the MATLAB code, but I think it comes down to a difference in how tol is used. 我不确定我是否100%理解MATLAB代码,但是我认为这toltol的用法不同。 The MATLAB correspondence to Positive in R is: MATLAB与R中的Positive对应为:

`r = sum(s>tol)`

Where tol is what's supplied by the user; 其中tol是什么用户提供; if none is supplied, we get: 如果没有提供,我们得到:

m = 0;
% I don't get the point of this for loop -- why not just `m = max(size(A))`?
for i = 1:n 
   m = max(m,length(A(:,i)));
end
% contrast with simply `tol * Xsvd$d[1L]` in R
%   (note: i believe the elements of d are sorted largest to smallest)
tol = m*eps(max(s)); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 R:两种不同的网络抓取方法会产生两种不同的结果? - R : Two Different Methods of Webscraping Produce Two Different Results? 百分比回归-r,python和matlab中的结果不同 - regression for percentages - different results in r, python and matlab 不同的 plot 结果为 Matlab 和 R - 更新问题 - Different plot results in Matlab and R - Update question LASSO与$ \\ lambda = 0 $和OLS在R glmnet中产生不同的结果 - LASSO with $\lambda = 0$ and OLS produce different results in R glmnet R 和 Matlab 中的拟合分布给出了非常不同的结果 - Fitting distributions in R and Matlab give very different results 从Excel导入R的日期在以不同方式转换为字符时会产生不同的结果 - Dates imported from Excel into R produce different results when converted to character in different ways 为什么这个 Butterworth 滤波器在 R 和 Matlab 中呈现不同的结果? - why is this butterworth filter presenting different results in R and Matlab? 为什么foreach和ginv在R中不能一起工作 - Why doesn't foreach and ginv work together in R 为什么聚合和施加不同的结果? - Why aggregate and lapply produce different results? 为什么这两个代码会产生不同的结果? - Why do these two codes produce different results?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM