1
$\begingroup$

I wrote a code inspired by Rod's answer to this question. It is supposed to find the minimum variance portfolio of stocks and allows for arbitrary constraints on the weights of each stock:

optP[expR_,sd_,corr_,reqR_:0,cons_:0]:=Module[{pvar,wtsV,MV,pret,weights},
wtsV=Subscript[w,#]&/@Range[Length[expR]];
pret:=Sum[wtsV[[i]]*expR[[i]],{i,1,Length[expR]}];
pvar:=Sum[wtsV[[i]]*wtsV[[j]]*sd[[i]]*sd[[j]]*corr[[i]][[j]],{i,1,Length[expR]},{j,1,Length[expR]}];
If[cons==0,
    If[reqR==0,
    MV=NMinimize[{pvar,Total[wtsV]==1},wtsV],
    MV=NMinimize[{pvar,Total[wtsV]==1&&pret==reqR},wtsV]],
    If[reqR==0,
    MV=NMinimize[{pvar,Total[wtsV]==1&&(And@@(cons[[Position[wtsV,#][[1,1]]]][[1]]<=#<=cons[[Position[wtsV,#][[1,1]]]][[2]]&/@wtsV))},wtsV],
    MV=NMinimize[{pvar,Total[wtsV]==1&&pret==reqR&&(And@@(cons[[Position[wtsV,#][[1,1]]]][[1]]<=#<=cons[[Position[wtsV,#][[1,1]]]][[2]]&/@wtsV))},wtsV]]
];
Print[MV];
weights = wtsV /. MV[[2]];
Print["Expected return is"];
Print[Sum[weights[[i]]*expR[[i]], {i, 1, Length[weights]}]];
Print["Expected standard deviation is"];
Print[Sqrt[First[MV]]];
]

expR is a list containing expected returns, sd a list containing expected standard deviations, corr is the correlation matrix,reqR the required return and cons the constraints on the weights of individual stocks. wtsV is then the vector containing weights of each stock and pret and pvar are the portfolio return and variance, respectively. Now, if the weights are not restricted, i.e. one can short any stock(s), everything works well. But if I use constraints on the weights, the code returns unevaluated, i.e. only e.g. MV$10261 appears (for the Print[MV] line of code) along with the corresponding unevaluated expected return and standard deviation. If I run the code step by step outside the optP function, it seems to work. Any ideas on what the problem might be?

$\endgroup$

closed as off-topic by Dr. belisarius, MarcoB, m_goldberg, ilian, Sjoerd C. de Vries Oct 17 '15 at 11:37

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Dr. belisarius, MarcoB, m_goldberg, ilian, Sjoerd C. de Vries
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ the MV$10261 is indicating MV was never assigned in the module. Hard to say more without seeing the input. $\endgroup$ – george2079 Oct 16 '15 at 20:00
  • $\begingroup$ note if cons is a list then cons==0 is neither true nor false and neither expression inside the If block is evaluated. You might want that to be If[TrueQ[cons==0] , .. $\endgroup$ – george2079 Oct 16 '15 at 20:03
  • $\begingroup$ @george2079 You are right, changing the condition to TrueQ[cons==0] solved the problem. However, I thought that the two were equivalent...? $\endgroup$ – Skumin Oct 16 '15 at 20:08
  • $\begingroup$ Equal can return True or False or remain unevaluated. If you do for example {0}==0 it remains unevaluated. Wrapping in TrueQ forces any undetermined case to be False (more like you'd expect in a traditional language) $\endgroup$ – george2079 Oct 16 '15 at 20:57