Optimal portfolio weights maximizing the expected utility function?

Hi all, I am a MSc student and I am new to Matlab. I have written the following function, which I need to maximize:
function EU=expectedutility(x,blockP1,gamma)
U=zeros(18,1);
for j=1:18
U(j,1)=((1+x(1)*blockP1(j,2)+x(2)*blockP1(j,3))^(1-gamma))/(1-gamma)
end
EU=mean(U)
end
Now I need to find x(1) and x(2) such that they maximize such average (i.e. EU), subject to the following constraints on x(1) and x(2):
  • x(1) + x(2)=1;
  • -3<=x(1)<=+3;
  • -3<=x(2)<=+3;
blockP1(j,2) and blockP1(j,3) refer to the 18x3 matrix blockP1, already defined in my main code.
The function EU is defined correctly as I tested it with x(1)=1, x(2)=1 and gamma=5 in the following way:
expectedutility([1 1],blockP1,5)
and it correctly returned EU.
In my case however I do not need to set x(1) and x(2) but a "solver function" needs to give me those values so that EU is maximum (gamma=5 still holds).
After defining the function above, I have tried to run the optimization in my main code as follows:
ObjectFunction= @expectedutility;
lb=[-3 -3];
ub=[3 3];
Aeq=[1 1];
beq=1;
[x,fval]= linprog(ObjectFunction,[],[], Aeq, beq, lb, ub)
but it gives me the error:
Error using linprog (line 144)
LINPROG requires the following inputs to be of data type double: 'f'.
Error in optimization (line 72)
[x,fval]= linprog(ObjectFunction,[],[], Aeq, beq, lb, ub)
Could you kindly help me out with this?
Any help would be very much appreciated.
Thanks in advance,
Federico

 採用された回答

Alan Weiss
Alan Weiss 2017 年 3 月 29 日

0 投票

You simply need to change your objective function reference to
ObjectFunction = @(x)expectedutility(x,blockP1,gamma)
This means that blockP1 and gamma are constants (make sure that these variables are in your Workspace before defining fun), and the function is a function of x alone. For more information, see Passing Extra Parameters.
Alan Weiss
MATLAB mathematical toolbox documentation

1 件のコメント

Federico Rendina
Federico Rendina 2017 年 3 月 29 日
Thanks a lot Alan. I finally got it working following your advice!
Have a nice day!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeOptimization Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by