R 函数 `is.positive.definite` 出错
Error in R function `is.positive.definite`
我想测试 R 中的矩阵是否为正定矩阵。我使用了 R 函数 is.positive.definite
但不断收到以下错误消息,尽管我的矩阵是对称的,如函数 isSymmetric
所示.请问这是因为舍入误差吗?
Error in is.positive.definite(S) : argument x is not a symmetric matrix
我的工作代码附在下面。有人可以帮我吗?谢谢。
library(Matrix) # isSymmetric
library(matrixcalc) # is.positive.definite
library(expm) # sqrtm
###################################################################################################
theta0 <- c(0.2, 10)
###################################################################################################
OS.mean <- function(shape, rank, n=10){
term1 <- factorial(n)/(factorial(rank-1)*factorial(n-rank))
term2 <- beta(n-rank+1, rank) - beta(n-rank+shape+1, rank)
term1*term2/shape
}
OS.mean.theta0.10 <- as.matrix(OS.mean(theta0[1], rank=seq(1, 10, by=1)))
###################################################################################################
OSsq.mean <- function(shape, rank, n=10){
term1 <- factorial(n)/(factorial(rank-1)*factorial(n-rank))
term2 <- beta(n-rank+1, rank) - 2*beta(n-rank+shape+1, rank) + beta(n-rank+2*shape+1, rank)
term1*term2/(shape*shape)
}
OSsq.mean.theta0.10 <- as.matrix(OSsq.mean(theta0[1], rank=seq(1, 10, by=1)))
###################################################################################################
OSprod.mean <- function(shape, rank1, rank2, n=10){
term1 <- factorial(n)/(factorial(rank1-1)*factorial(rank2-rank1-1)*factorial(n-rank2))
term2 <- beta(n-rank1+1, rank1) - beta(n-rank1+shape+1, rank1)
term3 <- beta(n-rank2+1, rank2-rank1)
term4 <- beta(n-rank1+shape+1, rank1) - beta(n-rank1+2*shape+1, rank1)
term5 <- beta(n-rank2+shape+1, rank2-rank1)
term1*(term2*term3-term4*term5)/(shape*shape)
}
OS.cov <- function(shape, rank1, rank2){
OSprod.mean(shape, rank1, rank2) - OS.mean(shape, rank1)*OS.mean(shape, rank2)
}
###################################################################################################
spacing <- seq(1, 10, by=1)
OS.varcov.10 <- function(shape, n=10){
V.diag <- diag(c(OSsq.mean.theta0.10 - OS.mean.theta0.10^2))
V.upper <- matrix(0, nrow=10, ncol=10)
for(i in 1:9){
for(j in (i+1):10){
V.upper[i, j] <- OS.cov(shape, spacing[i], spacing[j])
}
}
V.upper + V.diag + t(V.upper)
}
###################################################################################################
V.theta0.10 <- OS.varcov.10(theta0[1])
kappa(V.theta0.10)
isSymmetric(V.theta0.10)
is.positive.definite(V.theta0.10)
S <- sqrtm(V.theta0.10)
isSymmetric(S)
is.positive.definite(S)
您的 S
矩阵由于 loss of significance 而不是对称的,但默认的输出小数位数隐藏了它。自己看看:
> options(digits=20)
> S[1,2]
[1] 0.033457660484940172
> S[2,1]
[1] 0.033457660484940213
问题是,matrixcalc
包中的 is.symmetric.matrix
并没有考虑微小的差异(即它只是比较矩阵元素与严格的 ==
而不是 all.equal
方法) 而 isSymmetric
来自 Matrix
包。如果你四舍五入矩阵,一切都会好起来的:
> S=round(S,10)
> is.symmetric.matrix(S)
[1] TRUE
> is.positive.definite(S)
[1] TRUE
我想测试 R 中的矩阵是否为正定矩阵。我使用了 R 函数 is.positive.definite
但不断收到以下错误消息,尽管我的矩阵是对称的,如函数 isSymmetric
所示.请问这是因为舍入误差吗?
Error in is.positive.definite(S) : argument x is not a symmetric matrix
我的工作代码附在下面。有人可以帮我吗?谢谢。
library(Matrix) # isSymmetric
library(matrixcalc) # is.positive.definite
library(expm) # sqrtm
###################################################################################################
theta0 <- c(0.2, 10)
###################################################################################################
OS.mean <- function(shape, rank, n=10){
term1 <- factorial(n)/(factorial(rank-1)*factorial(n-rank))
term2 <- beta(n-rank+1, rank) - beta(n-rank+shape+1, rank)
term1*term2/shape
}
OS.mean.theta0.10 <- as.matrix(OS.mean(theta0[1], rank=seq(1, 10, by=1)))
###################################################################################################
OSsq.mean <- function(shape, rank, n=10){
term1 <- factorial(n)/(factorial(rank-1)*factorial(n-rank))
term2 <- beta(n-rank+1, rank) - 2*beta(n-rank+shape+1, rank) + beta(n-rank+2*shape+1, rank)
term1*term2/(shape*shape)
}
OSsq.mean.theta0.10 <- as.matrix(OSsq.mean(theta0[1], rank=seq(1, 10, by=1)))
###################################################################################################
OSprod.mean <- function(shape, rank1, rank2, n=10){
term1 <- factorial(n)/(factorial(rank1-1)*factorial(rank2-rank1-1)*factorial(n-rank2))
term2 <- beta(n-rank1+1, rank1) - beta(n-rank1+shape+1, rank1)
term3 <- beta(n-rank2+1, rank2-rank1)
term4 <- beta(n-rank1+shape+1, rank1) - beta(n-rank1+2*shape+1, rank1)
term5 <- beta(n-rank2+shape+1, rank2-rank1)
term1*(term2*term3-term4*term5)/(shape*shape)
}
OS.cov <- function(shape, rank1, rank2){
OSprod.mean(shape, rank1, rank2) - OS.mean(shape, rank1)*OS.mean(shape, rank2)
}
###################################################################################################
spacing <- seq(1, 10, by=1)
OS.varcov.10 <- function(shape, n=10){
V.diag <- diag(c(OSsq.mean.theta0.10 - OS.mean.theta0.10^2))
V.upper <- matrix(0, nrow=10, ncol=10)
for(i in 1:9){
for(j in (i+1):10){
V.upper[i, j] <- OS.cov(shape, spacing[i], spacing[j])
}
}
V.upper + V.diag + t(V.upper)
}
###################################################################################################
V.theta0.10 <- OS.varcov.10(theta0[1])
kappa(V.theta0.10)
isSymmetric(V.theta0.10)
is.positive.definite(V.theta0.10)
S <- sqrtm(V.theta0.10)
isSymmetric(S)
is.positive.definite(S)
您的 S
矩阵由于 loss of significance 而不是对称的,但默认的输出小数位数隐藏了它。自己看看:
> options(digits=20)
> S[1,2]
[1] 0.033457660484940172
> S[2,1]
[1] 0.033457660484940213
问题是,matrixcalc
包中的 is.symmetric.matrix
并没有考虑微小的差异(即它只是比较矩阵元素与严格的 ==
而不是 all.equal
方法) 而 isSymmetric
来自 Matrix
包。如果你四舍五入矩阵,一切都会好起来的:
> S=round(S,10)
> is.symmetric.matrix(S)
[1] TRUE
> is.positive.definite(S)
[1] TRUE