0
$\begingroup$

I have to calculate the basic statistics of a stock portfolio: annualized yield, annualized volatility ...

Here is an extract of the share prices:

> head(df)
        Date  .SXQR  .SXTR  .SXNR  .SXMR  .SXAR  .SX3R  .SX6R  .SXFR  .SXOR  .SXDR
1 2000-01-03 364.94 223.93 489.04 586.38 306.56 246.81 385.36 403.82 283.78 455.39
2 2000-01-04 345.04 218.90 474.05 566.15 301.13 239.24 374.64 390.41 275.93 434.92
3 2000-01-05 338.22 215.88 464.20 542.29 298.22 239.55 373.26 383.48 272.54 430.05
4 2000-01-06 343.13 218.18 470.82 529.33 300.69 249.75 377.26 383.48 272.47 434.15
5 2000-01-07 349.46 220.10 478.87 531.65 306.50 255.17 381.19 390.23 273.76 447.02
6 2000-01-10 356.20 223.01 484.07 581.82 310.84 252.75 387.74 393.75 278.76 453.80
   .SX4R  .SXRR  .SXER   .SXKR  .SX7R   .SX8R  .SXIR  .SXPR
1 427.43 498.08 457.57 1016.39 489.65 1070.72 466.36 368.62
2 414.10 476.17 435.72  971.51 471.23 1015.13 450.38 365.89
3 406.33 466.19 436.23  924.57 464.75  949.91 446.67 363.78
4 417.91 464.59 438.26  887.88 461.62  935.48 448.10 370.22
5 428.54 474.40 445.40  918.33 465.41  970.17 456.69 376.62
6 431.81 473.14 440.15  944.22 467.89 1002.93 460.26 373.81

Here is the data file.

I have the portfolio weights with:

w = optimalPortfolio(Sigma = Sigma,control = list(type = 'erc', constraint = 'lo'))

And the annual returns of the shares:

yearly_return <- df %>% 
  group_by(gr = floor_date(Date, unit = "year")) %>%
  summarize_at(vars(-Date, -gr), percent_change2) %>%
  ungroup()

# Generamos el xts, indicando la columna con la info de tiempo # ISSUE : there is nas
yearly_return <- xts(yearly_return[,-1], order.by=yearly_return$gr)

So How to calculate the annual returns of a portfolio with the given coefficients?

I tried:

yearly_return_erc <- yearly_return*w
Error in `*.default`(yearly_return, w) : non-conformable arrays

My data

Here is the yearly_returnof the stock:

> yearly_return
                 .SXQR       .SXTR        .SXNR        .SXMR       .SXAR       .SX3R
2000-01-01 -0.04211651 -0.01692493 -0.134079012 -0.110542652 -0.13658011  0.27697419
2001-01-01 -0.06170831 -0.06020640 -0.270292443 -0.244983557  0.03086915 -0.01268035
2002-01-01 -0.13181676 -0.31916713 -0.370798530 -0.450515898 -0.28119776 -0.11107462
2003-01-01  0.12010929  0.23596541  0.195487179  0.091163773  0.18582579 -0.02995400
2004-01-01  0.09574009  0.07114804  0.093577436  0.079726746  0.02328106  0.06988229
2005-01-01  0.32241764  0.24503874  0.346011845  0.137005650  0.20891419  0.26745187
2006-01-01  0.19390175  0.25618254  0.255671803  0.095515590  0.25897784  0.17402985
2007-01-01  0.07151176 -0.15668573  0.099268407 -0.043097704  0.24907118  0.12253799
2008-01-01 -0.39909398 -0.43417808 -0.454896057 -0.374645801 -0.42310256 -0.28321255
2009-01-01  0.37555060  0.15490882  0.376812650  0.197982252  0.15445790  0.33306810
2010-01-01  0.29645850  0.26603074  0.331675002  0.157542404  0.43139233  0.21087586
2011-01-01  0.02689063 -0.13311326 -0.152939580 -0.082821116 -0.25170969  0.07262980
2012-01-01  0.23297708  0.33254856  0.236761181  0.200574474  0.34626234  0.20785336
2013-01-01  0.15758295  0.28226061  0.224889270  0.352383599  0.36813147  0.09852600
2014-01-01  0.12771485  0.20728740  0.007498316  0.106319669  0.07123982  0.14959034
2015-01-01  0.23221073  0.22357015  0.082448980  0.156892993  0.15695521  0.20384265
2016-01-01  0.07382496 -0.07403532  0.141806854 -0.027957980  0.02966412 -0.01935074
2017-01-01  0.11678507  0.15511923  0.162564962 -0.003737683  0.14260345  0.12560078
                  .SX6R       .SXFR        .SXOR       .SXDR       .SX4R
