<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Mgarch on 格物堂</title><link>https://gewutang.cn/tags/mgarch/</link><description>Recent content in Mgarch on 格物堂</description><generator>Hugo -- gohugo.io</generator><language>zh-cn</language><lastBuildDate>Mon, 10 Jun 2013 00:00:00 +0000</lastBuildDate><atom:link href="https://gewutang.cn/tags/mgarch/index.xml" rel="self" type="application/rss+xml"/><item><title>安装mgarch包终极解决方案</title><link>https://gewutang.cn/2013/06/10/recipe-about-mgarch/</link><pubDate>Mon, 10 Jun 2013 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2013/06/10/recipe-about-mgarch/</guid><description>&lt;p&gt;&lt;code&gt;mgarch&lt;/code&gt;包是一个可以拟合和诊断&lt;code&gt;BEKK&lt;/code&gt;模型的&lt;code&gt;R&lt;/code&gt;包。包中的主函数很少，只有&lt;code&gt;mvBEKK.sim&lt;/code&gt;，&lt;code&gt;mvBEKK.diag&lt;/code&gt;，&lt;code&gt;mvBEKK.est&lt;/code&gt;三个。这三个函数各司其职，分别用来模拟&lt;code&gt;BEKK&lt;/code&gt;数据、诊断&lt;code&gt;BEKK&lt;/code&gt;模型、拟合&lt;code&gt;BEKK&lt;/code&gt;模型。照常理说，这样的包很好啦。就只有一样不好，作者自从2009年更新一次之后，再也不提供更新的&lt;code&gt;.zip&lt;/code&gt;包了。很多需要拟合&lt;code&gt;BEKK&lt;/code&gt;模型的同学急得团团转就是不知道怎么安装和使用这个包。这里提供三个方案：&lt;/p&gt;&#10;&lt;h3 id="方案一"&gt;&lt;a href="#%e6%96%b9%e6%a1%88%e4%b8%80" class="header-anchor"&gt;&lt;/a&gt;方案一：&#10;&lt;/h3&gt;&lt;p&gt;下载&lt;a class="link" href="http://sourceforge.net/projects/mgarch/?source=directory" target="_blank" rel="noopener"&#10; &gt;mgarch&lt;/a&gt;的源代码包&lt;code&gt;mgarch_0.00-1.tar.gz&lt;/code&gt;。解压缩到&lt;code&gt;R&lt;/code&gt;的当前工作文件夹中。如果不知道当前工作文件夹路径。敲入&lt;code&gt;get.wd()&lt;/code&gt;函数就可以看到了。解压缩之后，按照&lt;code&gt;mgarch-&amp;gt;&amp;gt;R&lt;/code&gt;的顺序打开文件夹，你会在R文件夹中看到好几个&lt;code&gt;.r&lt;/code&gt;格式的文件。用&lt;code&gt;source()&lt;/code&gt;函数载入进来就可以用了。&lt;/p&gt;&#10;&lt;h3 id="方案二"&gt;&lt;a href="#%e6%96%b9%e6%a1%88%e4%ba%8c" class="header-anchor"&gt;&lt;/a&gt;方案二：&#10;&lt;/h3&gt;&lt;p&gt;拿着mgarch包，按照&lt;a class="link" href="http://cos.name/2011/05/write-r-packages-like-a-ninja/" target="_blank" rel="noopener"&#10; &gt;开发R程序包之忍者篇&lt;/a&gt;自己编译&lt;a class="link" href="http://sourceforge.net/projects/mgarch/?source=directory" target="_blank" rel="noopener"&#10; &gt;mgarch&lt;/a&gt;包。&lt;/p&gt;&#10;&lt;h3 id="方案三"&gt;&lt;a href="#%e6%96%b9%e6%a1%88%e4%b8%89" class="header-anchor"&gt;&lt;/a&gt;方案三:&#10;&lt;/h3&gt;&lt;p&gt;这个方案跟方案二类似，也得先安装&lt;code&gt;Rtools&lt;/code&gt;，目的是可以编译源代码。同时，由于&lt;a class="link" href="http://sourceforge.net/projects/mgarch/?source=directory" target="_blank" rel="noopener"&#10; &gt;mgarch&lt;/a&gt;依赖于&lt;code&gt;zoo&lt;/code&gt;、&lt;code&gt;tseries&lt;/code&gt;、&lt;code&gt;mvnorm&lt;/code&gt;三个支撑包，所以，也得捎带手安装一下，并保证它们被升级了到合适的版本。接着，将&lt;code&gt;mgarch_0.00-1.tar.gz&lt;/code&gt;放到当前工作文件夹中（不知道当前工作文件夹是哪个？看方案一）。最后，敲入下面的代码：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;install.packages(&amp;quot;mgarch_0.00-1.tar.gz&amp;quot;,repos=NULL,type=&amp;quot;source&amp;quot;) ##安装源代码文件&#10;library(mgarch) ##查看是否可以使用&#10;&lt;/code&gt;&lt;/pre&gt;&#10;</description></item><item><title>BEKK模型</title><link>https://gewutang.cn/2010/04/15/about-bekk-model/</link><pubDate>Thu, 15 Apr 2010 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2010/04/15/about-bekk-model/</guid><description>&lt;p&gt;BEKK模型是目前比较流行的多变量波动性模型，可以看作GARCH模型在多维空间的实现。BEKK模型在应用过程中也有诸多缺点，其中最大的一个不足之处在于模型的待估计参数会随着模型维数的增加指数型增长。然而，这一点肯定会随着计算机的发展而被逐渐克服的。&lt;/p&gt;&#10;&lt;p&gt;R软件中提供了mgarch和mgarchBEKK两个包来估计BEKK模型参数。&lt;/p&gt;&#10;&lt;h2 id="mgarch包及其示例"&gt;&lt;a href="#mgarch%e5%8c%85%e5%8f%8a%e5%85%b6%e7%a4%ba%e4%be%8b" class="header-anchor"&gt;&lt;/a&gt;mgarch包及其示例&#10;&lt;/h2&gt;&lt;pre&gt;&lt;code class="language-r"&gt;# 安装包（如果未安装）&#10;# devtools::install_github(&amp;quot;https://github.com/vst/mgarch&amp;quot;)&#10;&#10;library(mgarch)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：tseries&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Registered S3 method overwritten by 'quantmod':&#10;## method from&#10;## as.zoo.data.frame zoo&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：mvtnorm&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;先看mgarch包，该包有三个主要函数：&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;mvBEKK.sim: 模拟一个MGARCH-BEKK过程&lt;/li&gt;&#10;&lt;li&gt;mvBEKK.est: 估计MGARCH-BEKK模型的参数&lt;/li&gt;&#10;&lt;li&gt;mvBEKK.diag:对拟合的MGARCH-BEKK模型进行诊断&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;可以通过以下命令查询相关函数的用法：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;?mvBEKK.sim&#10;?mvBEKK.est&#10;?mvBEKK.diag&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;运行以下命令可以模拟一个MGARCH-BEKK过程：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;set.seed(123)&#10;sim = mvBEKK.sim(series.count = 3, T = 1000)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Class attributes are accessible through following names:&#10;## length series.count order params true.params eigenvalues uncond.cov.matrix white.noise eps cor sd&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;合并模拟的时间序列：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;eps = data.frame(sim$eps[[1]], sim$eps[[2]], sim$eps[[3]]) &#10;head(eps)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## sim.eps..1.. sim.eps..2.. sim.eps..3..&#10;## 1 0.9472441 1.2177306 0.5491724&#10;## 2 -1.1241768 -0.3366113 -0.6261458&#10;## 3 0.7179649 -0.3272567 1.6880032&#10;## 4 -0.4857962 1.2230006 -1.9563961&#10;## 5 -1.1301479 4.0603942 -1.3856283&#10;## 6 0.2057734 1.1435079 -0.6392246&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;tail(eps)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## sim.eps..1.. sim.eps..2.. sim.eps..3..&#10;## 995 -1.43678861 -0.8651118 -1.55380698&#10;## 996 -1.10173653 -0.5863329 -1.14479754&#10;## 997 -1.06902893 -0.4398771 -3.08558958&#10;## 998 -0.11478218 0.7226409 -3.00375826&#10;## 999 -1.18562636 2.4487154 -0.04579639&#10;## 1000 0.05764529 -0.2241948 0.86390660&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;估计模型参数：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;est &amp;lt;- mvBEKK.est(eps)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Warning in mvBEKK.est(eps): negative inverted hessian matrix element&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;查看估计结果包含哪些信息：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;names(est)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;eps&amp;quot; &amp;quot;series.length&amp;quot; &amp;quot;estimation.time&amp;quot; &#10;## [4] &amp;quot;total.time&amp;quot; &amp;quot;order&amp;quot; &amp;quot;estimation&amp;quot; &#10;## [7] &amp;quot;aic&amp;quot; &amp;quot;asy.se.coef&amp;quot; &amp;quot;est.params&amp;quot; &#10;## [10] &amp;quot;cor&amp;quot; &amp;quot;sd&amp;quot; &amp;quot;H.estimated&amp;quot; &#10;## [13] &amp;quot;eigenvalues&amp;quot; &amp;quot;uncond.cov.matrix&amp;quot; &amp;quot;residuals&amp;quot;&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;模型诊断：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;mvBEKK.diag(est)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#9;Number of estimated series : 3 &#10;## &#9;Length of estimated series : 1000 &#10;## &#9;Estimation Time : 32.41842 &#10;## &#9;Total Time : 32.98894 &#10;## &#9;BEKK order : 1 1 &#10;## &#9;Eigenvalues : 0.8982855 0.5101535 0.4535976 0.3020472 0.2611067 0.2258987 0.1373383 0.05469751 0.05280793 &#10;## &#9;aic : 4163.509 &#10;## &#9;unconditional cov. matrix : 2.654942 -0.4582163 0.8566066 -0.4582163 2.38067 0.1093766 0.8566066 0.1093766 2.01 &#10;## &#9;var(resid 1 ) : 0.999399 &#10;## &#9;mean(resid 1 ) : -0.03860414 &#10;## &#9;var(resid 2 ) : 0.9988757 &#10;## &#9;mean(resid 2 ) : 0.04335019 &#10;## &#9;var(resid 3 ) : 0.997348 &#10;## &#9;mean(resid 3 ) : 0.02152967 &#10;## &#9;Estimated parameters :&#10;## &#10;## &#9;C estimates:&#10;## [,1] [,2] [,3]&#10;## [1,] 0.8909642 0.3328637 0.34152387&#10;## [2,] 0.0000000 0.9827892 0.05270326&#10;## [3,] 0.0000000 0.0000000 1.01236445&#10;## &#10;## &#9;ARCH estimates:&#10;## [,1] [,2] [,3]&#10;## [1,] 0.340809760 0.06992293 0.07918491&#10;## [2,] -0.064873614 0.36049917 0.35553210&#10;## [3,] -0.008381618 -0.06665216 0.50150310&#10;## &#10;## &#9;GARCH estimates:&#10;## [,1] [,2] [,3]&#10;## [1,] -0.3353086 0.37376137 -0.5032390&#10;## [2,] 0.2151724 0.03756262 0.4608374&#10;## [3,] 0.1756196 0.01577004 -0.4594647&#10;## &#10;## &#9;asy.se.coef : &#10;## &#10;## &#9;C estimates, standard errors:&#10;## [,1] [,2] [,3]&#10;## [1,] 0.07738385 0.1351731 0.2359051&#10;## [2,] 0.00000000 0.1141723 0.2671477&#10;## [3,] 0.00000000 0.0000000 0.2311768&#10;## &#10;## &#9;ARCH estimates, standard errors:&#10;## [,1] [,2] [,3]&#10;## [1,] 0.05481912 0.05484527 0.07796872&#10;## [2,] 0.04677913 0.05338068 0.07267995&#10;## [3,] 0.02304154 0.02555282 0.04677987&#10;## &#10;## &#9;GARCH estimates, standard errors:&#10;## [,1] [,2] [,3]&#10;## [1,] 0.17105425 0.19123870 0.31018515&#10;## [2,] 0.17509179 0.26972309 0.33665408&#10;## [3,] 0.05380941 0.07028591 0.09210273&#10;## Called from: mvBEKK.diag(est)&#10;## debug: for (i in 1:length(estimation$eps)) {&#10;## plot(estimation$residuals[[i]])&#10;## browser()&#10;## dev.off()&#10;## }&#10;## debug: plot(estimation$residuals[[i]])&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## debug: browser()&#10;## debug: dev.off()&#10;## debug: plot(estimation$residuals[[i]])&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## debug: browser()&#10;## debug: dev.off()&#10;## debug: plot(estimation$residuals[[i]])&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## debug: browser()&#10;## debug: dev.off()&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;h2 id="mgarchbekk包及其示例"&gt;&lt;a href="#mgarchbekk%e5%8c%85%e5%8f%8a%e5%85%b6%e7%a4%ba%e4%be%8b" class="header-anchor"&gt;&lt;/a&gt;mgarchBEKK包及其示例&#10;&lt;/h2&gt;&lt;pre&gt;&lt;code class="language-r"&gt;# 安装包（如未安装）&#10;# devtools::install_github(&amp;quot;https://github.com/vst/mgarchBEKK&amp;quot;)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 加载包&#10;library(mgarchBEKK)&#10;# 模拟mgarchBEKK过程&#10;simulated &amp;lt;- simulateBEKK(2, 1000, c(1,1))&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Class attributes are accessible through following names:&#10;## length series.count order params true.params eigenvalues uncond.cov.matrix white.noise eps cor sd&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 合并模拟数据&#10;simulated &amp;lt;- do.call(cbind, simulated$eps)&#10;# 估计模型&#10;estimated &amp;lt;- BEKK(simulated)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;## H IS SINGULAR!...&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Warning in BEKK(simulated): negative inverted hessian matrix element&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 模型诊断&#10;diagnoseBEKK(estimated)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#9;Number of estimated series : 2000 &#10;## &#9;Length of estimated series : 1000 &#10;## &#9;Estimation Time : 3.208226 &#10;## &#9;Total Time : 3.330154 &#10;## &#9;BEKK order : 1 1 &#10;## &#9;Eigenvalues : 0.7940983 0.7122278 0.5839173 0.5353965 &#10;## &#9;aic : 4614.775 &#10;## &#9;unconditional cov. matrix : 6.481396 1.496551 1.496551 7.939293 &#10;## &#9;var(resid 1 ) : 1.000363 &#10;## &#9;mean(resid 1 ) : -0.007109519 &#10;## &#9;var(resid 2 ) : 0.9958192 &#10;## &#9;mean(resid 2 ) : -0.01444951 &#10;## &#9;Estimated parameters :&#10;## &#10;## &#9;C estimates:&#10;## [,1] [,2]&#10;## [1,] 0.9304752 0.4214324&#10;## [2,] 0.0000000 1.6921455&#10;## &#10;## &#9;ARCH estimates:&#10;## [,1] [,2]&#10;## [1,] -0.5382996 0.003724502&#10;## [2,] 0.3896205 -0.351431137&#10;## &#10;## &#9;GARCH estimates:&#10;## [,1] [,2]&#10;## [1,] 0.3804035 0.5270529&#10;## [2,] -0.2806881 0.6526116&#10;## &#10;## &#9;asy.se.coef : &#10;## &#10;## &#9;C estimates, standard errors:&#10;## [,1] [,2]&#10;## [1,] 0.2069876 0.5373757&#10;## [2,] 0.0000000 0.2889314&#10;## &#10;## &#9;ARCH estimates, standard errors:&#10;## [,1] [,2]&#10;## [1,] 0.04044854 0.04641720&#10;## [2,] 0.02825686 0.04050683&#10;## &#10;## &#9;GARCH estimates, standard errors:&#10;## [,1] [,2]&#10;## [1,] 0.05265359 0.05895967&#10;## [2,] 0.06856104 0.07941475&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Called from: diagnoseBEKK(estimated)&#10;## debug: dev.off()&#10;## debug: plot(estimation$residuals[[i]])&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## debug: browser()&#10;## debug: dev.off()&#10;&lt;/code&gt;&lt;/pre&gt;&#10;</description></item></channel></rss>