有哇有哇,你是要VaR函数吧~~~
PerformanceAnalytics 包
fExtremes 包
fAssets 包
actuar 包
上面这几个都提供了计算VaR的函数。
下面用PerformanceAnalytics包中的VaR函数示范一下计算VaR的方法。
> local({pkg <-select.list(sort(.packages(all.available = TRUE)),graphics=TRUE)
+ if(nchar(pkg)) library(pkg,character.only=TRUE)})
> setRepositories()
> utils:::menuInstallPkgs()
also installing the dependency ‘xts’
试开URL’http://cran.dataguru.cn/bin/windows/contrib/2.15/xts_0.8-6.zip'
Content type 'application/zip' length525617 bytes (513 Kb)
打开了URL
downloaded 513 Kb
试开URL’http://cran.dataguru.cn/bin/wind ... alytics_1.0.4.4.zip
Content type 'application/zip' length1815970 bytes (1.7 Mb)
打开了URL
downloaded 1.7 Mb
程序包‘xts’打开成功,MD5和检查也通过
程序包‘PerformanceAnalytics’打开成功,MD5和检查也通过
下载的二进制程序包在
C:\Users\Administrator.PC-20120103CRUD\AppData\Local\Temp\RtmpMlKZ4R\downloaded_packages里
> library(PerformanceAnalytics)
载入需要的程辑包:zoo
载入程辑包:‘zoo’
The following object(s) are masked from‘package:base’:
as.Date, as.Date.numeric
载入需要的程辑包:xts
Package PerformanceAnalytics (1.0.4.4)loaded.
Econometric tools for performance and riskanalysis.
(c) 2004-2012 Peter Carl, Brian G.Peterson, Kris Boudt, Eric Zivot. License: GPL
http://r-forge.r-project.org/projects/returnanalytics/
载入程辑包:‘PerformanceAnalytics’
The following object(s) are masked from‘package:graphics’:
legend
警告信息:
1: 程辑包‘PerformanceAnalytics’是用R版本2.15.1 来建造的
2: 程辑包‘xts’是用R版本2.15.1 来建造的
> data(edhec)
> head(edhec)
Convertible Arbitrage CTA Global Distressed Securities Emerging MarketsEquity Market Neutral Event Driven Fixed Income Arbitrage Global Macro
1997-01-31 0.0119 0.0393 0.0178 0.0791 0.0189 0.0213 0.0191 0.0573
1997-02-28 0.0123 0.0298 0.0122 0.0525 0.0101 0.0084 0.0122 0.0175
1997-03-31 0.0078 -0.0021 -0.0012 -0.0120 0.0016 -0.0023 0.0109 -0.0119
1997-04-30 0.0086 -0.0170 0.0030 0.0119 0.0119 -0.0005 0.0130 0.0172
1997-05-31 0.0156 -0.0015 0.0233 0.0315 0.0189 0.0346 0.0118 0.0108
1997-06-30 0.0212 0.0085 0.0217 0.0581 0.0165 0.0258 0.0108 0.0218
Long/Short Equity Merger Arbitrage Relative Value Short Selling Funds ofFunds
1997-01-31 0.0281 0.0150 0.0180 -0.0166 0.0317
1997-02-28 -0.0006 0.0034 0.0118 0.0426 0.0106
1997-03-31 -0.0084 0.0060 0.0010 0.0778 -0.0077
1997-04-30 0.0084 -0.0001 0.0122 -0.0129 0.0009
1997-05-31 0.0394 0.0197 0.0173 -0.0737 0.0275
1997-06-30 0.0223 0.0231 0.0198 -0.0065 0.0225
> V=VaR(edhec,p=0.95,method="historical")
> t(V)
VaR
Convertible Arbitrage -0.019160
CTA Global -0.035400
Distressed Securities -0.018875
Emerging Markets -0.044605
Equity Market Neutral -0.006385
Event Driven -0.022540
Fixed Income Arbitrage -0.009290
Global Macro -0.016240
Long/Short Equity -0.025440
Merger Arbitrage -0.013455
Relative Value -0.013175
Short Selling -0.078480
Funds of Funds -0.021265
>B=VaR(edhec,portfolio_method="component")
no weights passed in, assuming equal weightedportfolio
> B
$MVaR
[,1]
[1,] 0.01459739
$contribution
Convertible Arbitrage CTA Global Distressed Securities Emerging Markets Equity Market Neutral Event Driven Fixed Income Arbitrage
2.167008e-03 -9.876816e-05 1.720857e-03 3.691141e-03 4.935426e-04 1.780578e-03 1.486652e-03
Global Macro Long/ShortEquity Merger Arbitrage Relative Value Short Selling Funds of Funds
9.209540e-04 1.784986e-03 6.778369e-04 1.324299e-03 -3.150584e-03 1.798890e-03
$pct_contrib_MVaR
Convertible Arbitrage CTA Global Distressed Securities Emerging Markets Equity Market Neutral Event Driven Fixed Income Arbitrage
0.148451703 -0.006766151 0.117887954 0.252863009 0.033810326 0.121979204 0.101843703
Global Macro Long/ShortEquity Merger Arbitrage Relative Value Short Selling Funds of Funds
0.063090308 0.122281163 0.046435481 0.090721618 -0.215831957 0.123233638
> t(B)
MVaR contributionpct_contrib_MVaR
[1,] 0.01459739 Numeric,13 Numeric,13
终于贴完了...快给我点赞!