简体   繁体   中英

Portfolio optimization

I am trying to build a portfolio which is optimized with respect to another in R.

I am trying to minimize the objective function

$$min Var(return_p-return'weight_{bm})$$

with the constraints

$$ 1_n'w = 1$$

$$w > .005$$

$$w < .8$$

with w being the returns from a portfolio. there are 10 securities, so I set the benchmark weights at .1 each. I know that

$$ Var(return_p-return'weight_{bm})= var(r) + var(r'w_{bm}) - 2*cov(r_p, r'w_{bm})=var(r'w)-2cov(r'w,r'w_{bm})=w'var(r)w-2cov(r'w,r'w_{bm})$$

$$=w'var(r)w-2cov(r',r'w_bm)w$$

the last term is of the form I need so I tried to solve this with solve.QP in R, the constraints are giving me a problem though.

here is my code

trackport <- array(rnorm(obs * assets, mean = .2, sd = .15), dim = c(obs,     
assets)) #this is the portfolio which the assets are tracked against
wbm <- matrix(rep(1/assets, assets)) #random numbers for the weights
Aeq <- t(matrix(rep(1,assets), nrow=assets, ncol = 1)) #col of 1's to add    
                                                       #the weights
Beq <- 1 # weights should sum to 1's
H = 2*cov(trackport) #times 2 because of the syntax

#multiplies the returns times coefficients to create a vector of returns for     
#the benchmark
rbm = trackport %*% wbm 

#covariance between the tracking portfolio and benchmark returns
eff <- cov(trackport, rbm)

#constraints
Amatrix <- t(matrix(c(Aeq, diag(assets), -diag(assets)), ncol = assets,     
byrow = T))
Bvector <- matrix(c(1,rep(.005, assets), rep(.8, assets)))

#solve
solQP3 <- solve.QP(Dmat = H,
                   dvec = zeros, #reduces to min var portfolio for  
                                 #troubleshooting purposes
                   Amat = Amatrix,
                   bvec = Bvector,
                   meq = 1)

the error I am getting is "constraints are inconsistent, no solution!" but I can't find what's wrong with my A matrix

My (transposed) A matrix looks like this

[1,1,...,1]
[1,0,...,0]
[0,1,...,0]
...
[0,0,...,1]
[-1,0,...,0]
[0,-1,...,0]
...
[0,0,...,-1]

and my $b_0$ looks like this

[1]
[.005]
[.005]
...
[.005]
[.8]
[.8]
...
[.8]

so I'm not sure why it isn't finding a solution, could anyone take a look?

I'm not familiar with the package, but just took a quick look at https://cran.r-project.org/web/packages/quadprog/quadprog.pdf , which apparently is what you are using.

Your RHS values of .8 should be -0.8 because this function uses ≥ inequalities. So you have been constraining the variables to be ≥ .005 and ≤ -0.8, which of course is not what you want, and is infeasible.

So leave transposed A as is and make

 b0:
    [1]
    [.005]
    [.005]
    ...
    [.005]
    [-.8]
    [-.8]
    ...
    [-.8]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM