Home > OS >  Save multiple vectors, calculated by an equation, in a list
Save multiple vectors, calculated by an equation, in a list

Time:06-11

I would appreciate help with how to save multiple vectors in a list, where the vectors are obtained by the help of an equation in R.

Using the fact that portfolios from the minimum variance line satisfy w = s*w_min (1−s)*w_m I want to compute optimal portfolios for s =[-2,2] with step-size 0.1. I have already computed w_min (minimum variance portfolio) and w_m (market portfolio). And I have found that one can print out all the optimal portfolios (the vectors) using the following:

for(s in seq(-2, 2, by=0.1)){
  my_out <- (s*w_min) (1-s)*w_m
  print(my_out)}

And this gives me outputs in the following correct form (though more than what is presented below):

##             BBD        OXY        NOK      AAPL    MSFT
## [1,] -0.4111434 -0.1284589 -0.1824016 0.4634135 1.25859
##             BBD        OXY        NOK      AAPL    MSFT
## [1,] -0.3953277 -0.1217342 -0.1734786 0.4559005 1.23464

And now I want to save these results in a list in R so that I can use these different optimal portfolios for future calculations, this is my attempt of doing this:

my_vec <- list()                 # Create empty list

for(s in seq(-2, 2, by=0.1)) {   # Head of for-loop
  my_out <- (s*w_min) (1-s)*w_m  # Create some output
  my_vec <- c(my_vec, my_out)    # Save output in vector
}

But this does not give the result I am looking for. Instead of giving me the first portfolio when calling my_vec[1] I am given ## [1] -0.4111434 which is the first value in the first portfolio. What is it that prevents each portfolio from being saved as a vector?

Edit: Here is w_min and w_m as requested

w_min
##             BBD        OXY        NOK      AAPL      MSFT
## [1,] 0.06332585 0.07328322 0.08528878 0.2380229 0.5400793
w_m
##              BBD         OXY          NOK      AAPL     MSFT
## [1,] -0.09483055 0.006035851 -0.003941358 0.3131531 0.779583

CodePudding user response:

We may change the code by initializing the 'my_vec' as a list with length equal to the length of the sequence, loop over the sequence, and assign the output to the corresponding list element with the index

s1 <- seq(-2, 2, by = 0.1)
my_vec <- vector('list', length(s1))
for(i in seq_along(s1)) {
    my_vec[[i]] <- (s1[i]*w_min) (1-s1[i])*w_m
}

-output

> head(my_vec)
[[1]]
            BBD      OXY      NOK    AAPL     MSFT
[1,] -0.4086517 -0.12831 -0.18238 -0.3821 1.256842

[[2]]
            BBD       OXY       NOK     AAPL    MSFT
[1,] -0.3929191 -0.121593 -0.173458 -0.36143 1.23295

[[3]]
            BBD       OXY       NOK     AAPL     MSFT
[1,] -0.3771865 -0.114876 -0.164536 -0.34076 1.209058

[[4]]
            BBD       OXY       NOK     AAPL     MSFT
[1,] -0.3614539 -0.108159 -0.155614 -0.32009 1.185166

[[5]]
            BBD       OXY       NOK     AAPL     MSFT
[1,] -0.3457213 -0.101442 -0.146692 -0.29942 1.161274

[[6]]
            BBD       OXY      NOK     AAPL     MSFT
[1,] -0.3299887 -0.094725 -0.13777 -0.27875 1.137382

CodePudding user response:

You can also use crossprod:

t(crossprod(rbind(w_min, w_m), rbind(s, 1-s)))

              BBD           OXY          NOK      AAPL      MSFT
 [1,] -0.41114335 -0.1284588870 -0.182401634 0.4634135 1.2585904
 [2,] -0.39532771 -0.1217341501 -0.173478620 0.4559005 1.2346400
 [3,] -0.37951207 -0.1150094132 -0.164555606 0.4483875 1.2106897
 [4,] -0.36369643 -0.1082846763 -0.155632593 0.4408744 1.1867393
 [5,] -0.34788079 -0.1015599394 -0.146709579 0.4333614 1.1627889
 [6,] -0.33206515 -0.0948352025 -0.137786565 0.4258484 1.1388386

Note that the results here matches the output printed by your for-loop

  • Related