# Problem with NMinimize [closed]

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]]]][]<=#<=cons[[Position[wtsV,#][[1,1]]]][]&/@wtsV))},wtsV],
MV=NMinimize[{pvar,Total[wtsV]==1&&pret==reqR&&(And@@(cons[[Position[wtsV,#][[1,1]]]][]<=#<=cons[[Position[wtsV,#][[1,1]]]][]&/@wtsV))},wtsV]]
];
Print[MV];
weights = wtsV /. MV[];
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? • the MV$10261 is indicating MV was never assigned in the module. Hard to say more without seeing the input. Oct 16 '15 at 20:00
• 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] , ..  Oct 16 '15 at 20:03
• @george2079 You are right, changing the condition to TrueQ[cons==0] solved the problem. However, I thought that the two were equivalent...? Oct 16 '15 at 20:08
• 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) Oct 16 '15 at 20:57
{"ft_lang_label":"__label__en","ft_lang_prob":0.68756133,"math_prob":0.9933308,"size":2448,"snap":"2021-31-2021-39","text_gpt3_token_len":774,"char_repetition_ratio":0.10065466,"word_repetition_ratio":0.0073260074,"special_character_ratio":0.29370916,"punctuation_ratio":0.19215687,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99720174,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-09-17T13:24:38Z\",\"WARC-Record-ID\":\"<urn:uuid:6640f8a7-7ee7-4819-9bbe-13c1f222b980>\",\"Content-Length\":\"144085\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:b92cd185-bbaa-4fb6-91c9-113d9c808edd>\",\"WARC-Concurrent-To\":\"<urn:uuid:a2383f96-c384-4cab-a9c8-da49ead2efb8>\",\"WARC-IP-Address\":\"151.101.1.69\",\"WARC-Target-URI\":\"https://mathematica.stackexchange.com/questions/97187/problem-with-nminimize\",\"WARC-Payload-Digest\":\"sha1:CNHEL3WM7BQJQ5PSQ3S3UWDJH5S6V6GM\",\"WARC-Block-Digest\":\"sha1:F7FLXX5ES7U4JVG6PYSRLIPHY447ADXW\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-39/CC-MAIN-2021-39_segments_1631780055645.75_warc_CC-MAIN-20210917120628-20210917150628-00157.warc.gz\"}"}