Minimizing a scalar value using "fmincon" derived through many variables

1 Ansicht (letzte 30 Tage)
I have portfolio optimal allocation problem (two assets equity and bond) and I want to use "fmincon" to minimize "PCS", which is probability of consumption shortfall.
Using Matlab code I have derived the "PCS" which is a scalar. The code is below:
Return_portfolio = equity_share*Return_equity+(1-equity_share)*Return_bond
W = matrix of wealth in each period
B = matrix of withdrawal in each period
P = (B < b); % matrix showing the incidence of shortfall (realized benefit is less than target)
S = (cumsum(P, 2) == 1) .* P; % Finding the first incidence of shortfall
shortprob = sum(S)/sim; % vector of shortfall probability
PCS = sum(shortprob) ; % Probability of consumption shortfall
Now I want to minimize PCS by chooing right allocation (weight) of portfoilio. Initially I assumed it to be 0.5. This is my "fmincon" set up
fun = @(eq_share) PCS
x0 = [0.5; 0.5]; % initial weight of two assets
Aeq = [1, 1; ER_equity, ER_bonds];
beq = [1; CS];
lb = [0, 0];
ub = [1, 1];
[weights, PCS] = fmincon(fun, x0, Aeq, beq, lb, ub);
Can anyone tell me how is the set up correct because I am not even able to set it because my PCS is scalar and I do not how to define it in a function.

Akzeptierte Antwort

Matt J
Matt J am 27 Jul. 2020
Bearbeitet: Matt J am 27 Jul. 2020
Much is unclear from your post including what your unknowns are and how they determine shortprob. However, if S is a function of the unknowns, then it is clear that fmincon is not applicable to your problem. In particular, S is a binary-valued matrix and therefore sum(S) can only assume a finite number of values. That means PCS cannot be a continuous, differentiable function of whatever the unknowns are, and therefore it cannot be minimized by a derivative-based solver like fmincon.
  8 Kommentare
Matt J
Matt J am 28 Jul. 2020
Yes. I gather that you only have a single unknown variable, eq_share. If so, you can just use fminbnd. Unlike fmincon, the algorithm used by fminbnd does not require differentiability.
susman
susman am 28 Jul. 2020
Thanks for your support. It worked perfectly :-)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 27 Jul. 2020
Bearbeitet: Walter Roberson am 27 Jul. 2020
W = matrix of wealth in each period
B = matrix of withdrawal in each period
fun = @(eq_share) PCS(eq_share, W, B);
x0 = [0.5; 0.5]; % initial weight of two assets
Aeq = [1, 1; ER_equity, ER_bonds];
beq = [1; CS];
lb = [0, 0];
ub = [1, 1];
[weights, PCS] = fmincon(fun, x0, Aeq, beq, lb, ub);
function prob_comp_shortfall = PCS(equity_share, W, B)
Return_portfolio = equity_share*Return_equity+(1-equity_share)*Return_bond
P = (B < b); % matrix showing the incidence of shortfall (realized benefit is less than target)
S = (cumsum(P, 2) == 1) .* P; % Finding the first incidence of shortfall
shortprob = sum(S)/sim; % vector of shortfall probability
prob_comp_shortfall = sum(shortprob) ; % Probability of consumption shortfall
end
Except that you need to define Return_equity and Return_bond, and b, and sim, and you do not use Return_porfolio in calculating PCS.
Your equity_share is going to be received as a 1 x 2 vector, implying two things to be optimized independently. However when I see you use (1-equity_share) it looks to me more like you are trying to decide a single weight. Unless, that is, you have a whole series of equity and bonds that for some reason are paired? I am having difficulty figuring out why you would want to do that... but if there is a reason to define a whole series of equity / bond balances, then I would normally expect that you would have one variable for each of them rather than exactly two variables to optimize.
With you having two variables, Return_portfolio seems likely to become a vector... it is not clear how that would fit into the calculations below that point. Also not clear what W is doing for your PCS calculation.
Also, your constraints are confused. The top row of AEQ, [1 1], together with the [1] at top of beq, tells us that
1*eq_share(1) + 1*eq_share(2) == 1
but if you really only had two variables and you had that definition, then you would reduce down to a single variable and use 1-variable for the second variable, much like you did in your Return_portfolio calculation.
When you then look at the bottom Aeq beq in combination with that, you have
x(1) * ER_equity + (1-x(1)) * ER_bonds == CS
which has exactly one solution,
x(1) == (CS - ER_bonds)/(ER_equity - ER_bonds)
and since those are equality constraints, those define that there is exactly one valid value for x(1), in which case there is no point doing an optimization.
  2 Kommentare
susman
susman am 28 Jul. 2020
Thank you for your response. As I got some food for thought from your feedback.
Return_equity and Return_bonds are randomly generated returns. The values of b and sim have been defined in my full code but I did not think of writing them here as my primary question was to see whether I can use fmincon for this setup or not.
You are right that the two weights are paired because of assumption and I can optimize one to figure out the second.
Regarding constrainst, I know I have realized that I have put them in wrongly (I am sorry as I have done this optimization very first time). But I will fix them once I understand the "func" setup of fmincon.
My objective is to minimize "PCS" by selecting the optimal "eq_share"
The whole point is that if PCS (1-by-1) is created from the logical matrix "P" (1000-by-30) that is derived from another matrix "B" (1000-by-30) using a condition that each cell of matrix is greater than a parameter "b". The matix "B" and "W" are linked and derived from Portfolio returns, which are further calculated from random returns of stocks and bonds. So if we use fmincon, then what should come in "func" of fmincon?
susman
susman am 28 Jul. 2020
Please find the code to avoid further confusion in the answer below.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by