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

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.

 Réponse acceptée

Matt J
Matt J le 27 Juil 2020
Modifié(e) : Matt J le 27 Juil 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 commentaires

Thank you for your response. You are right, I should have formulated the question more precisely.
But yes, S is not a function but a logical matrix based on incidence of a condition say, (realized value > target value). The shortfall probability "PCS" is determined from many small steps performed earlier and "S" was one of the steps.
So it means if PCS is generating from "S" then I cannot use fmincon?
Since I am trying to convert the excel calculations "Prototype" in MatLab therefore, I am trying to follow the same steps as in Excel. In excel, I have used Excel solver and it worked out.
Matt J
Matt J le 28 Juil 2020
Modifié(e) : Matt J le 28 Juil 2020
You've omitted so much code from your post, that we cannot tell which quantities are known constants and which depend on unknows that you are trying to optimize. We know that equity_share is unknown, but what about b or B? Are they unknown as well? If yes, then S is also unknown and you cannot use fmincon for the reasons I mentioned above . What about sim? How does it depend on the unknowns?
If b and B and therefore S are known, then it is not clear why you are recomputing S every time the objective function is called. It would more efficient just to compute it one time. Also If S is a known positive matrix and sim is a scalar quantity depending on the unknowns in some way, then that would mean that minimizing PCS is equivalent to maximizing sim which is equivalent to minimzing -sim. You don't need to have S in the calculations at all.
susman
susman le 28 Juil 2020
Modifié(e) : susman le 29 Juil 2020
%% Probability of shortfall
Please find the full code attached as it will reduce the confusion :-)
Matt J
Matt J le 28 Juil 2020
Modifié(e) : Matt J le 28 Juil 2020
OK, but is your question already answered? You have accepted Walter's answer, so I assume you have a solution that is working for you. If not, you should unaccept the answer so people know the conversation needs to continue.
Oh I am new to the platform and dont have idea about it.
Did you check the code?
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.
Thanks for your support. It worked perfectly :-)

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 27 Juil 2020
Modifié(e) : Walter Roberson le 27 Juil 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 commentaires

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?
Please find the code to avoid further confusion in the answer below.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Linear Programming and Mixed-Integer Linear Programming dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by