计算矩阵的逆:系统在计算上是奇异的[错误]
Calculate the inverse of a matrix : system is computationally singular [error]
我有一个矩阵 m
:
(m <- matrix(c(26,14,24,14,20,42,24,42,90), 3))
# [,1] [,2] [,3]
# [1,] 26 14 24
# [2,] 14 20 42
# [3,] 24 42 90
当我 运行 solve(m)
计算矩阵的逆时,我得到这个错误信息:
solve(m)
Error in solve.default(m) :
system is computationally singular: reciprocal condition number = 6.21104e-18
问题是列不是线性独立的。
第一列*-1/3+第二列*7/3等于第三列
-m[, 1] * 1/3 + 7/3 * m[, 2]
# [1] 24 42 90
我们可以从几个方面看出这一定是这样的,每个方面都暗示 non-invertability:
1)m
的行列式为零:
> det(m)
[1] -2.685852e-12
2) m 的特征值为零,即 eigen(m)$values[3]
。等效地,m
的 nullspace 是 non-null——它等于 eigen(m)$vectors[, 3]
跨越的一维 space
> e <- eigen(m); e
$values
[1] 1.180000e+02 1.800000e+01 -6.446353e-15
$vectors
[,1] [,2] [,3]
[1,] -0.2881854 9.486833e-01 0.1301889
[2,] -0.4116935 1.110223e-16 -0.9113224
[3,] -0.8645563 -3.162278e-01 0.3905667
> N <- e$vector[, 3] # nullspace
> m %*% N # shows that N is indeed mapped to zero
[,1]
[1,] 5.329071e-15
[2,] 0.000000e+00
[3,] 0.000000e+00
3) m
的列 不是 线性独立的。特别是在其他列上回归 m[,1]
给出了完美的拟合(即拟合值等于 m[, 1]
),因此从线性模型的系数我们得到 7 * m[,2] - 3 * m[, 3]
等于 m[, 1]
。
> fm <- lm(m[, 1] ~ m[, 2] + m[, 3] + 0)
> all.equal(fitted(fm), m[, 1]) # perfect fit
[1] TRUE
> coef(fm)
m[, 2] m[, 3]
7 -3
> all.equal(7 * m[, 2] - 3 * m[, 3], m[, 1])
[1] TRUE
4) cholesky 分解的对角线上有一个零:
> chol(m, pivot = TRUE)
[,1] [,2] [,3]
[1,] 9.486833 2.529822 4.4271887
[2,] 0.000000 4.427189 0.6324555
[3,] 0.000000 0.000000 0.0000000
attr(,"pivot")
[1] 3 1 2
attr(,"rank")
[1] 2
Warning message:
In chol.default(m, pivot = TRUE) :
the matrix is either rank-deficient or indefinite
5) m
不是满秩,即秩小于3:
> attr(chol(m, pivot = TRUE), "rank")
[1] 2
Warning message:
In chol.default(m, pivot = TRUE) :
the matrix is either rank-deficient or indefinite
注意: 输入可重复地由:
m <- matrix(c(26, 14, 24, 14, 20, 42, 24, 42, 90), 3)
我有一个矩阵 m
:
(m <- matrix(c(26,14,24,14,20,42,24,42,90), 3))
# [,1] [,2] [,3]
# [1,] 26 14 24
# [2,] 14 20 42
# [3,] 24 42 90
当我 运行 solve(m)
计算矩阵的逆时,我得到这个错误信息:
solve(m)
Error in solve.default(m) : system is computationally singular: reciprocal condition number = 6.21104e-18
问题是列不是线性独立的。
第一列*-1/3+第二列*7/3等于第三列
-m[, 1] * 1/3 + 7/3 * m[, 2]
# [1] 24 42 90
我们可以从几个方面看出这一定是这样的,每个方面都暗示 non-invertability:
1)m
的行列式为零:
> det(m)
[1] -2.685852e-12
2) m 的特征值为零,即 eigen(m)$values[3]
。等效地,m
的 nullspace 是 non-null——它等于 eigen(m)$vectors[, 3]
> e <- eigen(m); e
$values
[1] 1.180000e+02 1.800000e+01 -6.446353e-15
$vectors
[,1] [,2] [,3]
[1,] -0.2881854 9.486833e-01 0.1301889
[2,] -0.4116935 1.110223e-16 -0.9113224
[3,] -0.8645563 -3.162278e-01 0.3905667
> N <- e$vector[, 3] # nullspace
> m %*% N # shows that N is indeed mapped to zero
[,1]
[1,] 5.329071e-15
[2,] 0.000000e+00
[3,] 0.000000e+00
3) m
的列 不是 线性独立的。特别是在其他列上回归 m[,1]
给出了完美的拟合(即拟合值等于 m[, 1]
),因此从线性模型的系数我们得到 7 * m[,2] - 3 * m[, 3]
等于 m[, 1]
。
> fm <- lm(m[, 1] ~ m[, 2] + m[, 3] + 0)
> all.equal(fitted(fm), m[, 1]) # perfect fit
[1] TRUE
> coef(fm)
m[, 2] m[, 3]
7 -3
> all.equal(7 * m[, 2] - 3 * m[, 3], m[, 1])
[1] TRUE
4) cholesky 分解的对角线上有一个零:
> chol(m, pivot = TRUE)
[,1] [,2] [,3]
[1,] 9.486833 2.529822 4.4271887
[2,] 0.000000 4.427189 0.6324555
[3,] 0.000000 0.000000 0.0000000
attr(,"pivot")
[1] 3 1 2
attr(,"rank")
[1] 2
Warning message:
In chol.default(m, pivot = TRUE) :
the matrix is either rank-deficient or indefinite
5) m
不是满秩,即秩小于3:
> attr(chol(m, pivot = TRUE), "rank")
[1] 2
Warning message:
In chol.default(m, pivot = TRUE) :
the matrix is either rank-deficient or indefinite
注意: 输入可重复地由:
m <- matrix(c(26, 14, 24, 14, 20, 42, 24, 42, 90), 3)