性能分析

R 在將 p=0.995 用於計算 VaR 時返回 numeric(0)

  • July 27, 2017

我的程式碼實際上工作得很好。我不明白的是,如果我輸入 p=0.995 而不是 0.95,控制台會給我數字(0)。什麼會導致此錯誤?但是,當我使用“高斯”方法時,它工作得很好。

該函式嘗試以 99.5% 的機率水平計算風險價值。在 95% 時,該功能可以正常工作。您可以使用以下程式碼進行重建。

謝謝!

VaR(bonds.returns,p=0.995, weights= weights,portfolio_method = "component", method="historical")

債券.回報

  structure(c(0.0075452479085516, -0.000430602750240983, -0.000618087487474384, 
0.00222086866888427, 0.0106496372204352, 0.00136921667853951, 
0.00423968343697001, 0.0024904263575809, -0.000195676402276579, 
0.000280808046426939, 0.00478090360779571, 0.0034797185745854, 
0.00730203951222119, -0.000343622174203362, -0.00819027347184287, 
-0.00430557592076686, 0.0121157759451644, 0.00511236623854172, 
0.00632861635220117, 0.00281239014100998, -0.000272659992988644, 
0.00102275383776207, 0.00580914478101358, 0.00408258034556819, 
0.00683299709040042, 0.00444050627813075, 0.00112276199450645, 
-0.00105140888790989, 0.00547308066278407, 0.00292103243043851, 
0.000354539209820759, 0.000498394063572771, 0.000774893452150316, 
0.00578507825894592, 0.00786336442020064, 0.00264068177602228, 
0.0044426685809873, 0.00123386036476414, 0.000437716332879923, 
0.00273957849531858, 0.00459152849567035, 0.00344127119889892, 
0.00372234677044103, 0.00235998145728855, -0.000988017658188123, 
0.000196396130996312, 0.00448116019271083, 0.00288335334692413, 
0.00421227034351057, 0.00228605784355529, -0.00052312928966014, 
0.00158416670737593, 0.00452201783723516, 0.0028924387350957, 
0.00519960504742145, 0.00304595059792567, 0.00181483311621222, 
0.00192813007255133, 0.00722328637152936, 0.00474544339681326, 
0.00443227606157648, 0.00133718717534648, -5.13615959075731e-05, 
-0.00041091387245229, 0.00307284387075546, 0.00220280114340743, 
0.00575621301775153, 0.00313461918612856, -0.000187677120282226, 
0.000272182906913443, 0.00450387051372281, 0.00321330157395727, 
0.0056956454201833, 0.00323328013180268, 0.000923749602274482, 
0.00117925737548585, 0.00519286314193823, 0.00386179068890669, 
0.00449726636750203, 0.00136557388242409, 6.81855816717647e-05, 
-9.74013324506195e-06, 0.00322401550644313, 0.00166993533855031, 
0.00521548174581388, 0.0035394698147293, -0.000146521679975864, 
0.000879258910347014, 0.00566832951610041, 0.00377491914601547, 
0.00289058379650298, -0.00118324046075591, 8.10011745171479e-05, 
-0.000577086623739564, 0.0027756673251278, 0.00217195850043939, 
0.00844551759115331, 0.00488711529000163, 0.000326399164418012, 
0.0021100258858846, 0.00678345055135887, 0.00597233751253223, 
0, -0.00294687131050764, -0.0124380701571574, -0.00119904076738608, 
0.00780792316926782, -0.00107682631649164, 0.00416801143436785, 
0.00224983241044274, -5.49742537244713e-05, 0.000394003811757804, 
0.00469870579507048, 0.00247967034970653, 0.00207520724910104, 
0.00483573674509374, 0.000787690448444067, 0.00624265490733067, 
0.00121078347334125, -0.00618572147130281, 0.00466954738246472, 
0.00228923662382208, -0.00131503490280616, -0.000811840997970381, 
0.00319055121231049, 0.00204454540964982, 0.000172922203210746, 
0.0043951044178534, 0.00170323796408711, -0.000533617929562413, 
0.000841575646791437, 0.0009493670886076, 0.00477505036185932, 
0.00240800263076935, 0.00104766339315954, 0.000909138960833111, 
0.00509077850888784, 0.00328909345018547, 0.00160412259506937, 
0.00107103890774041, 0.00254974502549743, 0.000578467062284815, 
0.00337908555366173, -0.00140072718602857, 0.00422467406148375, 
0.00172189447526239, -0.000234400179706773, -0.000341913740047883, 
0.00276556239616932, 0.00202703361172563), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC", class = c("xts", 
"zoo"), index = structure(c(1491177600, 1491264000, 1491350400, 
1491436800, 1491523200, 1491782400), tzone = "UTC", tclass = "Date"), .Dim = c(6L, 25L), .Dimnames = list(NULL, c("BE0000343526", "BE6248644013", 
"FR0010171975", "FR0013152907", "IE00BV8C9186", "SI0002103677", 
"XS0162513211", "XS0162869076", "XS0162990229", "XS0908570459", 
"XS1117298247", "XS1146286205", "XS1196380031", "XS1196817586", 
"XS1200679667", "XS1202849086", "XS1203860934", "XS1313004928", 
"XS1362373224", "XS1388864503", "XS1405784015", "XS1418788599", 
"XS1463101680", "XS1538284230", "XS1570260460")))