2000-01-01  0.104681337  0.15999703 -0.043590105  0.26370803  0.05147042
2001-01-01 -0.071406543 -0.15554701  0.019947448 -0.07791252 -0.13987785
2002-01-01 -0.230112301 -0.34337396 -0.289506263 -0.28954601 -0.21697798
2003-01-01  0.107872960  0.13056199  0.244542568  0.08783802  0.04559812
2004-01-01  0.281793979  0.23979186  0.242456556  0.02901212  0.15874338
2005-01-01  0.292232260  0.33659939  0.326988422  0.29211304  0.31711918
2006-01-01  0.393387562  0.47290317  0.378672907  0.04249343  0.23364378
2007-01-01  0.200444605 -0.11887693 -0.008781149 -0.09816185  0.26299600
2008-01-01 -0.355704066 -0.53960190 -0.449588567 -0.16008695 -0.36264681
2009-01-01  0.041725299  0.29868730  0.372961769  0.16775463  0.45728827
2010-01-01 -0.047881523  0.16608250  0.027759902  0.08104497  0.23118616
2011-01-01 -0.127988837 -0.19952875 -0.188775294  0.14888869 -0.08247720
2012-01-01  0.023502640  0.24994212  0.176444777  0.15020270  0.30207712
2013-01-01  0.111748340  0.36818622  0.221097462  0.22388398  0.15067803
2014-01-01  0.197105509  0.12921216  0.076237379  0.21724504  0.07132732
2015-01-01  0.006913529  0.21694561  0.190190108  0.17151173  0.07280142
2016-01-01 -0.027294030 -0.01439982  0.141503689 -0.06332644  0.09755300
2017-01-01  0.088810599  0.19514217  0.099277978  0.03878557  0.12007593
                  .SXRR        .SXER        .SXKR        .SX7R       .SX8R
2000-01-01 -0.225365403  0.043774723 -0.361583644  0.128316144 -0.17028728
2001-01-01 -0.009904841 -0.009732258 -0.283345384 -0.060287204 -0.39441865
2002-01-01 -0.291441728 -0.150425114 -0.365582268 -0.236771176 -0.55863189
2003-01-01  0.088919239  0.022473035  0.151242142  0.210549933  0.26432695
2004-01-01  0.125110885  0.138461912  0.121153901  0.121524454 -0.02500651
2005-01-01  0.106820548  0.343225228  0.003708784  0.243734200  0.22048081
2006-01-01  0.273049830  0.063976954  0.216590444  0.220488172  0.03986477
2007-01-01 -0.007858586  0.093594463  0.172031349 -0.154626065 -0.01157508
2008-01-01 -0.412050836 -0.374351734 -0.327785050 -0.626580865 -0.47660446
2009-01-01  0.323031727  0.243966979  0.152915965  0.471351371  0.20212243
2010-01-01  0.120842042  0.013476388  0.078833242 -0.115211802  0.16435588
2011-01-01 -0.049966570  0.039481850 -0.009725331 -0.310751184 -0.12290388
2012-01-01  0.117352173 -0.013181300 -0.061099921  0.258543547  0.22139256
2013-01-01  0.208803171  0.056021733  0.351386970  0.187817561  0.24531130
2014-01-01 -0.011008923 -0.114184021  0.127413678  0.008276349  0.09512400
2015-01-01  0.106651835 -0.037800910  0.129595315 -0.008791811  0.15368136
2016-01-01 -0.018977205  0.306591774 -0.099367626 -0.002784941  0.08031547
2017-01-01 -0.013230032  0.010090399 -0.003590741  0.111173975  0.19937089
                 .SXIR       .SXPR
2000-01-01  0.21292564 -0.18061961
2001-01-01 -0.28494845  0.18713470
2002-01-01 -0.49789995 -0.17531545
2003-01-01  0.07890223  0.19142430
2004-01-01  0.09043759  0.12806068
2005-01-01  0.32582934  0.52502544
2006-01-01  0.19226424  0.38868295
2007-01-01 -0.10726150  0.28007157
2008-01-01 -0.43832446 -0.63296576
2009-01-01  0.15149087  0.88588641
2010-01-01  0.03285816  0.25505589
2011-01-01 -0.11482104 -0.28741280
2012-01-01  0.36427523  0.06243915
2013-01-01  0.30237368 -0.14556775
2014-01-01  0.14390280 -0.02503591
2015-01-01  0.18403687 -0.31307481
2016-01-01  0.01939247  0.71674991
2017-01-01  0.10445053  0.22769644

And the weights:

> w
           [,1]       [,2]       [,3]       [,4]       [,5]       [,6]       [,7]
[1,] 0.04591712 0.04078667 0.04126135 0.05131896 0.04349168 0.04834431 0.04694083
           [,8]       [,9]      [,10]      [,11]      [,12]      [,13]      [,14]
[1,] 0.03904389 0.04117694 0.04537461 0.04692524 0.04045692 0.04696848 0.05087293
         [,15]      [,16]      [,17]     [,18]
[1,] 0.1713231 0.08499888 0.04396601 0.0708321
$\endgroup$
  • $\begingroup$ Try transposing t() one of the arguments to make them conformable. Or maybe one or both of the arguments are not what you think they are? $\endgroup$ – Richard Hardy Mar 30 at 12:27
  • $\begingroup$ @RichardHardy I tried to transpose one of the two but it gave me back >yearly_return_erc <- yearly_return*t(w) Error in *.default(yearly_return, t(w)) : non-conformable arrays I'm going to add them in the question $\endgroup$ – ThePassenger Mar 30 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.