Thursday, March 24, 2016

r - Portfolio Optimization with equal weight for assets selected


I have a data frame of bets, with 1 being a win and 0 being a loss. These bets are correlated so I cannot just pick the highest winning percentage. Goal is to get 2 optimizations, 1 for max sharpe ration, and 1 for minMAD for a given return. This is similar to any portfolio questions, but with the unique constraint that each bet can only be placed once and some bets are correlated with one another


> df
X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
A 0 1 0 1 0 0 0 1 1 0
B 0 1 0 0 1 1 1 1 1 1

C 1 0 0 0 0 0 0 1 1 0
D 1 0 1 1 1 0 0 1 0 1
E 0 0 1 0 1 0 1 1 1 0
F 0 1 0 1 0 1 1 0 1 1
G 1 0 0 0 0 1 1 1 1 0
H 0 0 1 1 0 0 1 1 0 1
I 1 0 0 0 0 1 1 1 1 0
J 0 1 1 0 0 1 0 1 0 0

Bets can either be included or excluded, but each bet can only be placed 1 time. I posted a questions over the weekend Optimization with Binary decision variable and simulated database thinking an equal weighted quadratic optimization could solve the problem but I could not figure out how to program binary variables. After doing so research today, I saw an example of a GLPK program that used mean absolute deviation and transformed it for a LP.



I am trying to tweak this for a binary variable, but have no experience with optimizations. What are some good resource to learn how to program the matrix. Here is the goal.


Minimize - Mean Absolute Deviation


S.T.


Number of bets = 4


Bets are a binary variable


Winning percentage >= 0.52


And


Maximize winning percentage / MAD


ST


Weights are equal (1/n) or 0.25, and sum to 1



Can someone help point me in the right direction. I am new to R and never set up a LP before. Some resource to help learn, and advice on set up will be much appreciated. I spend this weekend trying to figure it out spinning circles. Also I plan on playing around with the constraints and objective function once I get a base model going so please pass along good resources on R optimizations so I can learn. S.O. has been a great resource helping me learn R, right now I am using excel as a stop gap but the read data set is too big for the standard excel solve so I have to make several adjustments to get any usable results.


Thanks for all of your help.


So this is what I have so far, but the output is not correct.


> nAssets = nrow(df)
> nScenarios = ncol(df)
> Mean = rowMeans(df)
> data = as.matrix(df)
> sucessRate = .52
> vec <- c(weights=rep(0, nAssets), scenarios=rep(1/nScenarios, nScenarios))
> mat <- rbind(

+ MAD.LE = cbind(data, -diag(nScenarios)),
+ MAD.GE = cbind(data, +diag(nScenarios)),
+ RETURN = t(c(Mean, rep(0, nScenarios))),
+ BUDGET = t(c(rep(1, nAssets), rep(0, nScenarios))),
+ X = cbind(matrix(rep(0, nAssets*nScenarios),ncol=nAssets), diag(nScenarios)),
+ WEIGHTS = cbind(diag(nAssets), matrix(rep(0, nScenarios*nAssets), nrow=nAssets)))
> mat <- rbind(
+ MAD.LE = cbind(data, -diag(nScenarios)),
+ MAD.GE = cbind(data, +diag(nScenarios)),
+ RETURN = t(c(Mean, rep(0, nScenarios))),

+ BUDGET = t(c(rep(1, nAssets), rep(0, nScenarios))),
+ X = cbind(matrix(rep(0, nAssets*nScenarios),ncol=nAssets), diag(nScenarios)),
+ WEIGHTS = cbind(diag(nAssets), matrix(rep(0, nScenarios*nAssets), nrow=nAssets)))
> rhs <- c(
+ MAD.LE = rep(0, nScenarios),
+ MAD.GE = rep(0, nScenarios),
+ RETURN = sucessRate,
+ BUDGET = 1,
+ X = rep(0, nScenarios),
+ WEIGHTS = rep(0, nAssets))

> dir <- c(
+ MAD.LE = rep("<=", nScenarios),
+ MAS.GE = rep(">=", nScenarios),
+ RETURN = "==",
+ BUDGET = "==",
+ X = rep(">=", nScenarios),
+ WEIGHTS = rep(">=", nAssets))
> Rglpk::Rglpk_solve_LP(vec, mat, dir, rhs)
$optimum
[1] 0.31


$solution
[1] 0.000000e+00 1.000000e-01 0.000000e+00 0.000000e+00 9.000000e-01 0.000000e+00 0.000000e+00
[8] 0.000000e+00 0.000000e+00 9.860761e-32 1.000000e-01 1.000000e+00 0.000000e+00 9.000000e-01
[15] 9.000000e-01 1.000000e-01 0.000000e+00 9.860761e-32 0.000000e+00 1.000000e-01

$status
[1] 0

What mistake did I make that is yielding more than 10 weights, and how do I get the weights equal and only 4 bets selected. Here is MatLab code for almost exactly what I am looking for, can someone help translate what is missing to R.



http://www.mathworks.com/help/optim/examples/mixed-integer-quadratic-programming-portfolio-optimization.html




No comments:

Post a Comment

technique - How credible is wikipedia?

I understand that this question relates more to wikipedia than it does writing but... If I was going to use wikipedia for a source for a res...