Solving a system equation with R

98 views Asked by At

This is the Markowitz's efficient portfolio problem, but the "original". I need to maximize the return of a portfolio subject to a level of risk.

I have a vector mu of returns:

mu
                   [,1]        [,2]         [,3]         [,4]         [,5]         [,6]
2020-11-11 0.0002720645 0.000436814 0.0001976725 7.367183e-05 0.0001061771 2.123921e-05
                   [,7]          [,8]         [,9]        [,10]        [,11]         [,12]
2020-11-11 0.0002674939 -7.217231e-05 7.246612e-05 0.0003106428 0.0002488269 -9.916666e-05
                  [,13]       [,14]        [,15]        [,16]        [,17]        [,18]
2020-11-11 0.0001324967 0.000121239 0.0001060435 0.0002293328 0.0001029351 8.083295e-05

A variance covariance matrix:

sigma
               [,1]          [,2]          [,3]          [,4]          [,5]          [,6]
 [1,]  5.592601e-05  1.867019e-07 -1.928308e-06  6.144346e-07 -2.773157e-07  1.659026e-05
 [2,]  1.867019e-07  2.484421e-06  3.387304e-07 -1.116725e-07  4.748165e-08  6.849967e-06
 [3,] -1.928308e-06  3.387304e-07  1.203656e-06 -5.667758e-08  1.085685e-07 -3.056249e-07
 [4,]  6.144346e-07 -1.116725e-07 -5.667758e-08  2.100015e-08 -6.407951e-09 -5.022572e-08
 [5,] -2.773157e-07  4.748165e-08  1.085685e-07 -6.407951e-09  2.156967e-07  3.545964e-09
 [6,]  1.659026e-05  6.849967e-06 -3.056249e-07 -5.022572e-08  3.545964e-09  5.226998e-05
 [7,]  1.801405e-05  8.490776e-06 -5.241004e-07  1.107834e-07  6.741364e-09  3.427730e-05
 [8,]  4.081106e-05  2.616975e-06 -2.783963e-06  6.162108e-07 -2.925430e-07  4.131267e-05
 [9,]  5.406343e-08 -9.440572e-09 -5.508222e-09  1.364631e-09 -5.884534e-10 -3.247901e-09
[10,]  1.436211e-05  6.552363e-06 -1.738885e-07 -1.002123e-07  1.232430e-08  3.661951e-05
[11,]  9.153688e-06  4.140858e-07 -4.691999e-07  1.450149e-07 -5.655290e-08  6.904077e-06
[12,]  2.503943e-05  9.261196e-07 -6.080288e-07  3.882300e-07 -8.190216e-08  1.727989e-05
[13,]  2.346539e-06 -2.721675e-07 -1.601445e-08  5.728516e-08 -4.386966e-09  2.463735e-07
[14,]  1.231017e-06 -2.161433e-07 -1.686978e-08  3.623457e-08  3.462555e-08 -1.252470e-07
[15,]  2.803784e-06 -4.366683e-07  1.349062e-07  1.065253e-07  1.319820e-08 -2.084133e-07
[16,]  5.000938e-06 -8.268924e-07  6.099295e-07  1.324316e-07  5.926589e-08 -9.965225e-07
[17,] -1.062064e-07  2.265318e-08  2.875487e-08 -3.366920e-09  1.776274e-09  4.806526e-09
[18,] -4.585421e-07  6.782657e-08  3.215220e-07 -1.258659e-08  2.188159e-08 -1.362058e-07
               [,7]          [,8]          [,9]         [,10]         [,11]         [,12]
 [1,]  1.801405e-05  4.081106e-05  5.406343e-08  1.436211e-05  9.153688e-06  2.503943e-05
 [2,]  8.490776e-06  2.616975e-06 -9.440572e-09  6.552363e-06  4.140858e-07  9.261196e-07
 [3,] -5.241004e-07 -2.783963e-06 -5.508222e-09 -1.738885e-07 -4.691999e-07 -6.080288e-07
 [4,]  1.107834e-07  6.162108e-07  1.364631e-09 -1.002123e-07  1.450149e-07  3.882300e-07
 [5,]  6.741364e-09 -2.925430e-07 -5.884534e-10  1.232430e-08 -5.655290e-08 -8.190216e-08
 [6,]  3.427730e-05  4.131267e-05 -3.247901e-09  3.661951e-05  6.904077e-06  1.727989e-05
 [7,]  5.237200e-05  3.680683e-05  5.354780e-09  2.968337e-05  7.161037e-06  1.813164e-05
 [8,]  3.680683e-05  1.662421e-04  5.065368e-08  7.083157e-05  1.028446e-05  2.650823e-05
 [9,]  5.354780e-09  5.065368e-08  1.520614e-10 -7.552573e-09  1.266719e-08  3.327868e-08
[10,]  2.968337e-05  7.083157e-05 -7.552573e-09  4.900492e-05  4.501299e-06  1.198971e-05
[11,]  7.161037e-06  1.028446e-05  1.266719e-08  4.501299e-06  1.493544e-05  2.091619e-05
[12,]  1.813164e-05  2.650823e-05  3.327868e-08  1.198971e-05  2.091619e-05  6.634911e-05
[13,]  5.699686e-07  1.961050e-06  4.834209e-09 -6.040182e-08  2.463262e-06  4.880533e-06
[14,]  1.583679e-07  1.116495e-06  3.072008e-09 -2.197953e-07  2.903155e-07  8.507265e-07
[15,]  7.753749e-07  2.291707e-06  4.353285e-09 -5.523707e-07  6.641738e-07  2.131070e-06
[16,]  2.247954e-07  3.167986e-06  1.075832e-08 -1.326120e-06  9.988603e-07  3.492155e-06
[17,] -9.016614e-09 -1.176373e-07 -3.044293e-10  1.502323e-08 -2.420553e-08 -4.921572e-08
[18,] -1.588521e-07 -7.068861e-07 -1.222680e-09 -1.076004e-07 -9.341448e-08 -9.692232e-08
              [,13]         [,14]         [,15]         [,16]         [,17]         [,18]
 [1,]  2.346539e-06  1.231017e-06  2.803784e-06  5.000938e-06 -1.062064e-07 -4.585421e-07
 [2,] -2.721675e-07 -2.161433e-07 -4.366683e-07 -8.268924e-07  2.265318e-08  6.782657e-08
 [3,] -1.601445e-08 -1.686978e-08  1.349062e-07  6.099295e-07  2.875487e-08  3.215220e-07
 [4,]  5.728516e-08  3.623457e-08  1.065253e-07  1.324316e-07 -3.366920e-09 -1.258659e-08
 [5,] -4.386966e-09  3.462555e-08  1.319820e-08  5.926589e-08  1.776274e-09  2.188159e-08
 [6,]  2.463735e-07 -1.252470e-07 -2.084133e-07 -9.965225e-07  4.806526e-09 -1.362058e-07
 [7,]  5.699686e-07  1.583679e-07  7.753749e-07  2.247954e-07 -9.016614e-09 -1.588521e-07
 [8,]  1.961050e-06  1.116495e-06  2.291707e-06  3.167986e-06 -1.176373e-07 -7.068861e-07
 [9,]  4.834209e-09  3.072008e-09  4.353285e-09  1.075832e-08 -3.044293e-10 -1.222680e-09
[10,] -6.040182e-08 -2.197953e-07 -5.523707e-07 -1.326120e-06  1.502323e-08 -1.076004e-07
[11,]  2.463262e-06  2.903155e-07  6.641738e-07  9.988603e-07 -2.420553e-08 -9.341448e-08
[12,]  4.880533e-06  8.507265e-07  2.131070e-06  3.492155e-06 -4.921572e-08 -9.692232e-08
[13,]  8.999334e-07  1.321428e-07  3.261184e-07  6.050874e-07 -6.496856e-09  9.309784e-09
[14,]  1.321428e-07  9.120545e-08  1.986952e-07  3.772898e-07 -4.786053e-09  7.777481e-09
[15,]  3.261184e-07  1.986952e-07  8.154712e-07  1.007473e-06 -5.816547e-09  6.094590e-08
[16,]  6.050874e-07  3.772898e-07  1.007473e-06  2.313540e-06 -2.842490e-09  2.360389e-07
[17,] -6.496856e-09 -4.786053e-09 -5.816547e-09 -2.842490e-09  1.682421e-09  8.910643e-09
[18,]  9.309784e-09  7.777481e-09  6.094590e-08  2.360389e-07  8.910643e-09  1.407667e-07

And I want to solve the following maximization problem:

Optimization problem

Where mu_p is the portfolio return (weighted average), sigma_p**2 is the variance of the portfolio, SIGMA is the varcov matrix, 1 is a vector of 1 and x are the weights

Plus a constraint that make all values of x greater or equal than cero

I really don't know how to do that in R, I have review some packages like ROI and CVXR, but I don't understand how to solve this.

0

There are 0 answers