權重

c(0.039728041, 0.040869022, 0.042067239, 0.039212564, 0.039765805, 
0.040603312, 0.041035065, 0.04158508, 0.039500351, 0.042032909, 
0.040638835, 0.040266658, 0.040995045, 0.04002851, 0.040191534, 
0.039809092, 0.039765018, 0.041233473, 0.04085667, 0.027147317, 
0.041430642, 0.040688867, 0.040016652, 0.040498212, 0.040034086
)

問題是由於您的數據而出現的。您的某些列在該百分位數中沒有負值(例如檢查XS1463101680XS1203860934- 所有正數),因此歷史 VaR 是沒有意義的(您的損失不能是正值,損失總是負數)。因此,您將收到錯誤消息。

此外,您的程式碼p = 0.95返回相同的錯誤。

編輯

不幸的是,如果您的數據觀察(返回)少於**200 個,則此特定功能將不起作用。**為什麼?好吧,在研究了程式碼之後,我發現了以下內容:

頁面的底部,您可以看到用於計算歷史分量 VaR 的公式:

component = {
       # @todo need to add another loop here for subsetting, I think, when weights is a timeseries
       #if (mu=NULL or sigma=NULL) {
       #     pfolioret = Return.portfolio(R, weights, wealth.index = FALSE, contribution=FALSE, method = c("simple"))
       #}
       # for now, use as.vector
       weights=as.vector(weights)
       names(weights)<-colnames(R)

   switch(method,
       modified = { return(VaR.CornishFisher.portfolio(p,weights,mu,sigma,m3,m4))},
       gaussian = { return(VaR.Gaussian.portfolio(p,weights,mu,sigma)) },
       historical = { return(VaR.historical.portfolio(R, p,weights)) },
       kernel = { return(VaR.kernel.portfolio(R, p,weights)) }
   )

}, # end component portfolio switch

追溯函式VaR.historical.portfolio,在本頁底部我們觀察到:

VaR.historical.portfolio = function(R,p,w)
{
   alpha = .setalphaprob(p)
   portret = c();
   T = dim(R)[1]
   N = dim(R)[2]
   for( t in c(1:T) ){
      portret = c(portret,sum(w*as.numeric(R[t,])))
   }
   hVaR = -1* sort(portret)[floor(alpha*T)]
   return(hVaR)
}

T = dim(R)[1]一塊給你你的數據的行數。你的 alpha 是1 - 0.9950.005。然後你有這段程式碼floor(alpha*T),它基本上破壞了一切,並且alpha*T是0。然後返回。0.76``floor(0.76)``sort(portret)[0]``numeric(0)

該函式適用,p = 0.95因為您的 alpha 是0.05102 * 0.05 = 5.1因此floor(5.1) = 5您將第五個元素作為您的歷史分量 VaR。

引用自:https://quant.stackexchange.com/questions/35295