compute.ret <- function
(
rl, # risk-level
cm, # matrix
weights # market weights
)
{
ret <<- as.numeric(rl) * cm %*% as.numeric(weights)
return(ret)
}
compute.ret(rl, mat, mkt)
rl
- это постоянное число, и если я определю mat
как одну матрицу k * n и mkt
как один вектор k * 1, эта функция работает отлично.Однако если я определю mat
как список, содержащий несколько разных матриц k * n, и mkt
как список, содержащий несколько разных векторов k * 1 для разных периодов, я получу следующее сообщение об ошибке:
Ошибка в compute.ret (rl, mat, mkt): (список) объект не может быть приведен к типу 'double'
Если я попытаюсь определить cm
и weights
внутрифункция как матрица и вектор, я получаю следующее сообщение об ошибке:
Ошибка в as.matrix (см)% *% as.vector (вес): требуется числовая / сложная матрица / векторАргументы
Я определил mat
и mkt
следующим образом:
#For the first period
mat <- cov(returns[2:100,])
mkt <- mkt.pct[100,]
#For multiple periods
mat <- lapply(100:nrow(returns), function(y) cov(returns[2:y,]))
mkt <- lapply(100:nrow(mkt.pct), function(y) Mkt.pct[y,])
#risk-level is constant:
rl <- 5
#dput(head(returns,2))
structure(list(DNB = c(0.114243002653732, 0.195422174486189),
`NORSK HYDRO` = c(0.1122798949739, -0.00460120398819178),
ORKLA = c(0.161921233302634, 0.0975985380644365), STOREBRAND = c(-0.0104598710465411,
0.134080646470034), ATEA = c(0.230813685612872, 0.0833298658193907
), `SCHIBSTED A` = c(0.175083182810205, 0.025055387901995
), BONHEUR = c(0.122042954064391, 0.0685451093572719), EKORNES = c(0.0308399733914992,
0.387914655275288), `KONGSBERG GRUPPEN` = c(0.146331747503733,
0.144545571434711), `TOMRA SYSTEMS` = c(0.229735097295185,
0.113694123289024), VEIDEKKE = c(0.0956482232945592, 0.127761864415601
), `ARENDALS FOSSEKOMPANI` = c(-0.00490324849781087, 0.0300289375740116
), `OLAV THON EIEP.` = c(0.149949369069503, 0.124473689874361
), `PETROLEUM GEO SERVICES` = c(0.143132959750097, 0.0775223758035154
), `SPAREBANK 1 SR BANK` = c(0.116836172780265, 0.136008691393096
), `STOLT-NIELSEN` = c(0.0203245318798289, 0.105361175863283
), `ODFJELL 'A'` = c(0.0928105116601813, 0.0951529274561497
), `SPAREBANK 1 NORD-NORGE` = c(0.101567028883779, 0.0178314553554076
), `SPAREBANK 1 SMN` = c(0.0737988562169507, 0.0494500632192766
), `WILHS.WILHELMSEN HDG.'A'` = c(0.0183315120772754, 0.056632199816499
), `NORDEA BANK (~NK)` = c(-0.0453314420778795, 0.148848639131562
), `ATLAS COPCO 'A' (~NK)` = c(0.0248312597721068, 0.0290019908934364
), `VOLVO 'B' (~NK)` = c(0.0731428324040068, 0.0809789583310443
), `SANDVIK (~NK)` = c(0.0215711016815666, 0.0478190202909319
), `SWEDBANK 'A' (~NK)` = c(-0.00387080379817896, 0.175104194416416
), `ERICSSON 'B' (~NK)` = c(0.0867095270438493, 0.0422849025158128
), `SVENSKA HANDBKN.'A' (~NK)` = c(-0.112150502158686, 0.106884133216956
), `HENNES & MAURITZ 'B' (~NK)` = c(0.0699802466749088, -0.0165562560357835
), `SEB 'A' (~NK)` = c(0.0241105004437796, 0.100514480686855
), `INVESTOR 'B' (~NK)` = c(0.0901067257894706, 0.0720965788285361
), `SWEDISH MATCH (~NK)` = c(0.0495931153170738, 0.00673046936294565
), `ELECTROLUX 'B' (~NK)` = c(-0.0159231362522644, 0.0948956647849578
), `SKANSKA 'B' (~NK)` = c(-0.0149499928083486, 0.0559276474130345
), `SCA 'B' (~NK)` = c(0.0153704546006071, 0.112653682679431
), `SECURITAS 'B' (~NK)` = c(0.0859336634195991, -0.0157794854084339
), `HOLMEN 'B' (~NK)` = c(-0.0103328627361939, 0.15208408056624
), `SSAB 'A' (~NK)` = c(0.0775964977767735, -0.00139032020103993
), `ERICSSON 'A' (~NK)` = c(0.0883253562867765, 0.0455789424195976
), `INVESTOR 'A' (~NK)` = c(0.0927668419378803, 0.0658048552831031
), `VOLVO 'A' (~NK)` = c(0.0676940440190063, 0.0844327289446728
), `NOVO NORDISK 'B' (~NK)` = c(0.00836898160564752, 0.111705492228705
), `DANSKE BANK (~NK)` = c(0.0320936491074429, 0.161716610294261
), `COLOPLAST 'B' (~NK)` = c(-0.0485797018299386, 0.0971342879304209
), `CARLSBERG 'B' (~NK)` = c(-0.00952448228427664, 0.0481822504669644
), `A P MOLLER - MAERSK 'B' (~NK)` = c(0.134842700281774,
0.101913237684838), `TDC (~NK)` = c(0.0252955834254122, 0.100036296355213
), `TOPDANMARK (~NK)` = c(0.0492085405235308, 0.0885565764800388
), `WILLIAM DEMANT HLDG. (~NK)` = c(-0.0328990626156782,
0.0970785204593115), `JYSKE BANK (~NK)` = c(0.0260312437317136,
0.155304805452083), `KOBENHAVNS LUFTHAVNE (~NK)` = c(0.067217427832867,
0.0305874626848826), `NKT (~NK)` = c(-0.0201934852148808,
0.170861427458748), `ROCKWOOL 'B' (~NK)` = c(-0.00151625629222524,
-0.021101011531117), `SYDBANK (~NK)` = c(0.109683728491056,
0.0495161923681742), `FLSMIDTH & CO.'B' (~NK)` = c(0.0798367466050992,
0.0261601909079402), `GN STORE NORD (~NK)` = c(0.0244859713531514,
0.0854282276786818), `ALK-ABELLO (~NK)` = c(-0.0396520859817241,
0.0808896077540951), `BANG & OLUFSEN 'B' (~NK)` = c(0.00304894573872123,
0.220052552386606), `SANTA FE GROUP (~NK)` = c(0.291329154090765,
0.0472354961961209), `CARLSBERG 'A' (~NK)` = c(-0.00700101105484289,
0.0359347297366485), `ROCKWOOL 'A' (~NK)` = c(-0.0245025749334355,
-0.00139537865863981), `NOKIA (~NK)` = c(0.126441684721964,
0.00769527210049607), `SAMPO 'A' (~NK)` = c(0.0940079555690148,
0.155713195034335), `KONE 'B' (~NK)` = c(0.0376163345061415,
0.0457195380715649), `UPM-KYMMENE (~NK)` = c(0.00115990210766144,
0.0609824011070531), `WARTSILA (~NK)` = c(0.0329251954092374,
0.0762362885269973), `METSO (~NK)` = c(-0.0276370997573922,
0.158750156652548), `STORA ENSO 'R' (~NK)` = c(-0.0058540569153482,
0.0970682931630558), `HUHTAMAKI (~NK)` = c(-0.00284777905703818,
0.0224783334681041), `FINNAIR (~NK)` = c(0.0442869319742045,
0.0525404295882317), `KEMIRA (~NK)` = c(-0.104316294022408,
0.0465316047015052), `UPONOR (~NK)` = c(0.0458579624296079,
0.0802649812687683), `KESKO 'B' (~NK)` = c(0.0217802707650036,
0.115006843859385), `ORION 'B' (~NK)` = c(0.0483993278432202,
0.0446430392461813), `OUTOKUMPU 'A' (~NK)` = c(0.0173239680759633,
0.0354718724242646), `RAISIO (~NK)` = c(0.244082260408811,
0.177805383079262), `TIETO OYJ (~NK)` = c(0.104391540656928,
0.0204030283840117), `METSA BOARD 'B' (~NK)` = c(0.0133698481269666,
0.179810871730302), `ORION 'A' (~NK)` = c(0.0223035782719256,
0.0332939199192042), `STOCKMANN 'A' (~NK)` = c(0.0155244469329926,
0.038305372243036), `STORA ENSO 'A' (~NK)` = c(-0.0200792186174187,
0.103131677314942)), .Names = c("DNB", "NORSK HYDRO", "ORKLA",
"STOREBRAND", "ATEA", "SCHIBSTED A", "BONHEUR", "EKORNES", "KONGSBERG GRUPPEN",
"TOMRA SYSTEMS", "VEIDEKKE", "ARENDALS FOSSEKOMPANI", "OLAV THON EIEP.",
"PETROLEUM GEO SERVICES", "SPAREBANK 1 SR BANK", "STOLT-NIELSEN",
"ODFJELL 'A'", "SPAREBANK 1 NORD-NORGE", "SPAREBANK 1 SMN", "WILHS.WILHELMSEN HDG.'A'",
"NORDEA BANK (~NK)", "ATLAS COPCO 'A' (~NK)", "VOLVO 'B' (~NK)",
"SANDVIK (~NK)", "SWEDBANK 'A' (~NK)", "ERICSSON 'B' (~NK)",
"SVENSKA HANDBKN.'A' (~NK)", "HENNES & MAURITZ 'B' (~NK)", "SEB 'A' (~NK)",
"INVESTOR 'B' (~NK)", "SWEDISH MATCH (~NK)", "ELECTROLUX 'B' (~NK)",
"SKANSKA 'B' (~NK)", "SCA 'B' (~NK)", "SECURITAS 'B' (~NK)",
"HOLMEN 'B' (~NK)", "SSAB 'A' (~NK)", "ERICSSON 'A' (~NK)", "INVESTOR 'A' (~NK)",
"VOLVO 'A' (~NK)", "NOVO NORDISK 'B' (~NK)", "DANSKE BANK (~NK)",
"COLOPLAST 'B' (~NK)", "CARLSBERG 'B' (~NK)", "A P MOLLER - MAERSK 'B' (~NK)",
"TDC (~NK)", "TOPDANMARK (~NK)", "WILLIAM DEMANT HLDG. (~NK)",
"JYSKE BANK (~NK)", "KOBENHAVNS LUFTHAVNE (~NK)", "NKT (~NK)",
"ROCKWOOL 'B' (~NK)", "SYDBANK (~NK)", "FLSMIDTH & CO.'B' (~NK)",
"GN STORE NORD (~NK)", "ALK-ABELLO (~NK)", "BANG & OLUFSEN 'B' (~NK)",
"SANTA FE GROUP (~NK)", "CARLSBERG 'A' (~NK)", "ROCKWOOL 'A' (~NK)",
"NOKIA (~NK)", "SAMPO 'A' (~NK)", "KONE 'B' (~NK)", "UPM-KYMMENE (~NK)",
"WARTSILA (~NK)", "METSO (~NK)", "STORA ENSO 'R' (~NK)", "HUHTAMAKI (~NK)",
"FINNAIR (~NK)", "KEMIRA (~NK)", "UPONOR (~NK)", "KESKO 'B' (~NK)",
"ORION 'B' (~NK)", "OUTOKUMPU 'A' (~NK)", "RAISIO (~NK)", "TIETO OYJ (~NK)",
"METSA BOARD 'B' (~NK)", "ORION 'A' (~NK)", "STOCKMANN 'A' (~NK)",
"STORA ENSO 'A' (~NK)"), row.names = 2:3, class = "data.frame")
#dput(head(Mkt.pct,2))
structure(list(DNB = c(0.0130911031863844, 0.0148436443461116
), `NORSK HYDRO` = c(0.0656202028062552, 0.060983838741199),
ORKLA = c(0.0157059383874119, 0.0161601293815974), STOREBRAND = c(0.00797050065007084,
0.00850323028627703), ATEA = c(0.00174625378791056, 0.00177198576286123
), `SCHIBSTED A` = c(0.00732288011104105, 0.00700863433828195
), BONHEUR = c(0.00252070586866636, 0.00251956609171883),
EKORNES = c(0.000946154462841077, 0.00130004244856961), `KONGSBERG GRUPPEN` = c(0.00118371896832389,
0.00127609728965736), `TOMRA SYSTEMS` = c(0.0035652668350741,
0.00372753152033644), VEIDEKKE = c(0.000989241833288067,
0.0010488933009586), `ARENDALS FOSSEKOMPANI` = c(0.00111913526141214,
0.00107655674335278), `OLAV THON EIEP.` = c(0.0016431202999304,
0.00173644571995502), `PETROLEUM GEO SERVICES` = c(0.00673959864366955,
0.00679696353383315), `SPAREBANK 1 SR BANK` = c(0.0011331504939971,
0.00121132435136855), `STOLT-NIELSEN` = c(0.00293774584412645,
0.00304923447363627), `ODFJELL 'A'` = c(0.00152003720160408,
0.00155990426043415), `SPAREBANK 1 NORD-NORGE` = c(0.000910122057445298,
0.000864717259377188), `SPAREBANK 1 SMN` = c(0.000942240298785819,
0.000924070451131194), `WILHS.WILHELMSEN HDG.'A'` = c(0.00137174878071225,
0.00135496596927216), `NORDEA BANK (~NK)` = c(0.0286748896677251,
0.0310448801949994), `ATLAS COPCO 'A' (~NK)` = c(0.0147325426053798,
0.014159480185436), `VOLVO 'B' (~NK)` = c(0.0380824093624216,
0.0385424504764243), `SANDVIK (~NK)` = c(0.0274705187491339,
0.0269020010085569), `SWEDBANK 'A' (~NK)` = c(0.0232605669980714,
0.0258531443770606), `ERICSSON 'B' (~NK)` = c(0.147959094491921,
0.144169856176502), `SVENSKA HANDBKN.'A' (~NK)` = c(0.0288306986497957,
0.0299494020090758), `HENNES & MAURITZ 'B' (~NK)` = c(0.0272859690705124,
0.0250578644115827), `SEB 'A' (~NK)` = c(0.0244360078250768,
0.0252140983228561), `INVESTOR 'B' (~NK)` = c(0.025860187464626,
0.0259389069155966), `SWEDISH MATCH (~NK)` = c(0.00857693566046275,
0.00806038785730358), `ELECTROLUX 'B' (~NK)` = c(0.0208092033865695,
0.0213555908169106), `SKANSKA 'B' (~NK)` = c(0.0252236623180593,
0.024897530854419), `SCA 'B' (~NK)` = c(0.0141970202526503,
0.0148227134170042), `SECURITAS 'B' (~NK)` = c(0.0106139108269937,
0.0097541503230873), `HOLMEN 'B' (~NK)` = c(0.00926214322246672,
0.0100609857162177), `SSAB 'A' (~NK)` = c(0.00824783022142955,
0.00769026830605565), `ERICSSON 'A' (~NK)` = c(0.0140974142471957,
0.0137714512687781), `INVESTOR 'A' (~NK)` = c(0.0179665890901973,
0.017910040128126), `VOLVO 'A' (~NK)` = c(0.0166912502737251,
0.0169517570686256), `NOVO NORDISK 'B' (~NK)` = c(0.0292245771907756,
0.0304804344949926), `DANSKE BANK (~NK)` = c(0.021474769105159,
0.0235529643446683), `COLOPLAST 'B' (~NK)` = c(0.00408057915928444,
0.00419512583639377), `CARLSBERG 'B' (~NK)` = c(0.00931366656342796,
0.00912251150027678), `A P MOLLER - MAERSK 'B' (~NK)` = c(0.0157048020172023,
0.0162280778953254), `TDC (~NK)` = c(0.0173613851475325,
0.0179019141752701), `TOPDANMARK (~NK)` = c(0.00241780123302007,
0.0024663587018541), `WILLIAM DEMANT HLDG. (~NK)` = c(0.00373297613850623,
0.00383763524797797), `JYSKE BANK (~NK)` = c(0.00345383153139609,
0.00376353359846821), `KOBENHAVNS LUFTHAVNE (~NK)` = c(0.00501413096084276,
0.00482603126995735), `NKT (~NK)` = c(0.00212372598398948,
0.00235050520251664), `ROCKWOOL 'B' (~NK)` = c(0.00223220988170647,
0.0020406775812588), `SYDBANK (~NK)` = c(0.00188848945914436,
0.00185196186023889), `FLSMIDTH & CO.'B' (~NK)` = c(0.00541980723420693,
0.00519344461488411), `GN STORE NORD (~NK)` = c(0.00306333842666562,
0.00311373605790372), `ALK-ABELLO (~NK)` = c(0.00301973811133235,
0.00305568096511678), `BANG & OLUFSEN 'B' (~NK)` = c(0.00285217085417642,
0.00331530075852983), `SANTA FE GROUP (~NK)` = c(0.00266960982342172,
0.00261259651769184), `CARLSBERG 'A' (~NK)` = c(0.0114906204436931,
0.0111184450035804), `ROCKWOOL 'A' (~NK)` = c(0.00319547302936972,
0.00297912246162981), `NOKIA (~NK)` = c(0.0669832551982801,
0.0630171384266337), `SAMPO 'A' (~NK)` = c(0.00611072821309801,
0.00666190990401108), `KONE 'B' (~NK)` = c(0.00281647778155156,
0.00275191113903857), `UPM-KYMMENE (~NK)` = c(0.0280138852128935,
0.0277911327959644), `WARTSILA (~NK)` = c(0.00383901368368869,
0.00386678013665095), `METSO (~NK)` = c(0.00667914217022736,
0.00730364749675182), `STORA ENSO 'R' (~NK)` = c(0.0043575851807918,
0.00448110363934146), `HUHTAMAKI (~NK)` = c(0.0039143613417524,
0.00373719025671851), `FINNAIR (~NK)` = c(0.00306533496599219,
0.00301550590216998), `KEMIRA (~NK)` = c(0.00745529091483776,
0.00729035848359746), `UPONOR (~NK)` = c(0.00257306570436523,
0.00260210171756828), `KESKO 'B' (~NK)` = c(0.00409924246168501,
0.00429142717840808), `ORION 'B' (~NK)` = c(0.00431466353100038,
0.00421128973539609), `OUTOKUMPU 'A' (~NK)` = c(0.0105198998665294,
0.0101745217055348), `RAISIO (~NK)` = c(0.00472191020486253,
0.00526236120241807), `TIETO OYJ (~NK)` = c(0.00388740411511377,
0.0037037990443058), `METSA BOARD 'B' (~NK)` = c(0.00380089004144887,
0.00424439492600249), `ORION 'A' (~NK)` = c(0.00491468278458397,
0.00474303360888234), `STOCKMANN 'A' (~NK)` = c(0.00250731406140473,
0.00243184540389359), `STORA ENSO 'A' (~NK)` = c(0.00443444010767518,
0.00458775310362995)), .Names = c("DNB", "NORSK HYDRO", "ORKLA",
"STOREBRAND", "ATEA", "SCHIBSTED A", "BONHEUR", "EKORNES", "KONGSBERG GRUPPEN",
"TOMRA SYSTEMS", "VEIDEKKE", "ARENDALS FOSSEKOMPANI", "OLAV THON EIEP.",
"PETROLEUM GEO SERVICES", "SPAREBANK 1 SR BANK", "STOLT-NIELSEN",
"ODFJELL 'A'", "SPAREBANK 1 NORD-NORGE", "SPAREBANK 1 SMN", "WILHS.WILHELMSEN HDG.'A'",
"NORDEA BANK (~NK)", "ATLAS COPCO 'A' (~NK)", "VOLVO 'B' (~NK)",
"SANDVIK (~NK)", "SWEDBANK 'A' (~NK)", "ERICSSON 'B' (~NK)",
"SVENSKA HANDBKN.'A' (~NK)", "HENNES & MAURITZ 'B' (~NK)", "SEB 'A' (~NK)",
"INVESTOR 'B' (~NK)", "SWEDISH MATCH (~NK)", "ELECTROLUX 'B' (~NK)",
"SKANSKA 'B' (~NK)", "SCA 'B' (~NK)", "SECURITAS 'B' (~NK)",
"HOLMEN 'B' (~NK)", "SSAB 'A' (~NK)", "ERICSSON 'A' (~NK)", "INVESTOR 'A' (~NK)",
"VOLVO 'A' (~NK)", "NOVO NORDISK 'B' (~NK)", "DANSKE BANK (~NK)",
"COLOPLAST 'B' (~NK)", "CARLSBERG 'B' (~NK)", "A P MOLLER - MAERSK 'B' (~NK)",
"TDC (~NK)", "TOPDANMARK (~NK)", "WILLIAM DEMANT HLDG. (~NK)",
"JYSKE BANK (~NK)", "KOBENHAVNS LUFTHAVNE (~NK)", "NKT (~NK)",
"ROCKWOOL 'B' (~NK)", "SYDBANK (~NK)", "FLSMIDTH & CO.'B' (~NK)",
"GN STORE NORD (~NK)", "ALK-ABELLO (~NK)", "BANG & OLUFSEN 'B' (~NK)",
"SANTA FE GROUP (~NK)", "CARLSBERG 'A' (~NK)", "ROCKWOOL 'A' (~NK)",
"NOKIA (~NK)", "SAMPO 'A' (~NK)", "KONE 'B' (~NK)", "UPM-KYMMENE (~NK)",
"WARTSILA (~NK)", "METSO (~NK)", "STORA ENSO 'R' (~NK)", "HUHTAMAKI (~NK)",
"FINNAIR (~NK)", "KEMIRA (~NK)", "UPONOR (~NK)", "KESKO 'B' (~NK)",
"ORION 'B' (~NK)", "OUTOKUMPU 'A' (~NK)", "RAISIO (~NK)", "TIETO OYJ (~NK)",
"METSA BOARD 'B' (~NK)", "ORION 'A' (~NK)", "STOCKMANN 'A' (~NK)",
"STORA ENSO 'A' (~NK)"), row.names = 1:2, class = "data.frame"
)