How to use the optimization toolboox for a value at risk based problem?

Hi,
I am currently trying to optimize a portfolio based on the risk measure value at risk (VaR) with the optimization toolbox. I know that the conditional value at risk does have better mathematic properties and so on, but I still need the VaR optimization (optimal asset allocation) for comparison.
So far I tried the following code for my 29 analyzed assets:
m=mean(Returns)
cv=cov(Returns)
m1 = [m.Adidas, m.Allianz, ...]
ValueatRiskE = 0 * ones(1,29);
for i=1:29
ValueatRiskE(i) = quantile(tick2ret(DAX30(:,i)),0.05)
end
pwgt2 = 1/29 * ones(1,29);
Until this point I have the mean of my 29 assets m1 and a vector ValueatRiskE (29*1) that contains each individual VaR per asset. Additionally I have a naive diversification vector pwgt2 = [1/29 1/29 .. 1/29] that could be the starting allocation for my optimization problem.
Now I want to optimize the following problem:
max Value = (ExpectedReturn-r0)/ValueatRiskPortfolio
where
ExpectedReturn = sum(pwgt2.*m1)
ValueatRiskPortfolio = [sqrt(pwgt2*cv*pwgt2')]*1.96
r0=0.00002
Can I now use the following optimization function to get the optimal allocation pwgt2*?
function s = sharp(pwgt2,cv,m1)
s = {sum(pwgt2.*m1)-r0]/sqrt(pwgt2*cv*pwgt2');
end
I tried to enter the objective function "@s", the start point "pwgt2" and the constraints sum(pwgt2)=1 in the optimization toolbox, but it doesnt work. How can I solve this problem?
Thank you in advance for your help! :)
Greets, phanta

 Accepted Answer

You probably just didn't pass your extra parameters properly. I assume that you have cv and m1 in your workspace, and you want to optimize over the parameters pwgt2. If so, you should set your objective function to
fun = @(pwgt2)sharp(pgwt2,cv,m1);
and then call
[x,fval,exitflag] = fmincon(fun,x0,...)
Alan Weiss
MATLAB mathematical toolbox documentation

11 Comments

Hey, thanks for your fast reply!
Its correct, I have cv, m1 and pwgt2 in my workspace. Then I plugged in your suggested objective function. But how can I start the optimization and plug in the optimization problem? Where should I place it?
s = {sum(pwgt2.*m1)-r0]/sqrt(pwgt2*cv*pwgt2');
You have a '{' where you want a '['.
I would also be wanting to check the sizes of the variables: it looks to me as if possibly the denominator is an array there, in which case I would want to know if you really want matrix division, mrdivide ? If you do then it is best to comment it. Possibly you want element-by-element division, in which case use ./ instead of /
Ah okay, so I have the following (the division works if I try it with an example pwgt2-weight):
function s = sharp(pwgt2,cv,m1)
s = [sum(pwgt2.*m1)-r0]/sqrt(pwgt2*cv*pwgt2');
end
fun = @(pwgt2)sharp(pwgt2,cv,m1);
[x,fval,exitflag] = fmincon(fun,pwgt2)
Then the solver runs into that problem:
Error in @(pwgt2)sharp(pwgt2,cv,m1)
Error in fmincon (line 534)
initVals.f = feval(funfcn{3},X,varargin{:});
You need to ensure that your objective function sharp.m evaluates without error before you throw it into an optimization. Whatever your initial value for pwgt2 is (call this initial value x0), you need to have
sharp(x0,cv,m1)
evaluate to a finite real scalar value.
Alan Weiss
MATLAB mathematical toolbox documentation
Your code works in the case where the first input argument is a row vector. You should check whether you are getting passed a row vector or a column vector. I suspect you will find you are being passed a column vector. It is safer to use column vectors, in that a lot of the optimization routines use column vectors for the points.
Hey guys,
thanks for your suggestions!
Now I have the following code with column vector x0:
x0 =
0.0345
0.0345
0.0345
0.0345
0.0345
0.0345
0.0345
0.0345
0.0345
0.0345
0.0345
0.0345
0.0345
0.0345
0.0345
0.0345
0.0345
0.0345
0.0345
0.0345
0.0345
0.0345
0.0345
0.0345
0.0345
0.0345
0.0345
0.0345
0.0345
Objective function sharp.m including the following code:
function s = sharp(x0,cv,m1)
s = ((m1*x0)-r0)/((sqrt(x0'*cv*x0))*1.96);
end
I understood that I have to enter only the following code into the command window to get an optimization result:
fun = @(x0)sharp(x0,cv,m1);
[x,fval,exitflag] = fmincon(fun,x0)
Or am I wrong? I guess you have to explain it for a total newbie :-/
What about your constraints? If you have no constraints, use fminunc. If you have constraints, such as variables that must be positive or less than one, use fmincon. See the function reference pages for details.
Alan Weiss
MATLAB mathematical toolbox documentation
Dear Alan,
I found a mistake in my function with the brackets..now I can run the function sharp with different values for x0, e.g.:
sharp(x0,cv,m1)
ans =
0.0861
Thats the first good thing. If I then run the optimization with your suggested function fminuic, I am glad to get my first optimization results.
Optimization running.
Objective function value: -0.22593494430585187
Solver stopped prematurely.
fminunc stopped because it exceeded the function evaluation limit,
options.MaxFunEvals = 2900 (the default value).
Now I want to use two constraints:
1. Sum of Xi = 1
2. X_i greater/equal than 1
For the first equation I tried this:
Aeq = ones(1,29);
beq = 1
Question 1: Is this correct?
The second equation is stated in the help as followed: A*x ≤ b. So my suggestion would be:
A = - ones(1,29)';
b = 0
Question 2: Can I do it like this?
Question 3: Now I am minimizing the function, but i I want to maximize it..should I then only include a minus sign in front of the function?
function s = sharp(x0,cv,m1)
r0 = 0.00002;
s = - 1.96*((m1*x0)-r0)/(sqrt(x0'*cv*x0));
end
Question 4: If the optimization is done I just have to enter "x" at the command window to get my optimal allocation weights, right?
Thanks again for your help!
Greets, phanta
  1. Yes, setting Aeq as a row vector of 1 and beq = 1 ensures that the sum of the x(i) is equal to 1.
  2. No, the way to ensure that all the x(i) are nonnegative is to include a lower bound: lb = zeros(29,1) . While you CAN write this as a linear constraint, you shouldn't: see which constraints are better.
  3. Yes, to maximize a function you just take the negative of the objective function, as explained here.
  4. If you set x as the solution (such as x = fmincon(fun,x0,...)) then yes, x holds the solution.
Alan Weiss
MATLAB mathematical toolbox documentation
It works perfectly. I used the optimization toolbox with your suggestions and exported the results to the workspace to get the optimal allocation.
Thank you very much!
Hi phanta! As I read through your article I realised I went through most of the same issues. Would you mind sharing your final code with us? Thank you!

Sign in to comment.

More Answers (0)

Categories

Asked:

on 26 May 2015

Commented:

on 6 Apr 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!