• R语言
  • 求助:Error in solve.default(A) : system is computationally singular: reciprocal

大家好,我在使用rmgarch包中dccfit函数进行拟合DCC-GARCH模型时,程序报如下的错误:
Error in solve.default(A) :
system is computationally singular: reciprocal condition number = 1.99569e-16
我在网上查了一下,但是没有找到解决方法,麻烦哪位朋友帮我看一下?谢谢!

我的数据x的都是一些股票指数的对数收益率(所有变量无缺失值,且都通过平稳性检验)。

我的程序如下:

x.xts <- xts(x=x[ , 2:ncol(x)], order.by=x[ , 1])

uspec = ugarchspec(variance.model = list(model="fGARCH", submodel="GARCH"), mean.model = list(armaOrder=c(1, 0)),distribution.model = "std")

mspec = multispec(replicate(n=ncol(x)-1, uspec))

DCCspec = dccspec(mspec)
dccgarch.fit = dccfit(DCCspec, data=x.xts, solver=c("hybrid", "solnp"))
Error in solve.default(A) :
system is computationally singular: reciprocal condition number = 1.94649e-16

这种情况并不是一直出现,完全一样的程序,改变数据集的时候,程序就正常运行,但遇到上面的数据时,程序就报出类似的错误。

在网上找到一些网页,但我看不出什么具体的方法,供大家参考:
http://stackoverflow.com/questions/13521937/error-in-mlogit-error-in-solve-defaulth-gfixed-system-is-computationall

http://tolstoy.newcastle.edu.au/R/e5/help/08/09/1293.html

2 年 后

