# Error: "Your objective function is not a scalar"

Hello, I don’t know where I am wrong in the code, here stdev=(x’ * Sigma * x)^2, but because I don’t know how to calculate Sigma, so I use maximize(avg*x-Delta * stdev)

Sigma = covariance matrix for random vector r

avg = [1.03 1.04 1.01 1.09 1.06]
stdev = [0.05 0.08 0.01 0.13 0.065]
Delta = 0.2
cvx_begin quiet
variables x(5);
maximize(avg * x-Delta * stdev)
subject to
ones(1,5)*x == 1;
x >= 0;
x(1)>= 0.2;
x(2)+x(3)+x(5) <= 0.75;
x(4) <= x(1);
cvx_end
x

The question is to find x

I think you forgot to multiply `stdev` by `x`. That would make the objective function a scalar.

The resulting model seems mathematically “immature” (i.e., lousy) from a mathematical finance perspective, but I’ll leave it to your determination what model your school assignment is supposed to use (but don’t expect me as a portfolio optimization client).

Hello, I am not where multiply stdev, is maximize(avg * x-Delta * stdev*x?)

That is the simplest fix (but still “wrong”) to your “incorrect” way of computing portfolio standard deviation, which perhaps is supposed to be “Risk”?

You perhaps really want `delta*norm(stdev'.*x)`, using you stdev array, not the formula you show for stdev (which should have sqrt, not ^2), But it is for you to learn proper theory and calculation, which are separate from optimization knowledge. You ought to learn what Sigma (covariance matrix) is, then see how that produces an equivalent result to what I showed, given the assumed independence of the asset returns.

Edit: I inserted a missing `'.` in the argument of `norm` to make that argument a vector.
{"ft_lang_label":"__label__en","ft_lang_prob":0.93761986,"math_prob":0.9780045,"size":1005,"snap":"2023-14-2023-23","text_gpt3_token_len":216,"char_repetition_ratio":0.08391608,"word_repetition_ratio":0.0,"special_character_ratio":0.2119403,"punctuation_ratio":0.114583336,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.999358,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-03-31T01:05:49Z\",\"WARC-Record-ID\":\"<urn:uuid:c6d7e501-835a-4e20-88e6-87c70f0b2edb>\",\"Content-Length\":\"22054\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:3825b6dd-d75e-4153-a860-a163579d30f0>\",\"WARC-Concurrent-To\":\"<urn:uuid:fba42c48-cfc2-4c7d-92ed-9c5c0c85163f>\",\"WARC-IP-Address\":\"104.131.20.137\",\"WARC-Target-URI\":\"https://ask.cvxr.com/t/error-your-objective-function-is-not-a-scalar/9673\",\"WARC-Payload-Digest\":\"sha1:JNEOEWMA6YKWBW3HCBBGXZFTBU22BOYJ\",\"WARC-Block-Digest\":\"sha1:V6QS4MLO5PCMRSMOR52Z6C36OVSTZDI4\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-14/CC-MAIN-2023-14_segments_1679296949506.62_warc_CC-MAIN-20230330225648-20230331015648-00100.warc.gz\"}"}