简单来说,方阵不满秩,例子:线性模型中解释变量存在完全共线性。跟数据质量没啥关系,当然把矩阵singular当成数据质量问题好像也没什么错。
如果你能找到是哪个矩阵求逆出的问题,提供两个我能脑补出来的方案:
1.用广义逆
2.手动给该矩阵加上影响不大的一个小扰动
题外话,出现这种错误其实还是数据的原因,仔细检查下数据的各种特征,毕竟改别人的package比改数据难很多了。

    holydudu
    刚测试了下solve函数,就只有出现全部是0的情况之下才会出现此类singular错误信息~?

    > matrix(runif(25), nc = 5) %>% solve.default
               [,1]        [,2]         [,3]       [,4]       [,5]
    [1,]  0.9069072 -0.72940986 -2.013602193  0.6193949  1.6909356
    [2,]  2.5310537  0.09066826 -2.397023376 -0.1885138  0.1726358
    [3,]  1.9180058 -0.91873135 -0.193985265  0.1208054 -0.5724837
    [4,]  0.5484761  0.27082491  0.003404529 -0.9784695  0.4331857
    [5,] -5.0098767  1.41098122  3.990183295  1.5030079 -1.0398107
    > matrix(runif(25), nc = 5) %>% solve
                [,1]         [,2]       [,3]       [,4]       [,5]
    [1,] -2.54403041 -3.270732792  3.8787546  1.4360276  0.2553427
    [2,] -0.06831753 -0.564190663 -0.1453375  0.2050192  1.6809986
    [3,] -1.69945917  0.002586251  0.5663346  0.7378965  0.3933298
    [4,]  1.10519682  0.722224859  0.1642177 -0.8689460 -0.1381785
    [5,]  4.88303095  3.945996611 -5.8467706 -0.7501183 -1.7452565
    > matrix(c(runif(24), Inf), nc = 5) %>% solve
         [,1] [,2] [,3] [,4] [,5]
    [1,]  NaN  NaN  NaN  NaN  NaN
    [2,]  NaN  NaN  NaN  NaN  NaN
    [3,]  NaN  NaN  NaN  NaN  NaN
    [4,]  NaN  NaN  NaN  NaN  NaN
    [5,]  NaN  NaN  NaN  NaN  NaN
    > matrix(c(runif(24), -Inf), nc = 5) %>% solve
         [,1] [,2] [,3] [,4] [,5]
    [1,]  NaN  NaN  NaN  NaN  NaN
    [2,]  NaN  NaN  NaN  NaN  NaN
    [3,]  NaN  NaN  NaN  NaN  NaN
    [4,]  NaN  NaN  NaN  NaN  NaN
    [5,]  NaN  NaN  NaN  NaN  NaN
    > matrix(c(runif(24), NA), nc = 5) %>% solve
         [,1] [,2] [,3] [,4] [,5]
    [1,]   NA   NA   NA   NA   NA
    [2,]   NA   NA   NA   NA   NA
    [3,]   NA   NA   NA   NA   NA
    [4,]   NA   NA   NA   NA   NA
    [5,]   NA   NA   NA   NA   NA
    > solve.default(matrix(rep(0, 25), nc = 5))
    Error in solve.default(matrix(rep(0, 25), nc = 5)) : Lapack routine dgesv: system is exactly singular: U[1,1] = 0

    就如 https://d.cosx.org/d/6181-6181/28 帖子所述,目前得阅读些参考文献,才能知道Copula原理和如何使用运用:

    • 略读过rmgarch作者的使用Rcpp编程(僕还不会高效率的C++
    • 阅读过他的背景和部落格,不简单。博士生背景经验老道的量化交易兼某家公司的首席经济学家。

    兄台您真是爱说笑了,小巫见大巫。?
    这两天得嚼嚼以下文献才行... 之后还得学习高频率模型、投资建模、投资组合建模吖。一摞摞的科研,叹气~(身边每个人频频催僕赶快找工作,僕就连科研都尚未完成,如何踏入量化交易行业呢?)

    照您这么卖力学量化,相信不久后必然可以直播做量化了?,纯粹说笑,莫介意!

    set.seed(123)                 
    aaa <- matrix(rnorm(20), 5, 4)
    bbb <- cbind(aaa, aaa[, 4])   
    solve(bbb)                    
    #> Error in solve.default(bbb): system is computationally singular: reciprocal condition number = 2.55787e-18

    报的错误不同,也只是分母非常接近于0和分母等于0的区别,我猜。

      holydudu 多谢前辈教导,不过就不晓得为啥电脑程序也会出错呢??

      > solve(matrix(rnorm(25), 5, 5))
                  [,1]          [,2]       [,3]       [,4]       [,5]
      [1,]  8.24446226  -8.799586412 -7.8797734 -8.4941483 10.2240770
      [2,]  0.12945761   0.003591567 -0.2092521 -0.4213559  0.6124343
      [3,]  9.97508114 -10.277994259 -8.7824676 -9.8373951 12.4311528
      [4,]  8.35414720  -7.059080960 -7.5637485 -7.8476009  9.7568802
      [5,] -0.02154905   0.210138922 -0.2691161  0.2612327  0.1006492
      > ccc <- cbind(aaa[,1:2], aaa[,1:2], aaa[,1])
      > solve(ccc)
      Error in solve.default(ccc) : Lapack routine dgesv: system is exactly singular: U[4,4] = 0
      > solve(matrix(rep(0, 25), 5,5))
      Error in solve.default(matrix(rep(0, 25), 5, 5)) : Lapack routine dgesv: system is exactly singular: U[1,1] = 0
      > solve(ccc)
      Error in solve.default(ccc) : Lapack routine dgesv: system is exactly singular: U[4,4] = 0

      以下乃错误预测价格之一。

      • 2014-07-10 USDCNY.Lo 2.201就是数据错误吧?毕竟人民币都未曾涨至2.201的兑换率。
      • 预测价错误2015-07-28 USDCHF.Op 0.962 -1674.2440018,运行结果的T+1预测价竟然会是Loglikelihood-1674.2440018

      比方说程序在计算时偶尔出错还有可能(例子:使用cluster或者for loop连续与并行计算,基本上还有可能出现内存地址储存错误,3.3 指针 (Pointers)),晚辈不明的是为啥僕尝试单一(没使用for loop和 cluster预测单日价格)运行代码也会出错??

      Error data
      .id	Cat	Date	        Type	        Price	Price.T1	Akaike	        Bayes	        Shibata	        Hannan.Quinn	diff	       se
      USDCHF	pred1	2015-07-28	USDCHF.Op	0.962	-1674.2440018	-6.855036	-6.691600	-6.858992	-6.789348	1740.3783803	1
      USDCHF	pred1	2015-01-15	USDCHF.Lo	0.733	1.0165024	-5.799861	-5.718143	-5.800879	-5.767017	1.3867699	1
      USDCNY	pred1	2014-07-10	USDCNY.Lo	2.201	6.1863967	-2.713266	-2.631323	-2.714291	-2.680328	2.8107209	1
      USDCNY	pred1	2014-07-14	USDCNY.Lo	6.196	1.2948723	-3.150394	-3.013822	-3.153188	-3.095496	0.2089852	1
      USDJPY	pred1	2013-06-27	USDJPY.Op	98.468	78.3949808	4.713287	4.795004	4.712269	4.746131	0.7961468	1
      USDJPY	pred1	2013-06-30	USDJPY.Op	99.411	78.3964420	4.735608	4.817551	4.734583	4.768547	0.7886093	1

      原文:binary.com 面试试题 I - GARCH模型中的ARIMA(p,d,q)参数最优化